EnumeratorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/object-enumerator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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 SebastianBergmann\ObjectEnumerator;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\ObjectEnumerator\Fixtures\ExceptionThrower;
  13. use stdClass;
  14. /**
  15. * @covers \SebastianBergmann\ObjectEnumerator\Enumerator
  16. */
  17. class EnumeratorTest extends TestCase
  18. {
  19. /**
  20. * @var Enumerator
  21. */
  22. private $enumerator;
  23. protected function setUp(): void
  24. {
  25. $this->enumerator = new Enumerator;
  26. }
  27. public function testEnumeratesSingleObject(): void
  28. {
  29. $a = new stdClass;
  30. $objects = $this->enumerator->enumerate($a);
  31. $this->assertCount(1, $objects);
  32. $this->assertSame($a, $objects[0]);
  33. }
  34. public function testEnumeratesArrayWithSingleObject(): void
  35. {
  36. $a = new stdClass;
  37. $objects = $this->enumerator->enumerate([$a]);
  38. $this->assertCount(1, $objects);
  39. $this->assertSame($a, $objects[0]);
  40. }
  41. public function testEnumeratesArrayWithTwoReferencesToTheSameObject(): void
  42. {
  43. $a = new stdClass;
  44. $objects = $this->enumerator->enumerate([$a, $a]);
  45. $this->assertCount(1, $objects);
  46. $this->assertSame($a, $objects[0]);
  47. }
  48. public function testEnumeratesArrayOfObjects(): void
  49. {
  50. $a = new stdClass;
  51. $b = new stdClass;
  52. $objects = $this->enumerator->enumerate([$a, $b, null]);
  53. $this->assertCount(2, $objects);
  54. $this->assertSame($a, $objects[0]);
  55. $this->assertSame($b, $objects[1]);
  56. }
  57. public function testEnumeratesObjectWithAggregatedObject(): void
  58. {
  59. $a = new stdClass;
  60. $b = new stdClass;
  61. $a->b = $b;
  62. $a->c = null;
  63. $objects = $this->enumerator->enumerate($a);
  64. $this->assertCount(2, $objects);
  65. $this->assertSame($a, $objects[0]);
  66. $this->assertSame($b, $objects[1]);
  67. }
  68. public function testEnumeratesObjectWithAggregatedObjectsInArray(): void
  69. {
  70. $a = new stdClass;
  71. $b = new stdClass;
  72. $a->b = [$b];
  73. $objects = $this->enumerator->enumerate($a);
  74. $this->assertCount(2, $objects);
  75. $this->assertSame($a, $objects[0]);
  76. $this->assertSame($b, $objects[1]);
  77. }
  78. public function testEnumeratesObjectsWithCyclicReferences(): void
  79. {
  80. $a = new stdClass;
  81. $b = new stdClass;
  82. $a->b = $b;
  83. $b->a = $a;
  84. $objects = $this->enumerator->enumerate([$a, $b]);
  85. $this->assertCount(2, $objects);
  86. $this->assertSame($a, $objects[0]);
  87. $this->assertSame($b, $objects[1]);
  88. }
  89. public function testEnumeratesClassThatThrowsException(): void
  90. {
  91. $thrower = new ExceptionThrower();
  92. $objects = $this->enumerator->enumerate($thrower);
  93. $this->assertSame($thrower, $objects[0]);
  94. }
  95. public function testExceptionIsRaisedForInvalidArgument(): void
  96. {
  97. $this->expectException(InvalidArgumentException::class);
  98. $this->enumerator->enumerate(null);
  99. }
  100. public function testExceptionIsRaisedForInvalidArgument2(): void
  101. {
  102. $this->expectException(InvalidArgumentException::class);
  103. $this->enumerator->enumerate([], '');
  104. }
  105. }