PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/units/classes/test/adapter.php

http://github.com/mageekguy/atoum
PHP | 470 lines | 389 code | 27 blank | 54 comment | 0 complexity | 4aac258b09db1a691d97c237a395ac7c MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\test;
  3. use
  4. mageekguy\atoum\test,
  5. mageekguy\atoum\test\adapter\call,
  6. mageekguy\atoum\test\adapter as testedClass
  7. ;
  8. require_once __DIR__ . '/../../runner.php';
  9. class adapter extends test
  10. {
  11. public function testClass()
  12. {
  13. $this->testedClass->extends('mageekguy\atoum\adapter');
  14. }
  15. public function test__construct()
  16. {
  17. $this
  18. ->if($adapter = new testedClass())
  19. ->and($storage = new test\adapter\storage())
  20. ->then
  21. ->array($adapter->getInvokers())->isEmpty()
  22. ->object($adapter->getCalls())->isEqualTo(new test\adapter\calls())
  23. ->boolean($storage->contains($adapter))->isFalse()
  24. ->if(testedClass::setStorage($storage))
  25. ->and($otherAdapter = new testedClass())
  26. ->then
  27. ->array($otherAdapter->getInvokers())->isEmpty()
  28. ->object($otherAdapter->getCalls())->isEqualTo(new test\adapter\calls())
  29. ->boolean($storage->contains($adapter))->isFalse()
  30. ->boolean($storage->contains($otherAdapter))->isTrue()
  31. ;
  32. }
  33. public function test__clone()
  34. {
  35. $this
  36. ->if($adapter = new testedClass())
  37. ->and($storage = new test\adapter\storage())
  38. ->and($clone = clone $adapter)
  39. ->then
  40. ->object($clone->getCalls())->isCloneOf($adapter->getCalls())
  41. ->boolean($storage->contains($clone))->isFalse()
  42. ->if(testedClass::setStorage($storage))
  43. ->and($otherClone = clone $adapter)
  44. ->then
  45. ->object($otherClone->getCalls())->isCloneOf($adapter->getCalls())
  46. ->boolean($storage->contains($clone))->isFalse()
  47. ->boolean($storage->contains($otherClone))->isTrue()
  48. ;
  49. }
  50. public function test__set()
  51. {
  52. $this
  53. ->if($adapter = new testedClass())
  54. ->and($adapter->md5 = $closure = function() {})
  55. ->then
  56. ->object($adapter->md5->getClosure())->isIdenticalTo($closure)
  57. ->if($adapter->md5 = $return = uniqid())
  58. ->then
  59. ->object($adapter->md5)->isInstanceOf('mageekguy\atoum\test\adapter\invoker')
  60. ->object($adapter->MD5)->isInstanceOf('mageekguy\atoum\test\adapter\invoker')
  61. ->string($adapter->invoke('md5'))->isEqualTo($return)
  62. ->string($adapter->invoke('MD5'))->isEqualTo($return)
  63. ->if($adapter->MD5 = $return = uniqid())
  64. ->then
  65. ->object($adapter->md5)->isInstanceOf('mageekguy\atoum\test\adapter\invoker')
  66. ->object($adapter->MD5)->isInstanceOf('mageekguy\atoum\test\adapter\invoker')
  67. ->string($adapter->invoke('md5'))->isEqualTo($return)
  68. ->string($adapter->invoke('MD5'))->isEqualTo($return)
  69. ;
  70. }
  71. public function test__get()
  72. {
  73. $this
  74. ->if($adapter = new testedClass())
  75. ->and($adapter->md5 = $closure = function() {})
  76. ->then
  77. ->object($adapter->md5->getClosure())->isIdenticalTo($closure)
  78. ->object($adapter->MD5->getClosure())->isIdenticalTo($closure)
  79. ->if($adapter->md5 = uniqid())
  80. ->then
  81. ->object($adapter->md5->getClosure())->isInstanceOf('closure')
  82. ->object($adapter->MD5->getClosure())->isInstanceOf('closure')
  83. ;
  84. }
  85. public function test__isset()
  86. {
  87. $this
  88. ->if($adapter = new testedClass())
  89. ->then
  90. ->boolean(isset($adapter->md5))->isFalse()
  91. ->if($adapter->{$function = strtolower(uniqid())} = function() {})
  92. ->then
  93. ->boolean(isset($adapter->{$function}))->isTrue()
  94. ->boolean(isset($adapter->{strtoupper($function)}))->isTrue()
  95. ->if($adapter->{$function = strtoupper(uniqid())} = function() {})
  96. ->then
  97. ->boolean(isset($adapter->{strtolower($function)}))->isTrue()
  98. ->boolean(isset($adapter->{$function}))->isTrue()
  99. ->if($adapter->{$function = strtolower(uniqid())} = uniqid())
  100. ->then
  101. ->boolean(isset($adapter->{$function}))->isTrue()
  102. ->boolean(isset($adapter->{strtoupper($function)}))->isTrue()
  103. ->if($adapter->{$function = strtoupper(uniqid())} = uniqid())
  104. ->then
  105. ->boolean(isset($adapter->{$function}))->isTrue()
  106. ->boolean(isset($adapter->{strtolower($function)}))->isTrue()
  107. ->if($adapter->{$function = uniqid()}[2] = uniqid())
  108. ->then
  109. ->boolean(isset($adapter->{$function}))->isFalse()
  110. ->boolean(isset($adapter->{$function}[0]))->isFalse()
  111. ->boolean(isset($adapter->{$function}[1]))->isFalse()
  112. ->boolean(isset($adapter->{$function}[2]))->isTrue()
  113. ->boolean(isset($adapter->{$function}[3]))->isFalse()
  114. ;
  115. }
  116. public function test__unset()
  117. {
  118. $this
  119. ->if($adapter = new testedClass())
  120. ->then
  121. ->array($adapter->getInvokers())->isEmpty()
  122. ->array($adapter->getCalls()->toArray())->isEmpty()
  123. ->when(function() use ($adapter) { unset($adapter->md5); })
  124. ->array($adapter->getInvokers())->isEmpty()
  125. ->array($adapter->getCalls()->toArray())->isEmpty()
  126. ->when(function() use ($adapter) { unset($adapter->MD5); })
  127. ->array($adapter->getInvokers())->isEmpty()
  128. ->array($adapter->getCalls()->toArray())->isEmpty()
  129. ->when(function() use ($adapter) { $adapter->md5 = uniqid(); $adapter->md5(uniqid()); })
  130. ->array($adapter->getInvokers())->isNotEmpty()
  131. ->array($adapter->getCalls()->toArray())->isNotEmpty()
  132. ->when(function() use ($adapter) { unset($adapter->{uniqid()}); })
  133. ->array($adapter->getInvokers())->isNotEmpty()
  134. ->array($adapter->getCalls()->toArray())->isNotEmpty()
  135. ->when(function() use ($adapter) { unset($adapter->md5); })
  136. ->array($adapter->getInvokers())->isEmpty()
  137. ->array($adapter->getCalls()->toArray())->isEmpty()
  138. ->when(function() use ($adapter) { $adapter->MD5 = uniqid(); $adapter->MD5(uniqid()); })
  139. ->array($adapter->getInvokers())->isNotEmpty()
  140. ->array($adapter->getCalls()->toArray())->isNotEmpty()
  141. ->when(function() use ($adapter) { unset($adapter->{uniqid()}); })
  142. ->array($adapter->getInvokers())->isNotEmpty()
  143. ->array($adapter->getCalls()->toArray())->isNotEmpty()
  144. ->when(function() use ($adapter) { unset($adapter->MD5); })
  145. ->array($adapter->getInvokers())->isEmpty()
  146. ->array($adapter->getCalls()->toArray())->isEmpty()
  147. ;
  148. }
  149. public function test__call()
  150. {
  151. $this
  152. /*
  153. ->if($adapter = new testedClass())
  154. ->then
  155. ->string($adapter->md5($hash = uniqid()))->isEqualTo(md5($hash))
  156. ->string($adapter->MD5($hash = uniqid()))->isEqualTo(md5($hash))
  157. ->if($adapter->md5 = $md5 = uniqid())
  158. ->then
  159. ->string($adapter->md5($hash))->isEqualTo($md5)
  160. ->string($adapter->MD5($hash))->isEqualTo($md5)
  161. ->if($adapter->md5 = $md5 = uniqid())
  162. ->then
  163. ->string($adapter->md5($hash))->isEqualTo($md5)
  164. ->string($adapter->MD5($hash))->isEqualTo($md5)
  165. ->exception(function() use ($adapter) {
  166. $adapter->require(uniqid());
  167. }
  168. )
  169. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  170. ->hasMessage('Function \'require()\' is not invokable by an adapter')
  171. ->exception(function() use ($adapter) {
  172. $adapter->REQUIRE(uNiqid());
  173. }
  174. )
  175. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  176. ->hasMessage('Function \'REQUIRE()\' is not invokable by an adapter')
  177. ->if($adapter->md5 = 0)
  178. ->and($adapter->md5[1] = 1)
  179. ->and($adapter->md5[2] = 2)
  180. ->and($adapter->resetCalls())
  181. ->then
  182. ->integer($adapter->md5())->isEqualTo(1)
  183. ->integer($adapter->md5())->isEqualTo(2)
  184. ->integer($adapter->md5())->isEqualTo(0)
  185. ->if($adapter->resetCalls())
  186. ->then
  187. ->integer($adapter->MD5())->isEqualTo(1)
  188. ->integer($adapter->MD5())->isEqualTo(2)
  189. ->integer($adapter->MD5())->isEqualTo(0)
  190. ->if($adapter->MD5 = 0)
  191. ->and($adapter->MD5[1] = 1)
  192. ->and($adapter->MD5[2] = 2)
  193. ->and($adapter->resetCalls())
  194. ->then
  195. ->integer($adapter->md5())->isEqualTo(1)
  196. ->integer($adapter->md5())->isEqualTo(2)
  197. ->integer($adapter->md5())->isEqualTo(0)
  198. ->if($adapter->resetCalls())
  199. ->then
  200. ->integer($adapter->MD5())->isEqualTo(1)
  201. ->integer($adapter->MD5())->isEqualTo(2)
  202. ->integer($adapter->MD5())->isEqualTo(0)
  203. */
  204. ->if($adapter = new testedClass())
  205. ->and($adapter->sha1[2] = $sha1 = uniqid())
  206. ->then
  207. ->string($adapter->sha1($string = uniqid()))->isEqualTo(sha1($string))
  208. // ->string($adapter->sha1($string = uniqid()))->isEqualTo(sha1($string))
  209. ->string($adapter->sha1(uniqid()))->isEqualTo($sha1)
  210. // ->string($adapter->sha1($otherString = uniqid()))->isEqualTo(sha1($otherString))
  211. ;
  212. }
  213. public function test__sleep()
  214. {
  215. $this
  216. ->if($adapter = new testedClass())
  217. ->then
  218. ->array($adapter->__sleep())->isEmpty()
  219. ;
  220. }
  221. public function test__toString()
  222. {
  223. $this
  224. ->if($adapter = new testedClass())
  225. ->and($calls = new test\adapter\calls())
  226. ->then
  227. ->castToString($adapter)->isEqualTo((string) $calls)
  228. ;
  229. }
  230. public function testSerialize()
  231. {
  232. $this
  233. ->if($adapter = new testedClass())
  234. ->then
  235. ->string(serialize($adapter))->isNotEmpty()
  236. ->if($adapter->md5 = function() {})
  237. ->then
  238. ->string(serialize($adapter))->isNotEmpty()
  239. ;
  240. }
  241. public function testSetCalls()
  242. {
  243. $this
  244. ->if($adapter = new testedClass())
  245. ->then
  246. ->object($adapter->setCalls($calls = new test\adapter\calls()))->isIdenticalTo($adapter)
  247. ->object($adapter->getCalls())->isIdenticalTo($calls)
  248. ->object($adapter->setCalls())->isIdenticalTo($adapter)
  249. ->object($adapter->getCalls())
  250. ->isNotIdenticalTo($calls)
  251. ->isEqualTo(new test\adapter\calls())
  252. ->if($calls = new test\adapter\calls())
  253. ->and($calls[] = new test\adapter\call(uniqid()))
  254. ->and($adapter->setCalls($calls))
  255. ->then
  256. ->object($adapter->getCalls())
  257. ->isIdenticalTo($calls)
  258. ->hasSize(0)
  259. ;
  260. }
  261. public function testGetCalls()
  262. {
  263. $this
  264. ->if($adapter = new testedClass())
  265. ->and($adapter->setCalls($calls = new \mock\mageekguy\atoum\test\adapter\calls()))
  266. ->and($this->calling($calls)->get = $innerCalls = new test\adapter\calls())
  267. ->then
  268. ->object($adapter->getCalls())->isIdenticalTo($calls)
  269. ->object($adapter->getCalls($call = new test\adapter\call(uniqid())))->isIdenticalTo($innerCalls)
  270. ->mock($calls)->call('get')->withArguments($call, false)->once()
  271. ;
  272. }
  273. public function testGetCallsEqualTo()
  274. {
  275. $this
  276. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  277. ->and($this->calling($calls)->getEqualTo = $equalCalls = new test\adapter\calls())
  278. ->and($adapter = new testedClass())
  279. ->and($adapter->setCalls($calls))
  280. ->then
  281. ->object($adapter->getCallsEqualTo($call = new call('md5')))->isIdenticalTo($equalCalls)
  282. ->mock($calls)->call('getEqualTo')->withArguments($call)->once()
  283. ;
  284. }
  285. public function testGetPreviousCalls()
  286. {
  287. $this
  288. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  289. ->and($this->calling($calls)->getPrevious = $previousCalls = new test\adapter\calls())
  290. ->and($adapter = new testedClass())
  291. ->and($adapter->setCalls($calls))
  292. ->then
  293. ->object($adapter->getPreviousCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX)))->isIdenticalTo($previousCalls)
  294. ->mock($calls)->call('getPrevious')->withArguments($call, $position, false)->once()
  295. ->object($adapter->getPreviousCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX), true))->isIdenticalTo($previousCalls)
  296. ->mock($calls)->call('getPrevious')->withArguments($call, $position, true)->once()
  297. ;
  298. }
  299. public function testHasPreviousCalls()
  300. {
  301. $this
  302. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  303. ->and($this->calling($calls)->hasPrevious = $has = (boolean) rand(0, 1))
  304. ->and($adapter = new testedClass())
  305. ->and($adapter->setCalls($calls))
  306. ->then
  307. ->boolean($adapter->hasPreviousCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX)))->isEqualTo($has)
  308. ->mock($calls)->call('hasPrevious')->withArguments($call, $position, false)->once()
  309. ->boolean($adapter->hasPreviousCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX), true))->isEqualTo($has)
  310. ->mock($calls)->call('hasPrevious')->withArguments($call, $position, true)->once()
  311. ;
  312. }
  313. public function testGetAfterCalls()
  314. {
  315. $this
  316. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  317. ->and($this->calling($calls)->getAfter = $afterCalls = new test\adapter\calls())
  318. ->and($adapter = new testedClass())
  319. ->and($adapter->setCalls($calls))
  320. ->then
  321. ->object($adapter->getAfterCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX)))->isIdenticalTo($afterCalls)
  322. ->mock($calls)->call('getAfter')->withArguments($call, $position, false)->once()
  323. ->object($adapter->getAfterCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX), true))->isIdenticalTo($afterCalls)
  324. ->mock($calls)->call('getAfter')->withArguments($call, $position, true)->once()
  325. ;
  326. }
  327. public function testHasAfterCalls()
  328. {
  329. $this
  330. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  331. ->and($this->calling($calls)->hasAfter = $has = (boolean) rand(0, 1))
  332. ->and($adapter = new testedClass())
  333. ->and($adapter->setCalls($calls))
  334. ->then
  335. ->boolean($adapter->hasAfterCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX)))->isEqualTo($has)
  336. ->mock($calls)->call('hasAfter')->withArguments($call, $position, false)->once()
  337. ->boolean($adapter->hasAfterCalls($call = new call('md5'), $position = rand(1, PHP_INT_MAX), true))->isEqualTo($has)
  338. ->mock($calls)->call('hasAfter')->withArguments($call, $position, true)->once()
  339. ;
  340. }
  341. public function testGetCallsIdenticalTo()
  342. {
  343. $this
  344. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  345. ->and($this->calling($calls)->getIdenticalTo = $identicalCalls = new test\adapter\calls())
  346. ->and($adapter = new testedClass())
  347. ->and($adapter->setCalls($calls))
  348. ->then
  349. ->object($adapter->getCallsIdenticalTo($call = new call('md5')))->isIdenticalTo($identicalCalls)
  350. ->mock($calls)->call('getIdenticalTo')->withArguments($call)->once()
  351. ;
  352. }
  353. public function testGetCallNumber()
  354. {
  355. $this
  356. ->if($calls = new \mock\mageekguy\atoum\test\adapter\calls())
  357. ->and($this->calling($calls)->count = 0)
  358. ->and($adapter = new testedClass())
  359. ->and($adapter->setCalls($calls))
  360. ->then
  361. ->integer($adapter->getCallNumber())->isZero()
  362. ->and($this->calling($calls)->count = $callNumber = rand(1, PHP_INT_MAX))
  363. ->then
  364. ->integer($adapter->getCallNumber())->isEqualTo($callNumber)
  365. ;
  366. }
  367. public function testGetTimeline()
  368. {
  369. $this
  370. ->if($adapter = new testedClass())
  371. ->and($adapter->setCalls($calls = new \mock\mageekguy\atoum\test\adapter\calls()))
  372. ->and($this->calling($calls)->getTimeline = array())
  373. ->then
  374. ->array($adapter->getTimeline())->isEmpty()
  375. ->mock($calls)->call('getTimeline')->withArguments(null, false)->once()
  376. ;
  377. }
  378. public function testAddCall()
  379. {
  380. $this
  381. ->if($adapter = new testedClass())
  382. ->and($adapter->setCalls($calls = new \mock\mageekguy\atoum\test\adapter\calls()))
  383. ->and($this->calling($calls)->addCall = $calls)
  384. ->then
  385. ->object($adapter->addCall($method = uniqid(), $args = array(uniqid())))->isIdenticalTo($adapter)
  386. ->mock($calls)->call('addCall')->withArguments(new test\adapter\call($method, $args))->once()
  387. ->object($adapter->addCall($otherMethod = uniqid(), $otherArgs = array(uniqid(), uniqid())))->isIdenticalTo($adapter)
  388. ->mock($calls)->call('addCall')->withArguments(new test\adapter\call($otherMethod, $otherArgs))->once()
  389. ->object($adapter->addCall($method, $anotherArgs = array(uniqid())))->isIdenticalTo($adapter)
  390. ->mock($calls)->call('addCall')->withArguments(new test\adapter\call($method, $anotherArgs))->once()
  391. ->if($arg = 'foo')
  392. ->and($arguments = array(& $arg))
  393. ->then
  394. ->object($adapter->addCall($method, $arguments))->isIdenticalTo($adapter)
  395. ->mock($calls)->call('addCall')->withArguments(new test\adapter\call($method, $arguments))->once()
  396. ;
  397. }
  398. public function testResetCalls()
  399. {
  400. $this
  401. ->if($adapter = new testedClass())
  402. ->and($adapter->md5(uniqid()))
  403. ->then
  404. ->sizeof($adapter->getCalls())->isGreaterThan(0)
  405. ->object($adapter->resetCalls())->isIdenticalTo($adapter)
  406. ->sizeof($adapter->getCalls())->isZero(0)
  407. ;
  408. }
  409. public function testReset()
  410. {
  411. $this
  412. ->if($adapter = new testedClass())
  413. ->then
  414. ->array($adapter->getInvokers())->isEmpty()
  415. ->sizeof($adapter->getCalls())->isZero()
  416. ->object($adapter->reset())->isIdenticalTo($adapter)
  417. ->array($adapter->getInvokers())->isEmpty()
  418. ->sizeof($adapter->getCalls())->isZero()
  419. ->if($adapter->md5(uniqid()))
  420. ->then
  421. ->array($adapter->getInvokers())->isEmpty()
  422. ->sizeof($adapter->getCalls())->isGreaterThan(0)
  423. ->object($adapter->reset())->isIdenticalTo($adapter)
  424. ->array($adapter->getInvokers())->isEmpty()
  425. ->sizeof($adapter->getCalls())->isZero()
  426. ->if($adapter->md5 = uniqid())
  427. ->then
  428. ->array($adapter->getInvokers())->isNotEmpty()
  429. ->sizeof($adapter->getCalls())->isZero(0)
  430. ->object($adapter->reset())->isIdenticalTo($adapter)
  431. ->array($adapter->getInvokers())->isEmpty()
  432. ->sizeof($adapter->getCalls())->isZero()
  433. ->if($adapter->md5 = uniqid())
  434. ->and($adapter->md5(uniqid()))
  435. ->then
  436. ->array($adapter->getInvokers())->isNotEmpty()
  437. ->sizeof($adapter->getCalls())->isGreaterThan(0)
  438. ->object($adapter->reset())->isIdenticalTo($adapter)
  439. ->array($adapter->getInvokers())->isEmpty()
  440. ->sizeof($adapter->getCalls())->isZero()
  441. ;
  442. }
  443. }