EnumeratorTest.php 3.3 KB

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