2
2
3
3
namespace spec \Http \Client \Common \Plugin ;
4
4
5
+ use Prophecy \Argument ;
6
+ use Prophecy \Comparator \Factory as ComparatorFactory ;
5
7
use Http \Message \StreamFactory ;
6
8
use Http \Promise \FulfilledPromise ;
7
9
use PhpSpec \ObjectBehavior ;
@@ -39,27 +41,28 @@ function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $i
39
41
$ request ->getUri ()->willReturn ('/ ' );
40
42
$ response ->getStatusCode ()->willReturn (200 );
41
43
$ response ->getBody ()->willReturn ($ stream );
42
- $ response ->getHeader ('Cache-Control ' )->willReturn (array ());
43
- $ response ->getHeader ('Expires ' )->willReturn (array ());
44
- $ response ->getHeader ('ETag ' )->willReturn (array ());
44
+ $ response ->getHeader ('Cache-Control ' )->willReturn (array ())-> shouldBeCalled () ;
45
+ $ response ->getHeader ('Expires ' )->willReturn (array ())-> shouldBeCalled () ;
46
+ $ response ->getHeader ('ETag ' )->willReturn (array ())-> shouldBeCalled () ;
45
47
46
48
$ pool ->getItem ('e3b717d5883a45ef9493d009741f7c64 ' )->shouldBeCalled ()->willReturn ($ item );
47
49
$ item ->isHit ()->willReturn (false );
48
- $ item ->set ()->willReturn ($ item )->shouldBeCalled ();
49
50
$ item ->expiresAfter (1060 )->willReturn ($ item )->shouldBeCalled ();
50
- $ pool ->save ($ item )->shouldBeCalled ();
51
+
52
+ $ item ->set (Argument::that ($ this ->getCacheItemMatcher ([
53
+ 'response ' => $ response ->getWrappedObject (),
54
+ 'body ' => $ httpBody ,
55
+ 'expiresAt ' => 0 ,
56
+ 'createdAt ' => 0 ,
57
+ 'etag ' => []
58
+ ])))->willReturn ($ item )->shouldBeCalled ();
59
+ $ pool ->save (Argument::any ())->shouldBeCalled ();
51
60
52
61
$ next = function (RequestInterface $ request ) use ($ response ) {
53
62
return new FulfilledPromise ($ response ->getWrappedObject ());
54
63
};
55
64
56
65
$ this ->handleRequest ($ request , $ next , function () {});
57
- $ item ->get ()->shouldHaveKeyWithValue ('response ' , $ response );
58
- $ item ->get ()->shouldHaveKeyWithValue ('body ' , $ httpBody );
59
- $ item ->get ()->shouldHaveKey ('expiresAt ' );
60
- $ item ->get ()->shouldHaveKey ('createdAt ' );
61
- $ item ->get ()->shouldHaveKey ('etag ' );
62
-
63
66
}
64
67
65
68
function it_doesnt_store_failed_responses (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
@@ -107,13 +110,20 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
107
110
$ response ->getHeader ('Cache-Control ' )->willReturn (array ('max-age=40 ' ));
108
111
$ response ->getHeader ('Age ' )->willReturn (array ('15 ' ));
109
112
$ response ->getHeader ('Expires ' )->willReturn (array ());
113
+ $ response ->getHeader ('ETag ' )->willReturn (array ());
110
114
111
115
$ pool ->getItem ('e3b717d5883a45ef9493d009741f7c64 ' )->shouldBeCalled ()->willReturn ($ item );
112
116
$ item ->isHit ()->willReturn (false );
113
117
114
- // 40-15 should be 25
115
- $ item ->set (['response ' => $ response , 'body ' => $ httpBody ])->willReturn ($ item )->shouldBeCalled ();
116
- $ item ->expiresAfter (25 )->willReturn ($ item )->shouldBeCalled ();
118
+ $ item ->set (Argument::that ($ this ->getCacheItemMatcher ([
119
+ 'response ' => $ response ->getWrappedObject (),
120
+ 'body ' => $ httpBody ,
121
+ 'expiresAt ' => 0 ,
122
+ 'createdAt ' => 0 ,
123
+ 'etag ' => []
124
+ ])))->willReturn ($ item )->shouldBeCalled ();
125
+ // 40-15 should be 25 + the default 1000
126
+ $ item ->expiresAfter (1025 )->willReturn ($ item )->shouldBeCalled ();
117
127
$ pool ->save ($ item )->shouldBeCalled ();
118
128
119
129
$ next = function (RequestInterface $ request ) use ($ response ) {
@@ -122,4 +132,25 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
122
132
123
133
$ this ->handleRequest ($ request , $ next , function () {});
124
134
}
135
+
136
+ private function getCacheItemMatcher (array $ expectedData )
137
+ {
138
+ return function (array $ actualData ) use ($ expectedData ) {
139
+ foreach ($ expectedData as $ key => $ value ) {
140
+ if (!isset ($ actualData [$ key ])) {
141
+ return false ;
142
+ }
143
+
144
+ if ($ key === 'expiresAt ' || $ key === 'createdAt ' ) {
145
+ // We do not need to validate the value of these fields.
146
+ continue ;
147
+ }
148
+
149
+ if ($ actualData [$ key ] !== $ value ) {
150
+ return false ;
151
+ }
152
+ }
153
+ return true ;
154
+ };
155
+ }
125
156
}
0 commit comments