ResourceCheckerConfigCacheTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Resource\FileResource;
  13. use Symfony\Component\Config\ResourceCheckerConfigCache;
  14. use Symfony\Component\Config\Tests\Resource\ResourceStub;
  15. class ResourceCheckerConfigCacheTest extends TestCase
  16. {
  17. private $cacheFile = null;
  18. protected function setUp()
  19. {
  20. $this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
  21. }
  22. protected function tearDown()
  23. {
  24. $files = array($this->cacheFile, "{$this->cacheFile}.meta");
  25. foreach ($files as $file) {
  26. if (file_exists($file)) {
  27. unlink($file);
  28. }
  29. }
  30. }
  31. public function testGetPath()
  32. {
  33. $cache = new ResourceCheckerConfigCache($this->cacheFile);
  34. $this->assertSame($this->cacheFile, $cache->getPath());
  35. }
  36. public function testCacheIsNotFreshIfEmpty()
  37. {
  38. $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock()
  39. ->expects($this->never())->method('supports');
  40. /* If there is nothing in the cache, it needs to be filled (and thus it's not fresh).
  41. It does not matter if you provide checkers or not. */
  42. unlink($this->cacheFile); // remove tempnam() side effect
  43. $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
  44. $this->assertFalse($cache->isFresh());
  45. }
  46. public function testCacheIsFreshIfNocheckerProvided()
  47. {
  48. /* For example in prod mode, you may choose not to run any checkers
  49. at all. In that case, the cache should always be considered fresh. */
  50. $cache = new ResourceCheckerConfigCache($this->cacheFile);
  51. $this->assertTrue($cache->isFresh());
  52. }
  53. public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
  54. {
  55. /* As in the previous test, but this time we have a resource. */
  56. $cache = new ResourceCheckerConfigCache($this->cacheFile);
  57. $cache->write('', array(new ResourceStub()));
  58. $this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
  59. }
  60. public function testIsFreshWithchecker()
  61. {
  62. $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
  63. $checker->expects($this->once())
  64. ->method('supports')
  65. ->willReturn(true);
  66. $checker->expects($this->once())
  67. ->method('isFresh')
  68. ->willReturn(true);
  69. $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
  70. $cache->write('', array(new ResourceStub()));
  71. $this->assertTrue($cache->isFresh());
  72. }
  73. public function testIsNotFreshWithchecker()
  74. {
  75. $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
  76. $checker->expects($this->once())
  77. ->method('supports')
  78. ->willReturn(true);
  79. $checker->expects($this->once())
  80. ->method('isFresh')
  81. ->willReturn(false);
  82. $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
  83. $cache->write('', array(new ResourceStub()));
  84. $this->assertFalse($cache->isFresh());
  85. }
  86. public function testCacheIsNotFreshWhenUnserializeFails()
  87. {
  88. $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
  89. $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
  90. $cache->write('foo', array(new FileResource(__FILE__)));
  91. $metaFile = "{$this->cacheFile}.meta";
  92. file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
  93. $this->assertFalse($cache->isFresh());
  94. }
  95. public function testCacheKeepsContent()
  96. {
  97. $cache = new ResourceCheckerConfigCache($this->cacheFile);
  98. $cache->write('FOOBAR');
  99. $this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
  100. }
  101. public function testCacheIsNotFreshIfNotExistsMetaFile()
  102. {
  103. $checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
  104. $cache = new ResourceCheckerConfigCache($this->cacheFile, array($checker));
  105. $cache->write('foo', array(new FileResource(__FILE__)));
  106. $metaFile = "{$this->cacheFile}.meta";
  107. unlink($metaFile);
  108. $this->assertFalse($cache->isFresh());
  109. }
  110. }