1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-24 10:03:27 +01:00
guzzle/tests/HandlerStackTest.php

169 lines
5.2 KiB
PHP
Raw Normal View History

2015-02-24 22:50:52 -08:00
<?php
namespace GuzzleHttp\Tests;
2015-03-22 10:43:34 -07:00
use GuzzleHttp\HandlerStack;
2015-02-24 22:50:52 -08:00
2015-03-22 10:43:34 -07:00
class HandlerStackTest extends \PHPUnit_Framework_TestCase
2015-02-24 22:50:52 -08:00
{
2015-03-22 10:43:34 -07:00
public function testSetsHandlerInCtor()
2015-02-24 22:50:52 -08:00
{
$f = function () {};
$m1 = function () {};
2015-03-22 10:43:34 -07:00
$h = new HandlerStack($f, [$m1]);
2015-02-24 22:50:52 -08:00
$this->assertTrue($h->hasHandler());
}
public function testCanSetDifferentHandlerAfterConstruction()
{
$f = function () {};
2015-03-22 10:43:34 -07:00
$h = new HandlerStack();
2015-02-24 22:50:52 -08:00
$h->setHandler($f);
$h->resolve();
}
/**
* @expectedException \LogicException
*/
public function testEnsuresHandlerIsSet()
{
2015-03-22 10:43:34 -07:00
$h = new HandlerStack();
2015-02-24 22:50:52 -08:00
$h->resolve();
}
2015-02-25 22:49:50 -08:00
public function testAppendsInOrder()
{
$meths = $this->getFunctions();
2015-03-22 10:43:34 -07:00
$builder = new HandlerStack();
2015-02-25 22:49:50 -08:00
$builder->setHandler($meths[1]);
$builder->append($meths[2]);
$builder->append($meths[3]);
$builder->append($meths[4]);
$composed = $builder->resolve();
$this->assertEquals('Hello - test123', $composed('test'));
$this->assertEquals(
[['a', 'test'], ['b', 'test1'], ['c', 'test12']],
$meths[0]
);
}
public function testPrependsInOrder()
{
$meths = $this->getFunctions();
2015-03-22 10:43:34 -07:00
$builder = new HandlerStack();
2015-02-25 22:49:50 -08:00
$builder->setHandler($meths[1]);
$builder->prepend($meths[2]);
$builder->prepend($meths[3]);
$builder->prepend($meths[4]);
$composed = $builder->resolve();
$this->assertEquals('Hello - test321', $composed('test'));
$this->assertEquals(
[['c', 'test'], ['b', 'test3'], ['a', 'test32']],
$meths[0]
);
}
2015-03-22 10:43:34 -07:00
public function testCanRemoveMiddlewareByInstance()
2015-02-25 22:49:50 -08:00
{
$meths = $this->getFunctions();
2015-03-22 10:43:34 -07:00
$builder = new HandlerStack();
2015-02-25 22:49:50 -08:00
$builder->setHandler($meths[1]);
$builder->prepend($meths[2]);
2015-03-22 10:43:34 -07:00
$builder->prepend($meths[3]);
$builder->append($meths[4]);
$builder->remove($meths[2]);
$builder->remove($meths[3]);
$builder->remove($meths[4]);
2015-02-25 22:49:50 -08:00
$composed = $builder->resolve();
2015-03-22 10:43:34 -07:00
$this->assertEquals('Hello - test', $composed('test'));
2015-02-25 22:49:50 -08:00
}
2015-03-22 10:43:34 -07:00
public function testCanPrintMiddleware()
2015-02-25 22:49:50 -08:00
{
$meths = $this->getFunctions();
2015-03-22 10:43:34 -07:00
$builder = new HandlerStack();
2015-02-25 22:49:50 -08:00
$builder->setHandler($meths[1]);
2015-03-22 10:43:34 -07:00
$builder->append($meths[2], 'a');
$builder->append([__CLASS__, 'foo']);
$builder->append([$this, 'bar']);
$builder->append(__CLASS__ . '::' . 'foo');
$lines = explode("\n", (string) $builder);
$this->assertContains("0) Name: a, Function: callable(", $lines[0]);
$this->assertContains("1) Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[1]);
$this->assertContains("2) Function: callable(['GuzzleHttp\\Tests\\HandlerStackTest', 'bar'])", $lines[2]);
$this->assertContains("3) Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[3]);
$this->assertContains("4) Handler: callable(", $lines[4]);
2015-02-25 22:49:50 -08:00
}
2015-03-22 10:43:34 -07:00
public function testCanAddBeforeByName()
2015-02-25 22:49:50 -08:00
{
$meths = $this->getFunctions();
2015-03-22 10:43:34 -07:00
$builder = new HandlerStack();
2015-02-25 22:49:50 -08:00
$builder->setHandler($meths[1]);
2015-03-22 10:43:34 -07:00
$builder->append($meths[2], 'a');
$builder->before('a', $meths[3], 'b');
$builder->before('b', $meths[4], 'c');
$lines = explode("\n", (string) $builder);
$this->assertContains('0) Name: c', $lines[0]);
$this->assertContains('1) Name: b', $lines[1]);
$this->assertContains('2) Name: a', $lines[2]);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testEnsuresHandlerExistsByName()
{
$builder = new HandlerStack();
$builder->before('foo', function () {});
}
public function testCanAddAfterByName()
{
$meths = $this->getFunctions();
$builder = new HandlerStack();
$builder->setHandler($meths[1]);
$builder->append($meths[2], 'a');
$builder->append($meths[3], 'b');
$builder->after('a', $meths[4], 'c');
$lines = explode("\n", (string) $builder);
$this->assertContains('0) Name: a', $lines[0]);
$this->assertContains('1) Name: c', $lines[1]);
$this->assertContains('2) Name: b', $lines[2]);
2015-02-25 22:49:50 -08:00
}
private function getFunctions()
{
$calls = [];
$a = function (callable $next) use (&$calls) {
return function ($v) use ($next, &$calls) {
$calls[] = ['a', $v];
return $next($v . '1');
};
};
$b = function (callable $next) use (&$calls) {
return function ($v) use ($next, &$calls) {
$calls[] = ['b', $v];
return $next($v . '2');
};
};
$c = function (callable $next) use (&$calls) {
return function ($v) use ($next, &$calls) {
$calls[] = ['c', $v];
return $next($v . '3');
};
};
$handler = function ($v) {
return 'Hello - ' . $v;
};
return [&$calls, $handler, $a, $b, $c];
}
2015-03-22 10:43:34 -07:00
public static function foo() {}
public function bar () {}
2015-02-24 22:50:52 -08:00
}