96a66771a06bc366c2024aacb978612ee1198222
[yaffs-website] / vendor / symfony / http-kernel / Tests / HttpCache / HttpCacheTestCase.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\HttpCache;
13
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;
20
21 class HttpCacheTestCase extends TestCase
22 {
23     protected $kernel;
24     protected $cache;
25     protected $caches;
26     protected $cacheConfig;
27     protected $request;
28     protected $response;
29     protected $responses;
30     protected $catch;
31     protected $esi;
32     protected $store;
33
34     protected function setUp()
35     {
36         $this->kernel = null;
37
38         $this->cache = null;
39         $this->esi = null;
40         $this->caches = array();
41         $this->cacheConfig = array();
42
43         $this->request = null;
44         $this->response = null;
45         $this->responses = array();
46
47         $this->catch = false;
48
49         $this->clearDirectory(sys_get_temp_dir().'/http_cache');
50     }
51
52     protected function tearDown()
53     {
54         if ($this->cache) {
55             $this->cache->getStore()->cleanup();
56         }
57         $this->kernel = null;
58         $this->cache = null;
59         $this->caches = null;
60         $this->request = null;
61         $this->response = null;
62         $this->responses = null;
63         $this->cacheConfig = null;
64         $this->catch = null;
65         $this->esi = null;
66
67         $this->clearDirectory(sys_get_temp_dir().'/http_cache');
68     }
69
70     public function assertHttpKernelIsCalled()
71     {
72         $this->assertTrue($this->kernel->hasBeenCalled());
73     }
74
75     public function assertHttpKernelIsNotCalled()
76     {
77         $this->assertFalse($this->kernel->hasBeenCalled());
78     }
79
80     public function assertResponseOk()
81     {
82         $this->assertEquals(200, $this->response->getStatusCode());
83     }
84
85     public function assertTraceContains($trace)
86     {
87         $traces = $this->cache->getTraces();
88         $traces = current($traces);
89
90         $this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
91     }
92
93     public function assertTraceNotContains($trace)
94     {
95         $traces = $this->cache->getTraces();
96         $traces = current($traces);
97
98         $this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
99     }
100
101     public function assertExceptionsAreCaught()
102     {
103         $this->assertTrue($this->kernel->isCatchingExceptions());
104     }
105
106     public function assertExceptionsAreNotCaught()
107     {
108         $this->assertFalse($this->kernel->isCatchingExceptions());
109     }
110
111     public function request($method, $uri = '/', $server = array(), $cookies = array(), $esi = false, $headers = array())
112     {
113         if (null === $this->kernel) {
114             throw new \LogicException('You must call setNextResponse() before calling request().');
115         }
116
117         $this->kernel->reset();
118
119         $this->store = new Store(sys_get_temp_dir().'/http_cache');
120
121         $this->cacheConfig['debug'] = true;
122
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);
127
128         $this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
129
130         $this->responses[] = $this->response;
131     }
132
133     public function getMetaStorageValues()
134     {
135         $values = array();
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);
138         }
139
140         return $values;
141     }
142
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)
145     {
146         $this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
147     }
148
149     public function setNextResponses($responses)
150     {
151         $this->kernel = new TestMultipleHttpKernel($responses);
152     }
153
154     public function catchExceptions($catch = true)
155     {
156         $this->catch = $catch;
157     }
158
159     public static function clearDirectory($directory)
160     {
161         if (!is_dir($directory)) {
162             return;
163         }
164
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);
173                 } else {
174                     unlink($directory.'/'.$file);
175                 }
176             }
177         }
178
179         closedir($fp);
180     }
181 }