|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\UX\LiveComponent\Tests\Unit\Twig; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Cache\Adapter\NullAdapter; |
| 16 | +use Symfony\Component\Cache\Adapter\PhpArrayAdapter; |
| 17 | +use Symfony\UX\LiveComponent\Twig\TemplateCacheWarmer; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Simon André <[email protected]> |
| 21 | + */ |
| 22 | +final class TemplateCacheWarmerTest extends TestCase |
| 23 | +{ |
| 24 | + private string $cacheDir; |
| 25 | + private string $cacheFile; |
| 26 | + private TemplateCacheWarmer $templateCacheWarmer; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->cacheDir ??= sys_get_temp_dir(); |
| 31 | + $this->cacheFile ??= $this->cacheDir.'/cache_file'; |
| 32 | + if (file_exists($this->cacheFile)) { |
| 33 | + unlink($this->cacheFile); |
| 34 | + } |
| 35 | + $this->templateCacheWarmer ??= new TemplateCacheWarmer( |
| 36 | + new \ArrayObject(['template1', 'template2']), |
| 37 | + 'cache_file', |
| 38 | + 'secret' |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testWarmUpCreatesCacheFile(): void |
| 43 | + { |
| 44 | + $this->assertFileDoesNotExist($this->cacheFile); |
| 45 | + |
| 46 | + $this->templateCacheWarmer->warmUp($this->cacheDir); |
| 47 | + |
| 48 | + $this->assertFileExists($this->cacheFile); |
| 49 | + } |
| 50 | + |
| 51 | + public function testWarmUpCreatesCorrectCacheContent(): void |
| 52 | + { |
| 53 | + $this->templateCacheWarmer->warmUp($this->cacheDir); |
| 54 | + $adapter = new PhpArrayAdapter($this->cacheFile, new NullAdapter()); |
| 55 | + $item = $adapter->getItem('map'); |
| 56 | + |
| 57 | + $this->assertSame( |
| 58 | + [ |
| 59 | + hash('xxh128', 'template1secret') => 'template1', |
| 60 | + hash('xxh128', 'template2secret') => 'template2', |
| 61 | + ], |
| 62 | + $item->get() |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function testWarmUpCreatesReproductibleTemplateMap(): void |
| 67 | + { |
| 68 | + $this->templateCacheWarmer->warmUp($this->cacheDir); |
| 69 | + $adapter = new PhpArrayAdapter($this->cacheFile, new NullAdapter()); |
| 70 | + $map1 = $adapter->getItem('map')->get(); |
| 71 | + |
| 72 | + $this->templateCacheWarmer->warmUp($this->cacheDir); |
| 73 | + $adapter = new PhpArrayAdapter($this->cacheFile, new NullAdapter()); |
| 74 | + $map2 = $adapter->getItem('map')->get(); |
| 75 | + |
| 76 | + $this->assertSame($map1, $map2); |
| 77 | + } |
| 78 | +} |
0 commit comments