123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- <?php
- /**
- * Hoa
- *
- *
- * @license
- *
- * New BSD License
- *
- * Copyright © 2007-2017, Hoa community. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the Hoa nor the names of its contributors may be
- * used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- namespace Hoa\Exception\Test\Unit;
- use Hoa\Exception\Group as SUT;
- use Hoa\Test;
- /**
- * Class \Hoa\Exception\Test\Unit\Group.
- *
- * Test suite of the group class.
- *
- * @copyright Copyright © 2007-2017 Hoa community
- * @license New BSD License
- */
- class Group extends Test\Unit\Suite
- {
- public function case_is_an_exception_arrayaccess_iteratoraggregate_countable()
- {
- $this
- ->when($result = new SUT('foo'))
- ->then
- ->object($result)
- ->isInstanceOf('Hoa\Exception\Exception')
- ->isInstanceOf('ArrayAccess')
- ->isInstanceOf('IteratorAggregate')
- ->isInstanceOf('Countable');
- }
- public function case_constructor()
- {
- $this
- ->given(
- $message = 'foo %s %d %s',
- $code = 7,
- $arguments = ['arg', 42, null],
- $previous = new SUT('previous')
- )
- ->when($result = new SUT($message, $code, $arguments, $previous), $line = __LINE__)
- ->then
- ->string($result->getMessage())
- ->isEqualTo('foo arg 42 (null)')
- ->integer($result->getCode())
- ->isEqualTo(7)
- ->array($result->getArguments())
- ->isEqualTo(['arg', 42, '(null)'])
- ->object($result->getPreviousThrow())
- ->isIdenticalTo($previous)
- ->boolean($result->hasUncommittedExceptions())
- ->isFalse();
- }
- public function case_raise_zero_exception()
- {
- $this
- ->given($group = new SUT('foo'), $line = __LINE__)
- ->when($result = $group->raise())
- ->then
- ->string($result)
- ->isEqualTo(
- __METHOD__ . '(): (0) foo' . "\n" .
- 'in ' . __FILE__ . ' at line ' . $line . '.'
- );
- }
- public function case_raise_one_exception()
- {
- $this
- ->given(
- $exception1 = new SUT('bar'), $barLine = __LINE__,
- $group = new SUT('foo'), $fooLine = __LINE__,
- $group[] = $exception1
- )
- ->when($result = $group->raise())
- ->then
- ->string($result)
- ->isEqualTo(
- __METHOD__ . '(): (0) foo' . "\n" .
- 'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
- 'Contains the following exceptions:' . "\n\n" .
- ' • ' . __METHOD__ . '(): (0) bar' . "\n" .
- ' in ' . __FILE__ . ' at line ' . $barLine . '.'
- );
- }
- public function case_raise_more_exceptions()
- {
- $this
- ->given(
- $exception1 = new SUT('bar'), $barLine = __LINE__,
- $exception2 = new SUT('baz'), $bazLine = __LINE__,
- $group = new SUT('foo'), $fooLine = __LINE__,
- $group[] = $exception1,
- $group[] = $exception2
- )
- ->when($result = $group->raise())
- ->then
- ->string($result)
- ->isEqualTo(
- __METHOD__ . '(): (0) foo' . "\n" .
- 'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
- 'Contains the following exceptions:' . "\n\n" .
- ' • ' . __METHOD__ . '(): (0) bar' . "\n" .
- ' in ' . __FILE__ . ' at line ' . $barLine . '.' . "\n\n" .
- ' • ' . __METHOD__ . '(): (0) baz' . "\n" .
- ' in ' . __FILE__ . ' at line ' . $bazLine . '.'
- );
- }
- public function case_begin_transaction()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $oldStackSize = $group->getStackSize()
- )
- ->when(
- $result = $group->beginTransaction(),
- $stackSize = $group->getStackSize()
- )
- ->then
- ->integer($oldStackSize)
- ->isEqualTo(1)
- ->object($result)
- ->isIdenticalTo($group)
- ->integer($stackSize)
- ->isEqualTo($oldStackSize + 1);
- }
- public function case_rollback_transaction_with_an_empty_stack()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $oldStackSize = $group->getStackSize()
- )
- ->when(
- $result = $group->rollbackTransaction(),
- $stackSize = $group->getStackSize()
- )
- ->then
- ->integer($oldStackSize)
- ->isEqualTo(1)
- ->object($result)
- ->isIdenticalTo($group)
- ->integer($stackSize)
- ->isEqualTo($oldStackSize);
- }
- public function case_rollback_transaction()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $oldStackSize = $group->getStackSize(),
- $group->rollbackTransaction()
- )
- ->when(
- $result = $group->rollbackTransaction(),
- $stackSize = $group->getStackSize()
- )
- ->then
- ->integer($oldStackSize)
- ->isEqualTo(3)
- ->object($result)
- ->isIdenticalTo($group)
- ->integer($stackSize)
- ->isEqualTo($oldStackSize - 2);
- }
- public function case_commit_transaction_with_an_empty_stack()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $oldCount = count($group),
- $oldStackSize = $group->getStackSize()
- )
- ->when(
- $result = $group->commitTransaction(),
- $count = count($group),
- $stackSize = $group->getStackSize()
- )
- ->then
- ->integer($oldCount)
- ->isEqualTo(0)
- ->integer($oldStackSize)
- ->isEqualTo(2)
- ->object($result)
- ->isIdenticalTo($group)
- ->integer($count)
- ->isEqualTo($oldCount)
- ->integer($stackSize)
- ->isEqualTo($oldStackSize - 1);
- }
- public function case_commit_transaction()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $exception1 = new SUT('bar'),
- $exception2 = new SUT('baz'),
- $group[] = $exception1,
- $group[] = $exception2,
- $oldCount = count($group),
- $oldStackSize = $group->getStackSize()
- )
- ->when(
- $result = $group->commitTransaction(),
- $count = count($group),
- $stackSize = $group->getStackSize()
- )
- ->then
- ->integer($oldCount)
- ->isEqualTo(0)
- ->integer($oldStackSize)
- ->isEqualTo(2)
- ->object($result)
- ->isIdenticalTo($group)
- ->integer($count)
- ->isEqualTo($oldCount + 2)
- ->integer($stackSize)
- ->isEqualTo($oldStackSize - 1)
- ->array(iterator_to_array($group->getIterator()))
- ->isEqualTo([
- 0 => $exception1,
- 1 => $exception2
- ]);
- }
- public function case_has_uncommitted_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group[] = new SUT('bar')
- )
- ->when($result = $group->hasUncommittedExceptions())
- ->then
- ->boolean($result)
- ->isTrue();
- }
- public function case_has_no_uncommitted_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction()
- )
- ->when($result = $group->hasUncommittedExceptions())
- ->then
- ->boolean($result)
- ->isFalse();
- }
- public function case_has_no_uncommitted_exceptions_with_empty_stack()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group[] = new SUT('bar')
- )
- ->when($result = $group->hasUncommittedExceptions())
- ->then
- ->boolean($result)
- ->isFalse();
- }
- public function case_offset_exists_with_no_uncommited_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group['bar'] = new SUT('bar')
- )
- ->when($result = $group->offsetExists('bar'))
- ->then
- ->boolean($result)
- ->isTrue();
- }
- public function case_offset_does_not_exist_with_no_uncommited_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group['bar'] = new SUT('bar')
- )
- ->when($result = $group->offsetExists('baz'))
- ->then
- ->boolean($result)
- ->isFalse();
- }
- public function case_offset_exists()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $group['bar'] = new SUT('bar')
- )
- ->when($result = $group->offsetExists('bar'))
- ->then
- ->boolean($result)
- ->isTrue();
- }
- public function case_offset_does_not_exist()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $group['bar'] = new SUT('bar')
- )
- ->when($result = $group->offsetExists('baz'))
- ->then
- ->boolean($result)
- ->isFalse();
- }
- public function case_offset_get_with_no_uncommited_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $group['bar'] = $exception1
- )
- ->when($result = $group->offsetGet('bar'))
- ->then
- ->object($result)
- ->isIdenticalTo($exception1);
- }
- public function case_offset_get_does_not_exist_with_no_uncommited_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $group['bar'] = $exception1
- )
- ->when($result = $group->offsetGet('baz'))
- ->then
- ->variable($result)
- ->isNull();
- }
- public function case_offset_get()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $exception1 = new SUT('bar'),
- $group['bar'] = $exception1
- )
- ->when($result = $group->offsetGet('bar'))
- ->then
- ->object($result)
- ->isIdenticalTo($exception1);
- }
- public function case_offset_get_does_not_exist()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $exception1 = new SUT('bar'),
- $group['bar'] = $exception1
- )
- ->when($result = $group->offsetGet('baz'))
- ->then
- ->variable($result)
- ->isNull();
- }
- public function case_offset_set_not_an_exception()
- {
- $this
- ->given($group = new SUT('foo'))
- ->when($group->offsetSet('bar', new \StdClass()))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isFalse();
- }
- public function case_offset_set()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar')
- )
- ->when($result = $group->offsetExists('bar'))
- ->then
- ->boolean($result)
- ->isFalse()
- ->when($group->offsetSet('bar', $exception1))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isTrue()
- ->object($group->offsetGet('bar'))
- ->isIdenticalTo($exception1);
- }
- public function case_offset_set_with_a_null_index()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar')
- )
- ->when($group->offsetSet(null, $exception1))
- ->then
- ->boolean($group->offsetExists(0))
- ->isTrue()
- ->object($group->offsetGet(0))
- ->isIdenticalTo($exception1);
- }
- public function case_offset_set_with_an_integer_index()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar')
- )
- ->when($group->offsetSet(42, $exception1))
- ->then
- ->boolean($group->offsetExists(42))
- ->isFalse()
- ->boolean($group->offsetExists(0))
- ->isTrue()
- ->object($group->offsetGet(0))
- ->isIdenticalTo($exception1);
- }
- public function case_offset_unset_with_no_uncommited_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group['bar'] = new SUT('bar')
- )
- ->when($group->offsetUnset('bar'))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isFalse();
- }
- public function case_offset_unset_does_not_exist_with_no_uncommited_exceptions()
- {
- $this
- ->given($group = new SUT('foo'))
- ->when($group->offsetUnset('bar'))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isFalse();
- }
- public function case_offset_unset()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction(),
- $group['bar'] = new SUT('bar')
- )
- ->when($result = $group->offsetUnset('bar'))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isFalse();
- }
- public function case_offset_unset_does_not_exist()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $group->beginTransaction(),
- $group->beginTransaction()
- )
- ->when($result = $group->offsetUnset('bar'))
- ->then
- ->boolean($group->offsetExists('bar'))
- ->isFalse();
- }
- public function case_get_exceptions()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $exception2 = new SUT('baz'),
- $group['bar'] = $exception1,
- $group->beginTransaction(),
- $group['baz'] = $exception2
- )
- ->when($result = $group->getExceptions())
- ->then
- ->object($result)
- ->isInstanceOf('ArrayObject')
- ->object($result['bar'])
- ->isIdenticalTo($exception1);
- }
- public function case_get_iterator()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $group['bar'] = $exception1
- )
- ->when($result = $group->getIterator())
- ->then
- ->object($result)
- ->isInstanceOf('ArrayIterator')
- ->array(iterator_to_array($result))
- ->isEqualTo([
- 'bar' => $exception1
- ]);
- }
- public function case_count()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $exception2 = new SUT('baz'),
- $group['bar'] = $exception1,
- $group->beginTransaction(),
- $group['baz'] = $exception2
- )
- ->when($result = count($group))
- ->then
- ->integer($result)
- ->isEqualTo(1);
- }
- public function get_get_stack_size()
- {
- $this
- ->given(
- $group = new SUT('foo'),
- $exception1 = new SUT('bar'),
- $exception2 = new SUT('baz'),
- $group['bar'] = $exception1,
- $group->beginTransaction(),
- $group['baz'] = $exception2
- )
- ->when($result = $group->getStackSize())
- ->then
- ->integer($result)
- ->isEqualTo(2);
- }
- }
|