DirectoryResourceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::CHILD_FIRST);
  32. foreach ($iterator as $path) {
  33. if (preg_match('#/\.\.?$#', $path->__toString())) {
  34. continue;
  35. }
  36. if ($path->isDir()) {
  37. rmdir($path->__toString());
  38. } else {
  39. unlink($path->__toString());
  40. }
  41. }
  42. rmdir($directory);
  43. }
  44. /**
  45. * @covers Symfony\Component\Config\Resource\DirectoryResource::getResource
  46. */
  47. public function testGetResource()
  48. {
  49. $resource = new DirectoryResource($this->directory);
  50. $this->assertEquals($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
  51. }
  52. /**
  53. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  54. */
  55. public function testIsFresh()
  56. {
  57. $resource = new DirectoryResource($this->directory);
  58. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
  59. $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
  60. $resource = new DirectoryResource('/____foo/foobar'.rand(1, 999999));
  61. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
  62. }
  63. /**
  64. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  65. */
  66. public function testIsFreshUpdateFile()
  67. {
  68. $resource = new DirectoryResource($this->directory);
  69. touch($this->directory.'/tmp.xml', time() + 20);
  70. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
  71. }
  72. /**
  73. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  74. */
  75. public function testIsFreshNewFile()
  76. {
  77. $resource = new DirectoryResource($this->directory);
  78. touch($this->directory.'/new.xml', time() + 20);
  79. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
  80. }
  81. /**
  82. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  83. */
  84. public function testIsFreshDeleteFile()
  85. {
  86. $resource = new DirectoryResource($this->directory);
  87. unlink($this->directory.'/tmp.xml');
  88. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
  89. }
  90. /**
  91. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  92. */
  93. public function testIsFreshDeleteDirectory()
  94. {
  95. $resource = new DirectoryResource($this->directory);
  96. $this->removeDirectory($this->directory);
  97. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
  98. }
  99. /**
  100. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  101. */
  102. public function testIsFreshCreateFileInSubdirectory()
  103. {
  104. $subdirectory = $this->directory.'/subdirectory';
  105. mkdir($subdirectory);
  106. $resource = new DirectoryResource($this->directory);
  107. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
  108. touch($subdirectory.'/newfile.xml', time() + 20);
  109. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
  110. }
  111. /**
  112. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  113. */
  114. public function testIsFreshModifySubdirectory()
  115. {
  116. $resource = new DirectoryResource($this->directory);
  117. $subdirectory = $this->directory.'/subdirectory';
  118. mkdir($subdirectory);
  119. touch($subdirectory, time() + 20);
  120. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
  121. }
  122. /**
  123. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  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. /**
  132. * @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
  133. */
  134. public function testFilterRegexListMatch()
  135. {
  136. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  137. touch($this->directory.'/new.xml', time() + 20);
  138. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
  139. }
  140. }