DirectoryResourceTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Resource;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Resource\DirectoryResource;
  13. class DirectoryResourceTest extends TestCase
  14. {
  15. protected $directory;
  16. protected function setUp()
  17. {
  18. $this->directory = sys_get_temp_dir().\DIRECTORY_SEPARATOR.'symfonyDirectoryIterator';
  19. if (!file_exists($this->directory)) {
  20. mkdir($this->directory);
  21. }
  22. touch($this->directory.'/tmp.xml');
  23. }
  24. protected function tearDown()
  25. {
  26. if (!is_dir($this->directory)) {
  27. return;
  28. }
  29. $this->removeDirectory($this->directory);
  30. }
  31. protected function removeDirectory($directory)
  32. {
  33. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::CHILD_FIRST);
  34. foreach ($iterator as $path) {
  35. if (preg_match('#[/\\\\]\.\.?$#', $path->__toString())) {
  36. continue;
  37. }
  38. if ($path->isDir()) {
  39. rmdir($path->__toString());
  40. } else {
  41. unlink($path->__toString());
  42. }
  43. }
  44. rmdir($directory);
  45. }
  46. public function testGetResource()
  47. {
  48. $resource = new DirectoryResource($this->directory);
  49. $this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
  50. }
  51. public function testGetPattern()
  52. {
  53. $resource = new DirectoryResource('foo', 'bar');
  54. $this->assertEquals('bar', $resource->getPattern());
  55. }
  56. public function testIsFresh()
  57. {
  58. $resource = new DirectoryResource($this->directory);
  59. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
  60. $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
  61. $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
  62. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
  63. }
  64. public function testIsFreshUpdateFile()
  65. {
  66. $resource = new DirectoryResource($this->directory);
  67. touch($this->directory.'/tmp.xml', time() + 20);
  68. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
  69. }
  70. public function testIsFreshNewFile()
  71. {
  72. $resource = new DirectoryResource($this->directory);
  73. touch($this->directory.'/new.xml', time() + 20);
  74. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
  75. }
  76. public function testIsFreshNewFileWithDifferentPattern()
  77. {
  78. $resource = new DirectoryResource($this->directory, '/.xml$/');
  79. touch($this->directory.'/new.yaml', time() + 20);
  80. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
  81. }
  82. public function testIsFreshDeleteFile()
  83. {
  84. $resource = new DirectoryResource($this->directory);
  85. $time = time();
  86. sleep(1);
  87. unlink($this->directory.'/tmp.xml');
  88. $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
  89. }
  90. public function testIsFreshDeleteDirectory()
  91. {
  92. $resource = new DirectoryResource($this->directory);
  93. $this->removeDirectory($this->directory);
  94. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
  95. }
  96. public function testIsFreshCreateFileInSubdirectory()
  97. {
  98. $subdirectory = $this->directory.'/subdirectory';
  99. mkdir($subdirectory);
  100. $resource = new DirectoryResource($this->directory);
  101. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
  102. touch($subdirectory.'/newfile.xml', time() + 20);
  103. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
  104. }
  105. public function testIsFreshModifySubdirectory()
  106. {
  107. $resource = new DirectoryResource($this->directory);
  108. $subdirectory = $this->directory.'/subdirectory';
  109. mkdir($subdirectory);
  110. touch($subdirectory, time() + 20);
  111. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
  112. }
  113. public function testFilterRegexListNoMatch()
  114. {
  115. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  116. touch($this->directory.'/new.bar', time() + 20);
  117. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
  118. }
  119. public function testFilterRegexListMatch()
  120. {
  121. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  122. touch($this->directory.'/new.xml', time() + 20);
  123. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
  124. }
  125. public function testSerializeUnserialize()
  126. {
  127. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  128. $unserialized = unserialize(serialize($resource));
  129. $this->assertSame($this->directory, $resource->getResource());
  130. $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
  131. }
  132. public function testResourcesWithDifferentPatternsAreDifferent()
  133. {
  134. $resourceA = new DirectoryResource($this->directory, '/.xml$/');
  135. $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
  136. $this->assertCount(2, array_unique(array($resourceA, $resourceB)));
  137. }
  138. }