ConfigCacheTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\Component\Config\Tests;
  11. use Symfony\Component\Config\ConfigCache;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. class ConfigTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $resourceFile = null;
  16. private $cacheFile = null;
  17. private $metaFile = null;
  18. public function setUp()
  19. {
  20. $this->resourceFile = tempnam(sys_get_temp_dir(), '_resource');
  21. $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
  22. $this->metaFile = $this->cacheFile.'.meta';
  23. $this->makeCacheFresh();
  24. $this->generateMetaFile();
  25. }
  26. public function tearDown()
  27. {
  28. $files = array($this->cacheFile, $this->metaFile, $this->resourceFile);
  29. foreach ($files as $file) {
  30. if (file_exists($file)) {
  31. unlink($file);
  32. }
  33. }
  34. }
  35. public function testToString()
  36. {
  37. $cache = new ConfigCache($this->cacheFile, true);
  38. $this->assertSame($this->cacheFile, (string) $cache);
  39. }
  40. public function testCacheIsNotFreshIfFileDoesNotExist()
  41. {
  42. unlink($this->cacheFile);
  43. $cache = new ConfigCache($this->cacheFile, false);
  44. $this->assertFalse($cache->isFresh());
  45. }
  46. public function testCacheIsAlwaysFreshIfFileExistsWithDebugDisabled()
  47. {
  48. $this->makeCacheStale();
  49. $cache = new ConfigCache($this->cacheFile, false);
  50. $this->assertTrue($cache->isFresh());
  51. }
  52. public function testCacheIsNotFreshWithoutMetaFile()
  53. {
  54. unlink($this->metaFile);
  55. $cache = new ConfigCache($this->cacheFile, true);
  56. $this->assertFalse($cache->isFresh());
  57. }
  58. public function testCacheIsFreshIfResourceIsFresh()
  59. {
  60. $cache = new ConfigCache($this->cacheFile, true);
  61. $this->assertTrue($cache->isFresh());
  62. }
  63. public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()
  64. {
  65. $this->makeCacheStale();
  66. $cache = new ConfigCache($this->cacheFile, true);
  67. $this->assertFalse($cache->isFresh());
  68. }
  69. public function testWriteDumpsFile()
  70. {
  71. unlink($this->cacheFile);
  72. unlink($this->metaFile);
  73. $cache = new ConfigCache($this->cacheFile, false);
  74. $cache->write('FOOBAR');
  75. $this->assertFileExists($this->cacheFile, 'Cache file is created');
  76. $this->assertSame('FOOBAR', file_get_contents($this->cacheFile));
  77. $this->assertFileNotExists($this->metaFile, 'Meta file is not created');
  78. }
  79. public function testWriteDumpsMetaFileWithDebugEnabled()
  80. {
  81. unlink($this->cacheFile);
  82. unlink($this->metaFile);
  83. $metadata = array(new FileResource($this->resourceFile));
  84. $cache = new ConfigCache($this->cacheFile, true);
  85. $cache->write('FOOBAR', $metadata);
  86. $this->assertFileExists($this->cacheFile, 'Cache file is created');
  87. $this->assertFileExists($this->metaFile, 'Meta file is created');
  88. $this->assertSame(serialize($metadata), file_get_contents($this->metaFile));
  89. }
  90. private function makeCacheFresh()
  91. {
  92. touch($this->resourceFile, filemtime($this->cacheFile) - 3600);
  93. }
  94. private function makeCacheStale()
  95. {
  96. touch($this->cacheFile, time() - 3600);
  97. }
  98. private function generateMetaFile()
  99. {
  100. file_put_contents($this->metaFile, serialize(array(new FileResource($this->resourceFile))));
  101. }
  102. }