You are here

class DirectoryIterationTestCase in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/DirectoryIterationTestCase.php \org\bovigo\vfs\DirectoryIterationTestCase

Test for directory iteration.

@group dir @group iteration

Hierarchy

Expanded class hierarchy of DirectoryIterationTestCase

File

vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/DirectoryIterationTestCase.php, line 18

Namespace

org\bovigo\vfs
View source
class DirectoryIterationTestCase extends vfsStreamWrapperBaseTestCase {

  /**
   * clean up test environment
   */
  public function tearDown() {
    vfsStream::enableDotfiles();
  }

  /**
   * @return  array
   */
  public function provideSwitchWithExpectations() {
    return array(
      array(
        function () {
          vfsStream::disableDotfiles();
        },
        array(
          'bar',
          'baz2',
        ),
      ),
      array(
        function () {
          vfsStream::enableDotfiles();
        },
        array(
          '.',
          '..',
          'bar',
          'baz2',
        ),
      ),
    );
  }

  /**
   * assertion for directoy count
   *
   * @param  int  $expectedCount
   * @param  int  $actualCount
   */
  private function assertDirectoryCount($expectedCount, $actualCount) {
    $this
      ->assertEquals($expectedCount, $actualCount, 'Directory foo contains ' . $expectedCount . ' children, but got ' . $actualCount . ' children while iterating over directory contents');
  }

  /**
   * @param  \Closure  $dotFilesSwitch
   * @param  string[]  $expectedDirectories
   * @test
   * @dataProvider  provideSwitchWithExpectations
   */
  public function directoryIteration(\Closure $dotFilesSwitch, array $expectedDirectories) {
    $dotFilesSwitch();
    $dir = dir($this->fooURL);
    $i = 0;
    while (false !== ($entry = $dir
      ->read())) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    $dir
      ->rewind();
    $i = 0;
    while (false !== ($entry = $dir
      ->read())) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    $dir
      ->close();
  }

  /**
   * @param  \Closure  $dotFilesSwitch
   * @param  string[]  $expectedDirectories
   * @test
   * @dataProvider  provideSwitchWithExpectations
   */
  public function directoryIterationWithDot(\Closure $dotFilesSwitch, array $expectedDirectories) {
    $dotFilesSwitch();
    $dir = dir($this->fooURL . '/.');
    $i = 0;
    while (false !== ($entry = $dir
      ->read())) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    $dir
      ->rewind();
    $i = 0;
    while (false !== ($entry = $dir
      ->read())) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    $dir
      ->close();
  }

  /**
   * assure that a directory iteration works as expected
   *
   * @param  \Closure  $dotFilesSwitch
   * @param  string[]  $expectedDirectories
   * @test
   * @dataProvider  provideSwitchWithExpectations
   * @group  regression
   * @group  bug_2
   */
  public function directoryIterationWithOpenDir_Bug_2(\Closure $dotFilesSwitch, array $expectedDirectories) {
    $dotFilesSwitch();
    $handle = opendir($this->fooURL);
    $i = 0;
    while (false !== ($entry = readdir($handle))) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    rewinddir($handle);
    $i = 0;
    while (false !== ($entry = readdir($handle))) {
      $i++;
      $this
        ->assertTrue(in_array($entry, $expectedDirectories));
    }
    $this
      ->assertDirectoryCount(count($expectedDirectories), $i);
    closedir($handle);
  }

  /**
   * assure that a directory iteration works as expected
   *
   * @author  Christoph Bloemer
   * @param  \Closure  $dotFilesSwitch
   * @param  string[]  $expectedDirectories
   * @test
   * @dataProvider  provideSwitchWithExpectations
   * @group  regression
   * @group  bug_4
   */
  public function directoryIteration_Bug_4(\Closure $dotFilesSwitch, array $expectedDirectories) {
    $dotFilesSwitch();
    $dir = $this->fooURL;
    $list1 = array();
    if ($handle = opendir($dir)) {
      while (false !== ($listItem = readdir($handle))) {
        if ('.' != $listItem && '..' != $listItem) {
          if (is_file($dir . '/' . $listItem) === true) {
            $list1[] = 'File:[' . $listItem . ']';
          }
          elseif (is_dir($dir . '/' . $listItem) === true) {
            $list1[] = 'Folder:[' . $listItem . ']';
          }
        }
      }
      closedir($handle);
    }
    $list2 = array();
    if ($handle = opendir($dir)) {
      while (false !== ($listItem = readdir($handle))) {
        if ('.' != $listItem && '..' != $listItem) {
          if (is_file($dir . '/' . $listItem) === true) {
            $list2[] = 'File:[' . $listItem . ']';
          }
          elseif (is_dir($dir . '/' . $listItem) === true) {
            $list2[] = 'Folder:[' . $listItem . ']';
          }
        }
      }
      closedir($handle);
    }
    $this
      ->assertEquals($list1, $list2);
    $this
      ->assertEquals(2, count($list1));
    $this
      ->assertEquals(2, count($list2));
  }

  /**
   * assure that a directory iteration works as expected
   *
   * @param  \Closure  $dotFilesSwitch
   * @param  string[]  $expectedDirectories
   * @test
   * @dataProvider  provideSwitchWithExpectations
   */
  public function directoryIterationShouldBeIndependent(\Closure $dotFilesSwitch, array $expectedDirectories) {
    $dotFilesSwitch();
    $list1 = array();
    $list2 = array();
    $handle1 = opendir($this->fooURL);
    if (false !== ($listItem = readdir($handle1))) {
      $list1[] = $listItem;
    }
    $handle2 = opendir($this->fooURL);
    if (false !== ($listItem = readdir($handle2))) {
      $list2[] = $listItem;
    }
    if (false !== ($listItem = readdir($handle1))) {
      $list1[] = $listItem;
    }
    if (false !== ($listItem = readdir($handle2))) {
      $list2[] = $listItem;
    }
    closedir($handle1);
    closedir($handle2);
    $this
      ->assertEquals($list1, $list2);
    $this
      ->assertEquals(2, count($list1));
    $this
      ->assertEquals(2, count($list2));
  }

  /**
   * @test
   * @group  issue_50
   */
  public function recursiveDirectoryIterationWithDotsEnabled() {
    vfsStream::enableDotfiles();
    vfsStream::setup();
    $structure = array(
      'Core' => array(
        'AbstractFactory' => array(
          'test.php' => 'some text content',
          'other.php' => 'Some more text content',
          'Invalid.csv' => 'Something else',
        ),
        'AnEmptyFolder' => array(),
        'badlocation.php' => 'some bad content',
      ),
    );
    $root = vfsStream::create($structure);
    $rootPath = vfsStream::url($root
      ->getName());
    $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootPath), \RecursiveIteratorIterator::CHILD_FIRST);
    $pathes = array();
    foreach ($iterator as $fullFileName => $fileSPLObject) {
      $pathes[] = $fullFileName;
    }
    $this
      ->assertEquals(array(
      'vfs://root' . DIRECTORY_SEPARATOR . '.',
      'vfs://root' . DIRECTORY_SEPARATOR . '..',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . '.',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . '..',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . '.',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . '..',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'test.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'other.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'Invalid.csv',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AnEmptyFolder' . DIRECTORY_SEPARATOR . '.',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AnEmptyFolder' . DIRECTORY_SEPARATOR . '..',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AnEmptyFolder',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'badlocation.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core',
    ), $pathes);
  }

  /**
   * @test
   * @group  issue_50
   */
  public function recursiveDirectoryIterationWithDotsDisabled() {
    vfsStream::disableDotfiles();
    vfsStream::setup();
    $structure = array(
      'Core' => array(
        'AbstractFactory' => array(
          'test.php' => 'some text content',
          'other.php' => 'Some more text content',
          'Invalid.csv' => 'Something else',
        ),
        'AnEmptyFolder' => array(),
        'badlocation.php' => 'some bad content',
      ),
    );
    $root = vfsStream::create($structure);
    $rootPath = vfsStream::url($root
      ->getName());
    $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootPath), \RecursiveIteratorIterator::CHILD_FIRST);
    $pathes = array();
    foreach ($iterator as $fullFileName => $fileSPLObject) {
      $pathes[] = $fullFileName;
    }
    $this
      ->assertEquals(array(
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'test.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'other.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory' . DIRECTORY_SEPARATOR . 'Invalid.csv',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AbstractFactory',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'AnEmptyFolder',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core' . DIRECTORY_SEPARATOR . 'badlocation.php',
      'vfs://root' . DIRECTORY_SEPARATOR . 'Core',
    ), $pathes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DirectoryIterationTestCase::assertDirectoryCount private function assertion for directoy count
DirectoryIterationTestCase::directoryIteration public function @test @dataProvider provideSwitchWithExpectations
DirectoryIterationTestCase::directoryIterationShouldBeIndependent public function assure that a directory iteration works as expected
DirectoryIterationTestCase::directoryIterationWithDot public function @test @dataProvider provideSwitchWithExpectations
DirectoryIterationTestCase::directoryIterationWithOpenDir_Bug_2 public function assure that a directory iteration works as expected
DirectoryIterationTestCase::directoryIteration_Bug_4 public function assure that a directory iteration works as expected
DirectoryIterationTestCase::provideSwitchWithExpectations public function
DirectoryIterationTestCase::recursiveDirectoryIterationWithDotsDisabled public function @test @group issue_50
DirectoryIterationTestCase::recursiveDirectoryIterationWithDotsEnabled public function @test @group issue_50
DirectoryIterationTestCase::tearDown public function clean up test environment
vfsStreamWrapperBaseTestCase::$bar protected property sub directory
vfsStreamWrapperBaseTestCase::$barURL protected property URL of sub directory
vfsStreamWrapperBaseTestCase::$baz1 protected property a file
vfsStreamWrapperBaseTestCase::$baz1URL protected property URL of file 1
vfsStreamWrapperBaseTestCase::$baz2 protected property another file
vfsStreamWrapperBaseTestCase::$baz2URL protected property URL of file 2
vfsStreamWrapperBaseTestCase::$foo protected property root directory
vfsStreamWrapperBaseTestCase::$fooURL protected property URL of root directory
vfsStreamWrapperBaseTestCase::setUp public function set up test environment