RegexDirectoryIteratorTest.php 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php

/**
 * @file
 * Contains \Drupal\Tests\Component\FileSystem\RegexDirectoryIteratorTest.
 */

namespace Drupal\Tests\Component\FileSystem;

use Drupal\Component\FileSystem\RegexDirectoryIterator;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;

/**
 * @coversDefaultClass \Drupal\Component\FileSystem\RegexDirectoryIterator
 * @group FileSystem
 */
class RegexDirectoryIteratorTest extends UnitTestCase {

  /**
   * @covers ::accept
   * @dataProvider providerTestRegexDirectoryIterator
   */
  public function testRegexDirectoryIterator(array $directory, $regex, array $expected) {
    vfsStream::setup('root', NULL, $directory);
    $iterator = new RegexDirectoryIterator(vfsStream::url('root'), $regex);

    // Create an array of filenames to assert against.
    $file_list = array_map(function(\SplFileInfo $file) {
      return $file->getFilename();
    }, array_values(iterator_to_array($iterator)));

    $this->assertSame($expected, $file_list);
  }

  /**
   * Provider for self::testRegexDirectoryIterator().
   */
  public function providerTestRegexDirectoryIterator() {
    return [
      [
        [
          '1.yml' => '',
        ],
        '/\.yml$/',
        [
          '1.yml',
        ],
      ],
      [
        [
          '1.yml' => '',
          '2.yml' => '',
          '3.txt' => '',
        ],
        '/\.yml$/',
        [
          '1.yml',
          '2.yml',
        ],
      ],
      [
        [
          '1.yml' => '',
          '2.yml' => '',
          '3.txt' => '',
        ],
        '/\.txt/',
        [
          '3.txt',
        ],
      ],
      [
        [
          '1.yml' => '',
          // Ensure we don't recurse in directories even if that match the
          // regex.
          '2.yml' => [
            '3.yml' => '',
            '4.yml' => '',
          ],
          '3.txt' => '',
        ],
        '/\.yml$/',
        [
          '1.yml',
        ],
      ],
      [
        [
          '1.yml' => '',
          '2.yml' => '',
          '3.txt' => '',
        ],
        '/^\d/',
        [
          '1.yml',
          '2.yml',
          '3.txt'
        ],
      ],
      [
        [
          '1.yml' => '',
          '2.yml' => '',
          '3.txt' => '',
        ],
        '/^\D/',
        [],
      ],
    ];
  }

}