PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/asserters/adapter.php

http://github.com/mageekguy/atoum
PHP | 923 lines | 800 code | 123 blank | 0 comment | 0 complexity | 0f8cd1ac17777785463672bedc1ecf64 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\asserters;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\test,
  6. mageekguy\atoum\asserter,
  7. mageekguy\atoum\tools\variable
  8. ;
  9. require_once __DIR__ . '/../../runner.php';
  10. class adapter extends atoum\test
  11. {
  12. public function testClass()
  13. {
  14. $this->testedClass->extends('mageekguy\atoum\asserter');
  15. }
  16. public function test__construct()
  17. {
  18. $this
  19. ->given($this->newTestedInstance)
  20. ->then
  21. ->object($this->testedInstance->getGenerator())->isEqualTo(new atoum\asserter\generator())
  22. ->object($this->testedInstance->getLocale())->isEqualTo(new atoum\locale())
  23. ->object($this->testedInstance->getAnalyzer())->isEqualTo(new atoum\tools\variable\analyzer())
  24. ->variable($this->testedInstance->getAdapter())->isNull()
  25. ->variable($this->testedInstance->getCall())->isEqualTo(new test\adapter\call())
  26. ->given($this->newTestedInstance($generator = new atoum\asserter\generator(), $analyzer = new variable\analyzer(), $locale = new atoum\locale()))
  27. ->then
  28. ->object($this->testedInstance->getGenerator())->isIdenticalTo($generator)
  29. ->object($this->testedInstance->getAnalyzer())->isEqualTo($analyzer)
  30. ->object($this->testedInstance->getLocale())->isIdenticalTo($locale)
  31. ->variable($this->testedInstance->getAdapter())->isNull()
  32. ->variable($this->testedInstance->getCall())->isEqualTo(new test\adapter\call())
  33. ;
  34. }
  35. public function testSetWith()
  36. {
  37. $this
  38. ->given($this->newTestedInstance($generator = new asserter\generator())
  39. ->setLocale($locale = new \mock\atoum\locale())
  40. ->setAnalyzer($analyzer = new \mock\atoum\tools\variable\analyzer())
  41. )
  42. ->if(
  43. $this->calling($locale)->_ = $notAnAdapter = uniqid(),
  44. $this->calling($analyzer)->getTypeOf = $type = uniqid()
  45. )
  46. ->then
  47. ->exception(function($test) use (& $value) { $test->testedInstance->setWith($value = uniqid()); })
  48. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  49. ->hasMessage($notAnAdapter)
  50. ->mock($locale)->call('_')->withArguments('%s is not a test adapter', $type)->once
  51. ->mock($analyzer)->call('getTypeOf')->withArguments($value)->once
  52. ->string($this->testedInstance->getAdapter())->isEqualTo($value)
  53. ->object($this->testedInstance->setWith($adapter = new test\adapter()))->isTestedInstance
  54. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  55. ;
  56. }
  57. public function testReset()
  58. {
  59. $this
  60. ->if($this->newTestedInstance(new asserter\generator()))
  61. ->then
  62. ->variable($this->testedInstance->getAdapter())->isNull()
  63. ->object($this->testedInstance->reset())->isTestedInstance
  64. ->variable($this->testedInstance->getAdapter())->isNull()
  65. ->if($this->testedInstance->setWith($adapter = new atoum\test\adapter()))
  66. ->then
  67. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  68. ->sizeOf($adapter->getCalls())->isZero()
  69. ->object($this->testedInstance->reset())->isTestedInstance
  70. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  71. ->sizeOf($adapter->getCalls())->isZero()
  72. ->if($adapter->md5(uniqid()))
  73. ->then
  74. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  75. ->sizeOf($adapter->getCalls())->isEqualTo(1)
  76. ->object($this->testedInstance->reset())->isTestedInstance
  77. ->object($this->testedInstance->getAdapter())->isIdenticalTo($adapter)
  78. ->sizeOf($adapter->getCalls())->isZero()
  79. ;
  80. }
  81. public function testCall()
  82. {
  83. $this
  84. ->mockGenerator->orphanize('asserterFail')
  85. ->if($this->newTestedInstance(new \mock\mageekguy\atoum\asserter\generator()))
  86. ->then
  87. ->exception(function($test) { $test->testedInstance->call(uniqid()); })
  88. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  89. ->hasMessage('Adapter is undefined')
  90. ->if($this->testedInstance->setWith($adapter = new test\adapter()))
  91. ->then
  92. ->object($this->testedInstance->call($function = uniqid()))->isTestedInstance
  93. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function))
  94. ->if($this->testedInstance->withArguments())
  95. ->then
  96. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array()))
  97. ->object($this->testedInstance->disableEvaluationChecking()->call($function = uniqid()))->isTestedInstance
  98. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function))
  99. ;
  100. }
  101. public function testWithArguments()
  102. {
  103. $this
  104. ->mockGenerator->orphanize('asserterFail')
  105. ->if($this->newTestedInstance(new \mock\mageekguy\atoum\asserter\generator()))
  106. ->then
  107. ->exception(function($test) { $test->testedInstance->withArguments(uniqid()); })
  108. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  109. ->hasMessage('Adapter is undefined')
  110. ->if($this->testedInstance->setWith($adapter = new test\adapter()))
  111. ->then
  112. ->exception(function($test) { $test->testedInstance->withArguments(uniqid()); })
  113. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  114. ->hasMessage('Call is undefined')
  115. ->if($this->testedInstance->call($function = uniqid()))
  116. ->then
  117. ->object($this->testedInstance->withArguments())->isTestedInstance
  118. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array()))
  119. ->object($this->testedInstance->withArguments($arg1 = uniqid()))->isTestedInstance
  120. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array($arg1)))
  121. ->object($this->testedInstance->disableEvaluationChecking()->withArguments($arg1 = uniqid(), $arg2 = uniqid()))->isTestedInstance
  122. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array($arg1, $arg2)))
  123. ;
  124. }
  125. public function testWithAnyArguments()
  126. {
  127. $this
  128. ->mockGenerator->orphanize('asserterFail')
  129. ->if($this->newTestedInstance(new \mock\mageekguy\atoum\asserter\generator()))
  130. ->then
  131. ->exception(function($test) { $test->testedInstance->withArguments(uniqid()); })
  132. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  133. ->hasMessage('Adapter is undefined')
  134. ->if($this->testedInstance->setWith($adapter = new test\adapter()))
  135. ->then
  136. ->exception(function($test) { $test->testedInstance->withArguments(uniqid()); })
  137. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  138. ->hasMessage('Call is undefined')
  139. ->if($this->testedInstance->call($function = uniqid()))
  140. ->then
  141. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function))
  142. ->object($this->testedInstance->withAnyArguments())->isTestedInstance
  143. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function))
  144. ->if($this->testedInstance->disableEvaluationChecking()->withArguments($arg = uniqid()))
  145. ->then
  146. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array($arg)))
  147. ->object($this->testedInstance->withAnyArguments())->isTestedInstance
  148. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function))
  149. ;
  150. }
  151. public function testWithoutAnyArgument()
  152. {
  153. $this
  154. ->mockGenerator->orphanize('asserterFail')
  155. ->if($this->newTestedInstance(new \mock\mageekguy\atoum\asserter\generator()))
  156. ->then
  157. ->exception(function($test) { $test->testedInstance->withoutAnyArgument(); })
  158. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  159. ->hasMessage('Adapter is undefined')
  160. ->if($this->testedInstance->setWith($adapter = new test\adapter()))
  161. ->then
  162. ->exception(function($test) { $test->testedInstance->withoutAnyArgument(); })
  163. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  164. ->hasMessage('Call is undefined')
  165. ->if($this->testedInstance->call($function = uniqid()))
  166. ->then
  167. ->object($this->testedInstance->disableEvaluationChecking()->withoutAnyArgument())->isTestedInstance
  168. ->object($this->testedInstance->getCall())->isEqualTo(new test\adapter\call($function, array()))
  169. ;
  170. }
  171. public function testOnce()
  172. {
  173. $this
  174. ->if($this->newTestedInstance($generator = new asserter\generator()))
  175. ->then
  176. ->exception(function($test) { $test->testedInstance->once(); })
  177. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  178. ->hasMessage('Adapter is undefined')
  179. ->exception(function($test) { $test->testedInstance->once; })
  180. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  181. ->hasMessage('Adapter is undefined')
  182. ->exception(function($test) { $test->testedInstance->oNCE; })
  183. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  184. ->hasMessage('Adapter is undefined')
  185. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  186. ->then
  187. ->exception(function($test) { $test->testedInstance->once(); })
  188. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  189. ->hasMessage('Call is undefined')
  190. ->exception(function($test) { $test->testedInstance->once; })
  191. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  192. ->hasMessage('Call is undefined')
  193. ->exception(function($test) { $test->testedInstance->oNCE; })
  194. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  195. ->hasMessage('Call is undefined')
  196. ->if(
  197. $this->testedInstance
  198. ->call(uniqid())
  199. ->setCall($call = new \mock\atoum\test\adapter\call())
  200. ->setLocale($locale = new \mock\atoum\locale()),
  201. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  202. $this->calling($calls)->count = 0,
  203. $this->calling($call)->__toString = $callAsString = uniqid(),
  204. $this->calling($locale)->__ = $notCalled = uniqid()
  205. )
  206. ->then
  207. ->exception(function($test) { $test->testedInstance->once(); })
  208. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  209. ->hasMessage($notCalled)
  210. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 1)->once
  211. ->exception(function($test) { $test->testedInstance->once; })
  212. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  213. ->hasMessage($notCalled)
  214. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 1)->twice
  215. ->exception(function($test) { $test->testedInstance->OncE; })
  216. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  217. ->hasMessage($notCalled)
  218. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 1)->thrice
  219. ->exception(function($test) use (& $failMessage) { $test->testedInstance->once($failMessage = uniqid()); })
  220. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  221. ->hasMessage($failMessage)
  222. ->if($this->calling($calls)->count = 1)
  223. ->then
  224. ->object($this->testedInstance->once())->isTestedInstance
  225. ->object($this->testedInstance->once)->isTestedInstance
  226. ->object($this->testedInstance->oNCE)->isTestedInstance
  227. ->if(
  228. $this->calling($calls)->count = $count = rand(2, PHP_INT_MAX),
  229. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  230. $this->calling($callsEqualTo)->count = rand(1, PHP_INT_MAX),
  231. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  232. )
  233. ->then
  234. ->exception(function($test) { $test->testedInstance->once(); })
  235. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  236. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  237. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 1)->once
  238. ->exception(function($test) { $test->testedInstance->once; })
  239. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  240. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  241. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 1)->twice
  242. ->exception(function($test) { $test->testedInstance->OncE; })
  243. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  244. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  245. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 1)->thrice
  246. ->exception(function($test) use (& $failMessage) { $test->testedInstance->once($failMessage = uniqid()); })
  247. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  248. ->hasMessage($failMessage)
  249. ;
  250. }
  251. public function testTwice()
  252. {
  253. $this
  254. ->if($this->newTestedInstance($generator = new asserter\generator()))
  255. ->then
  256. ->exception(function($test) { $test->testedInstance->twice(); })
  257. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  258. ->hasMessage('Adapter is undefined')
  259. ->exception(function($test) { $test->testedInstance->twice; })
  260. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  261. ->hasMessage('Adapter is undefined')
  262. ->exception(function($test) { $test->testedInstance->TWICe; })
  263. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  264. ->hasMessage('Adapter is undefined')
  265. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  266. ->then
  267. ->exception(function($test) { $test->testedInstance->twice(); })
  268. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  269. ->hasMessage('Call is undefined')
  270. ->exception(function($test) { $test->testedInstance->twice; })
  271. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  272. ->hasMessage('Call is undefined')
  273. ->exception(function($test) { $test->testedInstance->twICE; })
  274. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  275. ->hasMessage('Call is undefined')
  276. ->if(
  277. $this->testedInstance
  278. ->call(uniqid())
  279. ->setCall($call = new \mock\atoum\test\adapter\call())
  280. ->setLocale($locale = new \mock\atoum\locale()),
  281. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  282. $this->calling($calls)->count = 0,
  283. $this->calling($call)->__toString = $callAsString = uniqid(),
  284. $this->calling($locale)->__ = $notCalled = uniqid()
  285. )
  286. ->then
  287. ->exception(function($test) { $test->testedInstance->twice(); })
  288. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  289. ->hasMessage($notCalled)
  290. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 2)->once
  291. ->exception(function($test) { $test->testedInstance->twice; })
  292. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  293. ->hasMessage($notCalled)
  294. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 2)->twice
  295. ->exception(function($test) { $test->testedInstance->TWICe; })
  296. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  297. ->hasMessage($notCalled)
  298. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 2)->thrice
  299. ->exception(function($test) use (& $failMessage) { $test->testedInstance->twice($failMessage = uniqid()); })
  300. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  301. ->hasMessage($failMessage)
  302. ->if(
  303. $this->calling($calls)->count = 1,
  304. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  305. $this->calling($callsEqualTo)->count = 1,
  306. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  307. )
  308. ->then
  309. ->exception(function($test) { $test->testedInstance->twice(); })
  310. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  311. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  312. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 1, $callAsString, 1, 2)->once
  313. ->exception(function($test) { $test->testedInstance->twice; })
  314. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  315. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  316. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 1, $callAsString, 1, 2)->twice
  317. ->exception(function($test) { $test->testedInstance->TWICe; })
  318. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  319. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  320. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 1, $callAsString, 1, 2)->thrice
  321. ->exception(function($test) use (& $failMessage) { $test->testedInstance->twice($failMessage = uniqid()); })
  322. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  323. ->hasMessage($failMessage)
  324. ->if($this->calling($calls)->count = 2)
  325. ->then
  326. ->object($this->testedInstance->twice())->isTestedInstance
  327. ->object($this->testedInstance->twice)->isTestedInstance
  328. ->object($this->testedInstance->TWICe)->isTestedInstance
  329. ->if(
  330. $this->calling($calls)->count = $count = rand(3, PHP_INT_MAX),
  331. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  332. $this->calling($callsEqualTo)->count = rand(1, PHP_INT_MAX),
  333. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  334. )
  335. ->then
  336. ->exception(function($test) { $test->testedInstance->twice(); })
  337. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  338. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  339. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 2)->once
  340. ->exception(function($test) { $test->testedInstance->twice; })
  341. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  342. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  343. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 2)->twice
  344. ->exception(function($test) { $test->testedInstance->TWICe; })
  345. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  346. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  347. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 2)->thrice
  348. ->exception(function($test) use (& $failMessage) { $test->testedInstance->once($failMessage = uniqid()); })
  349. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  350. ->hasMessage($failMessage)
  351. ;
  352. }
  353. public function testThrice()
  354. {
  355. $this
  356. ->if($this->newTestedInstance($generator = new asserter\generator()))
  357. ->then
  358. ->exception(function($test) { $test->testedInstance->thrice(); })
  359. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  360. ->hasMessage('Adapter is undefined')
  361. ->exception(function($test) { $test->testedInstance->thrice; })
  362. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  363. ->hasMessage('Adapter is undefined')
  364. ->exception(function($test) { $test->testedInstance->tHRICe; })
  365. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  366. ->hasMessage('Adapter is undefined')
  367. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  368. ->then
  369. ->exception(function($test) { $test->testedInstance->thrice(); })
  370. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  371. ->hasMessage('Call is undefined')
  372. ->exception(function($test) { $test->testedInstance->thrice; })
  373. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  374. ->hasMessage('Call is undefined')
  375. ->exception(function($test) { $test->testedInstance->thRICE; })
  376. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  377. ->hasMessage('Call is undefined')
  378. ->if(
  379. $this->testedInstance
  380. ->call(uniqid())
  381. ->setCall($call = new \mock\atoum\test\adapter\call())
  382. ->setLocale($locale = new \mock\atoum\locale()),
  383. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  384. $this->calling($calls)->count = 0,
  385. $this->calling($call)->__toString = $callAsString = uniqid(),
  386. $this->calling($locale)->__ = $notCalled = uniqid()
  387. )
  388. ->then
  389. ->exception(function($test) { $test->testedInstance->thrice(); })
  390. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  391. ->hasMessage($notCalled)
  392. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 3)->once
  393. ->exception(function($test) { $test->testedInstance->thrice; })
  394. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  395. ->hasMessage($notCalled)
  396. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 3)->twice
  397. ->exception(function($test) { $test->testedInstance->THRIce; })
  398. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  399. ->hasMessage($notCalled)
  400. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, 3)->thrice
  401. ->exception(function($test) use (& $failMessage) { $test->testedInstance->thrice($failMessage = uniqid()); })
  402. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  403. ->hasMessage($failMessage)
  404. ->if(
  405. $this->calling($calls)->count = $count = rand(1, 2),
  406. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  407. $this->calling($callsEqualTo)->count = $count,
  408. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  409. )
  410. ->then
  411. ->exception(function($test) { $test->testedInstance->thrice(); })
  412. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  413. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  414. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->once
  415. ->exception(function($test) { $test->testedInstance->thrice; })
  416. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  417. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  418. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->twice
  419. ->exception(function($test) { $test->testedInstance->tHRICe; })
  420. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  421. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  422. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->thrice
  423. ->exception(function($test) use (& $failMessage) { $test->testedInstance->thrice($failMessage = uniqid()); })
  424. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  425. ->hasMessage($failMessage)
  426. ->if($this->calling($calls)->count = 3)
  427. ->then
  428. ->object($this->testedInstance->thrice())->isTestedInstance
  429. ->object($this->testedInstance->thrice)->isTestedInstance
  430. ->object($this->testedInstance->THRIcE)->isTestedInstance
  431. ->if(
  432. $this->calling($calls)->count = $count = rand(3, PHP_INT_MAX),
  433. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  434. $this->calling($callsEqualTo)->count = $count,
  435. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  436. )
  437. ->then
  438. ->exception(function($test) { $test->testedInstance->thrice(); })
  439. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  440. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  441. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->once
  442. ->exception(function($test) { $test->testedInstance->thrice; })
  443. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  444. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  445. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->twice
  446. ->exception(function($test) { $test->testedInstance->tHRICe; })
  447. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  448. ->hasMessage($notCalled . PHP_EOL . $callsEqualTo)
  449. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 3)->thrice
  450. ->exception(function($test) use (& $failMessage) { $test->testedInstance->thrice($failMessage = uniqid()); })
  451. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  452. ->hasMessage($failMessage)
  453. ;
  454. }
  455. public function testAtLeastOnce()
  456. {
  457. $this
  458. ->if($this->newTestedInstance($generator = new asserter\generator()))
  459. ->then
  460. ->exception(function($test) { $test->testedInstance->atLeastOnce(); })
  461. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  462. ->hasMessage('Adapter is undefined')
  463. ->exception(function($test) { $test->testedInstance->atLeastOnce; })
  464. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  465. ->hasMessage('Adapter is undefined')
  466. ->exception(function($test) { $test->testedInstance->atLEASToNce; })
  467. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  468. ->hasMessage('Adapter is undefined')
  469. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  470. ->then
  471. ->exception(function($test) { $test->testedInstance->atLeastOnce(); })
  472. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  473. ->hasMessage('Call is undefined')
  474. ->exception(function($test) { $test->testedInstance->atLeastOnce; })
  475. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  476. ->hasMessage('Call is undefined')
  477. ->exception(function($test) { $test->testedInstance->atLeASTonce; })
  478. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  479. ->hasMessage('Call is undefined')
  480. ->if(
  481. $this->testedInstance
  482. ->call(uniqid())
  483. ->setCall($call = new \mock\atoum\test\adapter\call())
  484. ->setLocale($locale = new \mock\atoum\locale()),
  485. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  486. $this->calling($calls)->count = 0,
  487. $this->calling($call)->__toString = $callAsString = uniqid(),
  488. $this->calling($locale)->_ = $notCalled = uniqid()
  489. )
  490. ->then
  491. ->exception(function($test) { $test->testedInstance->atLeastOnce(); })
  492. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  493. ->hasMessage($notCalled)
  494. ->mock($locale)->call('_')->withArguments('%s is called 0 time', $callAsString)->once
  495. ->exception(function($test) { $test->testedInstance->atLeastOnce; })
  496. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  497. ->hasMessage($notCalled)
  498. ->mock($locale)->call('_')->withArguments('%s is called 0 time', $callAsString)->twice
  499. ->exception(function($test) { $test->testedInstance->atLEASToNCE; })
  500. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  501. ->hasMessage($notCalled)
  502. ->mock($locale)->call('_')->withArguments('%s is called 0 time', $callAsString)->thrice
  503. ->exception(function($test) use (& $failMessage) { $test->testedInstance->atLeastOnce($failMessage = uniqid()); })
  504. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  505. ->hasMessage($failMessage)
  506. ->if($this->calling($calls)->count = rand(1, PHP_INT_MAX))
  507. ->then
  508. ->object($this->testedInstance->atLeastOnce())->isTestedInstance
  509. ->object($this->testedInstance->atLeastOnce)->isTestedInstance
  510. ->object($this->testedInstance->atLEASToNCe)->isTestedInstance
  511. ;
  512. }
  513. public function testExactly()
  514. {
  515. $this
  516. ->if($this->newTestedInstance($generator = new asserter\generator()))
  517. ->then
  518. ->exception(function($test) { $test->testedInstance->exactly(2); })
  519. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  520. ->hasMessage('Adapter is undefined')
  521. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  522. ->then
  523. ->exception(function($test) { $test->testedInstance->exactly(2); })
  524. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  525. ->hasMessage('Call is undefined')
  526. ->if(
  527. $this->testedInstance
  528. ->call(uniqid())
  529. ->setCall($call = new \mock\atoum\test\adapter\call())
  530. ->setLocale($locale = new \mock\atoum\locale()),
  531. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  532. $this->calling($calls)->count = 0,
  533. $this->calling($call)->__toString = $callAsString = uniqid(),
  534. $this->calling($locale)->__ = $notCalled = uniqid()
  535. )
  536. ->then
  537. ->exception(function($test) use (& $callNumber) { $test->testedInstance->exactly($callNumber = rand(1, PHP_INT_MAX)); })
  538. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  539. ->hasMessage($notCalled)
  540. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', 0, $callAsString, 0, $callNumber)->once
  541. ->exception(function($test) use (& $callNumber, & $failMessage) { $test->testedInstance->exactly($callNumber = rand(1, PHP_INT_MAX), $failMessage = uniqid()); })
  542. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  543. ->hasMessage($failMessage)
  544. ->object($this->testedInstance->exactly(0))->isTestedInstance
  545. ->if(
  546. $this->calling($calls)->count = $count = rand(1, PHP_INT_MAX),
  547. $this->calling($adapter)->getCallsEqualTo = $callsEqualTo = new \mock\atoum\test\adapter\calls(),
  548. $this->calling($callsEqualTo)->count = $count,
  549. $this->calling($callsEqualTo)->__toString = $callsEqualToAsString = uniqid()
  550. )
  551. ->then
  552. ->exception(function($test) { $test->testedInstance->exactly(0); })
  553. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  554. ->hasMessage($notCalled . PHP_EOL . $callsEqualToAsString)
  555. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 0)->once
  556. ->exception(function($test) use (& $failMessage) { $test->testedInstance->exactly(0, $failMessage = uniqid()); })
  557. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  558. ->hasMessage($failMessage)
  559. ->object($this->testedInstance->exactly($count))->isTestedInstance
  560. ;
  561. }
  562. public function testNever()
  563. {
  564. $this
  565. ->if($this->newTestedInstance($generator = new asserter\generator()))
  566. ->then
  567. ->exception(function($test) { $test->testedInstance->never(); })
  568. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  569. ->hasMessage('Adapter is undefined')
  570. ->exception(function($test) { $test->testedInstance->never; })
  571. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  572. ->hasMessage('Adapter is undefined')
  573. ->if($this->testedInstance->setWith($adapter = new \mock\atoum\test\adapter()))
  574. ->then
  575. ->exception(function($test) { $test->testedInstance->never(); })
  576. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  577. ->hasMessage('Call is undefined')
  578. ->exception(function($test) { $test->testedInstance->never; })
  579. ->isInstanceOf('mageekguy\atoum\asserters\adapter\exceptions\logic')
  580. ->hasMessage('Call is undefined')
  581. ->if(
  582. $this->testedInstance
  583. ->call(uniqid())
  584. ->setCall($call = new \mock\atoum\test\adapter\call())
  585. ->setLocale($locale = new \mock\atoum\locale()),
  586. $this->calling($adapter)->getCalls = $calls = new \mock\atoum\test\adapter\calls(),
  587. $this->calling($calls)->count = $count = rand(1, PHP_INT_MAX),
  588. $this->calling($call)->__toString = $callAsString = uniqid(),
  589. $this->calling($locale)->__ = $wasCalled = uniqid()
  590. )
  591. ->then
  592. ->exception(function($test) use (& $callNumber) { $test->testedInstance->never(); })
  593. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  594. ->hasMessage($wasCalled)
  595. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 0)->once
  596. ->exception(function($test) use (& $callNumber) { $test->testedInstance->never; })
  597. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  598. ->hasMessage($wasCalled)
  599. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 0)->twice
  600. ->exception(function($test) use (& $callNumber) { $test->testedInstance->NEvEr; })
  601. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  602. ->hasMessage($wasCalled)
  603. ->mock($locale)->call('__')->withArguments('%s is called %d time instead of %d', '%s is called %d times instead of %d', $count, $callAsString, $count, 0)->thrice
  604. ->exception(function($test) use (& $failMessage) { $test->testedInstance->never($failMessage = uniqid()); })
  605. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  606. ->hasMessage($failMessage)
  607. ->if($this->calling($calls)->count = 0)
  608. ->then
  609. ->object($this->testedInstance->never())->isTestedInstance
  610. ->object($this->testedInstance->never)->isTestedInstance
  611. ->object($this->testedInstance->nEVER)->isTestedInstance
  612. ;
  613. }
  614. public function testBefore()
  615. {
  616. $this
  617. ->given(
  618. $asserter = $this->newTestedInstance($generator = new atoum\asserter\generator()),
  619. $adapter = new test\adapter(),
  620. $adapter->shouldBeCallBefore = uniqid(),
  621. $asserter->setWith($adapter),
  622. $beforeAsserter = $this->newTestedInstance(new atoum\asserter\generator()),
  623. $beforeAdapter = new test\adapter(),
  624. $beforeAdapter->wasCalledAfter = uniqid(),
  625. $beforeAsserter->setWith($beforeAdapter),
  626. $asserter->call('shouldBeCallBefore')->before($beforeAsserter->call('wasCalledAfter'))
  627. )
  628. ->if(
  629. $adapter->shouldBeCallBefore(),
  630. $beforeAdapter->wasCalledAfter()
  631. )
  632. ->then
  633. ->object($asserter->once())->isIdenticalTo($asserter)
  634. ->if(
  635. $adapter->resetCalls(),
  636. $beforeAdapter->resetCalls(),
  637. $beforeAdapter->wasCalledAfter(),
  638. $adapter->shouldBeCallBefore()
  639. )
  640. ->then
  641. ->exception(function() use ($asserter) { $asserter->once(); })
  642. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  643. ->hasMessage(sprintf($generator->getLocale()->_('%s is not called before %s'), $asserter->getCall(), $beforeAsserter->getCall()))
  644. ->if(
  645. $adapter->resetCalls(),
  646. $beforeAdapter->resetCalls(),
  647. $beforeAdapter->wasCalledAfter(),
  648. $beforeAdapter->wasCalledAfter(),
  649. $adapter->shouldBeCallBefore()
  650. )
  651. ->then
  652. ->exception(function() use ($asserter) { $asserter->once(); })
  653. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  654. ->hasMessage(sprintf($generator->getLocale()->_('%s is not called before %s'), $asserter->getCall(), $beforeAsserter->getCall()))
  655. ->if(
  656. $adapter->resetCalls(),
  657. $beforeAdapter->resetCalls(),
  658. $adapter->shouldBeCallBefore(),
  659. $beforeAdapter->wasCalledAfter(),
  660. $beforeAdapter->wasCalledAfter(),
  661. $adapter->shouldBeCallBefore()
  662. )
  663. ->then
  664. ->object($asserter->once())->isIdenticalTo($asserter)
  665. ->exception(function() use ($asserter) { $asserter->twice(); })
  666. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  667. ->hasMessage(sprintf($generator->getLocale()->_('%s is called 1 time instead of 2 before %s'), $asserter->getCall(), $beforeAsserter->getCall()))
  668. ->if(
  669. $adapter->resetCalls(),
  670. $beforeAdapter->resetCalls(),
  671. $adapter->shouldBeCallBefore(),
  672. $beforeAdapter->wasCalledAfter(),
  673. $adapter->shouldBeCallBefore(),
  674. $beforeAdapter->wasCalledAfter()
  675. )
  676. ->then
  677. ->exception(function() use ($asserter) { $asserter->once(); })
  678. ->if(
  679. $adapter->resetCalls(),
  680. $beforeAdapter->resetCalls(),
  681. $adapter->shouldBeCallBefore(),
  682. $beforeAdapter->wasCalledAfter(),
  683. $beforeAdapter->wasCalledAfter()
  684. )
  685. ->then
  686. ->object($asserter->once())->isIdenticalTo($asserter)
  687. ->if(
  688. $adapter->resetCalls(),
  689. $beforeAdapter->resetCalls(),
  690. $adapter->shouldBeCallBefore(),
  691. $adapter->shouldBeCallBefore(),
  692. $beforeAdapter->wasCalledAfter(),
  693. $beforeAdapter->wasCalledAfter()
  694. )
  695. ->then
  696. ->object($asserter->twice())->isIdenticalTo($asserter)
  697. ;
  698. }
  699. public function testAfter()
  700. {
  701. $this
  702. ->given(
  703. $asserter = $this->newTestedInstance($generator = new atoum\asserter\generator()),
  704. $adapter = new test\adapter(),
  705. $adapter->shouldBeCallafter = uniqid(),
  706. $asserter->setWith($adapter),
  707. $afterAsserter = $this->newTestedInstance(new atoum\asserter\generator()),
  708. $afterAdapter = new test\adapter(),
  709. $afterAdapter->wasCalledBefore = uniqid(),
  710. $afterAsserter->setWith($afterAdapter),
  711. $asserter->call('shouldBeCallAfter')->after($afterAsserter->call('wasCalledBefore')),
  712. $afterAdapter->wasCalledBefore(),
  713. $adapter->shouldBeCallAfter()
  714. )
  715. ->then
  716. ->object($asserter->once())->isIdenticalTo($asserter)
  717. ->if(
  718. $adapter->resetCalls(),
  719. $afterAdapter->resetCalls(),
  720. $adapter->shouldBeCallAfter(),
  721. $afterAdapter->wasCalledBefore()
  722. )
  723. ->then
  724. ->exception(function() use ($asserter) { $asserter->once(); })
  725. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  726. ->hasMessage(sprintf($generator->getLocale()->_('%s is not called after %s'), $asserter->getCall(), $afterAsserter->getCall()))
  727. ->if(
  728. $adapter->resetCalls(),
  729. $afterAdapter->resetCalls(),
  730. $adapter->shouldBeCallAfter(),
  731. $adapter->shouldBeCallAfter(),
  732. $afterAdapter->wasCalledBefore()
  733. )
  734. ->then
  735. ->exception(function() use ($asserter) { $asserter->once(); })
  736. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  737. ->hasMessage(sprintf($generator->getLocale()->_('%s is not called after %s'), $asserter->getCall(), $afterAsserter->getCall()))
  738. ->if(
  739. $adapter->resetCalls(),
  740. $afterAdapter->resetCalls(),
  741. $adapter->shouldBeCallAfter(),
  742. $afterAdapter->wasCalledBefore(),
  743. $adapter->shouldBeCallAfter()
  744. )
  745. ->then
  746. ->object($asserter->once())->isIdenticalTo($asserter)
  747. ->exception(function() use ($asserter) { $asserter->twice(); })
  748. ->isInstanceOf('mageekguy\atoum\asserter\exception')
  749. ->hasMessage(sprintf($generator->getLocale()->_('%s is called 1 time instead of 2 after %s'), $asserter->getCall(), $afterAsserter->getCall()))
  750. ->if(
  751. $adapter->resetCalls(),
  752. $afterAdapter->resetCalls(),
  753. $afterAdapter->wasCalledBefore(),
  754. $adapter->shouldBeCallAfter(),
  755. $afterAdapter->wasCalledBefore(),
  756. $adapter->shouldBeCallAfter()
  757. )
  758. ->then
  759. ->object($asserter->twice())->isIdenticalTo($asserter)
  760. ->if(
  761. $adapter->resetCalls(),
  762. $afterAdapter->resetCalls(),
  763. $afterAdapter->wasCalledBefore(),
  764. $adapter->shouldBeCallAfter(),
  765. $afterAdapter->wasCalledBefore()
  766. )
  767. ->then
  768. ->object($asserter->once())->isIdenticalTo($asserter)
  769. ->if(
  770. $adapter->resetCalls(),
  771. $afterAdapter->resetCalls(),
  772. $afterAdapter->wasCalledBefore(),
  773. $adapter->shouldBeCallAfter(),
  774. $afterAdapter->wasCalledBefore(),
  775. $adapter->shouldBeCallAfter()
  776. )
  777. ->then
  778. ->object($asserter->twice())->isIdenticalTo($asserter)
  779. ->if(
  780. $adapter->resetCalls(),
  781. $afterAdapter->resetCalls(),
  782. $afterAdapter->wasCalledBefore(),
  783. $adapter->shouldBeCallAfter(),
  784. $adapter->shouldBeCallAfter(),
  785. $afterAdapter->wasCalledBefore()
  786. )
  787. ->then
  788. ->object($asserter->twice())->isIdenticalTo($asserter)
  789. ->if(
  790. $adapter->resetCalls(),
  791. $afterAdapter->resetCalls(),
  792. $afterAdapter->wasCalledBefore(),
  793. $afterAdapter->wasCalledBefore(),
  794. $adapter->shouldBeCallAfter()
  795. )
  796. ->then
  797. ->object($asserter->once())->isIdenticalTo($asserter)
  798. ;
  799. }
  800. }