HttpCacheTestCase.php 5.1 KB

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