PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Front_End/vendor/symfony/http-kernel/Tests/HttpCache/HttpCacheTestCase.php

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