No Description

HttpCacheTestCase.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $this->kernel = null;
  30. $this->cache = null;
  31. $this->esi = null;
  32. $this->caches = array();
  33. $this->cacheConfig = array();
  34. $this->request = null;
  35. $this->response = null;
  36. $this->responses = array();
  37. $this->catch = false;
  38. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  39. }
  40. protected function tearDown()
  41. {
  42. $this->kernel = null;
  43. $this->cache = null;
  44. $this->caches = null;
  45. $this->request = null;
  46. $this->response = null;
  47. $this->responses = null;
  48. $this->cacheConfig = null;
  49. $this->catch = null;
  50. $this->esi = null;
  51. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  52. }
  53. public function assertHttpKernelIsCalled()
  54. {
  55. $this->assertTrue($this->kernel->hasBeenCalled());
  56. }
  57. public function assertHttpKernelIsNotCalled()
  58. {
  59. $this->assertFalse($this->kernel->hasBeenCalled());
  60. }
  61. public function assertResponseOk()
  62. {
  63. $this->assertEquals(200, $this->response->getStatusCode());
  64. }
  65. public function assertTraceContains($trace)
  66. {
  67. $traces = $this->cache->getTraces();
  68. $traces = current($traces);
  69. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  70. }
  71. public function assertTraceNotContains($trace)
  72. {
  73. $traces = $this->cache->getTraces();
  74. $traces = current($traces);
  75. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  76. }
  77. public function assertExceptionsAreCaught()
  78. {
  79. $this->assertTrue($this->kernel->isCatchingExceptions());
  80. }
  81. public function assertExceptionsAreNotCaught()
  82. {
  83. $this->assertFalse($this->kernel->isCatchingExceptions());
  84. }
  85. public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
  86. {
  87. if (null === $this->kernel) {
  88. throw new \LogicException('You must call setNextResponse() before calling request().');
  89. }
  90. $this->kernel->reset();
  91. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  92. $this->cacheConfig['debug'] = true;
  93. $this->esi = $esi ? new Esi() : null;
  94. $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
  95. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  96. $this->request->headers->add($headers);
  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. public static 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. if (is_link($directory.'/'.$file)) {
  130. unlink($directory.'/'.$file);
  131. } elseif (is_dir($directory.'/'.$file)) {
  132. self::clearDirectory($directory.'/'.$file);
  133. rmdir($directory.'/'.$file);
  134. } else {
  135. unlink($directory.'/'.$file);
  136. }
  137. }
  138. }
  139. closedir($fp);
  140. }
  141. }