HttpCacheTestCase.php 5.0 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\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->clearDirectory(sys_get_temp_dir().'/http_cache');
  53. }
  54. public function assertHttpKernelIsCalled()
  55. {
  56. $this->assertTrue($this->kernel->hasBeenCalled());
  57. }
  58. public function assertHttpKernelIsNotCalled()
  59. {
  60. $this->assertFalse($this->kernel->hasBeenCalled());
  61. }
  62. public function assertResponseOk()
  63. {
  64. $this->assertEquals(200, $this->response->getStatusCode());
  65. }
  66. public function assertTraceContains($trace)
  67. {
  68. $traces = $this->cache->getTraces();
  69. $traces = current($traces);
  70. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  71. }
  72. public function assertTraceNotContains($trace)
  73. {
  74. $traces = $this->cache->getTraces();
  75. $traces = current($traces);
  76. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  77. }
  78. public function assertExceptionsAreCaught()
  79. {
  80. $this->assertTrue($this->kernel->isCatchingExceptions());
  81. }
  82. public function assertExceptionsAreNotCaught()
  83. {
  84. $this->assertFalse($this->kernel->isCatchingExceptions());
  85. }
  86. public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false)
  87. {
  88. if (null === $this->kernel) {
  89. throw new \LogicException('You must call setNextResponse() before calling request().');
  90. }
  91. $this->kernel->reset();
  92. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  93. $this->cacheConfig['debug'] = true;
  94. $this->esi = $esi ? new Esi() : null;
  95. $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
  96. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  97. $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
  98. $this->responses[] = $this->response;
  99. }
  100. public function getMetaStorageValues()
  101. {
  102. $values = array();
  103. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  104. $values[] = file_get_contents($file);
  105. }
  106. return $values;
  107. }
  108. // A basic response with 200 status code and a tiny body.
  109. public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
  110. {
  111. $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
  112. }
  113. public function setNextResponses($responses)
  114. {
  115. $this->kernel = new TestMultipleHttpKernel($responses);
  116. }
  117. public function catchExceptions($catch = true)
  118. {
  119. $this->catch = $catch;
  120. }
  121. static public function clearDirectory($directory)
  122. {
  123. if (!is_dir($directory)) {
  124. return;
  125. }
  126. $fp = opendir($directory);
  127. while (false !== $file = readdir($fp)) {
  128. if (!in_array($file, array('.', '..')))
  129. {
  130. if (is_link($directory.'/'.$file)) {
  131. unlink($directory.'/'.$file);
  132. } else if (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. }