| 1 |
<?php |
| 2 |
namespace GuzzleHttp\Tests; |
| 3 |
|
| 4 |
use GuzzleHttp\Client; |
| 5 |
use GuzzleHttp\Exception\ClientException; |
| 6 |
use GuzzleHttp\Handler\MockHandler; |
| 7 |
use GuzzleHttp\HandlerStack; |
| 8 |
use GuzzleHttp\Pool; |
| 9 |
use GuzzleHttp\Promise\Promise; |
| 10 |
use GuzzleHttp\Psr7\Request; |
| 11 |
use GuzzleHttp\Psr7\Response; |
| 12 |
use PHPUnit\Framework\TestCase; |
| 13 |
use Psr\Http\Message\RequestInterface; |
| 14 |
|
| 15 |
class PoolTest extends TestCase |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @expectedException \InvalidArgumentException |
| 19 |
*/ |
| 20 |
public function testValidatesIterable() |
| 21 |
{ |
| 22 |
$p = new Pool(new Client(), 'foo'); |
| 23 |
$p->promise()->wait(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @expectedException \InvalidArgumentException |
| 28 |
*/ |
| 29 |
public function testValidatesEachElement() |
| 30 |
{ |
| 31 |
$c = new Client(); |
| 32 |
$requests = ['foo']; |
| 33 |
$p = new Pool($c, new \ArrayIterator($requests)); |
| 34 |
$p->promise()->wait(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @doesNotPerformAssertions |
| 39 |
*/ |
| 40 |
public function testSendsAndRealizesFuture() |
| 41 |
{ |
| 42 |
$c = $this->getClient(); |
| 43 |
$p = new Pool($c, [new Request('GET', 'http://example.com')]); |
| 44 |
$p->promise()->wait(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @doesNotPerformAssertions |
| 49 |
*/ |
| 50 |
public function testExecutesPendingWhenWaiting() |
| 51 |
{ |
| 52 |
$r1 = new Promise(function () use (&$r1) { |
| 53 |
$r1->resolve(new Response()); |
| 54 |
}); |
| 55 |
$r2 = new Promise(function () use (&$r2) { |
| 56 |
$r2->resolve(new Response()); |
| 57 |
}); |
| 58 |
$r3 = new Promise(function () use (&$r3) { |
| 59 |
$r3->resolve(new Response()); |
| 60 |
}); |
| 61 |
$handler = new MockHandler([$r1, $r2, $r3]); |
| 62 |
$c = new Client(['handler' => $handler]); |
| 63 |
$p = new Pool($c, [ |
| 64 |
new Request('GET', 'http://example.com'), |
| 65 |
new Request('GET', 'http://example.com'), |
| 66 |
new Request('GET', 'http://example.com'), |
| 67 |
], ['pool_size' => 2]); |
| 68 |
$p->promise()->wait(); |
| 69 |
} |
| 70 |
|
| 71 |
public function testUsesRequestOptions() |
| 72 |
{ |
| 73 |
$h = []; |
| 74 |
$handler = new MockHandler([ |
| 75 |
function (RequestInterface $request) use (&$h) { |
| 76 |
$h[] = $request; |
| 77 |
return new Response(); |
| 78 |
} |
| 79 |
]); |
| 80 |
$c = new Client(['handler' => $handler]); |
| 81 |
$opts = ['options' => ['headers' => ['x-foo' => 'bar']]]; |
| 82 |
$p = new Pool($c, [new Request('GET', 'http://example.com')], $opts); |
| 83 |
$p->promise()->wait(); |
| 84 |
self::assertCount(1, $h); |
| 85 |
self::assertTrue($h[0]->hasHeader('x-foo')); |
| 86 |
} |
| 87 |
|
| 88 |
public function testCanProvideCallablesThatReturnResponses() |
| 89 |
{ |
| 90 |
$h = []; |
| 91 |
$handler = new MockHandler([ |
| 92 |
function (RequestInterface $request) use (&$h) { |
| 93 |
$h[] = $request; |
| 94 |
return new Response(); |
| 95 |
} |
| 96 |
]); |
| 97 |
$c = new Client(['handler' => $handler]); |
| 98 |
$optHistory = []; |
| 99 |
$fn = function (array $opts) use (&$optHistory, $c) { |
| 100 |
$optHistory = $opts; |
| 101 |
return $c->request('GET', 'http://example.com', $opts); |
| 102 |
}; |
| 103 |
$opts = ['options' => ['headers' => ['x-foo' => 'bar']]]; |
| 104 |
$p = new Pool($c, [$fn], $opts); |
| 105 |
$p->promise()->wait(); |
| 106 |
self::assertCount(1, $h); |
| 107 |
self::assertTrue($h[0]->hasHeader('x-foo')); |
| 108 |
} |
| 109 |
|
| 110 |
public function testBatchesResults() |
| 111 |
{ |
| 112 |
$requests = [ |
| 113 |
new Request('GET', 'http://foo.com/200'), |
| 114 |
new Request('GET', 'http://foo.com/201'), |
| 115 |
new Request('GET', 'http://foo.com/202'), |
| 116 |
new Request('GET', 'http://foo.com/404'), |
| 117 |
]; |
| 118 |
$fn = function (RequestInterface $request) { |
| 119 |
return new Response(substr($request->getUri()->getPath(), 1)); |
| 120 |
}; |
| 121 |
$mock = new MockHandler([$fn, $fn, $fn, $fn]); |
| 122 |
$handler = HandlerStack::create($mock); |
| 123 |
$client = new Client(['handler' => $handler]); |
| 124 |
$results = Pool::batch($client, $requests); |
| 125 |
self::assertCount(4, $results); |
| 126 |
self::assertSame([0, 1, 2, 3], array_keys($results)); |
| 127 |
self::assertSame(200, $results[0]->getStatusCode()); |
| 128 |
self::assertSame(201, $results[1]->getStatusCode()); |
| 129 |
self::assertSame(202, $results[2]->getStatusCode()); |
| 130 |
self::assertInstanceOf(ClientException::class, $results[3]); |
| 131 |
} |
| 132 |
|
| 133 |
public function testBatchesResultsWithCallbacks() |
| 134 |
{ |
| 135 |
$requests = [ |
| 136 |
new Request('GET', 'http://foo.com/200'), |
| 137 |
new Request('GET', 'http://foo.com/201') |
| 138 |
]; |
| 139 |
$mock = new MockHandler([ |
| 140 |
function (RequestInterface $request) { |
| 141 |
return new Response(substr($request->getUri()->getPath(), 1)); |
| 142 |
} |
| 143 |
]); |
| 144 |
$client = new Client(['handler' => $mock]); |
| 145 |
$results = Pool::batch($client, $requests, [ |
| 146 |
'fulfilled' => function ($value) use (&$called) { |
| 147 |
$called = true; |
| 148 |
} |
| 149 |
]); |
| 150 |
self::assertCount(2, $results); |
| 151 |
self::assertTrue($called); |
| 152 |
} |
| 153 |
|
| 154 |
public function testUsesYieldedKeyInFulfilledCallback() |
| 155 |
{ |
| 156 |
$r1 = new Promise(function () use (&$r1) { |
| 157 |
$r1->resolve(new Response()); |
| 158 |
}); |
| 159 |
$r2 = new Promise(function () use (&$r2) { |
| 160 |
$r2->resolve(new Response()); |
| 161 |
}); |
| 162 |
$r3 = new Promise(function () use (&$r3) { |
| 163 |
$r3->resolve(new Response()); |
| 164 |
}); |
| 165 |
$handler = new MockHandler([$r1, $r2, $r3]); |
| 166 |
$c = new Client(['handler' => $handler]); |
| 167 |
$keys = []; |
| 168 |
$requests = [ |
| 169 |
'request_1' => new Request('GET', 'http://example.com'), |
| 170 |
'request_2' => new Request('GET', 'http://example.com'), |
| 171 |
'request_3' => new Request('GET', 'http://example.com'), |
| 172 |
]; |
| 173 |
$p = new Pool($c, $requests, [ |
| 174 |
'pool_size' => 2, |
| 175 |
'fulfilled' => function ($res, $index) use (&$keys) { |
| 176 |
$keys[] = $index; |
| 177 |
} |
| 178 |
]); |
| 179 |
$p->promise()->wait(); |
| 180 |
self::assertCount(3, $keys); |
| 181 |
self::assertSame($keys, array_keys($requests)); |
| 182 |
} |
| 183 |
|
| 184 |
private function getClient($total = 1) |
| 185 |
{ |
| 186 |
$queue = []; |
| 187 |
for ($i = 0; $i < $total; $i++) { |
| 188 |
$queue[] = new Response(); |
| 189 |
} |
| 190 |
$handler = new MockHandler($queue); |
| 191 |
return new Client(['handler' => $handler]); |
| 192 |
} |
| 193 |
} |
| 194 |
|