1515use FOS \HttpCache \ProxyClient \Varnish ;
1616use FOS \HttpCache \Test \HttpClient \MockHttpAdapter ;
1717use Http \Adapter \Exception \HttpAdapterException ;
18+ use Http \Discovery \MessageFactoryDiscovery ;
1819use Psr \Http \Message \RequestInterface ;
1920use \Mockery ;
2021
@@ -31,7 +32,7 @@ class AbstractProxyClientTest extends \PHPUnit_Framework_TestCase
3132 /**
3233 * @dataProvider exceptionProvider
3334 *
34- * @param \Exception $exception The exception that curl should throw .
35+ * @param \Exception $exception Exception thrown by HTTP adapter .
3536 * @param string $type The returned exception class to be expected.
3637 * @param string $message Optional exception message to match against.
3738 */
@@ -74,30 +75,36 @@ public function exceptionProvider()
7475 $ unreachableException = new HttpAdapterException ();
7576 $ unreachableException ->setRequest ($ request );
7677
77- // Client exception (with response)
78- $ response = \Mockery::mock ('\Psr\Http\Message\ResponseInterface ' )
79- ->shouldReceive ('getStatusCode ' )->andReturn (500 )
80- ->shouldReceive ('getReasonPhrase ' )->andReturn ('Uh-oh! ' )
81- ->getMock ()
82- ;
83- $ responseException = new HttpAdapterException ();
84- $ responseException ->setRequest ($ request );
85- $ responseException ->setResponse ($ response );
86-
8778 return [
8879 [
8980 $ unreachableException ,
9081 '\FOS\HttpCache\Exception\ProxyUnreachableException ' ,
9182 'bla.com '
92- ],
93- [
94- $ responseException ,
95- '\FOS\HttpCache\Exception\ProxyResponseException ' ,
96- 'bla.com '
97- ],
83+ ]
9884 ];
9985 }
10086
87+ public function testErrorResponsesAreConvertedToExceptions ()
88+ {
89+ $ response = MessageFactoryDiscovery::find ()->createResponse (
90+ 405 ,
91+ 'Not allowed '
92+ );
93+ $ this ->client ->addResponse ($ response );
94+
95+ $ varnish = new Varnish (['127.0.0.1:123 ' ], 'my_hostname.dev ' , $ this ->client );
96+ try {
97+ $ varnish ->purge ('/ ' )->flush ();
98+ $ this ->fail ('Should have aborted with an exception ' );
99+ } catch (ExceptionCollection $ exceptions ) {
100+ $ this ->assertCount (1 , $ exceptions );
101+ $ this ->assertEquals (
102+ '405 error response "Not allowed" from caching proxy ' ,
103+ $ exceptions ->getFirst ()->getMessage ()
104+ );
105+ }
106+ }
107+
101108 /**
102109 * @expectedException \FOS\HttpCache\Exception\MissingHostException
103110 * @expectedExceptionMessage cannot be invalidated without a host
@@ -170,23 +177,23 @@ public function testFlushEmpty()
170177
171178 public function testFlushCountSuccess ()
172179 {
173- $ self = $ this ;
174180 $ httpAdapter = \Mockery::mock ('\Http\Adapter\HttpAdapter ' )
175181 ->shouldReceive ('sendRequests ' )
176182 ->once ()
177183 ->with (
178184 \Mockery::on (
179- function ($ requests ) use ( $ self ) {
185+ function ($ requests ) {
180186 /** @type RequestInterface[] $requests */
181- $ self ->assertCount (4 , $ requests );
187+ $ this ->assertCount (4 , $ requests );
182188 foreach ($ requests as $ request ) {
183- $ self ->assertEquals ('PURGE ' , $ request ->getMethod ());
189+ $ this ->assertEquals ('PURGE ' , $ request ->getMethod ());
184190 }
185191
186192 return true ;
187193 }
188194 )
189195 )
196+ ->andReturn ([])
190197 ->getMock ();
191198
192199 $ varnish = new Varnish (['127.0.0.1 ' , '127.0.0.2 ' ], 'fos.lo ' , $ httpAdapter );
@@ -202,23 +209,23 @@ function ($requests) use ($self) {
202209
203210 public function testEliminateDuplicates ()
204211 {
205- $ self = $ this ;
206212 $ client = \Mockery::mock ('\Http\Adapter\HttpAdapter ' )
207213 ->shouldReceive ('sendRequests ' )
208214 ->once ()
209215 ->with (
210216 \Mockery::on (
211- function ($ requests ) use ( $ self ) {
217+ function ($ requests ) {
212218 /** @type RequestInterface[] $requests */
213- $ self ->assertCount (4 , $ requests );
219+ $ this ->assertCount (4 , $ requests );
214220 foreach ($ requests as $ request ) {
215- $ self ->assertEquals ('PURGE ' , $ request ->getMethod ());
221+ $ this ->assertEquals ('PURGE ' , $ request ->getMethod ());
216222 }
217223
218224 return true ;
219225 }
220226 )
221227 )
228+ ->andReturn ([])
222229 ->getMock ();
223230
224231 $ varnish = new Varnish (array ('127.0.0.1 ' , '127.0.0.2 ' ), 'fos.lo ' , $ client );
0 commit comments