|
| 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\Bundle\FrameworkBundle\Tests\Test; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | +use Symfony\Component\Filesystem\Filesystem; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | +use Symfony\Component\HttpKernel\HttpCache\HttpCache; |
| 21 | +use Symfony\Component\HttpKernel\HttpCache\Store; |
| 22 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 23 | +use Symfony\Component\HttpKernel\Kernel; |
| 24 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 25 | + |
| 26 | +class KernelTestCaseHttpCacheTest extends KernelTestCase |
| 27 | +{ |
| 28 | + private static string $baseDir; |
| 29 | + |
| 30 | + public static function setUpBeforeClass(): void |
| 31 | + { |
| 32 | + self::$baseDir = sys_get_temp_dir().'/sf_http_cache_kernel_testcase_'.uniqid('', true); |
| 33 | + } |
| 34 | + |
| 35 | + public static function tearDownAfterClass(): void |
| 36 | + { |
| 37 | + if (isset(self::$baseDir)) { |
| 38 | + (new Filesystem())->remove(self::$baseDir); |
| 39 | + } |
| 40 | + |
| 41 | + parent::tearDownAfterClass(); |
| 42 | + } |
| 43 | + |
| 44 | + protected static function createKernel(array $options = []): KernelInterface |
| 45 | + { |
| 46 | + return new HttpCacheTestKernel(self::$baseDir, $options['environment'] ?? 'test', $options['debug'] ?? true); |
| 47 | + } |
| 48 | + |
| 49 | + public function testHttpCacheIsClearedBetweenKernelShutdowns() |
| 50 | + { |
| 51 | + DynamicHttpKernel::$counter = 0; |
| 52 | + |
| 53 | + $kernel = $this->bootKernelForHttpCache(); |
| 54 | + $response = $kernel->handle(Request::create('/')); |
| 55 | + |
| 56 | + $this->assertSame('count: 1', $response->getContent()); |
| 57 | + |
| 58 | + static::ensureKernelShutdown(); |
| 59 | + |
| 60 | + $kernel = $this->bootKernelForHttpCache(); |
| 61 | + $response = $kernel->handle(Request::create('/')); |
| 62 | + |
| 63 | + $this->assertSame('count: 2', $response->getContent()); |
| 64 | + } |
| 65 | + |
| 66 | + private function bootKernelForHttpCache(): KernelInterface |
| 67 | + { |
| 68 | + $kernel = static::createKernel(); |
| 69 | + $kernel->boot(); |
| 70 | + static::$kernel = $kernel; |
| 71 | + static::$booted = true; |
| 72 | + |
| 73 | + return $kernel; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class HttpCacheTestKernel extends Kernel |
| 78 | +{ |
| 79 | + public function __construct( |
| 80 | + private readonly string $baseDir, |
| 81 | + string $environment, |
| 82 | + bool $debug, |
| 83 | + ) { |
| 84 | + parent::__construct($environment, $debug); |
| 85 | + } |
| 86 | + |
| 87 | + public function registerBundles(): iterable |
| 88 | + { |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | + public function registerContainerConfiguration(\Symfony\Component\Config\Loader\LoaderInterface $loader): void |
| 93 | + { |
| 94 | + $loader->load(function (ContainerBuilder $container): void { |
| 95 | + $container->register('kernel', KernelInterface::class) |
| 96 | + ->setSynthetic(true) |
| 97 | + ->setPublic(true); |
| 98 | + |
| 99 | + $container->register('http_kernel', DynamicHttpKernel::class) |
| 100 | + ->setPublic(true); |
| 101 | + |
| 102 | + $container->register('http_cache.store', Store::class) |
| 103 | + ->setPublic(true) |
| 104 | + ->setArguments([$this->getCacheDir().'/http_cache']); |
| 105 | + |
| 106 | + $container->register('http_cache', HttpCache::class) |
| 107 | + ->setPublic(true) |
| 108 | + ->setArguments([ |
| 109 | + new Reference('kernel'), |
| 110 | + new Reference('http_cache.store'), |
| 111 | + null, |
| 112 | + [], |
| 113 | + ]); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + public function getProjectDir(): string |
| 118 | + { |
| 119 | + return $this->baseDir; |
| 120 | + } |
| 121 | + |
| 122 | + public function getCacheDir(): string |
| 123 | + { |
| 124 | + return $this->baseDir.'/cache'; |
| 125 | + } |
| 126 | + |
| 127 | + public function getLogDir(): string |
| 128 | + { |
| 129 | + return $this->baseDir.'/log'; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class DynamicHttpKernel implements HttpKernelInterface |
| 134 | +{ |
| 135 | + public static int $counter = 0; |
| 136 | + |
| 137 | + public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = true): Response |
| 138 | + { |
| 139 | + $response = new Response('count: '.++self::$counter); |
| 140 | + $response->setPublic(); |
| 141 | + $response->setMaxAge(60); |
| 142 | + |
| 143 | + return $response; |
| 144 | + } |
| 145 | +} |
0 commit comments