|
15 | 15 | use FOS\HttpCache\ProxyClient\Varnish; |
16 | 16 | use Guzzle\Http\Client; |
17 | 17 | use Guzzle\Http\Exception\CurlException; |
| 18 | +use Guzzle\Http\Exception\MultiTransferException; |
| 19 | +use Guzzle\Http\Exception\RequestException; |
18 | 20 | use Guzzle\Plugin\Mock\MockPlugin; |
19 | 21 | use Guzzle\Http\Message\Response; |
20 | 22 | use Guzzle\Http\Message\Request; |
@@ -176,6 +178,56 @@ public function testUnreachableException() |
176 | 178 | $varnish->purge('/path')->flush(); |
177 | 179 | } |
178 | 180 |
|
| 181 | + public function curlExceptionProvider() |
| 182 | + { |
| 183 | + $requestException = new RequestException('request'); |
| 184 | + $requestException->setRequest(new Request('GET', '/')); |
| 185 | + |
| 186 | + $curlException = new CurlException('curl'); |
| 187 | + $curlException->setRequest(new Request('GET', '/')); |
| 188 | + return array( |
| 189 | + array($curlException, '\FOS\HttpCache\Exception\ProxyUnreachableException'), |
| 190 | + array($requestException, '\FOS\HttpCache\Exception\ProxyResponseException'), |
| 191 | + array(new \InvalidArgumentException('something'), '\InvalidArgumentException'), |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @dataProvider curlExceptionProvider |
| 197 | + * |
| 198 | + * @param \Exception $exception The exception that curl should throw. |
| 199 | + * @param string $type The returned exception class to be expected. |
| 200 | + */ |
| 201 | + public function testExceptions(\Exception $exception, $type) |
| 202 | + { |
| 203 | + // the guzzle mock plugin does not allow arbitrary exceptions |
| 204 | + // mockery does not provide all methods of the interface |
| 205 | + $collection = new MultiTransferException(); |
| 206 | + $collection->setExceptions(array($exception)); |
| 207 | + $client = $this->getMock('\Guzzle\Http\ClientInterface'); |
| 208 | + $client->expects($this->any()) |
| 209 | + ->method('createRequest') |
| 210 | + ->willReturn(new Request('GET', '/')) |
| 211 | + ; |
| 212 | + $client->expects($this->once()) |
| 213 | + ->method('send') |
| 214 | + ->willThrowException($collection) |
| 215 | + ; |
| 216 | + |
| 217 | + $varnish = new Varnish(array('http://127.0.0.1:123'), 'my_hostname.dev', $client); |
| 218 | + |
| 219 | + $varnish->ban(array()); |
| 220 | + try { |
| 221 | + $varnish->flush(); |
| 222 | + $this->fail('Should have aborted with an exception'); |
| 223 | + } catch (ExceptionCollection $exceptions) { |
| 224 | + $this->assertCount(1, $exceptions); |
| 225 | + $this->assertInstanceOf($type, $exceptions->getFirst()); |
| 226 | + } catch (\Exception $e) { |
| 227 | + die(get_class($e)); |
| 228 | + } |
| 229 | + } |
| 230 | + |
179 | 231 | /** |
180 | 232 | * @expectedException \FOS\HttpCache\Exception\MissingHostException |
181 | 233 | * @expectedExceptionMessage cannot be invalidated without a host |
|
0 commit comments