ConfigCacheTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\ConfigCache;
  13. use Symfony\Component\Config\Tests\Resource\ResourceStub;
  14. class ConfigCacheTest extends TestCase
  15. {
  16. private $cacheFile = null;
  17. protected function setUp()
  18. {
  19. $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
  20. }
  21. protected function tearDown()
  22. {
  23. $files = array($this->cacheFile, $this->cacheFile.'.meta');
  24. foreach ($files as $file) {
  25. if (file_exists($file)) {
  26. unlink($file);
  27. }
  28. }
  29. }
  30. /**
  31. * @dataProvider debugModes
  32. */
  33. public function testCacheIsNotValidIfNothingHasBeenCached($debug)
  34. {
  35. unlink($this->cacheFile); // remove tempnam() side effect
  36. $cache = new ConfigCache($this->cacheFile, $debug);
  37. $this->assertFalse($cache->isFresh());
  38. }
  39. public function testIsAlwaysFreshInProduction()
  40. {
  41. $staleResource = new ResourceStub();
  42. $staleResource->setFresh(false);
  43. $cache = new ConfigCache($this->cacheFile, false);
  44. $cache->write('', array($staleResource));
  45. $this->assertTrue($cache->isFresh());
  46. }
  47. /**
  48. * @dataProvider debugModes
  49. */
  50. public function testIsFreshWhenNoResourceProvided($debug)
  51. {
  52. $cache = new ConfigCache($this->cacheFile, $debug);
  53. $cache->write('', array());
  54. $this->assertTrue($cache->isFresh());
  55. }
  56. public function testFreshResourceInDebug()
  57. {
  58. $freshResource = new ResourceStub();
  59. $freshResource->setFresh(true);
  60. $cache = new ConfigCache($this->cacheFile, true);
  61. $cache->write('', array($freshResource));
  62. $this->assertTrue($cache->isFresh());
  63. }
  64. public function testStaleResourceInDebug()
  65. {
  66. $staleResource = new ResourceStub();
  67. $staleResource->setFresh(false);
  68. $cache = new ConfigCache($this->cacheFile, true);
  69. $cache->write('', array($staleResource));
  70. $this->assertFalse($cache->isFresh());
  71. }
  72. public function debugModes()
  73. {
  74. return array(
  75. array(true),
  76. array(false),
  77. );
  78. }
  79. }