RegexDirectoryIteratorTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Drupal\Tests\Component\FileSystem;
  3. use Drupal\Component\FileSystem\RegexDirectoryIterator;
  4. use org\bovigo\vfs\vfsStream;
  5. use PHPUnit\Framework\TestCase;
  6. /**
  7. * @coversDefaultClass \Drupal\Component\FileSystem\RegexDirectoryIterator
  8. * @group FileSystem
  9. */
  10. class RegexDirectoryIteratorTest extends TestCase {
  11. /**
  12. * @covers ::accept
  13. * @dataProvider providerTestRegexDirectoryIterator
  14. */
  15. public function testRegexDirectoryIterator(array $directory, $regex, array $expected) {
  16. vfsStream::setup('root', NULL, $directory);
  17. $iterator = new RegexDirectoryIterator(vfsStream::url('root'), $regex);
  18. // Create an array of filenames to assert against.
  19. $file_list = array_map(function (\SplFileInfo $file) {
  20. return $file->getFilename();
  21. }, array_values(iterator_to_array($iterator)));
  22. $this->assertSame($expected, $file_list);
  23. }
  24. /**
  25. * Provider for self::testRegexDirectoryIterator().
  26. */
  27. public function providerTestRegexDirectoryIterator() {
  28. return [
  29. [
  30. [
  31. '1.yml' => '',
  32. ],
  33. '/\.yml$/',
  34. [
  35. '1.yml',
  36. ],
  37. ],
  38. [
  39. [
  40. '1.yml' => '',
  41. '2.yml' => '',
  42. '3.txt' => '',
  43. ],
  44. '/\.yml$/',
  45. [
  46. '1.yml',
  47. '2.yml',
  48. ],
  49. ],
  50. [
  51. [
  52. '1.yml' => '',
  53. '2.yml' => '',
  54. '3.txt' => '',
  55. ],
  56. '/\.txt/',
  57. [
  58. '3.txt',
  59. ],
  60. ],
  61. [
  62. [
  63. '1.yml' => '',
  64. // Ensure we don't recurse in directories even if that match the
  65. // regex.
  66. '2.yml' => [
  67. '3.yml' => '',
  68. '4.yml' => '',
  69. ],
  70. '3.txt' => '',
  71. ],
  72. '/\.yml$/',
  73. [
  74. '1.yml',
  75. ],
  76. ],
  77. [
  78. [
  79. '1.yml' => '',
  80. '2.yml' => '',
  81. '3.txt' => '',
  82. ],
  83. '/^\d/',
  84. [
  85. '1.yml',
  86. '2.yml',
  87. '3.txt',
  88. ],
  89. ],
  90. [
  91. [
  92. '1.yml' => '',
  93. '2.yml' => '',
  94. '3.txt' => '',
  95. ],
  96. '/^\D/',
  97. [],
  98. ],
  99. ];
  100. }
  101. }