HttpCacheTestCase.php 5.0 KB

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