/tests/units/classes/asserters/exception.php

https://github.com/Yacodo/atoum · PHP · 310 lines · 275 code · 35 blank · 0 comment · 0 complexity · 4bccc6b171127615b44f900008bca114 MD5 · raw file

  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\asserter,
  6. mageekguy\atoum\asserters
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class exception extends atoum\test
  10. {
  11. public function testClass()
  12. {
  13. $this->assert
  14. ->testedClass->isSubclassOf('mageekguy\atoum\asserter')
  15. ;
  16. }
  17. public function test__construct()
  18. {
  19. $asserter = new asserters\exception($generator = new asserter\generator($test = new self($score = new atoum\score())));
  20. $this->assert
  21. ->object($asserter->getScore())->isIdenticalTo($score)
  22. ->object($asserter->getLocale())->isIdenticalTo($test->getLocale())
  23. ->object($asserter->getGenerator())->isIdenticalTo($generator)
  24. ->variable($asserter->getValue())->isNull()
  25. ->boolean($asserter->wasSet())->isFalse()
  26. ;
  27. }
  28. public function testSetWith()
  29. {
  30. $asserter = new asserters\exception(new asserter\generator($test = new self($score = new atoum\score())));
  31. $this->assert
  32. ->exception(function() use (& $line, $asserter, & $value) { $line = __LINE__; $asserter->setWith($value = uniqid()); })
  33. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  34. ->hasMessage(sprintf($this->getLocale()->_('%s is not an exception'), $asserter->getTypeOf($value)))
  35. ->integer($score->getFailNumber())->isEqualTo(1)
  36. ->array($score->getFailAssertions())->isEqualTo(array(
  37. array(
  38. 'case' => null,
  39. 'class' => __CLASS__,
  40. 'method' => $test->getCurrentMethod(),
  41. 'file' => __FILE__,
  42. 'line' => $line,
  43. 'asserter' => get_class($asserter) . '::setWith()',
  44. 'fail' => sprintf($this->getLocale()->_('%s is not an exception'), $asserter->getTypeOf($value))
  45. )
  46. )
  47. )
  48. ->integer($score->getPassNumber())->isZero()
  49. ->string($asserter->getValue())->isEqualTo($value)
  50. ;
  51. $this->assert
  52. ->object($asserter->setWith($value = new \exception()))->isIdenticalTo($asserter); $line = __LINE__
  53. ;
  54. $this->assert
  55. ->integer($score->getFailNumber())->isEqualTo(1)
  56. ->integer($score->getPassNumber())->isEqualTo(1)
  57. ->exception($asserter->getValue())->isIdenticalTo($value)
  58. ;
  59. }
  60. public function testIsInstanceOf()
  61. {
  62. $asserter = new asserters\exception($generator = new asserter\generator($test = new self($score = new atoum\score())));
  63. $this->assert
  64. ->exception(function() use ($asserter) {
  65. $asserter->hasSize(rand(0, PHP_INT_MAX));
  66. }
  67. )
  68. ->isInstanceOf('mageekguy\atoum\exceptions\logic')
  69. ->hasMessage('Exception is undefined')
  70. ;
  71. $asserter->setWith(new \exception());
  72. $this->assert
  73. ->object($asserter->isInstanceOf('\Exception'))->isIdenticalTo($asserter)
  74. ->object($asserter->isInstanceOf('Exception'))->isIdenticalTo($asserter)
  75. ->object($asserter->isInstanceOf('\exception'))->isIdenticalTo($asserter)
  76. ->object($asserter->isInstanceOf('exception'))->isIdenticalTo($asserter)
  77. ->exception(function() use ($asserter) {
  78. $asserter->isInstanceOf(uniqid());
  79. }
  80. )
  81. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  82. ->hasMessage('Argument of mageekguy\atoum\asserters\exception::isInstanceOf() must be a \exception instance or an exception class name')
  83. ;
  84. $score->reset();
  85. $this->assert
  86. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->isInstanceOf('mageekguy\atoum\exceptions\runtime'); })
  87. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  88. ->hasMessage(sprintf($test->getLocale()->_('%s is not an instance of mageekguy\atoum\exceptions\runtime'), $asserter))
  89. ->integer($score->getFailNumber())->isEqualTo(1)
  90. ->array($score->getFailAssertions())->isEqualTo(array(
  91. array(
  92. 'case' => null,
  93. 'class' => __CLASS__,
  94. 'method' => $test->getCurrentMethod(),
  95. 'file' => __FILE__,
  96. 'line' => $line,
  97. 'asserter' => get_class($asserter) . '::isInstanceOf()',
  98. 'fail' => sprintf($test->getLocale()->_('%s is not an instance of mageekguy\atoum\exceptions\runtime'), $asserter)
  99. )
  100. )
  101. )
  102. ->integer($score->getPassNumber())->isZero()
  103. ;
  104. }
  105. public function testHasCode()
  106. {
  107. $asserter = new asserters\exception(new asserter\generator($test = new self($score = new atoum\score())));
  108. $this->assert
  109. ->boolean($asserter->wasSet())->isFalse()
  110. ->exception(function() use ($asserter) {
  111. $asserter->hasCode(rand(- PHP_INT_MAX, PHP_INT_MAX));
  112. }
  113. )
  114. ->isInstanceOf('logicException')
  115. ->hasMessage('Exception is undefined')
  116. ;
  117. $asserter->setWith(new atoum\exceptions\runtime(uniqid(), $code = rand(2, PHP_INT_MAX)));
  118. $score->reset();
  119. $this->assert
  120. ->integer($score->getPassNumber())->isZero()
  121. ->integer($score->getFailNumber())->isZero()
  122. ->exception(function() use (& $line, $asserter, & $otherCode) { $line = __LINE__; $asserter->hasCode($otherCode = 1); })
  123. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  124. ->hasMessage(sprintf($test->getLocale()->_('code is %d instead of %d'), $code, $otherCode))
  125. ->array($score->getFailAssertions())->isEqualTo(array(
  126. array(
  127. 'case' => null,
  128. 'class' => __CLASS__,
  129. 'method' => $test->getCurrentMethod(),
  130. 'file' => __FILE__,
  131. 'line' => $line,
  132. 'asserter' => get_class($asserter) . '::hasCode()',
  133. 'fail' => sprintf($test->getLocale()->_('code is %d instead of %d'), $code, $otherCode)
  134. )
  135. )
  136. )
  137. ->integer($score->getPassNumber())->isZero()
  138. ->integer($score->getFailNumber())->isEqualTo(1)
  139. ->object($asserter->hasCode($code))->isIdenticalTo($asserter)
  140. ->integer($score->getPassNumber())->isEqualTo(1)
  141. ->integer($score->getFailNumber())->isEqualTo(1)
  142. ;
  143. }
  144. public function testHasMessage()
  145. {
  146. $asserter = new asserters\exception(new asserter\generator($test = new self($score = new atoum\score())));
  147. $this->assert
  148. ->boolean($asserter->wasSet())->isFalse()
  149. ->exception(function() use ($asserter) {
  150. $asserter->hasMessage(uniqid());
  151. }
  152. )
  153. ->isInstanceOf('logicException')
  154. ->hasMessage('Exception is undefined')
  155. ;
  156. $asserter->setWith(new atoum\exceptions\runtime($message = uniqid()));
  157. $score->reset();
  158. $this->assert
  159. ->integer($score->getPassNumber())->isZero()
  160. ->integer($score->getFailNumber())->isZero()
  161. ->exception(function() use (& $line, $asserter, & $otherMessage) { $line = __LINE__; $asserter->hasMessage($otherMessage = uniqid()); })
  162. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  163. ->hasMessage(sprintf($test->getLocale()->_('message \'%s\' is not identical to \'%s\''), $message, $otherMessage))
  164. ->array($score->getFailAssertions())->isEqualTo(array(
  165. array(
  166. 'case' => null,
  167. 'class' => __CLASS__,
  168. 'method' => $test->getCurrentMethod(),
  169. 'file' => __FILE__,
  170. 'line' => $line,
  171. 'asserter' => get_class($asserter) . '::hasMessage()',
  172. 'fail' => sprintf($test->getLocale()->_('message \'%s\' is not identical to \'%s\''), $message, $otherMessage)
  173. )
  174. )
  175. )
  176. ->integer($score->getPassNumber())->isZero()
  177. ->integer($score->getFailNumber())->isEqualTo(1)
  178. ->object($asserter->hasMessage($message))->isIdenticalTo($asserter)
  179. ->integer($score->getPassNumber())->isEqualTo(1)
  180. ->integer($score->getFailNumber())->isEqualTo(1)
  181. ;
  182. }
  183. public function testHasNestedException()
  184. {
  185. $asserter = new asserters\exception(new asserter\generator($test = new self($score = new atoum\score())));
  186. $this->assert
  187. ->boolean($asserter->wasSet())->isFalse()
  188. ->exception(function() use ($asserter) {
  189. $asserter->hasNestedException();
  190. }
  191. )
  192. ->isInstanceOf('logicException')
  193. ->hasMessage('Exception is undefined')
  194. ;
  195. $asserter->setWith(new atoum\exceptions\runtime('', 0));
  196. $score->reset();
  197. $this->assert
  198. ->integer($score->getPassNumber())->isZero()
  199. ->integer($score->getFailNumber())->isZero()
  200. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->hasNestedException(); })
  201. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  202. ->hasMessage($test->getLocale()->_('exception does not contain any nested exception'))
  203. ->array($score->getFailAssertions())->isEqualTo(array(
  204. array(
  205. 'case' => null,
  206. 'class' => __CLASS__,
  207. 'method' => $test->getCurrentMethod(),
  208. 'file' => __FILE__,
  209. 'line' => $line,
  210. 'asserter' => get_class($asserter) . '::hasNestedException()',
  211. 'fail' => $test->getLocale()->_('exception does not contain any nested exception')
  212. )
  213. )
  214. )
  215. ->integer($score->getPassNumber())->isZero()
  216. ->integer($score->getFailNumber())->isEqualTo(1)
  217. ->exception(function() use (& $otherLine, $asserter) { $otherLine = __LINE__; $asserter->hasNestedException(new \exception()); })
  218. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  219. ->hasMessage($test->getLocale()->_('exception does not contain this nested exception'))
  220. ->array($score->getFailAssertions())->isEqualTo(array(
  221. array(
  222. 'case' => null,
  223. 'class' => __CLASS__,
  224. 'method' => $test->getCurrentMethod(),
  225. 'file' => __FILE__,
  226. 'line' => $line,
  227. 'asserter' => get_class($asserter) . '::hasNestedException()',
  228. 'fail' => $test->getLocale()->_('exception does not contain any nested exception')
  229. ),
  230. array(
  231. 'case' => null,
  232. 'class' => __CLASS__,
  233. 'method' => $test->getCurrentMethod(),
  234. 'file' => __FILE__,
  235. 'line' => $otherLine,
  236. 'asserter' => get_class($asserter) . '::hasNestedException()',
  237. 'fail' => $test->getLocale()->_('exception does not contain this nested exception')
  238. )
  239. )
  240. )
  241. ->integer($score->getPassNumber())->isZero()
  242. ->integer($score->getFailNumber())->isEqualTo(2)
  243. ;
  244. $asserter->setWith(new atoum\exceptions\runtime('', 0, $nestedException = new \exception()));
  245. $score->reset();
  246. $this->assert
  247. ->integer($score->getPassNumber())->isZero()
  248. ->integer($score->getFailNumber())->isZero()
  249. ->object($asserter->hasNestedException())->isIdenticalTo($asserter)
  250. ->integer($score->getPassNumber())->isEqualTo(1)
  251. ->integer($score->getFailNumber())->isZero()
  252. ->object($asserter->hasNestedException($nestedException))->isIdenticalTo($asserter)
  253. ->integer($score->getPassNumber())->isEqualTo(2)
  254. ->integer($score->getFailNumber())->isZero()
  255. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->hasNestedException(new \exception()); })
  256. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  257. ->hasMessage($test->getLocale()->_('exception does not contain this nested exception'))
  258. ->array($score->getFailAssertions())->isEqualTo(array(
  259. array(
  260. 'case' => null,
  261. 'class' => __CLASS__,
  262. 'method' => $test->getCurrentMethod(),
  263. 'file' => __FILE__,
  264. 'line' => $line,
  265. 'asserter' => get_class($asserter) . '::hasNestedException()',
  266. 'fail' => $test->getLocale()->_('exception does not contain this nested exception')
  267. )
  268. )
  269. )
  270. ->integer($score->getPassNumber())->isEqualTo(2)
  271. ->integer($score->getFailNumber())->isEqualTo(1)
  272. ;
  273. }
  274. }
  275. ?>