1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Protocol\Test\Unit\Node;
38
39use Hoa\Protocol as LUT;
40use Hoa\Protocol\Node\Node as SUT;
41use Hoa\Test;
42
43/**
44 * Class \Hoa\Protocol\Test\Unit\Node\Node.
45 *
46 * Test suite of the node class.
47 *
48 * @copyright  Copyright © 2007-2017 Hoa community
49 * @license    New BSD License
50 */
51class Node extends Test\Unit\Suite
52{
53    public function case_implements()
54    {
55        $this
56            ->when($result = new SUT())
57            ->then
58                ->object($result)
59                    ->isInstanceOf('ArrayAccess')
60                    ->isInstanceOf('IteratorAggregate');
61    }
62
63    public function case_empty_constructor()
64    {
65        $this
66            ->when($result = new SUT())
67            ->then
68                ->variable($result->getName())
69                    ->isNull()
70                ->array(iterator_to_array($result->getIterator()))
71                    ->isEmpty();
72    }
73
74    public function case_constructor_with_a_name()
75    {
76        $this
77            ->given($name = 'foo')
78            ->when($result = new SUT($name))
79            ->then
80                ->string($result->getName())
81                    ->isEqualTo($name)
82                ->array(iterator_to_array($result->getIterator()))
83                    ->isEmpty();
84    }
85
86    public function case_constructor_with_a_name_and_children()
87    {
88        $this
89            ->given(
90                $name     = 'foo',
91                $children = [new SUT('bar'), new SUT('baz')]
92            )
93            ->when($result = new SUT($name, '', $children))
94            ->then
95                ->string($result->getName())
96                    ->isEqualTo($name)
97                ->array(iterator_to_array($result->getIterator()))
98                    ->hasSize(2);
99    }
100
101    public function case_offset_set()
102    {
103        $this
104            ->given(
105                $root             = new SUT(),
106                $name             = 'foo',
107                $node             = new SUT(),
108                $oldCountChildren = count(iterator_to_array($root->getIterator()))
109            )
110            ->when($result = $root->offsetSet($name, $node))
111            ->then
112                ->integer(count(iterator_to_array($root->getIterator())))
113                    ->isEqualTo($oldCountChildren + 1)
114                ->object($root[$name])
115                    ->isIdenticalTo($node);
116    }
117
118    public function case_offset_set_not_a_node()
119    {
120        $this
121            ->given($root = new SUT())
122            ->exception(function () use ($root) {
123                $root->offsetSet('foo', null);
124            })
125                ->isInstanceOf('Hoa\Protocol\Exception');
126    }
127
128    public function case_offset_set_no_name()
129    {
130        $this
131            ->given($root = new SUT())
132            ->exception(function () use ($root) {
133                $root->offsetSet(null, new SUT());
134            })
135                ->isInstanceOf('Hoa\Protocol\Exception');
136    }
137
138    public function case_offset_get()
139    {
140        $this
141            ->given(
142                $root        = new SUT(),
143                $child       = new SUT(),
144                $root['foo'] = $child
145            )
146            ->when($result = $root->offsetGet('foo'))
147            ->then
148            ->object($result)
149                ->isIdenticalTo($child);
150    }
151
152    public function case_offset_get_an_unknown_name()
153    {
154        $this
155            ->given($root = new SUT())
156            ->exception(function () use ($root) {
157                $root->offsetGet('foo');
158            })
159                ->isInstanceOf('Hoa\Protocol\Exception');
160    }
161
162    public function case_offset_exists()
163    {
164        $this
165            ->given(
166                $root        = new SUT(),
167                $child       = new SUT(),
168                $root['foo'] = $child
169            )
170            ->when($result = $root->offsetExists('foo'))
171            ->then
172                ->boolean($result)
173                    ->isTrue();
174    }
175
176    public function case_offset_not_exists()
177    {
178        $this
179            ->given($root = new SUT())
180            ->when($result = $root->offsetExists('foo'))
181            ->then
182                ->boolean($result)
183                    ->isFalse();
184    }
185
186    public function case_offset_unset()
187    {
188        $this
189            ->given(
190                $root        = new SUT(),
191                $child       = new SUT(),
192                $root['foo'] = $child
193            )
194            ->when($result = $root->offsetUnset('foo'))
195            ->then
196                ->boolean($root->offsetExists('foo'))
197                    ->isFalse();
198    }
199
200    public function case_reach()
201    {
202        $this
203            ->given(
204                $reach = 'bar',
205                $node  = new SUT('foo', $reach)
206            )
207            ->when($result = $node->reach())
208            ->then
209                ->string($result)
210                    ->isEqualTo($reach);
211    }
212
213    public function case_reach_with_a_queue()
214    {
215        $this
216            ->given(
217                $queue = 'baz',
218                $node  = new SUT('foo', 'bar')
219            )
220            ->when($result = $node->reach('baz'))
221            ->then
222                ->string($result)
223                    ->isEqualTo($queue);
224    }
225
226    public function case_reach_id()
227    {
228        $this
229            ->given($node = new SUT())
230            ->exception(function () use ($node) {
231                $node->reachId('foo');
232            })
233                ->isInstanceOf('Hoa\Protocol\Exception');
234    }
235
236    public function case_set_reach()
237    {
238        $this
239            ->given(
240                $reach = 'bar',
241                $node  = new SUT('foo', $reach)
242            )
243            ->when($result = $node->setReach('baz'))
244            ->then
245                ->string($result)
246                    ->isEqualTo($reach)
247                ->string($node->reach())
248                    ->isEqualTo('baz');
249    }
250
251    public function case_get_name()
252    {
253        $this
254            ->given(
255                $name = 'foo',
256                $node = new SUT($name)
257            )
258            ->when($result = $node->getName())
259            ->then
260                ->string($result)
261                    ->isEqualTo($name);
262    }
263
264    public function case_get_iterator()
265    {
266        $this
267            ->given(
268                $childA   = new SUT('bar'),
269                $childB   = new SUT('baz'),
270                $children = [$childA, $childB]
271            )
272            ->when($result = new SUT('foo', '', $children))
273            ->then
274                ->object($result->getIterator())
275                    ->isInstanceOf('ArrayIterator')
276                ->array(iterator_to_array($result->getIterator()))
277                    ->isEqualTo([
278                        'bar' => $childA,
279                        'baz' => $childB
280                    ]);
281    }
282
283    public function case_get_root()
284    {
285        $this
286            ->when($result = SUT::getRoot())
287            ->then
288                ->object($result)
289                    ->isIdenticalTo(LUT::getInstance());
290    }
291
292    public function case_to_string_as_leaf()
293    {
294        $this
295            ->given($node = new SUT('foo'))
296            ->when($result = $node->__toString())
297            ->then
298                ->string($result)
299                    ->isEqualTo('foo' . "\n");
300    }
301
302    public function case_to_string_as_node()
303    {
304        $this
305            ->given(
306                $node   = new SUT('foo'),
307                $node[] = new SUT('bar'),
308                $node[] = new SUT('baz')
309            )
310            ->when($result = $node->__toString())
311            ->then
312                ->string($result)
313                    ->isEqualTo(
314                        'foo' . "\n" .
315                        '  bar' . "\n" .
316                        '  baz' . "\n"
317                    );
318    }
319}
320