4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\HttpCache;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\HttpCache\Esi;
17 use Symfony\Component\HttpKernel\HttpCache\HttpCache;
18 use Symfony\Component\HttpKernel\HttpCache\Store;
19 use Symfony\Component\HttpKernel\HttpKernelInterface;
21 class HttpCacheTestCase extends TestCase
26 protected $cacheConfig;
38 protected function setUp()
44 $this->caches = array();
45 $this->cacheConfig = array();
47 $this->request = null;
48 $this->response = null;
49 $this->responses = array();
53 $this->clearDirectory(sys_get_temp_dir().'/http_cache');
56 protected function tearDown()
59 $this->cache->getStore()->cleanup();
64 $this->request = null;
65 $this->response = null;
66 $this->responses = null;
67 $this->cacheConfig = null;
71 $this->clearDirectory(sys_get_temp_dir().'/http_cache');
74 public function assertHttpKernelIsCalled()
76 $this->assertTrue($this->kernel->hasBeenCalled());
79 public function assertHttpKernelIsNotCalled()
81 $this->assertFalse($this->kernel->hasBeenCalled());
84 public function assertResponseOk()
86 $this->assertEquals(200, $this->response->getStatusCode());
89 public function assertTraceContains($trace)
91 $traces = $this->cache->getTraces();
92 $traces = current($traces);
94 $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
97 public function assertTraceNotContains($trace)
99 $traces = $this->cache->getTraces();
100 $traces = current($traces);
102 $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
105 public function assertExceptionsAreCaught()
107 $this->assertTrue($this->kernel->isCatchingExceptions());
110 public function assertExceptionsAreNotCaught()
112 $this->assertFalse($this->kernel->isCatchingExceptions());
115 public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
117 if (null === $this->kernel) {
118 throw new \LogicException('You must call setNextResponse() before calling request().');
121 $this->kernel->reset();
123 $this->store = new Store(sys_get_temp_dir().'/http_cache');
125 $this->cacheConfig['debug'] = true;
127 $this->esi = $esi ? new Esi() : null;
128 $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
129 $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
130 $this->request->headers->add($headers);
132 $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
134 $this->responses[] = $this->response;
137 public function getMetaStorageValues()
140 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
141 $values[] = file_get_contents($file);
147 // A basic response with 200 status code and a tiny body.
148 public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
150 $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
153 public function setNextResponses($responses)
155 $this->kernel = new TestMultipleHttpKernel($responses);
158 public function catchExceptions($catch = true)
160 $this->catch = $catch;
163 public static function clearDirectory($directory)
165 if (!is_dir($directory)) {
169 $fp = opendir($directory);
170 while (false !== $file = readdir($fp)) {
171 if (!\in_array($file, array('.', '..'))) {
172 if (is_link($directory.'/'.$file)) {
173 unlink($directory.'/'.$file);
174 } elseif (is_dir($directory.'/'.$file)) {
175 self::clearDirectory($directory.'/'.$file);
176 rmdir($directory.'/'.$file);
178 unlink($directory.'/'.$file);