PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php

https://github.com/Exercise/symfony
PHP | 180 lines | 135 code | 36 blank | 9 comment | 7 complexity | f4b757f72254cb1abc0474d5ff84ab3a MD5 | raw file
  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. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  30. $this->markTestSkipped('The "HttpFoundation" component is not available');
  31. }
  32. $this->kernel = null;
  33. $this->cache = null;
  34. $this->esi = null;
  35. $this->caches = array();
  36. $this->cacheConfig = array();
  37. $this->request = null;
  38. $this->response = null;
  39. $this->responses = array();
  40. $this->catch = false;
  41. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  42. }
  43. protected function tearDown()
  44. {
  45. $this->kernel = null;
  46. $this->cache = null;
  47. $this->caches = null;
  48. $this->request = null;
  49. $this->response = null;
  50. $this->responses = null;
  51. $this->cacheConfig = null;
  52. $this->catch = null;
  53. $this->esi = null;
  54. $this->clearDirectory(sys_get_temp_dir().'/http_cache');
  55. }
  56. public function assertHttpKernelIsCalled()
  57. {
  58. $this->assertTrue($this->kernel->hasBeenCalled());
  59. }
  60. public function assertHttpKernelIsNotCalled()
  61. {
  62. $this->assertFalse($this->kernel->hasBeenCalled());
  63. }
  64. public function assertResponseOk()
  65. {
  66. $this->assertEquals(200, $this->response->getStatusCode());
  67. }
  68. public function assertTraceContains($trace)
  69. {
  70. $traces = $this->cache->getTraces();
  71. $traces = current($traces);
  72. $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
  73. }
  74. public function assertTraceNotContains($trace)
  75. {
  76. $traces = $this->cache->getTraces();
  77. $traces = current($traces);
  78. $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
  79. }
  80. public function assertExceptionsAreCaught()
  81. {
  82. $this->assertTrue($this->kernel->isCatchingExceptions());
  83. }
  84. public function assertExceptionsAreNotCaught()
  85. {
  86. $this->assertFalse($this->kernel->isCatchingExceptions());
  87. }
  88. public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false)
  89. {
  90. if (null === $this->kernel) {
  91. throw new \LogicException('You must call setNextResponse() before calling request().');
  92. }
  93. $this->kernel->reset();
  94. $this->store = new Store(sys_get_temp_dir().'/http_cache');
  95. $this->cacheConfig['debug'] = true;
  96. $this->esi = $esi ? new Esi() : null;
  97. $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
  98. $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
  99. $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
  100. $this->responses[] = $this->response;
  101. }
  102. public function getMetaStorageValues()
  103. {
  104. $values = array();
  105. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  106. $values[] = file_get_contents($file);
  107. }
  108. return $values;
  109. }
  110. // A basic response with 200 status code and a tiny body.
  111. public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
  112. {
  113. $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
  114. }
  115. public function setNextResponses($responses)
  116. {
  117. $this->kernel = new TestMultipleHttpKernel($responses);
  118. }
  119. public function catchExceptions($catch = true)
  120. {
  121. $this->catch = $catch;
  122. }
  123. static public function clearDirectory($directory)
  124. {
  125. if (!is_dir($directory)) {
  126. return;
  127. }
  128. $fp = opendir($directory);
  129. while (false !== $file = readdir($fp)) {
  130. if (!in_array($file, array('.', '..')))
  131. {
  132. if (is_link($directory.'/'.$file)) {
  133. unlink($directory.'/'.$file);
  134. } elseif (is_dir($directory.'/'.$file)) {
  135. self::clearDirectory($directory.'/'.$file);
  136. rmdir($directory.'/'.$file);
  137. } else {
  138. unlink($directory.'/'.$file);
  139. }
  140. }
  141. }
  142. closedir($fp);
  143. }
  144. }