|
| 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\TwigBundle\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\HttpKernel\Kernel; |
| 15 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 16 | +use Symfony\Component\Filesystem\Filesystem; |
| 17 | +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
| 18 | +use Symfony\Bundle\TwigBundle\TwigBundle; |
| 19 | + |
| 20 | +class TemplateCacheCacheWarmerTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + public function test() |
| 23 | + { |
| 24 | + $kernel = new TemplateCacheCacheWarmerKernel('TemplateCacheCacheWarmer', true); |
| 25 | + $kernel->boot(); |
| 26 | + |
| 27 | + $container = $kernel->getContainer(); |
| 28 | + $cacheWarmer = $container->get('cache_warmer'); |
| 29 | + |
| 30 | + $cacheWarmer->enableOptionalWarmers(); |
| 31 | + $cacheWarmer->warmUp($kernel->getCacheDir()); |
| 32 | + |
| 33 | + $cacheDirectory = $kernel->getCacheDir().'/twig'; |
| 34 | + $this->assertTrue(file_exists($cacheDirectory), 'Cache directory does not exist.'); |
| 35 | + |
| 36 | + $template = 'TwigBundle::layout.html.twig'; |
| 37 | + $twig = $container->get('twig'); |
| 38 | + $twig->loadTemplate($template); |
| 39 | + |
| 40 | + $cacheFileName = $twig->getCacheFilename($template); |
| 41 | + $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.'); |
| 42 | + } |
| 43 | + |
| 44 | + protected function setUp() |
| 45 | + { |
| 46 | + $this->deleteTempDir(); |
| 47 | + } |
| 48 | + |
| 49 | + protected function tearDown() |
| 50 | + { |
| 51 | + $this->deleteTempDir(); |
| 52 | + } |
| 53 | + |
| 54 | + protected function deleteTempDir() |
| 55 | + { |
| 56 | + if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/TemplateCacheCacheWarmerKernel')) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $fs = new Filesystem(); |
| 61 | + $fs->remove($dir); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class TemplateCacheCacheWarmerKernel extends Kernel |
| 66 | +{ |
| 67 | + public function registerBundles() |
| 68 | + { |
| 69 | + return array(new FrameworkBundle(), new TwigBundle()); |
| 70 | + } |
| 71 | + |
| 72 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 73 | + { |
| 74 | + $loader->load(function ($container) { |
| 75 | + $container->loadFromExtension('framework', array( |
| 76 | + 'secret' => '$ecret', |
| 77 | + 'router' => array( |
| 78 | + 'resource' => __DIR__.'/Resources/config/routing.yml', |
| 79 | + ), |
| 80 | + 'templating' => array( |
| 81 | + 'engines' => array('twig'), |
| 82 | + ), |
| 83 | + )); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public function getCacheDir() |
| 88 | + { |
| 89 | + return sys_get_temp_dir().'/'.Kernel::VERSION.'/TemplateCacheCacheWarmerKernel/cache/'.$this->environment; |
| 90 | + } |
| 91 | + |
| 92 | + public function getLogDir() |
| 93 | + { |
| 94 | + return sys_get_temp_dir().'/'.Kernel::VERSION.'/TemplateCacheCacheWarmerKernel/logs'; |
| 95 | + } |
| 96 | +} |
0 commit comments