33namespace spec \Http \Client \Common \Plugin ;
44
55use Http \Client \Common \Plugin \Cache \Generator \SimpleGenerator ;
6+ use PhpSpec \Wrapper \Collaborator ;
67use Prophecy \Argument ;
78use Http \Message \StreamFactory ;
89use Http \Promise \FulfilledPromise ;
1819
1920class CachePluginSpec extends ObjectBehavior
2021{
22+ /**
23+ * @var StreamFactory&Collaborator
24+ */
25+ private $ streamFactory ;
26+
2127 function let (CacheItemPoolInterface $ pool , StreamFactory $ streamFactory )
2228 {
29+ $ this ->streamFactory = $ streamFactory ;
2330 $ this ->beConstructedWith ($ pool , $ streamFactory , [
2431 'default_ttl ' => 60 ,
2532 'cache_lifetime ' => 1000
@@ -42,6 +49,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
4249 $ stream ->__toString ()->willReturn ($ httpBody );
4350 $ stream ->isSeekable ()->willReturn (true );
4451 $ stream ->rewind ()->shouldBeCalled ();
52+ $ stream ->detach ()->shouldBeCalled ();
4553
4654 $ request ->getMethod ()->willReturn ('GET ' );
4755 $ request ->getUri ()->willReturn ($ uri );
@@ -53,6 +61,9 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
5361 $ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
5462 $ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
5563 $ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
64+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
65+
66+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
5667
5768 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
5869 $ item ->isHit ()->willReturn (false );
@@ -128,6 +139,7 @@ function it_stores_post_requests_when_allowed(
128139 $ stream ->__toString ()->willReturn ($ httpBody );
129140 $ stream ->isSeekable ()->willReturn (true );
130141 $ stream ->rewind ()->shouldBeCalled ();
142+ $ stream ->detach ()->shouldBeCalled ();
131143
132144 $ request ->getMethod ()->willReturn ('POST ' );
133145 $ request ->getUri ()->willReturn ($ uri );
@@ -139,6 +151,9 @@ function it_stores_post_requests_when_allowed(
139151 $ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
140152 $ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
141153 $ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
154+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
155+
156+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
142157
143158 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
144159 $ item ->isHit ()->willReturn (false );
@@ -186,6 +201,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
186201 $ stream ->__toString ()->willReturn ($ httpBody );
187202 $ stream ->isSeekable ()->willReturn (true );
188203 $ stream ->rewind ()->shouldBeCalled ();
204+ $ stream ->detach ()->shouldBeCalled ();
189205
190206 $ request ->getMethod ()->willReturn ('GET ' );
191207 $ request ->getUri ()->willReturn ($ uri );
@@ -198,6 +214,9 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
198214 $ response ->getHeader ('Age ' )->willReturn (['15 ' ]);
199215 $ response ->getHeader ('Expires ' )->willReturn ([]);
200216 $ response ->getHeader ('ETag ' )->willReturn ([]);
217+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
218+
219+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
201220
202221 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
203222 $ item ->isHit ()->willReturn (false );
@@ -226,6 +245,7 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
226245 $ stream ->__toString ()->willReturn ($ httpBody );
227246 $ stream ->isSeekable ()->willReturn (true );
228247 $ stream ->rewind ()->shouldBeCalled ();
248+ $ stream ->detach ()->shouldBeCalled ();
229249 $ request ->getBody ()->shouldBeCalled ()->willReturn ($ stream );
230250
231251 $ request ->getMethod ()->willReturn ('GET ' );
@@ -236,6 +256,9 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
236256 $ response ->getHeader ('Cache-Control ' )->willReturn ([]);
237257 $ response ->getHeader ('Expires ' )->willReturn ([]);
238258 $ response ->getHeader ('ETag ' )->willReturn (['foo_etag ' ]);
259+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
260+
261+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
239262
240263 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
241264 $ item ->isHit ()->willReturn (false );
@@ -387,6 +410,7 @@ function it_caches_private_responses_when_allowed(
387410 $ stream ->__toString ()->willReturn ($ httpBody );
388411 $ stream ->isSeekable ()->willReturn (true );
389412 $ stream ->rewind ()->shouldBeCalled ();
413+ $ stream ->detach ()->shouldBeCalled ();
390414
391415 $ request ->getMethod ()->willReturn ('GET ' );
392416 $ request ->getUri ()->willReturn ($ uri );
@@ -398,6 +422,9 @@ function it_caches_private_responses_when_allowed(
398422 $ response ->getHeader ('Cache-Control ' )->willReturn (['private ' ])->shouldBeCalled ();
399423 $ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
400424 $ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
425+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
426+
427+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
401428
402429 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
403430 $ item ->isHit ()->willReturn (false );
@@ -484,6 +511,7 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
484511 $ stream ->__toString ()->willReturn ($ httpBody );
485512 $ stream ->isSeekable ()->willReturn (true );
486513 $ stream ->rewind ()->shouldBeCalled ();
514+ $ stream ->detach ()->shouldBeCalled ();
487515
488516 $ request ->getMethod ()->willReturn ('GET ' );
489517 $ request ->getUri ()->willReturn ($ uri );
@@ -495,6 +523,9 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
495523 $ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
496524 $ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
497525 $ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
526+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
527+
528+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
498529
499530 $ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
500531 $ item ->isHit ()->willReturn (false );
0 commit comments