PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/asserters/error.php

http://github.com/mageekguy/atoum
PHP | 339 lines | 297 code | 42 blank | 0 comment | 0 complexity | bc829e79d94b02575cdaf36ee63631ce MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\asserter,
  6. mageekguy\atoum\tools\variable
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class error extends atoum\test
  10. {
  11. public function testClass()
  12. {
  13. $this->testedClass->extends('mageekguy\atoum\asserter');
  14. }
  15. public function test__construct()
  16. {
  17. $this
  18. ->given($this->newTestedInstance)
  19. ->then
  20. ->object($this->testedInstance->getGenerator())->isEqualTo(new asserter\generator())
  21. ->object($this->testedInstance->getAnalyzer())->isEqualTo(new variable\analyzer())
  22. ->object($this->testedInstance->getLocale())->isEqualTo(new atoum\locale())
  23. ->object($this->testedInstance->getScore())->isInstanceOf('mageekguy\atoum\test\score')
  24. ->variable($this->testedInstance->getMessage())->isNull()
  25. ->variable($this->testedInstance->getType())->isNull()
  26. ->given($this->newTestedInstance($generator = new asserter\generator(), $score = new atoum\test\score(), $locale = new atoum\locale()))
  27. ->then
  28. ->object($this->testedInstance->getGenerator())->isIdenticalTo($generator)
  29. ->object($this->testedInstance->getAnalyzer())->isEqualTo(new variable\analyzer())
  30. ->object($this->testedInstance->getLocale())->isIdenticalTo($locale)
  31. ->object($this->testedInstance->getScore())->isIdenticalTo($score)
  32. ->variable($this->testedInstance->getMessage())->isNull()
  33. ->variable($this->testedInstance->getType())->isNull()
  34. ;
  35. }
  36. public function testInitWithTest()
  37. {
  38. $this
  39. ->if($this->newTestedInstance)
  40. ->then
  41. ->object($this->testedInstance->setWithTest($this))->isTestedInstance
  42. ->object($this->testedInstance->getScore())->isIdenticalTo($this->getScore())
  43. ;
  44. }
  45. public function testGetAsString()
  46. {
  47. $this
  48. ->string(atoum\asserters\error::getAsString(E_ERROR))->isEqualTo('E_ERROR')
  49. ->string(atoum\asserters\error::getAsString(E_WARNING))->isEqualTo('E_WARNING')
  50. ->string(atoum\asserters\error::getAsString(E_PARSE))->isEqualTo('E_PARSE')
  51. ->string(atoum\asserters\error::getAsString(E_NOTICE))->isEqualTo('E_NOTICE')
  52. ->string(atoum\asserters\error::getAsString(E_CORE_ERROR))->isEqualTo('E_CORE_ERROR')
  53. ->string(atoum\asserters\error::getAsString(E_CORE_WARNING))->isEqualTo('E_CORE_WARNING')
  54. ->string(atoum\asserters\error::getAsString(E_COMPILE_ERROR))->isEqualTo('E_COMPILE_ERROR')
  55. ->string(atoum\asserters\error::getAsString(E_COMPILE_WARNING))->isEqualTo('E_COMPILE_WARNING')
  56. ->string(atoum\asserters\error::getAsString(E_USER_ERROR))->isEqualTo('E_USER_ERROR')
  57. ->string(atoum\asserters\error::getAsString(E_USER_WARNING))->isEqualTo('E_USER_WARNING')
  58. ->string(atoum\asserters\error::getAsString(E_USER_NOTICE))->isEqualTo('E_USER_NOTICE')
  59. ->string(atoum\asserters\error::getAsString(E_STRICT))->isEqualTo('E_STRICT')
  60. ->string(atoum\asserters\error::getAsString(E_RECOVERABLE_ERROR))->isEqualTo('E_RECOVERABLE_ERROR')
  61. ->string(atoum\asserters\error::getAsString(E_DEPRECATED))->isEqualTo('E_DEPRECATED')
  62. ->string(atoum\asserters\error::getAsString(E_USER_DEPRECATED))->isEqualTo('E_USER_DEPRECATED')
  63. ->string(atoum\asserters\error::getAsString(E_ALL))->isEqualTo('E_ALL')
  64. ->string(atoum\asserters\error::getAsString('unknown error'))->isEqualTo('UNKNOWN')
  65. ;
  66. }
  67. public function testSetWith()
  68. {
  69. $this
  70. ->if($this->newTestedInstance)
  71. ->then
  72. ->object($this->testedInstance->setWith(null, null))->isTestedInstance
  73. ->variable($this->testedInstance->getMessage())->isNull()
  74. ->variable($this->testedInstance->getType())->isNull()
  75. ->object($this->testedInstance->setWith($message = uniqid(), null))->isTestedInstance
  76. ->string($this->testedInstance->getMessage())->isEqualTo($message)
  77. ->variable($this->testedInstance->getType())->isNull()
  78. ->object($this->testedInstance->setWith($message = uniqid(), $type = rand(0, PHP_INT_MAX)))->isTestedInstance
  79. ->string($this->testedInstance->getMessage())->isEqualTo($message)
  80. ->integer($this->testedInstance->getType())->isEqualTo($type)
  81. ;
  82. }
  83. public function testExists()
  84. {
  85. $this
  86. ->given($asserter = $this->newTestedInstance)
  87. ->if(
  88. $asserter->setLocale($locale = new \mock\atoum\locale()),
  89. $this->calling($locale)->_ = $errorNotExists = uniqid()
  90. )
  91. ->then
  92. ->exception(function() use (& $line, $asserter) { $asserter->exists(); })
  93. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  94. ->hasMessage($errorNotExists)
  95. ->mock($locale)->call('_')->withArguments('error does not exist')->once
  96. ->exception(function() use (& $line, $asserter) { $asserter->exists; })
  97. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  98. ->hasMessage($errorNotExists)
  99. ->mock($locale)->call('_')->withArguments('error does not exist')->twice
  100. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  101. ->then
  102. ->object($asserter->exists())->isTestedInstance
  103. ->array($asserter->getScore()->getErrors())->isEmpty()
  104. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  105. ->then
  106. ->object($asserter->exists)->isTestedInstance
  107. ->array($asserter->getScore()->getErrors())->isEmpty()
  108. ->if($asserter->setWith($message = uniqid(), null))
  109. ->then
  110. ->exception(function() use (& $line, $asserter) { $asserter->exists(); })
  111. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  112. ->hasMessage($errorNotExists)
  113. ->mock($locale)->call('_')->withArguments('error with message \'%s\' does not exist', $message)->once
  114. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), $message, uniqid(), rand(1, PHP_INT_MAX)))
  115. ->then
  116. ->object($asserter->exists())->isTestedInstance
  117. ->array($asserter->getScore()->getErrors())->isEmpty()
  118. ->if($asserter->setWith($message = uniqid(), $type = E_USER_ERROR))
  119. ->then
  120. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->exists(); })
  121. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  122. ->hasMessage($errorNotExists)
  123. ->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' does not exist', atoum\asserters\error::getAsString($type), $message)->once
  124. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, $message, uniqid(), rand(1, PHP_INT_MAX)))
  125. ->then
  126. ->object($asserter->exists())->isTestedInstance
  127. ->array($asserter->getScore()->getErrors())->isEmpty()
  128. ->if($asserter->setWith(null, $type = E_USER_ERROR))
  129. ->then
  130. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->exists(); })
  131. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  132. ->hasMessage($errorNotExists)
  133. ->mock($locale)->call('_')->withArguments('error of type %s does not exist', atoum\asserters\error::getAsString($type))->once
  134. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  135. ->then
  136. ->object($asserter->exists())->isTestedInstance
  137. ->array($asserter->getScore()->getErrors())->isEmpty()
  138. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(1, PHP_INT_MAX), $message = uniqid() . 'FOO' . uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  139. ->and($asserter->withPattern('/FOO/')->withType(null))
  140. ->then
  141. ->object($asserter->exists())->isTestedInstance
  142. ->array($asserter->getScore()->getErrors())->isEmpty()
  143. ;
  144. }
  145. public function testNotExists()
  146. {
  147. $this
  148. ->given($asserter = $this->newTestedInstance)
  149. ->if(
  150. $asserter->setLocale($locale = new \mock\atoum\locale()),
  151. $this->calling($locale)->_ = $errorExists = uniqid()
  152. )
  153. ->then
  154. ->object($asserter->notExists())->isTestedInstance
  155. ->object($asserter->notExists)->isTestedInstance
  156. ->array($asserter->getScore()->getErrors())->isEmpty()
  157. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  158. ->then
  159. ->exception(function() use (& $line, $asserter) { $asserter->notExists(); })
  160. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  161. ->hasMessage($errorExists)
  162. ->mock($locale)->call('_')->withArguments('error exists')->once
  163. ->exception(function() use (& $line, $asserter) { $asserter->notExists; })
  164. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  165. ->hasMessage($errorExists)
  166. ->mock($locale)->call('_')->withArguments('error exists')->twice
  167. ->if($asserter->setWith($message = uniqid(), null))
  168. ->then
  169. ->exception(function() use (& $line, $asserter) { $asserter->exists(); })
  170. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  171. ->hasMessage($errorExists)
  172. ->mock($locale)->call('_')->withArguments('error with message \'%s\' does not exist', $message)->once
  173. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(0, PHP_INT_MAX), $message, uniqid(), rand(1, PHP_INT_MAX)))
  174. ->then
  175. ->exception(function() use (& $line, $asserter) { $asserter->notExists(); })
  176. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  177. ->hasMessage($errorExists)
  178. ->mock($locale)->call('_')->withArguments('error with message \'%s\' exists')->once
  179. ->exception(function() use (& $line, $asserter) { $asserter->notExists; })
  180. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  181. ->hasMessage($errorExists)
  182. ->mock($locale)->call('_')->withArguments('error with message \'%s\' exists')->twice
  183. ->array($asserter->getScore()->getErrors())->isNotEmpty()
  184. ->if($asserter->setWith($message = uniqid(), $type = E_USER_ERROR))
  185. ->then
  186. ->object($asserter->notExists())->isTestedInstance
  187. ->object($asserter->notExists)->isTestedInstance
  188. ->array($asserter->getScore()->getErrors())->isNotEmpty()
  189. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, $message, uniqid(), rand(1, PHP_INT_MAX)))
  190. ->then
  191. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->notExists(); })
  192. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  193. ->hasMessage($errorExists)
  194. ->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists', atoum\asserters\error::getAsString($type), $message)->once
  195. ->if($asserter->setWith(null, $type = E_USER_ERROR))
  196. ->then
  197. ->exception(function() use (& $line, $asserter) { $line = __LINE__; $asserter->notExists(); })
  198. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  199. ->hasMessage($errorExists)
  200. ->mock($locale)->call('_')->withArguments('error of type %s exists', atoum\asserters\error::getAsString($type))->once
  201. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  202. ->then
  203. ->exception(function() use (& $line, $asserter) { $asserter->notExists(); })
  204. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  205. ->hasMessage($errorExists)
  206. ->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists')->once
  207. ->array($asserter->getScore()->getErrors())->isNotEmpty()
  208. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), $type, uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  209. ->then
  210. ->exception(function() use (& $line, $asserter) { $asserter->notExists; })
  211. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  212. ->hasMessage($errorExists)
  213. ->mock($locale)->call('_')->withArguments('error of type %s with message \'%s\' exists')->once
  214. ->array($asserter->getScore()->getErrors())->isNotEmpty()
  215. ->if($asserter->getScore()->addError(uniqid(), uniqid(), uniqid(), rand(1, PHP_INT_MAX), rand(1, PHP_INT_MAX), $message = uniqid() . 'FOO' . uniqid(), uniqid(), rand(1, PHP_INT_MAX)))
  216. ->and($asserter->withPattern('/FOO/')->withType(null))
  217. ->then
  218. ->object($asserter->exists())->isTestedInstance
  219. ->array($asserter->getScore()->getErrors())->isNotEmpty()
  220. ;
  221. }
  222. public function testWithType()
  223. {
  224. $this
  225. ->given($this->newTestedInstance)
  226. ->then
  227. ->object($this->testedInstance->withType($type = rand(1, PHP_INT_MAX)))->isTestedInstance
  228. ->integer($this->testedInstance->getType())->isEqualTo($type)
  229. ;
  230. }
  231. public function testWithAnyType()
  232. {
  233. $this
  234. ->given($this->newTestedInstance->withType(rand(1, PHP_INT_MAX)))
  235. ->then
  236. ->object($this->testedInstance->withAnyType())->isTestedInstance
  237. ->variable($this->testedInstance->getType())->isNull()
  238. ->given($this->newTestedInstance->withType(rand(1, PHP_INT_MAX)))
  239. ->then
  240. ->object($this->testedInstance->withAnyType)->isTestedInstance
  241. ->variable($this->testedInstance->getType())->isNull()
  242. ;
  243. }
  244. public function testWithMessage()
  245. {
  246. $this
  247. ->given($this->newTestedInstance)
  248. ->then
  249. ->object($this->testedInstance->withMessage($message = uniqid()))->isTestedInstance
  250. ->string($this->testedInstance->getMessage())->isEqualTo($message)
  251. ->boolean($this->testedInstance->messageIsPattern())->isFalse()
  252. ;
  253. }
  254. public function testWithPattern()
  255. {
  256. $this
  257. ->given($this->newTestedInstance)
  258. ->then
  259. ->object($this->testedInstance->withPattern($pattern = uniqid()))->isTestedInstance
  260. ->string($this->testedInstance->getMessage())->isEqualTo($pattern)
  261. ->boolean($this->testedInstance->messageIsPattern())->isTrue()
  262. ;
  263. }
  264. public function testWithAnyMessage()
  265. {
  266. $this
  267. ->given($this->newTestedInstance->withMessage(uniqid()))
  268. ->then
  269. ->object($this->testedInstance->withAnyMessage())->isTestedInstance
  270. ->variable($this->testedInstance->getMessage())->isNull()
  271. ->boolean($this->testedInstance->messageIsPattern())->isFalse()
  272. ->given($this->newTestedInstance->withMessage(uniqid()))
  273. ->then
  274. ->object($this->testedInstance->withAnyMessage)->isTestedInstance
  275. ->variable($this->testedInstance->getMessage())->isNull()
  276. ->boolean($this->testedInstance->messageIsPattern())->isFalse()
  277. ->if($this->testedInstance->withPattern(uniqid()))
  278. ->then
  279. ->object($this->testedInstance->withAnyMessage())->isTestedInstance
  280. ->variable($this->testedInstance->getMessage())->isNull()
  281. ->boolean($this->testedInstance->messageIsPattern())->isFalse()
  282. ;
  283. }
  284. public function testSetScore()
  285. {
  286. $this
  287. ->given($this->newTestedInstance)
  288. ->then
  289. ->object($this->testedInstance->setScore($score = new atoum\test\score()))->isTestedInstance
  290. ->object($this->testedInstance->getScore())->isIdenticalTo($score)
  291. ->object($this->testedInstance->setScore())->isTestedInstance
  292. ->object($this->testedInstance->getScore())
  293. ->isNotIdenticalTo($score)
  294. ->isInstanceOf('mageekguy\atoum\score')
  295. ;
  296. }
  297. }