DirectoryResourceTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(realpath($this->directory), $resource->getResource(), '->getResource() returns the path to the resource');
  50. }
  51. public function testGetPattern()
  52. {
  53. $resource = new DirectoryResource($this->directory, 'bar');
  54. $this->assertEquals('bar', $resource->getPattern());
  55. }
  56. /**
  57. * @expectedException \InvalidArgumentException
  58. * @expectedExceptionMessageRegExp /The directory ".*" does not exist./
  59. */
  60. public function testResourceDoesNotExist()
  61. {
  62. $resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
  63. }
  64. public function testIsFresh()
  65. {
  66. $resource = new DirectoryResource($this->directory);
  67. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
  68. $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
  69. }
  70. public function testIsFreshForDeletedResources()
  71. {
  72. $resource = new DirectoryResource($this->directory);
  73. $this->removeDirectory($this->directory);
  74. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
  75. }
  76. public function testIsFreshUpdateFile()
  77. {
  78. $resource = new DirectoryResource($this->directory);
  79. touch($this->directory.'/tmp.xml', time() + 20);
  80. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
  81. }
  82. public function testIsFreshNewFile()
  83. {
  84. $resource = new DirectoryResource($this->directory);
  85. touch($this->directory.'/new.xml', time() + 20);
  86. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
  87. }
  88. public function testIsFreshNewFileWithDifferentPattern()
  89. {
  90. $resource = new DirectoryResource($this->directory, '/.xml$/');
  91. touch($this->directory.'/new.yaml', time() + 20);
  92. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
  93. }
  94. public function testIsFreshDeleteFile()
  95. {
  96. $resource = new DirectoryResource($this->directory);
  97. $time = time();
  98. sleep(1);
  99. unlink($this->directory.'/tmp.xml');
  100. $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
  101. }
  102. public function testIsFreshDeleteDirectory()
  103. {
  104. $resource = new DirectoryResource($this->directory);
  105. $this->removeDirectory($this->directory);
  106. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
  107. }
  108. public function testIsFreshCreateFileInSubdirectory()
  109. {
  110. $subdirectory = $this->directory.'/subdirectory';
  111. mkdir($subdirectory);
  112. $resource = new DirectoryResource($this->directory);
  113. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
  114. touch($subdirectory.'/newfile.xml', time() + 20);
  115. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
  116. }
  117. public function testIsFreshModifySubdirectory()
  118. {
  119. $resource = new DirectoryResource($this->directory);
  120. $subdirectory = $this->directory.'/subdirectory';
  121. mkdir($subdirectory);
  122. touch($subdirectory, time() + 20);
  123. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
  124. }
  125. public function testFilterRegexListNoMatch()
  126. {
  127. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  128. touch($this->directory.'/new.bar', time() + 20);
  129. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
  130. }
  131. public function testFilterRegexListMatch()
  132. {
  133. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  134. touch($this->directory.'/new.xml', time() + 20);
  135. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
  136. }
  137. public function testSerializeUnserialize()
  138. {
  139. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  140. $unserialized = unserialize(serialize($resource));
  141. $this->assertSame(realpath($this->directory), $resource->getResource());
  142. $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
  143. }
  144. public function testResourcesWithDifferentPatternsAreDifferent()
  145. {
  146. $resourceA = new DirectoryResource($this->directory, '/.xml$/');
  147. $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
  148. $this->assertCount(2, array_unique([$resourceA, $resourceB]));
  149. }
  150. }