3
3
namespace spec \Http \Client \Common \Plugin ;
4
4
5
5
use Http \Client \Common \Plugin \Cache \Generator \SimpleGenerator ;
6
+ use PhpSpec \Wrapper \Collaborator ;
6
7
use Prophecy \Argument ;
7
8
use Http \Message \StreamFactory ;
8
9
use Http \Promise \FulfilledPromise ;
18
19
19
20
class CachePluginSpec extends ObjectBehavior
20
21
{
22
+ /**
23
+ * @var StreamFactory&Collaborator
24
+ */
25
+ private $ streamFactory ;
26
+
21
27
function let (CacheItemPoolInterface $ pool , StreamFactory $ streamFactory )
22
28
{
29
+ $ this ->streamFactory = $ streamFactory ;
23
30
$ this ->beConstructedWith ($ pool , $ streamFactory , [
24
31
'default_ttl ' => 60 ,
25
32
'cache_lifetime ' => 1000
@@ -42,6 +49,7 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
42
49
$ stream ->__toString ()->willReturn ($ httpBody );
43
50
$ stream ->isSeekable ()->willReturn (true );
44
51
$ stream ->rewind ()->shouldBeCalled ();
52
+ $ stream ->detach ()->shouldBeCalled ();
45
53
46
54
$ request ->getMethod ()->willReturn ('GET ' );
47
55
$ request ->getUri ()->willReturn ($ uri );
@@ -53,6 +61,9 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
53
61
$ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
54
62
$ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
55
63
$ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
64
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
65
+
66
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
56
67
57
68
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
58
69
$ item ->isHit ()->willReturn (false );
@@ -128,6 +139,7 @@ function it_stores_post_requests_when_allowed(
128
139
$ stream ->__toString ()->willReturn ($ httpBody );
129
140
$ stream ->isSeekable ()->willReturn (true );
130
141
$ stream ->rewind ()->shouldBeCalled ();
142
+ $ stream ->detach ()->shouldBeCalled ();
131
143
132
144
$ request ->getMethod ()->willReturn ('POST ' );
133
145
$ request ->getUri ()->willReturn ($ uri );
@@ -139,6 +151,9 @@ function it_stores_post_requests_when_allowed(
139
151
$ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
140
152
$ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
141
153
$ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
154
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
155
+
156
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
142
157
143
158
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
144
159
$ item ->isHit ()->willReturn (false );
@@ -186,6 +201,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
186
201
$ stream ->__toString ()->willReturn ($ httpBody );
187
202
$ stream ->isSeekable ()->willReturn (true );
188
203
$ stream ->rewind ()->shouldBeCalled ();
204
+ $ stream ->detach ()->shouldBeCalled ();
189
205
190
206
$ request ->getMethod ()->willReturn ('GET ' );
191
207
$ request ->getUri ()->willReturn ($ uri );
@@ -198,6 +214,9 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
198
214
$ response ->getHeader ('Age ' )->willReturn (['15 ' ]);
199
215
$ response ->getHeader ('Expires ' )->willReturn ([]);
200
216
$ response ->getHeader ('ETag ' )->willReturn ([]);
217
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
218
+
219
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
201
220
202
221
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
203
222
$ item ->isHit ()->willReturn (false );
@@ -226,6 +245,7 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
226
245
$ stream ->__toString ()->willReturn ($ httpBody );
227
246
$ stream ->isSeekable ()->willReturn (true );
228
247
$ stream ->rewind ()->shouldBeCalled ();
248
+ $ stream ->detach ()->shouldBeCalled ();
229
249
$ request ->getBody ()->shouldBeCalled ()->willReturn ($ stream );
230
250
231
251
$ request ->getMethod ()->willReturn ('GET ' );
@@ -236,6 +256,9 @@ function it_saves_etag(CacheItemPoolInterface $pool, CacheItemInterface $item, R
236
256
$ response ->getHeader ('Cache-Control ' )->willReturn ([]);
237
257
$ response ->getHeader ('Expires ' )->willReturn ([]);
238
258
$ response ->getHeader ('ETag ' )->willReturn (['foo_etag ' ]);
259
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
260
+
261
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
239
262
240
263
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
241
264
$ item ->isHit ()->willReturn (false );
@@ -387,6 +410,7 @@ function it_caches_private_responses_when_allowed(
387
410
$ stream ->__toString ()->willReturn ($ httpBody );
388
411
$ stream ->isSeekable ()->willReturn (true );
389
412
$ stream ->rewind ()->shouldBeCalled ();
413
+ $ stream ->detach ()->shouldBeCalled ();
390
414
391
415
$ request ->getMethod ()->willReturn ('GET ' );
392
416
$ request ->getUri ()->willReturn ($ uri );
@@ -398,6 +422,9 @@ function it_caches_private_responses_when_allowed(
398
422
$ response ->getHeader ('Cache-Control ' )->willReturn (['private ' ])->shouldBeCalled ();
399
423
$ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
400
424
$ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
425
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
426
+
427
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
401
428
402
429
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
403
430
$ item ->isHit ()->willReturn (false );
@@ -484,6 +511,7 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
484
511
$ stream ->__toString ()->willReturn ($ httpBody );
485
512
$ stream ->isSeekable ()->willReturn (true );
486
513
$ stream ->rewind ()->shouldBeCalled ();
514
+ $ stream ->detach ()->shouldBeCalled ();
487
515
488
516
$ request ->getMethod ()->willReturn ('GET ' );
489
517
$ request ->getUri ()->willReturn ($ uri );
@@ -495,6 +523,9 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
495
523
$ response ->getHeader ('Cache-Control ' )->willReturn ([])->shouldBeCalled ();
496
524
$ response ->getHeader ('Expires ' )->willReturn ([])->shouldBeCalled ();
497
525
$ response ->getHeader ('ETag ' )->willReturn ([])->shouldBeCalled ();
526
+ $ response ->withBody ($ stream )->shouldBeCalled ()->willReturn ($ response );
527
+
528
+ $ this ->streamFactory ->createStream ($ httpBody )->shouldBeCalled ()->willReturn ($ stream );
498
529
499
530
$ pool ->getItem (Argument::any ())->shouldBeCalled ()->willReturn ($ item );
500
531
$ item ->isHit ()->willReturn (false );
0 commit comments