2222
2323class CacheTest extends TestCase
2424{
25- private function createCache (string $ type = ' privte ' , int $ maxAge = 86400 , bool $ mustRevalidate = false ): Cache
25+ private function createCache (string $ type , int $ maxAge , bool $ mustRevalidate, bool $ withStreamFactory ): Cache
2626 {
27- return new Cache (new StreamFactory (), $ type , $ maxAge , $ mustRevalidate );
27+ $ streamFactory = $ withStreamFactory ? new StreamFactory () : null ;
28+ return new Cache ($ type , $ maxAge , $ mustRevalidate , $ streamFactory );
2829 }
2930
30-
3131 public function requestFactory (): ServerRequestInterface
3232 {
3333 $ serverRequestFactory = new ServerRequestFactory ();
@@ -71,7 +71,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7171
7272 public function testCacheControlHeader ()
7373 {
74- $ cache = $ this ->createCache ('public ' , 86400 );
74+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
7575 $ req = $ this ->requestFactory ();
7676
7777 $ res = $ cache ->process ($ req , $ this ->createRequestHandler (null ));
@@ -83,7 +83,7 @@ public function testCacheControlHeader()
8383
8484 public function testCacheControlHeaderWithMustRevalidate ()
8585 {
86- $ cache = $ this ->createCache ('private ' , 86400 , true );
86+ $ cache = $ this ->createCache ('private ' , 86400 , true , false );
8787 $ req = $ this ->requestFactory ();
8888
8989 $ res = $ cache ->process ($ req , $ this ->createRequestHandler (null ));
@@ -95,7 +95,7 @@ public function testCacheControlHeaderWithMustRevalidate()
9595
9696 public function testCacheControlHeaderWithZeroMaxAge ()
9797 {
98- $ cache = $ this ->createCache ('private ' , 0 , false );
98+ $ cache = $ this ->createCache ('private ' , 0 , false , false );
9999 $ req = $ this ->requestFactory ();
100100
101101 $ res = $ cache ->process ($ req , $ this ->createRequestHandler (null ));
@@ -107,7 +107,7 @@ public function testCacheControlHeaderWithZeroMaxAge()
107107
108108 public function testCacheControlHeaderDoesNotOverrideExistingHeader ()
109109 {
110- $ cache = $ this ->createCache ('public ' , 86400 );
110+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
111111 $ req = $ this ->requestFactory ();
112112
113113 $ res = $ this ->createResponse ()->withHeader ('Cache-Control ' , 'no-cache,no-store ' );
@@ -123,22 +123,24 @@ public function testLastModifiedWithCacheHit()
123123 $ now = time ();
124124 $ lastModified = gmdate ('D, d M Y H:i:s T ' , $ now + 86400 );
125125 $ ifModifiedSince = gmdate ('D, d M Y H:i:s T ' , $ now + 86400 );
126- $ cache = $ this ->createCache ('public ' , 86400 );
126+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
127127
128128 $ req = $ this ->requestFactory ()->withHeader ('If-Modified-Since ' , $ ifModifiedSince );
129129
130130 $ res = $ this ->createResponse ()->withHeader ('Last-Modified ' , $ lastModified );
131+ $ res ->getBody ()->write ('payload data ' );
131132 $ res = $ cache ->process ($ req , $ this ->createRequestHandler ($ res ));
132133
133134 $ this ->assertEquals (304 , $ res ->getStatusCode ());
135+ self ::assertSame ('payload data ' , (string ) $ res ->getBody ());
134136 }
135137
136138 public function testLastModifiedWithCacheHitAndNewerDate ()
137139 {
138140 $ now = time ();
139141 $ lastModified = gmdate ('D, d M Y H:i:s T ' , $ now + 86400 );
140142 $ ifModifiedSince = gmdate ('D, d M Y H:i:s T ' , $ now + 172800 ); // <-- Newer date
141- $ cache = $ this ->createCache ('public ' , 86400 );
143+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
142144 $ req = $ this ->requestFactory ()->withHeader ('If-Modified-Since ' , $ ifModifiedSince );
143145
144146 $ res = $ this ->createResponse ()->withHeader ('Last-Modified ' , $ lastModified );
@@ -152,7 +154,7 @@ public function testLastModifiedWithCacheHitAndOlderDate()
152154 $ now = time ();
153155 $ lastModified = gmdate ('D, d M Y H:i:s T ' , $ now + 86400 );
154156 $ ifModifiedSince = gmdate ('D, d M Y H:i:s T ' , $ now ); // <-- Older date
155- $ cache = $ this ->createCache ('public ' , 86400 );
157+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
156158 $ req = $ this ->requestFactory ()->withHeader ('If-Modified-Since ' , $ ifModifiedSince );
157159
158160 $ res = $ this ->createResponse ()->withHeader ('Last-Modified ' , $ lastModified );
@@ -166,7 +168,7 @@ public function testLastModifiedWithCacheMiss()
166168 $ now = time ();
167169 $ lastModified = gmdate ('D, d M Y H:i:s T ' , $ now + 86400 );
168170 $ ifModifiedSince = gmdate ('D, d M Y H:i:s T ' , $ now - 86400 );
169- $ cache = $ this ->createCache ('public ' , 86400 );
171+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
170172 $ req = $ this ->requestFactory ()->withHeader ('If-Modified-Since ' , $ ifModifiedSince );
171173
172174 $ res = $ this ->createResponse ()->withHeader ('Last-Modified ' , $ lastModified );
@@ -179,20 +181,22 @@ public function testETagWithCacheHit()
179181 {
180182 $ etag = 'abc ' ;
181183 $ ifNoneMatch = 'abc ' ;
182- $ cache = $ this ->createCache ('public ' , 86400 );
184+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
183185 $ req = $ this ->requestFactory ()->withHeader ('If-None-Match ' , $ ifNoneMatch );
184186
185187 $ res = $ this ->createResponse ()->withHeader ('Etag ' , $ etag );
188+ $ res ->getBody ()->write ('payload data ' );
186189 $ res = $ cache ->process ($ req , $ this ->createRequestHandler ($ res ));
187190
188191 $ this ->assertEquals (304 , $ res ->getStatusCode ());
192+ self ::assertSame ('payload data ' , (string ) $ res ->getBody ());
189193 }
190194
191195 public function testETagWithCacheMiss ()
192196 {
193197 $ etag = 'abc ' ;
194198 $ ifNoneMatch = 'xyz ' ;
195- $ cache = $ this ->createCache ('public ' , 86400 );
199+ $ cache = $ this ->createCache ('public ' , 86400 , false , false );
196200 $ req = $ this ->requestFactory ()->withHeader ('If-None-Match ' , $ ifNoneMatch );
197201
198202 $ res = $ this ->createResponse ()->withHeader ('Etag ' , $ etag );
@@ -201,10 +205,10 @@ public function testETagWithCacheMiss()
201205 $ this ->assertEquals (200 , $ res ->getStatusCode ());
202206 }
203207
204- public function testETagReturnsNoBodyOnCacheHit (): void
208+ public function testETagReturnsNoBodyOnCacheHitWhenAStreamFactoryIsProvided (): void
205209 {
206210 $ etag = 'abc ' ;
207- $ cache = $ this ->createCache ();
211+ $ cache = $ this ->createCache (' private ' , 86400 , false , true );
208212 $ req = $ this ->requestFactory ()->withHeader ('If-None-Match ' , $ etag );
209213
210214 $ res = $ this ->createResponse ()->withHeader ('Etag ' , $ etag );
@@ -215,12 +219,12 @@ public function testETagReturnsNoBodyOnCacheHit(): void
215219 self ::assertSame ('' , (string ) $ res ->getBody ());
216220 }
217221
218- public function testLastModifiedReturnsNoBodyOnCacheHit (): void
222+ public function testLastModifiedReturnsNoBodyOnCacheHitWhenAStreamFactoryIsProvided (): void
219223 {
220224 $ now = time () + 86400 ;
221225 $ lastModified = gmdate ('D, d M Y H:i:s T ' , $ now );
222226 $ ifModifiedSince = gmdate ('D, d M Y H:i:s T ' , $ now );
223- $ cache = $ this ->createCache ();
227+ $ cache = $ this ->createCache (' private ' , 86400 , false , true );
224228
225229 $ req = $ this ->requestFactory ()->withHeader ('If-Modified-Since ' , $ ifModifiedSince );
226230 $ res = $ this ->createResponse ()->withHeader ('Last-Modified ' , $ lastModified );
0 commit comments