Skip to content

Commit fb6fbe9

Browse files
committed
Merge pull request #105 from FriendsOfSymfony/test-exception
testing guzzle exceptions
2 parents 51e240e + c816751 commit fb6fbe9

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/ProxyClient/AbstractProxyClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Guzzle\Http\Client;
1919
use Guzzle\Http\ClientInterface;
2020
use Guzzle\Http\Exception\CurlException;
21-
use Guzzle\Http\Exception\MultiTransferException;
21+
use Guzzle\Common\Exception\ExceptionCollection as GuzzleExceptionCollection;
2222
use Guzzle\Http\Exception\RequestException;
2323
use Guzzle\Http\Message\RequestInterface;
2424

@@ -173,19 +173,19 @@ private function sendRequests(array $requests)
173173

174174
try {
175175
$this->client->send($allRequests);
176-
} catch (MultiTransferException $e) {
176+
} catch (GuzzleExceptionCollection $e) {
177177
$this->handleException($e);
178178
}
179179
}
180180

181181
/**
182182
* Handle request exception
183183
*
184-
* @param MultiTransferException $exceptions
184+
* @param GuzzleExceptionCollection $exceptions
185185
*
186186
* @throws ExceptionCollection
187187
*/
188-
protected function handleException(MultiTransferException $exceptions)
188+
protected function handleException(GuzzleExceptionCollection $exceptions)
189189
{
190190
$collection = new ExceptionCollection();
191191

tests/Unit/ProxyClient/VarnishTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use FOS\HttpCache\ProxyClient\Varnish;
1616
use Guzzle\Http\Client;
1717
use Guzzle\Http\Exception\CurlException;
18+
use Guzzle\Http\Exception\MultiTransferException;
19+
use Guzzle\Http\Exception\RequestException;
1820
use Guzzle\Plugin\Mock\MockPlugin;
1921
use Guzzle\Http\Message\Response;
2022
use Guzzle\Http\Message\Request;
@@ -176,6 +178,56 @@ public function testUnreachableException()
176178
$varnish->purge('/path')->flush();
177179
}
178180

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+
179231
/**
180232
* @expectedException \FOS\HttpCache\Exception\MissingHostException
181233
* @expectedExceptionMessage cannot be invalidated without a host

0 commit comments

Comments
 (0)