ResponseCacheStrategyTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This code is partially based on the Rack-Cache library by Ryan Tomayko,
  8. * which is released under the MIT license.
  9. * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  15. use PHPUnit\Framework\TestCase;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\HttpCache\ResponseCacheStrategy;
  18. class ResponseCacheStrategyTest extends TestCase
  19. {
  20. public function testMinimumSharedMaxAgeWins()
  21. {
  22. $cacheStrategy = new ResponseCacheStrategy();
  23. $response1 = new Response();
  24. $response1->setSharedMaxAge(60);
  25. $cacheStrategy->add($response1);
  26. $response2 = new Response();
  27. $response2->setSharedMaxAge(3600);
  28. $cacheStrategy->add($response2);
  29. $response = new Response();
  30. $response->setSharedMaxAge(86400);
  31. $cacheStrategy->update($response);
  32. $this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
  33. }
  34. public function testSharedMaxAgeNotSetIfNotSetInAnyEmbeddedRequest()
  35. {
  36. $cacheStrategy = new ResponseCacheStrategy();
  37. $response1 = new Response();
  38. $response1->setSharedMaxAge(60);
  39. $cacheStrategy->add($response1);
  40. $response2 = new Response();
  41. $cacheStrategy->add($response2);
  42. $response = new Response();
  43. $response->setSharedMaxAge(86400);
  44. $cacheStrategy->update($response);
  45. $this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
  46. }
  47. public function testSharedMaxAgeNotSetIfNotSetInMasterRequest()
  48. {
  49. $cacheStrategy = new ResponseCacheStrategy();
  50. $response1 = new Response();
  51. $response1->setSharedMaxAge(60);
  52. $cacheStrategy->add($response1);
  53. $response2 = new Response();
  54. $response2->setSharedMaxAge(3600);
  55. $cacheStrategy->add($response2);
  56. $response = new Response();
  57. $cacheStrategy->update($response);
  58. $this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
  59. }
  60. }