PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php

http://github.com/fabpot/symfony
PHP | 181 lines | 140 code | 33 blank | 8 comment | 5 complexity | 48f0c8bf011f5d67722785118d9d98a0 MD5 | raw file
  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(): void
  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(): void
  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. public function testResourceDoesNotExist()
  57. {
  58. $this->expectException('InvalidArgumentException');
  59. $this->expectExceptionMessageMatches('/The directory ".*" does not exist./');
  60. new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
  61. }
  62. public function testIsFresh()
  63. {
  64. $resource = new DirectoryResource($this->directory);
  65. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
  66. $this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
  67. }
  68. public function testIsFreshForDeletedResources()
  69. {
  70. $resource = new DirectoryResource($this->directory);
  71. $this->removeDirectory($this->directory);
  72. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
  73. }
  74. public function testIsFreshUpdateFile()
  75. {
  76. $resource = new DirectoryResource($this->directory);
  77. touch($this->directory.'/tmp.xml', time() + 20);
  78. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
  79. }
  80. public function testIsFreshNewFile()
  81. {
  82. $resource = new DirectoryResource($this->directory);
  83. touch($this->directory.'/new.xml', time() + 20);
  84. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
  85. }
  86. public function testIsFreshNewFileWithDifferentPattern()
  87. {
  88. $resource = new DirectoryResource($this->directory, '/.xml$/');
  89. touch($this->directory.'/new.yaml', time() + 20);
  90. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
  91. }
  92. public function testIsFreshDeleteFile()
  93. {
  94. $resource = new DirectoryResource($this->directory);
  95. $time = time();
  96. sleep(1);
  97. unlink($this->directory.'/tmp.xml');
  98. $this->assertFalse($resource->isFresh($time), '->isFresh() returns false if an existing file is removed');
  99. }
  100. public function testIsFreshDeleteDirectory()
  101. {
  102. $resource = new DirectoryResource($this->directory);
  103. $this->removeDirectory($this->directory);
  104. $this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
  105. }
  106. public function testIsFreshCreateFileInSubdirectory()
  107. {
  108. $subdirectory = $this->directory.'/subdirectory';
  109. mkdir($subdirectory);
  110. $resource = new DirectoryResource($this->directory);
  111. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if an unmodified subdirectory exists');
  112. touch($subdirectory.'/newfile.xml', time() + 20);
  113. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
  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. public function testFilterRegexListNoMatch()
  124. {
  125. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  126. touch($this->directory.'/new.bar', time() + 20);
  127. $this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
  128. }
  129. public function testFilterRegexListMatch()
  130. {
  131. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  132. touch($this->directory.'/new.xml', time() + 20);
  133. $this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
  134. }
  135. public function testSerializeUnserialize()
  136. {
  137. $resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
  138. unserialize(serialize($resource));
  139. $this->assertSame(realpath($this->directory), $resource->getResource());
  140. $this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
  141. }
  142. public function testResourcesWithDifferentPatternsAreDifferent()
  143. {
  144. $resourceA = new DirectoryResource($this->directory, '/.xml$/');
  145. $resourceB = new DirectoryResource($this->directory, '/.yaml$/');
  146. $this->assertCount(2, array_unique([$resourceA, $resourceB]));
  147. }
  148. }