DirectoryResourceTest.php 5.8KB

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