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\Exception\Test\Unit;
38
39use Hoa\Exception\Group as SUT;
40use Hoa\Test;
41
42/**
43 * Class \Hoa\Exception\Test\Unit\Group.
44 *
45 * Test suite of the group class.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50class Group extends Test\Unit\Suite
51{
52    public function case_is_an_exception_arrayaccess_iteratoraggregate_countable()
53    {
54        $this
55            ->when($result = new SUT('foo'))
56            ->then
57                ->object($result)
58                    ->isInstanceOf('Hoa\Exception\Exception')
59                    ->isInstanceOf('ArrayAccess')
60                    ->isInstanceOf('IteratorAggregate')
61                    ->isInstanceOf('Countable');
62    }
63
64    public function case_constructor()
65    {
66        $this
67            ->given(
68                $message   = 'foo %s %d %s',
69                $code      = 7,
70                $arguments = ['arg', 42, null],
71                $previous  = new SUT('previous')
72            )
73            ->when($result = new SUT($message, $code, $arguments, $previous), $line = __LINE__)
74            ->then
75                ->string($result->getMessage())
76                    ->isEqualTo('foo arg 42 (null)')
77                ->integer($result->getCode())
78                    ->isEqualTo(7)
79                ->array($result->getArguments())
80                    ->isEqualTo(['arg', 42, '(null)'])
81                ->object($result->getPreviousThrow())
82                    ->isIdenticalTo($previous)
83                ->boolean($result->hasUncommittedExceptions())
84                    ->isFalse();
85    }
86
87    public function case_raise_zero_exception()
88    {
89        $this
90            ->given($group = new SUT('foo'), $line = __LINE__)
91            ->when($result = $group->raise())
92            ->then
93                ->string($result)
94                    ->isEqualTo(
95                        __METHOD__ . '(): (0) foo' . "\n" .
96                        'in ' . __FILE__ . ' at line ' . $line . '.'
97                    );
98    }
99
100    public function case_raise_one_exception()
101    {
102        $this
103            ->given(
104                $exception1 = new SUT('bar'), $barLine = __LINE__,
105                $group      = new SUT('foo'), $fooLine = __LINE__,
106                $group[]    = $exception1
107            )
108            ->when($result = $group->raise())
109            ->then
110                ->string($result)
111                    ->isEqualTo(
112                        __METHOD__ . '(): (0) foo' . "\n" .
113                        'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
114                        'Contains the following exceptions:' . "\n\n" .
115                        '  • ' . __METHOD__ . '(): (0) bar' . "\n" .
116                        '    in ' . __FILE__ . ' at line ' . $barLine . '.'
117                    );
118    }
119
120    public function case_raise_more_exceptions()
121    {
122        $this
123            ->given(
124                $exception1 = new SUT('bar'), $barLine = __LINE__,
125                $exception2 = new SUT('baz'), $bazLine = __LINE__,
126                $group      = new SUT('foo'), $fooLine = __LINE__,
127                $group[]    = $exception1,
128                $group[]    = $exception2
129            )
130            ->when($result = $group->raise())
131            ->then
132                ->string($result)
133                    ->isEqualTo(
134                        __METHOD__ . '(): (0) foo' . "\n" .
135                        'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
136                        'Contains the following exceptions:' . "\n\n" .
137                        '  • ' . __METHOD__ . '(): (0) bar' . "\n" .
138                        '    in ' . __FILE__ . ' at line ' . $barLine . '.' . "\n\n" .
139                        '  • ' . __METHOD__ . '(): (0) baz' . "\n" .
140                        '    in ' . __FILE__ . ' at line ' . $bazLine . '.'
141                    );
142    }
143
144    public function case_begin_transaction()
145    {
146        $this
147            ->given(
148                $group = new SUT('foo'),
149                $oldStackSize = $group->getStackSize()
150            )
151            ->when(
152                $result    = $group->beginTransaction(),
153                $stackSize = $group->getStackSize()
154            )
155            ->then
156                ->integer($oldStackSize)
157                    ->isEqualTo(1)
158                ->object($result)
159                    ->isIdenticalTo($group)
160                ->integer($stackSize)
161                    ->isEqualTo($oldStackSize + 1);
162    }
163
164    public function case_rollback_transaction_with_an_empty_stack()
165    {
166        $this
167            ->given(
168                $group = new SUT('foo'),
169                $oldStackSize = $group->getStackSize()
170            )
171            ->when(
172                $result    = $group->rollbackTransaction(),
173                $stackSize = $group->getStackSize()
174            )
175            ->then
176                ->integer($oldStackSize)
177                    ->isEqualTo(1)
178                ->object($result)
179                    ->isIdenticalTo($group)
180                ->integer($stackSize)
181                    ->isEqualTo($oldStackSize);
182    }
183
184    public function case_rollback_transaction()
185    {
186        $this
187            ->given(
188                $group = new SUT('foo'),
189                $group->beginTransaction(),
190                $group->beginTransaction(),
191                $oldStackSize = $group->getStackSize(),
192                $group->rollbackTransaction()
193            )
194            ->when(
195                $result    = $group->rollbackTransaction(),
196                $stackSize = $group->getStackSize()
197            )
198            ->then
199                ->integer($oldStackSize)
200                    ->isEqualTo(3)
201                ->object($result)
202                    ->isIdenticalTo($group)
203                ->integer($stackSize)
204                    ->isEqualTo($oldStackSize - 2);
205    }
206
207    public function case_commit_transaction_with_an_empty_stack()
208    {
209        $this
210            ->given(
211                $group = new SUT('foo'),
212                $group->beginTransaction(),
213                $oldCount     = count($group),
214                $oldStackSize = $group->getStackSize()
215            )
216            ->when(
217                $result    = $group->commitTransaction(),
218                $count     = count($group),
219                $stackSize = $group->getStackSize()
220            )
221            ->then
222                ->integer($oldCount)
223                    ->isEqualTo(0)
224                ->integer($oldStackSize)
225                    ->isEqualTo(2)
226                ->object($result)
227                    ->isIdenticalTo($group)
228                ->integer($count)
229                    ->isEqualTo($oldCount)
230                ->integer($stackSize)
231                    ->isEqualTo($oldStackSize - 1);
232    }
233
234    public function case_commit_transaction()
235    {
236        $this
237            ->given(
238                $group = new SUT('foo'),
239                $group->beginTransaction(),
240                $exception1   = new SUT('bar'),
241                $exception2   = new SUT('baz'),
242                $group[]      = $exception1,
243                $group[]      = $exception2,
244                $oldCount     = count($group),
245                $oldStackSize = $group->getStackSize()
246            )
247            ->when(
248                $result    = $group->commitTransaction(),
249                $count     = count($group),
250                $stackSize = $group->getStackSize()
251            )
252            ->then
253                ->integer($oldCount)
254                    ->isEqualTo(0)
255                ->integer($oldStackSize)
256                    ->isEqualTo(2)
257                ->object($result)
258                    ->isIdenticalTo($group)
259                ->integer($count)
260                    ->isEqualTo($oldCount + 2)
261                ->integer($stackSize)
262                    ->isEqualTo($oldStackSize - 1)
263                ->array(iterator_to_array($group->getIterator()))
264                    ->isEqualTo([
265                        0 => $exception1,
266                        1 => $exception2
267                    ]);
268    }
269
270    public function case_has_uncommitted_exceptions()
271    {
272        $this
273            ->given(
274                $group = new SUT('foo'),
275                $group->beginTransaction(),
276                $group[] = new SUT('bar')
277            )
278            ->when($result = $group->hasUncommittedExceptions())
279            ->then
280                ->boolean($result)
281                    ->isTrue();
282    }
283
284    public function case_has_no_uncommitted_exceptions()
285    {
286        $this
287            ->given(
288                $group = new SUT('foo'),
289                $group->beginTransaction()
290            )
291            ->when($result = $group->hasUncommittedExceptions())
292            ->then
293                ->boolean($result)
294                    ->isFalse();
295    }
296
297    public function case_has_no_uncommitted_exceptions_with_empty_stack()
298    {
299        $this
300            ->given(
301                $group   = new SUT('foo'),
302                $group[] = new SUT('bar')
303            )
304            ->when($result = $group->hasUncommittedExceptions())
305            ->then
306                ->boolean($result)
307                    ->isFalse();
308    }
309
310    public function case_offset_exists_with_no_uncommited_exceptions()
311    {
312        $this
313            ->given(
314                $group        = new SUT('foo'),
315                $group['bar'] = new SUT('bar')
316            )
317            ->when($result = $group->offsetExists('bar'))
318            ->then
319                ->boolean($result)
320                    ->isTrue();
321    }
322
323    public function case_offset_does_not_exist_with_no_uncommited_exceptions()
324    {
325        $this
326            ->given(
327                $group        = new SUT('foo'),
328                $group['bar'] = new SUT('bar')
329            )
330            ->when($result = $group->offsetExists('baz'))
331            ->then
332                ->boolean($result)
333                    ->isFalse();
334    }
335
336    public function case_offset_exists()
337    {
338        $this
339            ->given(
340                $group = new SUT('foo'),
341                $group->beginTransaction(),
342                $group->beginTransaction(),
343                $group['bar'] = new SUT('bar')
344            )
345            ->when($result = $group->offsetExists('bar'))
346            ->then
347                ->boolean($result)
348                    ->isTrue();
349    }
350
351    public function case_offset_does_not_exist()
352    {
353        $this
354            ->given(
355                $group = new SUT('foo'),
356                $group->beginTransaction(),
357                $group->beginTransaction(),
358                $group['bar'] = new SUT('bar')
359            )
360            ->when($result = $group->offsetExists('baz'))
361            ->then
362                ->boolean($result)
363                    ->isFalse();
364    }
365
366    public function case_offset_get_with_no_uncommited_exceptions()
367    {
368        $this
369            ->given(
370                $group        = new SUT('foo'),
371                $exception1   = new SUT('bar'),
372                $group['bar'] = $exception1
373            )
374            ->when($result = $group->offsetGet('bar'))
375            ->then
376                ->object($result)
377                    ->isIdenticalTo($exception1);
378    }
379
380    public function case_offset_get_does_not_exist_with_no_uncommited_exceptions()
381    {
382        $this
383            ->given(
384                $group        = new SUT('foo'),
385                $exception1   = new SUT('bar'),
386                $group['bar'] = $exception1
387            )
388            ->when($result = $group->offsetGet('baz'))
389            ->then
390                ->variable($result)
391                    ->isNull();
392    }
393
394    public function case_offset_get()
395    {
396        $this
397            ->given(
398                $group = new SUT('foo'),
399                $group->beginTransaction(),
400                $group->beginTransaction(),
401                $exception1   = new SUT('bar'),
402                $group['bar'] = $exception1
403            )
404            ->when($result = $group->offsetGet('bar'))
405            ->then
406                ->object($result)
407                    ->isIdenticalTo($exception1);
408    }
409
410    public function case_offset_get_does_not_exist()
411    {
412        $this
413            ->given(
414                $group = new SUT('foo'),
415                $group->beginTransaction(),
416                $group->beginTransaction(),
417                $exception1   = new SUT('bar'),
418                $group['bar'] = $exception1
419            )
420            ->when($result = $group->offsetGet('baz'))
421            ->then
422                ->variable($result)
423                    ->isNull();
424    }
425
426    public function case_offset_set_not_an_exception()
427    {
428        $this
429            ->given($group = new SUT('foo'))
430            ->when($group->offsetSet('bar', new \StdClass()))
431            ->then
432                ->boolean($group->offsetExists('bar'))
433                    ->isFalse();
434    }
435
436    public function case_offset_set()
437    {
438        $this
439            ->given(
440                $group      = new SUT('foo'),
441                $exception1 = new SUT('bar')
442            )
443            ->when($result = $group->offsetExists('bar'))
444            ->then
445                ->boolean($result)
446                    ->isFalse()
447
448            ->when($group->offsetSet('bar', $exception1))
449            ->then
450                ->boolean($group->offsetExists('bar'))
451                    ->isTrue()
452                ->object($group->offsetGet('bar'))
453                    ->isIdenticalTo($exception1);
454    }
455
456    public function case_offset_set_with_a_null_index()
457    {
458        $this
459            ->given(
460                $group      = new SUT('foo'),
461                $exception1 = new SUT('bar')
462            )
463            ->when($group->offsetSet(null, $exception1))
464            ->then
465                ->boolean($group->offsetExists(0))
466                    ->isTrue()
467                ->object($group->offsetGet(0))
468                    ->isIdenticalTo($exception1);
469    }
470
471    public function case_offset_set_with_an_integer_index()
472    {
473        $this
474            ->given(
475                $group      = new SUT('foo'),
476                $exception1 = new SUT('bar')
477            )
478            ->when($group->offsetSet(42, $exception1))
479            ->then
480                ->boolean($group->offsetExists(42))
481                    ->isFalse()
482                ->boolean($group->offsetExists(0))
483                    ->isTrue()
484                ->object($group->offsetGet(0))
485                    ->isIdenticalTo($exception1);
486    }
487
488    public function case_offset_unset_with_no_uncommited_exceptions()
489    {
490        $this
491            ->given(
492                $group        = new SUT('foo'),
493                $group['bar'] = new SUT('bar')
494            )
495            ->when($group->offsetUnset('bar'))
496            ->then
497                ->boolean($group->offsetExists('bar'))
498                    ->isFalse();
499    }
500
501    public function case_offset_unset_does_not_exist_with_no_uncommited_exceptions()
502    {
503        $this
504            ->given($group = new SUT('foo'))
505            ->when($group->offsetUnset('bar'))
506            ->then
507                ->boolean($group->offsetExists('bar'))
508                    ->isFalse();
509    }
510
511    public function case_offset_unset()
512    {
513        $this
514            ->given(
515                $group = new SUT('foo'),
516                $group->beginTransaction(),
517                $group->beginTransaction(),
518                $group['bar'] = new SUT('bar')
519            )
520            ->when($result = $group->offsetUnset('bar'))
521            ->then
522                ->boolean($group->offsetExists('bar'))
523                    ->isFalse();
524    }
525
526    public function case_offset_unset_does_not_exist()
527    {
528        $this
529            ->given(
530                $group = new SUT('foo'),
531                $group->beginTransaction(),
532                $group->beginTransaction()
533            )
534            ->when($result = $group->offsetUnset('bar'))
535            ->then
536                ->boolean($group->offsetExists('bar'))
537                    ->isFalse();
538    }
539
540    public function case_get_exceptions()
541    {
542        $this
543            ->given(
544                $group        = new SUT('foo'),
545                $exception1   = new SUT('bar'),
546                $exception2   = new SUT('baz'),
547                $group['bar'] = $exception1,
548                $group->beginTransaction(),
549                $group['baz'] = $exception2
550            )
551            ->when($result = $group->getExceptions())
552            ->then
553                ->object($result)
554                    ->isInstanceOf('ArrayObject')
555                ->object($result['bar'])
556                    ->isIdenticalTo($exception1);
557    }
558
559    public function case_get_iterator()
560    {
561        $this
562            ->given(
563                $group        = new SUT('foo'),
564                $exception1   = new SUT('bar'),
565                $group['bar'] = $exception1
566            )
567            ->when($result = $group->getIterator())
568            ->then
569                ->object($result)
570                    ->isInstanceOf('ArrayIterator')
571                ->array(iterator_to_array($result))
572                    ->isEqualTo([
573                        'bar' => $exception1
574                    ]);
575    }
576
577    public function case_count()
578    {
579        $this
580            ->given(
581                $group        = new SUT('foo'),
582                $exception1   = new SUT('bar'),
583                $exception2   = new SUT('baz'),
584                $group['bar'] = $exception1,
585                $group->beginTransaction(),
586                $group['baz'] = $exception2
587            )
588            ->when($result = count($group))
589            ->then
590                ->integer($result)
591                    ->isEqualTo(1);
592    }
593
594    public function get_get_stack_size()
595    {
596        $this
597            ->given(
598                $group        = new SUT('foo'),
599                $exception1   = new SUT('bar'),
600                $exception2   = new SUT('baz'),
601                $group['bar'] = $exception1,
602                $group->beginTransaction(),
603                $group['baz'] = $exception2
604            )
605            ->when($result = $group->getStackSize())
606            ->then
607                ->integer($result)
608                    ->isEqualTo(2);
609    }
610}
611