CacheTestCase.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. class CacheTestCase extends \PHPUnit_Framework_TestCase
  16. {
  17. protected $kernel;
  18. protected $cache;
  19. protected $caches;
  20. protected $cacheConfig;
  21. protected $request;
  22. protected $response;
  23. protected $responses;
  24. public function setUp()
  25. {
  26. $this->kernel = null;
  27. $this->cache = null;
  28. $this->caches = array();
  29. $this->cacheConfig = array();
  30. $this->request = null;
  31. $this->response = null;
  32. $this->responses = array();
  33. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  34. }
  35. public function tearDown()
  36. {
  37. $this->kernel = null;
  38. $this->cache = null;
  39. $this->caches = null;
  40. $this->request = null;
  41. $this->response = null;
  42. $this->responses = null;
  43. $this->cacheConfig = null;
  44. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  45. }
  46. public function assertHttpKernelIsCalled()
  47. {
  48. $this->assertTrue($this->kernel->hasBeenCalled());
  49. }
  50. public function assertHttpKernelIsNotCalled()
  51. {
  52. $this->assertFalse($this->kernel->hasBeenCalled());
  53. }
  54. public function assertResponseOk()
  55. {
  56. $this->assertEquals(200, $this->response->getStatusCode());
  57. }
  58. public function assertTraceContains($trace)
  59. {
  60. $traces = $this->cache->getTraces();
  61. $traces = current($traces);
  62. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  63. }
  64. public function assertTraceNotContains($trace)
  65. {
  66. $traces = $this->cache->getTraces();
  67. $traces = current($traces);
  68. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  69. }
  70. public function request($method, $uri = '/', $server = array(), $cookies = array())
  71. {
  72. if (null === $this->kernel) {
  73. throw new \LogicException('You must call setNextResponse() before calling request().');
  74. }
  75. $this->kernel->reset();
  76. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  77. $this->cacheConfig['debug'] = true;
  78. $this->cache = new Cache($this->kernel, $this->store, null, $this->cacheConfig);
  79. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  80. $this->response = $this->cache->handle($this->request);
  81. $this->responses[] = $this->response;
  82. }
  83. public function getMetaStorageValues()
  84. {
  85. $values = array();
  86. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  87. $values[] = file_get_contents($file);
  88. }
  89. return $values;
  90. }
  91. // A basic response with 200 status code and a tiny body.
  92. public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
  93. {
  94. $called = false;
  95. $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
  96. }
  97. static public function clearDirectory($directory)
  98. {
  99. if (!is_dir($directory)) {
  100. return;
  101. }
  102. $fp = opendir($directory);
  103. while (false !== $file = readdir($fp)) {
  104. if (!in_array($file, array('.', '..')))
  105. {
  106. if (is_link($directory.'/'.$file)) {
  107. unlink($directory.'/'.$file);
  108. } else if (is_dir($directory.'/'.$file)) {
  109. self::clearDirectory($directory.'/'.$file);
  110. rmdir($directory.'/'.$file);
  111. } else {
  112. unlink($directory.'/'.$file);
  113. }
  114. }
  115. }
  116. closedir($fp);
  117. }
  118. }