1
+ <?php
2
+
3
+ namespace spec \Http \Client \Plugin ;
4
+
5
+ use Http \Client \Utils \Promise \FulfilledPromise ;
6
+ use PhpSpec \ObjectBehavior ;
7
+ use Psr \Cache \CacheItemInterface ;
8
+ use Psr \Cache \CacheItemPoolInterface ;
9
+ use Psr \Http \Message \RequestInterface ;
10
+ use Psr \Http \Message \ResponseInterface ;
11
+
12
+ class CachePluginSpec extends ObjectBehavior
13
+ {
14
+ function let (CacheItemPoolInterface $ pool )
15
+ {
16
+ $ this ->beConstructedWith ($ pool );
17
+ }
18
+
19
+ function it_is_initializable (CacheItemPoolInterface $ pool )
20
+ {
21
+ $ this ->shouldHaveType ('Http\Client\Plugin\CachePlugin ' );
22
+ }
23
+
24
+ function it_is_a_plugin ()
25
+ {
26
+ $ this ->shouldImplement ('Http\Client\Plugin\Plugin ' );
27
+ }
28
+
29
+ function it_caches_responses (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
30
+ {
31
+ $ request ->getMethod ()->willReturn ('GET ' );
32
+ $ request ->getUri ()->willReturn ('/ ' );
33
+ $ response ->getStatusCode ()->willReturn (200 );
34
+ $ response ->getHeader ('Cache-Control ' )->willReturn (array ());
35
+ $ response ->getHeader ('Expires ' )->willReturn (array ());
36
+
37
+ $ pool ->getItem ('e3b717d5883a45ef9493d009741f7c64 ' )->shouldBeCalled ()->willReturn ($ item );
38
+ $ item ->exists ()->willReturn (false );
39
+ $ item ->set ($ response , 60 )->shouldBeCalled ();
40
+ $ pool ->save ($ item )->shouldBeCalled ();
41
+
42
+ $ next = function (RequestInterface $ request ) use ($ response ) {
43
+ return new FulfilledPromise ($ response ->getWrappedObject ());
44
+ };
45
+
46
+ $ this ->handleRequest ($ request , $ next , function () {});
47
+ }
48
+
49
+ function it_doesnt_store_failed_responses (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
50
+ {
51
+ $ request ->getMethod ()->willReturn ('GET ' );
52
+ $ request ->getUri ()->willReturn ('/ ' );
53
+ $ response ->getStatusCode ()->willReturn (400 );
54
+ $ response ->getHeader ('Cache-Control ' )->willReturn (array ());
55
+ $ response ->getHeader ('Expires ' )->willReturn (array ());
56
+
57
+ $ pool ->getItem ('e3b717d5883a45ef9493d009741f7c64 ' )->shouldBeCalled ()->willReturn ($ item );
58
+ $ item ->exists ()->willReturn (false );
59
+
60
+ $ next = function (RequestInterface $ request ) use ($ response ) {
61
+ return new FulfilledPromise ($ response ->getWrappedObject ());
62
+ };
63
+
64
+ $ this ->handleRequest ($ request , $ next , function () {});
65
+ }
66
+
67
+ function it_doesnt_store_post_requests (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
68
+ {
69
+ $ request ->getMethod ()->willReturn ('POST ' );
70
+ $ request ->getUri ()->willReturn ('/ ' );
71
+
72
+ $ next = function (RequestInterface $ request ) use ($ response ) {
73
+ return new FulfilledPromise ($ response ->getWrappedObject ());
74
+ };
75
+
76
+ $ this ->handleRequest ($ request , $ next , function () {});
77
+ }
78
+
79
+
80
+ function it_calculate_age_from_response (CacheItemPoolInterface $ pool , CacheItemInterface $ item , RequestInterface $ request , ResponseInterface $ response )
81
+ {
82
+ $ request ->getMethod ()->willReturn ('GET ' );
83
+ $ request ->getUri ()->willReturn ('/ ' );
84
+ $ response ->getStatusCode ()->willReturn (200 );
85
+ $ response ->getHeader ('Cache-Control ' )->willReturn (array ('max-age=40 ' ));
86
+ $ response ->getHeader ('Age ' )->willReturn (array ('15 ' ));
87
+ $ response ->getHeader ('Expires ' )->willReturn (array ());
88
+
89
+ $ pool ->getItem ('e3b717d5883a45ef9493d009741f7c64 ' )->shouldBeCalled ()->willReturn ($ item );
90
+ $ item ->exists ()->willReturn (false );
91
+
92
+ // 40-15 should be 25
93
+ $ item ->set ($ response , 25 )->shouldBeCalled ();
94
+ $ pool ->save ($ item )->shouldBeCalled ();
95
+
96
+ $ next = function (RequestInterface $ request ) use ($ response ) {
97
+ return new FulfilledPromise ($ response ->getWrappedObject ());
98
+ };
99
+
100
+ $ this ->handleRequest ($ request , $ next , function () {});
101
+ }
102
+ }
0 commit comments