IteratorTestCase.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Finder\Tests\Iterator;
  11. abstract class IteratorTestCase extends \PHPUnit_Framework_TestCase
  12. {
  13. protected function assertIterator($expected, \Traversable $iterator)
  14. {
  15. // set iterator_to_array $use_key to false to avoid values merge
  16. // this made FinderTest::testAppendWithAnArray() fail with GnuFinderAdapter
  17. $values = array_map(function (\SplFileInfo $fileinfo) { return str_replace('/', DIRECTORY_SEPARATOR, $fileinfo->getPathname()); }, iterator_to_array($iterator, false));
  18. $expected = array_map(function ($path) { return str_replace('/', DIRECTORY_SEPARATOR, $path); }, $expected);
  19. sort($values);
  20. sort($expected);
  21. $this->assertEquals($expected, array_values($values));
  22. }
  23. protected function assertOrderedIterator($expected, \Traversable $iterator)
  24. {
  25. $values = array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator));
  26. $this->assertEquals($expected, array_values($values));
  27. }
  28. /**
  29. * Same as assertOrderedIterator, but checks the order of groups of
  30. * array elements.
  31. *
  32. * @param array $expected - an array of arrays. For any two subarrays
  33. * $a and $b such that $a goes before $b in $expected, the method
  34. * asserts that any element of $a goes before any element of $b
  35. * in the sequence generated by $iterator
  36. * @param \Traversable $iterator
  37. */
  38. protected function assertOrderedIteratorForGroups($expected, \Traversable $iterator)
  39. {
  40. $values = array_values(array_map(function (\SplFileInfo $fileinfo) { return $fileinfo->getPathname(); }, iterator_to_array($iterator)));
  41. foreach ($expected as $subarray) {
  42. $temp = array();
  43. while (count($values) && count($temp) < count($subarray)) {
  44. $temp[] = array_shift($values);
  45. }
  46. sort($temp);
  47. sort($subarray);
  48. $this->assertEquals($subarray, $temp);
  49. }
  50. }
  51. /**
  52. * Same as IteratorTestCase::assertIterator with foreach usage.
  53. *
  54. * @param array $expected
  55. * @param \Traversable $iterator
  56. */
  57. protected function assertIteratorInForeach($expected, \Traversable $iterator)
  58. {
  59. $values = array();
  60. foreach ($iterator as $file) {
  61. $this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
  62. $values[] = $file->getPathname();
  63. }
  64. sort($values);
  65. sort($expected);
  66. $this->assertEquals($expected, array_values($values));
  67. }
  68. /**
  69. * Same as IteratorTestCase::assertOrderedIterator with foreach usage.
  70. *
  71. * @param array $expected
  72. * @param \Traversable $iterator
  73. */
  74. protected function assertOrderedIteratorInForeach($expected, \Traversable $iterator)
  75. {
  76. $values = array();
  77. foreach ($iterator as $file) {
  78. $this->assertInstanceOf('Symfony\\Component\\Finder\\SplFileInfo', $file);
  79. $values[] = $file->getPathname();
  80. }
  81. $this->assertEquals($expected, array_values($values));
  82. }
  83. }