CacheClearCommandTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\FrameworkBundle\Tests\Command\CacheClearCommand;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Bundle\FrameworkBundle\Tests\Command\CacheClearCommand\Fixture\TestAppKernel;
  13. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  14. use Symfony\Component\Config\ConfigCacheFactory;
  15. use Symfony\Component\Config\Resource\ResourceInterface;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Output\NullOutput;
  18. use Symfony\Component\Filesystem\Filesystem;
  19. use Symfony\Component\Finder\Finder;
  20. class CacheClearCommandTest extends TestCase
  21. {
  22. /** @var TestAppKernel */
  23. private $kernel;
  24. /** @var Filesystem */
  25. private $fs;
  26. private $rootDir;
  27. protected function setUp()
  28. {
  29. $this->fs = new Filesystem();
  30. $this->kernel = new TestAppKernel('test', true);
  31. $this->rootDir = sys_get_temp_dir().\DIRECTORY_SEPARATOR.uniqid('sf2_cache_', true);
  32. $this->kernel->setRootDir($this->rootDir);
  33. $this->fs->mkdir($this->rootDir);
  34. }
  35. protected function tearDown()
  36. {
  37. $this->fs->remove($this->rootDir);
  38. }
  39. public function testCacheIsFreshAfterCacheClearedWithWarmup()
  40. {
  41. $input = new ArrayInput(array('cache:clear'));
  42. $application = new Application($this->kernel);
  43. $application->setCatchExceptions(false);
  44. $application->doRun($input, new NullOutput());
  45. // Ensure that all *.meta files are fresh
  46. $finder = new Finder();
  47. $metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
  48. // simply check that cache is warmed up
  49. $this->assertNotEmpty($metaFiles);
  50. $configCacheFactory = new ConfigCacheFactory(true);
  51. $that = $this;
  52. foreach ($metaFiles as $file) {
  53. $configCacheFactory->cache(substr($file, 0, -5), function () use ($that, $file) {
  54. $that->fail(sprintf('Meta file "%s" is not fresh', (string) $file));
  55. });
  56. }
  57. // check that app kernel file present in meta file of container's cache
  58. $containerRef = new \ReflectionObject($this->kernel->getContainer());
  59. $containerFile = $containerRef->getFileName();
  60. $containerMetaFile = $containerFile.'.meta';
  61. $kernelRef = new \ReflectionObject($this->kernel);
  62. $kernelFile = $kernelRef->getFileName();
  63. /** @var ResourceInterface[] $meta */
  64. $meta = unserialize(file_get_contents($containerMetaFile));
  65. $found = false;
  66. foreach ($meta as $resource) {
  67. if ((string) $resource === $kernelFile) {
  68. $found = true;
  69. break;
  70. }
  71. }
  72. $this->assertTrue($found, 'Kernel file should present as resource');
  73. $this->assertRegExp(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', \get_class($this->kernel->getContainer())), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
  74. }
  75. }