GlobResourceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\GlobResource;
  13. class GlobResourceTest extends TestCase
  14. {
  15. protected function tearDown()
  16. {
  17. $dir = \dirname(__DIR__).'/Fixtures';
  18. @rmdir($dir.'/TmpGlob');
  19. @unlink($dir.'/TmpGlob');
  20. @unlink($dir.'/Resource/TmpGlob');
  21. touch($dir.'/Resource/.hiddenFile');
  22. }
  23. public function testIterator()
  24. {
  25. $dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
  26. $resource = new GlobResource($dir, '/Resource', true);
  27. $paths = iterator_to_array($resource);
  28. $file = $dir.'/Resource'.\DIRECTORY_SEPARATOR.'ConditionalClass.php';
  29. $this->assertEquals([$file => new \SplFileInfo($file)], $paths);
  30. $this->assertInstanceOf('SplFileInfo', current($paths));
  31. $this->assertSame($dir, $resource->getPrefix());
  32. $resource = new GlobResource($dir, '/**/Resource', true);
  33. $paths = iterator_to_array($resource);
  34. $file = $dir.\DIRECTORY_SEPARATOR.'Resource'.\DIRECTORY_SEPARATOR.'ConditionalClass.php';
  35. $this->assertEquals([$file => $file], $paths);
  36. $this->assertInstanceOf('SplFileInfo', current($paths));
  37. $this->assertSame($dir, $resource->getPrefix());
  38. }
  39. public function testIteratorForExclusionDoesntIterateThroughSubfolders()
  40. {
  41. $dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
  42. $resource = new GlobResource($dir, \DIRECTORY_SEPARATOR.'Exclude', true, true);
  43. $paths = iterator_to_array($resource);
  44. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude';
  45. $this->assertArrayHasKey($file, $paths);
  46. $this->assertCount(1, $paths);
  47. }
  48. public function testIteratorSkipsFoldersForGivenExcludedPrefixes()
  49. {
  50. $dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
  51. $resource = new GlobResource($dir, '/*Exclude*', true, false, [$dir.\DIRECTORY_SEPARATOR.'Exclude' => true]);
  52. $paths = iterator_to_array($resource);
  53. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude'.\DIRECTORY_SEPARATOR.'AnExcludedFile.txt';
  54. $this->assertArrayNotHasKey($file, $paths);
  55. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude'.\DIRECTORY_SEPARATOR.'ExcludeToo'.\DIRECTORY_SEPARATOR.'AnotheExcludedFile.txt';
  56. $this->assertArrayNotHasKey($file, $paths);
  57. }
  58. public function testIteratorSkipsSubfoldersForGivenExcludedPrefixes()
  59. {
  60. $dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
  61. $resource = new GlobResource($dir, '/*Exclude/*', true, false, [$dir.\DIRECTORY_SEPARATOR.'Exclude' => true]);
  62. $paths = iterator_to_array($resource);
  63. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude'.\DIRECTORY_SEPARATOR.'AnExcludedFile.txt';
  64. $this->assertArrayNotHasKey($file, $paths);
  65. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude'.\DIRECTORY_SEPARATOR.'ExcludeToo'.\DIRECTORY_SEPARATOR.'AnotheExcludedFile.txt';
  66. $this->assertArrayNotHasKey($file, $paths);
  67. }
  68. public function testIteratorSkipsFoldersWithForwardSlashForGivenExcludedPrefixes()
  69. {
  70. $dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
  71. $resource = new GlobResource($dir, '/*Exclude*', true, false, [$dir.'/Exclude' => true]);
  72. $paths = iterator_to_array($resource);
  73. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude/AnExcludedFile.txt';
  74. $this->assertArrayNotHasKey($file, $paths);
  75. $file = $dir.\DIRECTORY_SEPARATOR.'Exclude'.\DIRECTORY_SEPARATOR.'ExcludeToo'.\DIRECTORY_SEPARATOR.'AnotheExcludedFile.txt';
  76. $this->assertArrayNotHasKey($file, $paths);
  77. }
  78. public function testIsFreshNonRecursiveDetectsNewFile()
  79. {
  80. $dir = \dirname(__DIR__).'/Fixtures';
  81. $resource = new GlobResource($dir, '/*', false);
  82. $this->assertTrue($resource->isFresh(0));
  83. mkdir($dir.'/TmpGlob');
  84. $this->assertTrue($resource->isFresh(0));
  85. rmdir($dir.'/TmpGlob');
  86. $this->assertTrue($resource->isFresh(0));
  87. touch($dir.'/TmpGlob');
  88. $this->assertFalse($resource->isFresh(0));
  89. unlink($dir.'/TmpGlob');
  90. $this->assertTrue($resource->isFresh(0));
  91. }
  92. public function testIsFreshNonRecursiveDetectsRemovedFile()
  93. {
  94. $dir = \dirname(__DIR__).'/Fixtures';
  95. $resource = new GlobResource($dir, '/*', false);
  96. touch($dir.'/TmpGlob');
  97. touch($dir.'/.TmpGlob');
  98. $this->assertTrue($resource->isFresh(0));
  99. unlink($dir.'/.TmpGlob');
  100. $this->assertTrue($resource->isFresh(0));
  101. unlink($dir.'/TmpGlob');
  102. $this->assertFalse($resource->isFresh(0));
  103. }
  104. public function testIsFreshRecursiveDetectsRemovedFile()
  105. {
  106. $dir = \dirname(__DIR__).'/Fixtures';
  107. $resource = new GlobResource($dir, '/*', true);
  108. touch($dir.'/Resource/TmpGlob');
  109. $this->assertTrue($resource->isFresh(0));
  110. unlink($dir.'/Resource/TmpGlob');
  111. $this->assertFalse($resource->isFresh(0));
  112. touch($dir.'/Resource/TmpGlob');
  113. $this->assertTrue($resource->isFresh(0));
  114. unlink($dir.'/Resource/.hiddenFile');
  115. $this->assertTrue($resource->isFresh(0));
  116. }
  117. public function testIsFreshRecursiveDetectsNewFile()
  118. {
  119. $dir = \dirname(__DIR__).'/Fixtures';
  120. $resource = new GlobResource($dir, '/*', true);
  121. $this->assertTrue($resource->isFresh(0));
  122. touch($dir.'/Resource/TmpGlob');
  123. $this->assertFalse($resource->isFresh(0));
  124. }
  125. }