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;
34 protected function setUp()
40 $this->caches = array();
41 $this->cacheConfig = array();
43 $this->request = null;
44 $this->response = null;
45 $this->responses = array();
49 $this->clearDirectory(sys_get_temp_dir().'/http_cache');
52 protected function tearDown()
55 $this->cache->getStore()->cleanup();
60 $this->request = null;
61 $this->response = null;
62 $this->responses = null;
63 $this->cacheConfig = null;
67 $this->clearDirectory(sys_get_temp_dir().'/http_cache');
70 public function assertHttpKernelIsCalled()
72 $this->assertTrue($this->kernel->hasBeenCalled());
75 public function assertHttpKernelIsNotCalled()
77 $this->assertFalse($this->kernel->hasBeenCalled());
80 public function assertResponseOk()
82 $this->assertEquals(200, $this->response->getStatusCode());
85 public function assertTraceContains($trace)
87 $traces = $this->cache->getTraces();
88 $traces = current($traces);
90 $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
93 public function assertTraceNotContains($trace)
95 $traces = $this->cache->getTraces();
96 $traces = current($traces);
98 $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
101 public function assertExceptionsAreCaught()
103 $this->assertTrue($this->kernel->isCatchingExceptions());
106 public function assertExceptionsAreNotCaught()
108 $this->assertFalse($this->kernel->isCatchingExceptions());
111 public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
113 if (null === $this->kernel) {
114 throw new \LogicException('You must call setNextResponse() before calling request().');
117 $this->kernel->reset();
119 $this->store = new Store(sys_get_temp_dir().'/http_cache');
121 $this->cacheConfig['debug'] = true;
123 $this->esi = $esi ? new Esi() : null;
124 $this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
125 $this->request = Request::create($uri, $method, array(), $cookies, array(), $server);
126 $this->request->headers->add($headers);
128 $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
130 $this->responses[] = $this->response;
133 public function getMetaStorageValues()
136 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
137 $values[] = file_get_contents($file);
143 // A basic response with 200 status code and a tiny body.
144 public function setNextResponse($statusCode = 200, array $headers = array(), $body = 'Hello World', \Closure $customizer = null)
146 $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
149 public function setNextResponses($responses)
151 $this->kernel = new TestMultipleHttpKernel($responses);
154 public function catchExceptions($catch = true)
156 $this->catch = $catch;
159 public static function clearDirectory($directory)
161 if (!is_dir($directory)) {
165 $fp = opendir($directory);
166 while (false !== $file = readdir($fp)) {
167 if (!in_array($file, array('.', '..'))) {
168 if (is_link($directory.'/'.$file)) {
169 unlink($directory.'/'.$file);
170 } elseif (is_dir($directory.'/'.$file)) {
171 self::clearDirectory($directory.'/'.$file);
172 rmdir($directory.'/'.$file);
174 unlink($directory.'/'.$file);