IteratorTestCase.php 3.4 KB

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