This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTestCase.php

188 lines
5.2 KiB
PHP
Raw Normal View History

2010-06-23 20:42:41 +01:00
<?php
/*
* This file is part of the Symfony package.
2010-06-23 20:42:41 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-06-23 20:42:41 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\Tests\HttpCache;
2010-06-23 20:42:41 +01:00
2017-02-08 07:24:27 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpKernelInterface;
2010-06-23 20:42:41 +01:00
2017-02-08 07:24:27 +00:00
class HttpCacheTestCase extends TestCase
2010-06-23 20:42:41 +01:00
{
protected $kernel;
protected $cache;
protected $caches;
protected $cacheConfig;
protected $request;
protected $response;
protected $responses;
protected $catch;
protected $esi;
/**
* @var Store
*/
protected $store;
2010-06-23 20:42:41 +01:00
2019-08-02 18:02:27 +01:00
protected function setUp()
2010-06-23 20:42:41 +01:00
{
$this->kernel = null;
$this->cache = null;
$this->esi = null;
2019-01-16 09:39:14 +00:00
$this->caches = [];
$this->cacheConfig = [];
2010-06-23 20:42:41 +01:00
$this->request = null;
$this->response = null;
2019-01-16 09:39:14 +00:00
$this->responses = [];
2010-06-23 20:42:41 +01:00
$this->catch = false;
2010-06-23 20:42:41 +01:00
$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}
2019-08-02 18:02:27 +01:00
protected function tearDown()
2010-06-23 20:42:41 +01:00
{
if ($this->cache) {
$this->cache->getStore()->cleanup();
}
2010-06-23 20:42:41 +01:00
$this->kernel = null;
$this->cache = null;
$this->caches = null;
$this->request = null;
$this->response = null;
$this->responses = null;
$this->cacheConfig = null;
$this->catch = null;
$this->esi = null;
2010-06-23 20:42:41 +01:00
$this->clearDirectory(sys_get_temp_dir().'/http_cache');
}
public function assertHttpKernelIsCalled()
{
$this->assertTrue($this->kernel->hasBeenCalled());
}
public function assertHttpKernelIsNotCalled()
{
$this->assertFalse($this->kernel->hasBeenCalled());
}
public function assertResponseOk()
{
$this->assertEquals(200, $this->response->getStatusCode());
}
public function assertTraceContains($trace)
{
$traces = $this->cache->getTraces();
$traces = current($traces);
$this->assertRegExp('/'.$trace.'/', implode(', ', $traces));
}
public function assertTraceNotContains($trace)
{
$traces = $this->cache->getTraces();
$traces = current($traces);
$this->assertNotRegExp('/'.$trace.'/', implode(', ', $traces));
}
public function assertExceptionsAreCaught()
{
$this->assertTrue($this->kernel->isCatchingExceptions());
}
public function assertExceptionsAreNotCaught()
{
$this->assertFalse($this->kernel->isCatchingExceptions());
}
2019-01-16 09:39:14 +00:00
public function request($method, $uri = '/', $server = [], $cookies = [], $esi = false, $headers = [])
2010-06-23 20:42:41 +01:00
{
if (null === $this->kernel) {
throw new \LogicException('You must call setNextResponse() before calling request().');
}
$this->kernel->reset();
$this->store = new Store(sys_get_temp_dir().'/http_cache');
if (!isset($this->cacheConfig['debug'])) {
$this->cacheConfig['debug'] = true;
}
$this->esi = $esi ? new Esi() : null;
$this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
2019-01-16 09:39:14 +00:00
$this->request = Request::create($uri, $method, [], $cookies, [], $server);
$this->request->headers->add($headers);
2010-06-23 20:42:41 +01:00
$this->response = $this->cache->handle($this->request, HttpKernelInterface::MASTER_REQUEST, $this->catch);
2010-06-23 20:42:41 +01:00
$this->responses[] = $this->response;
}
public function getMetaStorageValues()
{
2019-01-16 09:39:14 +00:00
$values = [];
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/http_cache/md', \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
2010-06-23 20:42:41 +01:00
$values[] = file_get_contents($file);
}
return $values;
}
// A basic response with 200 status code and a tiny body.
2019-01-16 09:39:14 +00:00
public function setNextResponse($statusCode = 200, array $headers = [], $body = 'Hello World', \Closure $customizer = null)
2010-06-23 20:42:41 +01:00
{
$this->kernel = new TestHttpKernel($body, $statusCode, $headers, $customizer);
}
public function setNextResponses($responses)
{
$this->kernel = new TestMultipleHttpKernel($responses);
}
public function catchExceptions($catch = true)
{
$this->catch = $catch;
}
2012-07-09 13:50:58 +01:00
public static function clearDirectory($directory)
2010-06-23 20:42:41 +01:00
{
if (!is_dir($directory)) {
return;
}
$fp = opendir($directory);
while (false !== $file = readdir($fp)) {
2019-01-16 09:39:14 +00:00
if (!\in_array($file, ['.', '..'])) {
2010-06-23 20:42:41 +01:00
if (is_link($directory.'/'.$file)) {
unlink($directory.'/'.$file);
2011-12-18 13:42:59 +00:00
} elseif (is_dir($directory.'/'.$file)) {
2010-06-23 20:42:41 +01:00
self::clearDirectory($directory.'/'.$file);
rmdir($directory.'/'.$file);
} else {
unlink($directory.'/'.$file);
}
}
}
closedir($fp);
}
}