HttpCacheTestCase.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Tests\HttpCache;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\HttpCache\Esi;
  14. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  15. use Symfony\Component\HttpKernel\HttpCache\Store;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. class HttpCacheTestCase extends TestCase
  18. {
  19. protected $kernel;
  20. protected $cache;
  21. protected $caches;
  22. protected $cacheConfig;
  23. protected $request;
  24. protected $response;
  25. protected $responses;
  26. protected $catch;
  27. protected $esi;
  28. protected $store;
  29. protected function setUp()
  30. {
  31. $this->kernel = null;
  32. $this->cache = null;
  33. $this->esi = null;
  34. $this->caches = array();
  35. $this->cacheConfig = array();
  36. $this->request = null;
  37. $this->response = null;
  38. $this->responses = array();
  39. $this->catch = false;
  40. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  41. }
  42. protected function tearDown()
  43. {
  44. if ($this->cache) {
  45. $this->cache->getStore()->cleanup();
  46. }
  47. $this->kernel = null;
  48. $this->cache = null;
  49. $this->caches = null;
  50. $this->request = null;
  51. $this->response = null;
  52. $this->responses = null;
  53. $this->cacheConfig = null;
  54. $this->catch = null;
  55. $this->esi = null;
  56. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  57. }
  58. public function assertHttpKernelIsCalled()
  59. {
  60. $this->assertTrue($this->kernel->hasBeenCalled());
  61. }
  62. public function assertHttpKernelIsNotCalled()
  63. {
  64. $this->assertFalse($this->kernel->hasBeenCalled());
  65. }
  66. public function assertResponseOk()
  67. {
  68. $this->assertEquals(200, $this->response->getStatusCode());
  69. }
  70. public function assertTraceContains($trace)
  71. {
  72. $traces = $this->cache->getTraces();
  73. $traces = current($traces);
  74. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  75. }
  76. public function assertTraceNotContains($trace)
  77. {
  78. $traces = $this->cache->getTraces();
  79. $traces = current($traces);
  80. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  81. }
  82. public function assertExceptionsAreCaught()
  83. {
  84. $this->assertTrue($this->kernel->isCatchingExceptions());
  85. }
  86. public function assertExceptionsAreNotCaught()
  87. {
  88. $this->assertFalse($this->kernel->isCatchingExceptions());
  89. }
  90. public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
  91. {
  92. if (null === $this->kernel) {
  93. throw new \LogicException('You must call setNextResponse() before calling request().');
  94. }
  95. $this->kernel->reset();
  96. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  97. $this->cacheConfig['debug'] = true;
  98. $this->esi = $esi ? new Esi() : null;
  99. $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
  100. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  101. $this->request->headers->add($headers);
  102. $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
  103. $this->responses[] = $this->response;
  104. }
  105. public function getMetaStorageValues()
  106. {
  107. $values = array();
  108. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  109. $values[] = file_get_contents($file);
  110. }
  111. return $values;
  112. }
  113. // A basic response with 200 status code and a tiny body.
  114. public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
  115. {
  116. $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
  117. }
  118. public function setNextResponses($responses)
  119. {
  120. $this->kernel = new TestMultipleHttpKernel($responses);
  121. }
  122. public function catchExceptions($catch = true)
  123. {
  124. $this->catch = $catch;
  125. }
  126. public static function clearDirectory($directory)
  127. {
  128. if (!is_dir($directory)) {
  129. return;
  130. }
  131. $fp = opendir($directory);
  132. while (false !== $file = readdir($fp)) {
  133. if (!\in_array($file, array('.', '..'))) {
  134. if (is_link($directory.'/'.$file)) {
  135. unlink($directory.'/'.$file);
  136. } elseif (is_dir($directory.'/'.$file)) {
  137. self::clearDirectory($directory.'/'.$file);
  138. rmdir($directory.'/'.$file);
  139. } else {
  140. unlink($directory.'/'.$file);
  141. }
  142. }
  143. }
  144. closedir($fp);
  145. }
  146. }