HttpCacheTestCase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /**
  29. * @var Store
  30. */
  31. protected $store;
  32. protected function setUp()
  33. {
  34. $this->kernel = null;
  35. $this->cache = null;
  36. $this->esi = null;
  37. $this->caches = array();
  38. $this->cacheConfig = array();
  39. $this->request = null;
  40. $this->response = null;
  41. $this->responses = array();
  42. $this->catch = false;
  43. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  44. }
  45. protected function tearDown()
  46. {
  47. if ($this->cache) {
  48. $this->cache->getStore()->cleanup();
  49. }
  50. $this->kernel = null;
  51. $this->cache = null;
  52. $this->caches = null;
  53. $this->request = null;
  54. $this->response = null;
  55. $this->responses = null;
  56. $this->cacheConfig = null;
  57. $this->catch = null;
  58. $this->esi = null;
  59. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  60. }
  61. public function assertHttpKernelIsCalled()
  62. {
  63. $this->assertTrue($this->kernel->hasBeenCalled());
  64. }
  65. public function assertHttpKernelIsNotCalled()
  66. {
  67. $this->assertFalse($this->kernel->hasBeenCalled());
  68. }
  69. public function assertResponseOk()
  70. {
  71. $this->assertEquals(200, $this->response->getStatusCode());
  72. }
  73. public function assertTraceContains($trace)
  74. {
  75. $traces = $this->cache->getTraces();
  76. $traces = current($traces);
  77. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  78. }
  79. public function assertTraceNotContains($trace)
  80. {
  81. $traces = $this->cache->getTraces();
  82. $traces = current($traces);
  83. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  84. }
  85. public function assertExceptionsAreCaught()
  86. {
  87. $this->assertTrue($this->kernel->isCatchingExceptions());
  88. }
  89. public function assertExceptionsAreNotCaught()
  90. {
  91. $this->assertFalse($this->kernel->isCatchingExceptions());
  92. }
  93. public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
  94. {
  95. if (null === $this->kernel) {
  96. throw new \LogicException('You must call setNextResponse() before calling request().');
  97. }
  98. $this->kernel->reset();
  99. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  100. $this->cacheConfig['debug'] = true;
  101. $this->esi = $esi ? new Esi() : null;
  102. $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
  103. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  104. $this->request->headers->add($headers);
  105. $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
  106. $this->responses[] = $this->response;
  107. }
  108. public function getMetaStorageValues()
  109. {
  110. $values = array();
  111. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  112. $values[] = file_get_contents($file);
  113. }
  114. return $values;
  115. }
  116. // A basic response with 200 status code and a tiny body.
  117. public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
  118. {
  119. $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
  120. }
  121. public function setNextResponses($responses)
  122. {
  123. $this->kernel = new TestMultipleHttpKernel($responses);
  124. }
  125. public function catchExceptions($catch = true)
  126. {
  127. $this->catch = $catch;
  128. }
  129. public static function clearDirectory($directory)
  130. {
  131. if (!is_dir($directory)) {
  132. return;
  133. }
  134. $fp = opendir($directory);
  135. while (false !== $file = readdir($fp)) {
  136. if (!in_array($file, array('.', '..'))) {
  137. if (is_link($directory.'/'.$file)) {
  138. unlink($directory.'/'.$file);
  139. } elseif (is_dir($directory.'/'.$file)) {
  140. self::clearDirectory($directory.'/'.$file);
  141. rmdir($directory.'/'.$file);
  142. } else {
  143. unlink($directory.'/'.$file);
  144. }
  145. }
  146. }
  147. closedir($fp);
  148. }
  149. }