CacheWarmingTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\Tests;
  11. use Symfony\Component\HttpKernel\Kernel;
  12. use Symfony\Component\Config\Loader\LoaderInterface;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
  15. use Symfony\Bundle\TwigBundle\TwigBundle;
  16. class NewCacheWamingTest extends \PHPUnit_Framework_TestCase
  17. {
  18. public function testCacheIsProperlyWarmedWhenTemplatingIsAvailable()
  19. {
  20. $kernel = new CacheWarmingKernel(true);
  21. $kernel->boot();
  22. $warmer = $kernel->getContainer()->get('cache_warmer');
  23. $warmer->enableOptionalWarmers();
  24. $warmer->warmUp($kernel->getCacheDir());
  25. $this->assertFileExists($kernel->getCacheDir().'/twig');
  26. }
  27. public function testCacheIsProperlyWarmedWhenTemplatingIsDisabled()
  28. {
  29. $kernel = new CacheWarmingKernel(false);
  30. $kernel->boot();
  31. $warmer = $kernel->getContainer()->get('cache_warmer');
  32. $warmer->enableOptionalWarmers();
  33. $warmer->warmUp($kernel->getCacheDir());
  34. $this->assertFileExists($kernel->getCacheDir().'/twig');
  35. }
  36. protected function setUp()
  37. {
  38. $this->deleteTempDir();
  39. }
  40. protected function tearDown()
  41. {
  42. $this->deleteTempDir();
  43. }
  44. private function deleteTempDir()
  45. {
  46. if (!file_exists($dir = sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel')) {
  47. return;
  48. }
  49. $fs = new Filesystem();
  50. $fs->remove($dir);
  51. }
  52. }
  53. class CacheWarmingKernel extends Kernel
  54. {
  55. private $withTemplating;
  56. public function __construct($withTemplating)
  57. {
  58. $this->withTemplating = $withTemplating;
  59. parent::__construct(($withTemplating ? 'with' : 'without').'_templating', true);
  60. }
  61. public function getName()
  62. {
  63. return 'CacheWarming';
  64. }
  65. public function registerBundles()
  66. {
  67. return array(new FrameworkBundle(), new TwigBundle());
  68. }
  69. public function registerContainerConfiguration(LoaderInterface $loader)
  70. {
  71. $loader->load(function ($container) {
  72. $container->loadFromExtension('framework', array(
  73. 'secret' => '$ecret',
  74. ));
  75. });
  76. if ($this->withTemplating) {
  77. $loader->load(function ($container) {
  78. $container->loadFromExtension('framework', array(
  79. 'secret' => '$ecret',
  80. 'templating' => array('engines' => array('twig')),
  81. 'router' => array('resource' => '%kernel.root_dir%/Resources/config/empty_routing.yml'),
  82. ));
  83. });
  84. }
  85. }
  86. public function getCacheDir()
  87. {
  88. return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/cache/'.$this->environment;
  89. }
  90. public function getLogDir()
  91. {
  92. return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/logs';
  93. }
  94. }