EnumeratorTest.php 3.3 KB

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