CacheTestCase.php 4.7 KB

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