Group.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Exception\Test\Unit;
  36. use Hoa\Exception\Group as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Exception\Test\Unit\Group.
  40. *
  41. * Test suite of the group class.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Group extends Test\Unit\Suite
  47. {
  48. public function case_is_an_exception_arrayaccess_iteratoraggregate_countable()
  49. {
  50. $this
  51. ->when($result = new SUT('foo'))
  52. ->then
  53. ->object($result)
  54. ->isInstanceOf('Hoa\Exception\Exception')
  55. ->isInstanceOf('ArrayAccess')
  56. ->isInstanceOf('IteratorAggregate')
  57. ->isInstanceOf('Countable');
  58. }
  59. public function case_constructor()
  60. {
  61. $this
  62. ->given(
  63. $message = 'foo %s %d %s',
  64. $code = 7,
  65. $arguments = ['arg', 42, null],
  66. $previous = new SUT('previous')
  67. )
  68. ->when($result = new SUT($message, $code, $arguments, $previous), $line = __LINE__)
  69. ->then
  70. ->string($result->getMessage())
  71. ->isEqualTo('foo arg 42 (null)')
  72. ->integer($result->getCode())
  73. ->isEqualTo(7)
  74. ->array($result->getArguments())
  75. ->isEqualTo(['arg', 42, '(null)'])
  76. ->object($result->getPreviousThrow())
  77. ->isIdenticalTo($previous)
  78. ->boolean($result->hasUncommittedExceptions())
  79. ->isFalse();
  80. }
  81. public function case_raise_zero_exception()
  82. {
  83. $this
  84. ->given($group = new SUT('foo'), $line = __LINE__)
  85. ->when($result = $group->raise())
  86. ->then
  87. ->string($result)
  88. ->isEqualTo(
  89. __METHOD__ . '(): (0) foo' . "\n" .
  90. 'in ' . __FILE__ . ' at line ' . $line . '.'
  91. );
  92. }
  93. public function case_raise_one_exception()
  94. {
  95. $this
  96. ->given(
  97. $exception1 = new SUT('bar'), $barLine = __LINE__,
  98. $group = new SUT('foo'), $fooLine = __LINE__,
  99. $group[] = $exception1
  100. )
  101. ->when($result = $group->raise())
  102. ->then
  103. ->string($result)
  104. ->isEqualTo(
  105. __METHOD__ . '(): (0) foo' . "\n" .
  106. 'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
  107. 'Contains the following exceptions:' . "\n\n" .
  108. ' • ' . __METHOD__ . '(): (0) bar' . "\n" .
  109. ' in ' . __FILE__ . ' at line ' . $barLine . '.'
  110. );
  111. }
  112. public function case_raise_more_exceptions()
  113. {
  114. $this
  115. ->given(
  116. $exception1 = new SUT('bar'), $barLine = __LINE__,
  117. $exception2 = new SUT('baz'), $bazLine = __LINE__,
  118. $group = new SUT('foo'), $fooLine = __LINE__,
  119. $group[] = $exception1,
  120. $group[] = $exception2
  121. )
  122. ->when($result = $group->raise())
  123. ->then
  124. ->string($result)
  125. ->isEqualTo(
  126. __METHOD__ . '(): (0) foo' . "\n" .
  127. 'in ' . __FILE__ . ' at line ' . $fooLine . '.' . "\n\n" .
  128. 'Contains the following exceptions:' . "\n\n" .
  129. ' • ' . __METHOD__ . '(): (0) bar' . "\n" .
  130. ' in ' . __FILE__ . ' at line ' . $barLine . '.' . "\n\n" .
  131. ' • ' . __METHOD__ . '(): (0) baz' . "\n" .
  132. ' in ' . __FILE__ . ' at line ' . $bazLine . '.'
  133. );
  134. }
  135. public function case_begin_transaction()
  136. {
  137. $this
  138. ->given(
  139. $group = new SUT('foo'),
  140. $oldStackSize = $group->getStackSize()
  141. )
  142. ->when(
  143. $result = $group->beginTransaction(),
  144. $stackSize = $group->getStackSize()
  145. )
  146. ->then
  147. ->integer($oldStackSize)
  148. ->isEqualTo(1)
  149. ->object($result)
  150. ->isIdenticalTo($group)
  151. ->integer($stackSize)
  152. ->isEqualTo($oldStackSize + 1);
  153. }
  154. public function case_rollback_transaction_with_an_empty_stack()
  155. {
  156. $this
  157. ->given(
  158. $group = new SUT('foo'),
  159. $oldStackSize = $group->getStackSize()
  160. )
  161. ->when(
  162. $result = $group->rollbackTransaction(),
  163. $stackSize = $group->getStackSize()
  164. )
  165. ->then
  166. ->integer($oldStackSize)
  167. ->isEqualTo(1)
  168. ->object($result)
  169. ->isIdenticalTo($group)
  170. ->integer($stackSize)
  171. ->isEqualTo($oldStackSize);
  172. }
  173. public function case_rollback_transaction()
  174. {
  175. $this
  176. ->given(
  177. $group = new SUT('foo'),
  178. $group->beginTransaction(),
  179. $group->beginTransaction(),
  180. $oldStackSize = $group->getStackSize(),
  181. $group->rollbackTransaction()
  182. )
  183. ->when(
  184. $result = $group->rollbackTransaction(),
  185. $stackSize = $group->getStackSize()
  186. )
  187. ->then
  188. ->integer($oldStackSize)
  189. ->isEqualTo(3)
  190. ->object($result)
  191. ->isIdenticalTo($group)
  192. ->integer($stackSize)
  193. ->isEqualTo($oldStackSize - 2);
  194. }
  195. public function case_commit_transaction_with_an_empty_stack()
  196. {
  197. $this
  198. ->given(
  199. $group = new SUT('foo'),
  200. $group->beginTransaction(),
  201. $oldCount = count($group),
  202. $oldStackSize = $group->getStackSize()
  203. )
  204. ->when(
  205. $result = $group->commitTransaction(),
  206. $count = count($group),
  207. $stackSize = $group->getStackSize()
  208. )
  209. ->then
  210. ->integer($oldCount)
  211. ->isEqualTo(0)
  212. ->integer($oldStackSize)
  213. ->isEqualTo(2)
  214. ->object($result)
  215. ->isIdenticalTo($group)
  216. ->integer($count)
  217. ->isEqualTo($oldCount)
  218. ->integer($stackSize)
  219. ->isEqualTo($oldStackSize - 1);
  220. }
  221. public function case_commit_transaction()
  222. {
  223. $this
  224. ->given(
  225. $group = new SUT('foo'),
  226. $group->beginTransaction(),
  227. $exception1 = new SUT('bar'),
  228. $exception2 = new SUT('baz'),
  229. $group[] = $exception1,
  230. $group[] = $exception2,
  231. $oldCount = count($group),
  232. $oldStackSize = $group->getStackSize()
  233. )
  234. ->when(
  235. $result = $group->commitTransaction(),
  236. $count = count($group),
  237. $stackSize = $group->getStackSize()
  238. )
  239. ->then
  240. ->integer($oldCount)
  241. ->isEqualTo(0)
  242. ->integer($oldStackSize)
  243. ->isEqualTo(2)
  244. ->object($result)
  245. ->isIdenticalTo($group)
  246. ->integer($count)
  247. ->isEqualTo($oldCount + 2)
  248. ->integer($stackSize)
  249. ->isEqualTo($oldStackSize - 1)
  250. ->array(iterator_to_array($group->getIterator()))
  251. ->isEqualTo([
  252. 0 => $exception1,
  253. 1 => $exception2
  254. ]);
  255. }
  256. public function case_has_uncommitted_exceptions()
  257. {
  258. $this
  259. ->given(
  260. $group = new SUT('foo'),
  261. $group->beginTransaction(),
  262. $group[] = new SUT('bar')
  263. )
  264. ->when($result = $group->hasUncommittedExceptions())
  265. ->then
  266. ->boolean($result)
  267. ->isTrue();
  268. }
  269. public function case_has_no_uncommitted_exceptions()
  270. {
  271. $this
  272. ->given(
  273. $group = new SUT('foo'),
  274. $group->beginTransaction()
  275. )
  276. ->when($result = $group->hasUncommittedExceptions())
  277. ->then
  278. ->boolean($result)
  279. ->isFalse();
  280. }
  281. public function case_has_no_uncommitted_exceptions_with_empty_stack()
  282. {
  283. $this
  284. ->given(
  285. $group = new SUT('foo'),
  286. $group[] = new SUT('bar')
  287. )
  288. ->when($result = $group->hasUncommittedExceptions())
  289. ->then
  290. ->boolean($result)
  291. ->isFalse();
  292. }
  293. public function case_offset_exists_with_no_uncommited_exceptions()
  294. {
  295. $this
  296. ->given(
  297. $group = new SUT('foo'),
  298. $group['bar'] = new SUT('bar')
  299. )
  300. ->when($result = $group->offsetExists('bar'))
  301. ->then
  302. ->boolean($result)
  303. ->isTrue();
  304. }
  305. public function case_offset_does_not_exist_with_no_uncommited_exceptions()
  306. {
  307. $this
  308. ->given(
  309. $group = new SUT('foo'),
  310. $group['bar'] = new SUT('bar')
  311. )
  312. ->when($result = $group->offsetExists('baz'))
  313. ->then
  314. ->boolean($result)
  315. ->isFalse();
  316. }
  317. public function case_offset_exists()
  318. {
  319. $this
  320. ->given(
  321. $group = new SUT('foo'),
  322. $group->beginTransaction(),
  323. $group->beginTransaction(),
  324. $group['bar'] = new SUT('bar')
  325. )
  326. ->when($result = $group->offsetExists('bar'))
  327. ->then
  328. ->boolean($result)
  329. ->isTrue();
  330. }
  331. public function case_offset_does_not_exist()
  332. {
  333. $this
  334. ->given(
  335. $group = new SUT('foo'),
  336. $group->beginTransaction(),
  337. $group->beginTransaction(),
  338. $group['bar'] = new SUT('bar')
  339. )
  340. ->when($result = $group->offsetExists('baz'))
  341. ->then
  342. ->boolean($result)
  343. ->isFalse();
  344. }
  345. public function case_offset_get_with_no_uncommited_exceptions()
  346. {
  347. $this
  348. ->given(
  349. $group = new SUT('foo'),
  350. $exception1 = new SUT('bar'),
  351. $group['bar'] = $exception1
  352. )
  353. ->when($result = $group->offsetGet('bar'))
  354. ->then
  355. ->object($result)
  356. ->isIdenticalTo($exception1);
  357. }
  358. public function case_offset_get_does_not_exist_with_no_uncommited_exceptions()
  359. {
  360. $this
  361. ->given(
  362. $group = new SUT('foo'),
  363. $exception1 = new SUT('bar'),
  364. $group['bar'] = $exception1
  365. )
  366. ->when($result = $group->offsetGet('baz'))
  367. ->then
  368. ->variable($result)
  369. ->isNull();
  370. }
  371. public function case_offset_get()
  372. {
  373. $this
  374. ->given(
  375. $group = new SUT('foo'),
  376. $group->beginTransaction(),
  377. $group->beginTransaction(),
  378. $exception1 = new SUT('bar'),
  379. $group['bar'] = $exception1
  380. )
  381. ->when($result = $group->offsetGet('bar'))
  382. ->then
  383. ->object($result)
  384. ->isIdenticalTo($exception1);
  385. }
  386. public function case_offset_get_does_not_exist()
  387. {
  388. $this
  389. ->given(
  390. $group = new SUT('foo'),
  391. $group->beginTransaction(),
  392. $group->beginTransaction(),
  393. $exception1 = new SUT('bar'),
  394. $group['bar'] = $exception1
  395. )
  396. ->when($result = $group->offsetGet('baz'))
  397. ->then
  398. ->variable($result)
  399. ->isNull();
  400. }
  401. public function case_offset_set_not_an_exception()
  402. {
  403. $this
  404. ->given($group = new SUT('foo'))
  405. ->when($group->offsetSet('bar', new \StdClass()))
  406. ->then
  407. ->boolean($group->offsetExists('bar'))
  408. ->isFalse();
  409. }
  410. public function case_offset_set()
  411. {
  412. $this
  413. ->given(
  414. $group = new SUT('foo'),
  415. $exception1 = new SUT('bar')
  416. )
  417. ->when($result = $group->offsetExists('bar'))
  418. ->then
  419. ->boolean($result)
  420. ->isFalse()
  421. ->when($group->offsetSet('bar', $exception1))
  422. ->then
  423. ->boolean($group->offsetExists('bar'))
  424. ->isTrue()
  425. ->object($group->offsetGet('bar'))
  426. ->isIdenticalTo($exception1);
  427. }
  428. public function case_offset_set_with_a_null_index()
  429. {
  430. $this
  431. ->given(
  432. $group = new SUT('foo'),
  433. $exception1 = new SUT('bar')
  434. )
  435. ->when($group->offsetSet(null, $exception1))
  436. ->then
  437. ->boolean($group->offsetExists(0))
  438. ->isTrue()
  439. ->object($group->offsetGet(0))
  440. ->isIdenticalTo($exception1);
  441. }
  442. public function case_offset_set_with_an_integer_index()
  443. {
  444. $this
  445. ->given(
  446. $group = new SUT('foo'),
  447. $exception1 = new SUT('bar')
  448. )
  449. ->when($group->offsetSet(42, $exception1))
  450. ->then
  451. ->boolean($group->offsetExists(42))
  452. ->isFalse()
  453. ->boolean($group->offsetExists(0))
  454. ->isTrue()
  455. ->object($group->offsetGet(0))
  456. ->isIdenticalTo($exception1);
  457. }
  458. public function case_offset_unset_with_no_uncommited_exceptions()
  459. {
  460. $this
  461. ->given(
  462. $group = new SUT('foo'),
  463. $group['bar'] = new SUT('bar')
  464. )
  465. ->when($group->offsetUnset('bar'))
  466. ->then
  467. ->boolean($group->offsetExists('bar'))
  468. ->isFalse();
  469. }
  470. public function case_offset_unset_does_not_exist_with_no_uncommited_exceptions()
  471. {
  472. $this
  473. ->given($group = new SUT('foo'))
  474. ->when($group->offsetUnset('bar'))
  475. ->then
  476. ->boolean($group->offsetExists('bar'))
  477. ->isFalse();
  478. }
  479. public function case_offset_unset()
  480. {
  481. $this
  482. ->given(
  483. $group = new SUT('foo'),
  484. $group->beginTransaction(),
  485. $group->beginTransaction(),
  486. $group['bar'] = new SUT('bar')
  487. )
  488. ->when($result = $group->offsetUnset('bar'))
  489. ->then
  490. ->boolean($group->offsetExists('bar'))
  491. ->isFalse();
  492. }
  493. public function case_offset_unset_does_not_exist()
  494. {
  495. $this
  496. ->given(
  497. $group = new SUT('foo'),
  498. $group->beginTransaction(),
  499. $group->beginTransaction()
  500. )
  501. ->when($result = $group->offsetUnset('bar'))
  502. ->then
  503. ->boolean($group->offsetExists('bar'))
  504. ->isFalse();
  505. }
  506. public function case_get_exceptions()
  507. {
  508. $this
  509. ->given(
  510. $group = new SUT('foo'),
  511. $exception1 = new SUT('bar'),
  512. $exception2 = new SUT('baz'),
  513. $group['bar'] = $exception1,
  514. $group->beginTransaction(),
  515. $group['baz'] = $exception2
  516. )
  517. ->when($result = $group->getExceptions())
  518. ->then
  519. ->object($result)
  520. ->isInstanceOf('ArrayObject')
  521. ->object($result['bar'])
  522. ->isIdenticalTo($exception1);
  523. }
  524. public function case_get_iterator()
  525. {
  526. $this
  527. ->given(
  528. $group = new SUT('foo'),
  529. $exception1 = new SUT('bar'),
  530. $group['bar'] = $exception1
  531. )
  532. ->when($result = $group->getIterator())
  533. ->then
  534. ->object($result)
  535. ->isInstanceOf('ArrayIterator')
  536. ->array(iterator_to_array($result))
  537. ->isEqualTo([
  538. 'bar' => $exception1
  539. ]);
  540. }
  541. public function case_count()
  542. {
  543. $this
  544. ->given(
  545. $group = new SUT('foo'),
  546. $exception1 = new SUT('bar'),
  547. $exception2 = new SUT('baz'),
  548. $group['bar'] = $exception1,
  549. $group->beginTransaction(),
  550. $group['baz'] = $exception2
  551. )
  552. ->when($result = count($group))
  553. ->then
  554. ->integer($result)
  555. ->isEqualTo(1);
  556. }
  557. public function get_get_stack_size()
  558. {
  559. $this
  560. ->given(
  561. $group = new SUT('foo'),
  562. $exception1 = new SUT('bar'),
  563. $exception2 = new SUT('baz'),
  564. $group['bar'] = $exception1,
  565. $group->beginTransaction(),
  566. $group['baz'] = $exception2
  567. )
  568. ->when($result = $group->getStackSize())
  569. ->then
  570. ->integer($result)
  571. ->isEqualTo(2);
  572. }
  573. }