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
11namespace SebastianBergmann\ObjectEnumerator;
12
13use SebastianBergmann\ObjectEnumerator\Fixtures\ExceptionThrower;
14
15/**
16 * @covers SebastianBergmann\ObjectEnumerator\Enumerator
17 */
18class EnumeratorTest extends \PHPUnit_Framework_TestCase
19{
20    /**
21     * @var Enumerator
22     */
23    private $enumerator;
24
25    protected function setUp()
26    {
27        $this->enumerator = new Enumerator;
28    }
29
30    public function testEnumeratesSingleObject()
31    {
32        $a = new \stdClass;
33
34        $objects = $this->enumerator->enumerate($a);
35
36        $this->assertCount(1, $objects);
37        $this->assertSame($a, $objects[0]);
38    }
39
40    public function testEnumeratesArrayWithSingleObject()
41    {
42        $a = new \stdClass;
43
44        $objects = $this->enumerator->enumerate([$a]);
45
46        $this->assertCount(1, $objects);
47        $this->assertSame($a, $objects[0]);
48    }
49
50    public function testEnumeratesArrayWithTwoReferencesToTheSameObject()
51    {
52        $a = new \stdClass;
53
54        $objects = $this->enumerator->enumerate([$a, $a]);
55
56        $this->assertCount(1, $objects);
57        $this->assertSame($a, $objects[0]);
58    }
59
60    public function testEnumeratesArrayOfObjects()
61    {
62        $a = new \stdClass;
63        $b = new \stdClass;
64
65        $objects = $this->enumerator->enumerate([$a, $b, null]);
66
67        $this->assertCount(2, $objects);
68        $this->assertSame($a, $objects[0]);
69        $this->assertSame($b, $objects[1]);
70    }
71
72    public function testEnumeratesObjectWithAggregatedObject()
73    {
74        $a = new \stdClass;
75        $b = new \stdClass;
76
77        $a->b = $b;
78        $a->c = null;
79
80        $objects = $this->enumerator->enumerate($a);
81
82        $this->assertCount(2, $objects);
83        $this->assertSame($a, $objects[0]);
84        $this->assertSame($b, $objects[1]);
85    }
86
87    public function testEnumeratesObjectWithAggregatedObjectsInArray()
88    {
89        $a = new \stdClass;
90        $b = new \stdClass;
91
92        $a->b = [$b];
93
94        $objects = $this->enumerator->enumerate($a);
95
96        $this->assertCount(2, $objects);
97        $this->assertSame($a, $objects[0]);
98        $this->assertSame($b, $objects[1]);
99    }
100
101    public function testEnumeratesObjectsWithCyclicReferences()
102    {
103        $a = new \stdClass;
104        $b = new \stdClass;
105
106        $a->b = $b;
107        $b->a = $a;
108
109        $objects = $this->enumerator->enumerate([$a, $b]);
110
111        $this->assertCount(2, $objects);
112        $this->assertSame($a, $objects[0]);
113        $this->assertSame($b, $objects[1]);
114    }
115
116    public function testEnumeratesClassThatThrowsException()
117    {
118        $thrower = new ExceptionThrower();
119
120        $objects = $this->enumerator->enumerate($thrower);
121
122        $this->assertSame($thrower, $objects[0]);
123    }
124
125    public function testExceptionIsRaisedForInvalidArgument()
126    {
127        $this->setExpectedException(InvalidArgumentException::class);
128
129        $this->enumerator->enumerate(null);
130    }
131
132    public function testExceptionIsRaisedForInvalidArgument2()
133    {
134        $this->setExpectedException(InvalidArgumentException::class);
135
136        $this->enumerator->enumerate([], '');
137    }
138}
139