EnumeratorTest.php 3.0 KB

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