PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mageekguy/atoum
PHP | 289 lines | 242 code | 47 blank | 0 comment | 0 complexity | 6916d96b93c3d1cb5168e33cbf7b8d12 MD5 | raw file
  1. <?php
  2. namespace mageekguy\atoum\tests\units\test\adapter;
  3. use
  4. mageekguy\atoum,
  5. mageekguy\atoum\test\adapter
  6. ;
  7. require_once __DIR__ . '/../../../runner.php';
  8. class invoker extends atoum\test
  9. {
  10. public function testClass()
  11. {
  12. $this->assert
  13. ->class($this->getTestedClassName())->hasInterface('ArrayAccess')
  14. ;
  15. }
  16. public function test__set()
  17. {
  18. $invoker = new adapter\invoker();
  19. $invoker->return = $return = uniqid();
  20. $this->assert
  21. ->string($invoker->invoke())->isEqualTo($return)
  22. ;
  23. $invoker->throw = $exception = new \exception();
  24. $this->assert
  25. ->exception(function() use ($invoker) {
  26. $invoker->invoke();
  27. }
  28. )
  29. ->isIdenticalTo($exception)
  30. ;
  31. $this->assert
  32. ->exception(function() use ($invoker) {
  33. $invoker->{uniqid()} = uniqid();
  34. }
  35. )
  36. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  37. ;
  38. }
  39. public function test__construct()
  40. {
  41. $invoker = new adapter\invoker();
  42. $this->assert
  43. ->boolean($invoker->isEmpty())->isTrue()
  44. ->variable($invoker->getCurrentCall())->isNull()
  45. ;
  46. }
  47. public function testSetClosure()
  48. {
  49. $invoker = new adapter\invoker();
  50. $this->assert
  51. ->exception(function() use ($invoker) {
  52. $invoker->setClosure(function() {}, - rand(1, PHP_INT_MAX));
  53. }
  54. )
  55. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  56. ->hasMessage('Call number must be greater than or equal to zero')
  57. ->object($invoker->setClosure($value = function() {}))->isIdenticalTo($invoker)
  58. ->boolean($invoker->isEmpty())->isFalse()
  59. ->object($invoker->getClosure())->isIdenticalTo($value)
  60. ->object($invoker->setClosure($value = function() {}, 0))->isIdenticalTo($invoker)
  61. ->boolean($invoker->isEmpty())->isFalse()
  62. ->object($invoker->getClosure(0))->isIdenticalTo($value)
  63. ->object($invoker->setClosure($otherValue = function() {}, $call = rand(2, PHP_INT_MAX)))->isIdenticalTo($invoker)
  64. ->boolean($invoker->isEmpty())->isFalse()
  65. ->object($invoker->getClosure($call))->isIdenticalTo($otherValue)
  66. ;
  67. }
  68. public function testGetClosure()
  69. {
  70. $invoker = new adapter\invoker();
  71. $this->assert
  72. ->exception(function() use ($invoker) {
  73. $invoker->getClosure(- rand(1, PHP_INT_MAX));
  74. }
  75. )
  76. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  77. ->hasMessage('Call number must be greater than or equal to zero')
  78. ->variable($invoker->getClosure(rand(0, PHP_INT_MAX)))->isNull()
  79. ;
  80. $invoker->setClosure($value = function() {}, 0);
  81. $this->assert
  82. ->object($invoker->getClosure(0))->isIdenticalTo($value)
  83. ->variable($invoker->getClosure(1))->isNull()
  84. ;
  85. }
  86. public function testClosureIsSet()
  87. {
  88. $invoker = new adapter\invoker();
  89. $this->assert
  90. ->exception(function() use ($invoker) {
  91. $invoker->closureIsSetForCall(- rand(1, PHP_INT_MAX), function() {});
  92. }
  93. )
  94. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  95. ->hasMessage('Call number must be greater than or equal to zero')
  96. ->boolean($invoker->closureIsSetForCall(rand(0, PHP_INT_MAX)))->isFalse()
  97. ;
  98. $invoker->setClosure(function() {}, 0);
  99. $this->assert
  100. ->boolean($invoker->closureIsSetForCall())->isTrue()
  101. ->boolean($invoker->closureIsSetForCall(0))->isTrue()
  102. ->boolean($invoker->closureIsSetForCall(rand(1, PHP_INT_MAX)))->isFalse()
  103. ;
  104. }
  105. public function testUnsetClosure()
  106. {
  107. $invoker = new adapter\invoker();
  108. $this->assert
  109. ->exception(function() use ($invoker) {
  110. $invoker->unsetClosure(- rand(1, PHP_INT_MAX), function() {});
  111. }
  112. )
  113. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  114. ->hasMessage('Call number must be greater than or equal to zero')
  115. ->exception(function() use ($invoker, & $call) {
  116. $invoker->unsetClosure($call = rand(0, PHP_INT_MAX), function() {});
  117. }
  118. )
  119. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  120. ->hasMessage('There is no closure defined for call ' . $call)
  121. ;
  122. $invoker->setClosure(function() {});
  123. $this->assert
  124. ->boolean($invoker->closureIsSetForCall())->isTrue()
  125. ->object($invoker->unsetClosure())->isIdenticalTo($invoker)
  126. ->boolean($invoker->closureIsSetForCall())->isFalse()
  127. ;
  128. }
  129. public function testOffsetSet()
  130. {
  131. $invoker = new adapter\invoker();
  132. $this->assert
  133. ->exception(function() use ($invoker) {
  134. $invoker->offsetSet(- rand(1, PHP_INT_MAX), function() {});
  135. }
  136. )
  137. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  138. ->hasMessage('Call number must be greater than or equal to zero')
  139. ->object($invoker->offsetSet(1, $value = function() {}))->isIdenticalTo($invoker)
  140. ->boolean($invoker->isEmpty())->isFalse()
  141. ->object($invoker->getClosure(1))->isIdenticalTo($value)
  142. ->object($invoker->offsetSet(2, $mixed = uniqid()))->isIdenticalTo($invoker)
  143. ->string($invoker->invoke(array(), 2))->isEqualTo($mixed)
  144. ;
  145. }
  146. public function testOffsetGet()
  147. {
  148. $invoker = new adapter\invoker();
  149. $this->assert
  150. ->exception(function() use ($invoker) {
  151. $invoker->offsetGet(- rand(1, PHP_INT_MAX));
  152. }
  153. )
  154. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  155. ->hasMessage('Call number must be greater than or equal to zero')
  156. ->variable($invoker->getClosure(rand(0, PHP_INT_MAX)))->isNull()
  157. ;
  158. $invoker->setClosure($value = function() {}, 0);
  159. $this->assert
  160. ->object($invoker->offsetGet(0))->isIdenticalTo($invoker)
  161. ->variable($invoker->getCurrentCall())->isEqualTo(0)
  162. ->object($invoker->offsetGet($call = rand(1, PHP_INT_MAX)))->isIdenticalTo($invoker)
  163. ->variable($invoker->getCurrentCall())->isEqualTo($call)
  164. ;
  165. }
  166. public function testOffsetExists()
  167. {
  168. $invoker = new adapter\invoker();
  169. $this->assert
  170. ->exception(function() use ($invoker) {
  171. $invoker->offsetExists(- rand(1, PHP_INT_MAX), function() {});
  172. }
  173. )
  174. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  175. ->hasMessage('Call number must be greater than or equal to zero')
  176. ->boolean($invoker->offsetExists(rand(0, PHP_INT_MAX)))->isFalse()
  177. ;
  178. $invoker->setClosure(function() {}, 0);
  179. $this->assert
  180. ->boolean($invoker->offsetExists(0))->isTrue()
  181. ->boolean($invoker->offsetExists(rand(1, PHP_INT_MAX)))->isFalse()
  182. ;
  183. }
  184. public function testOffsetUnset()
  185. {
  186. $invoker = new adapter\invoker();
  187. $this->assert
  188. ->exception(function() use ($invoker) {
  189. $invoker->offsetUnset(- rand(1, PHP_INT_MAX), function() {});
  190. }
  191. )
  192. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  193. ->hasMessage('Call number must be greater than or equal to zero')
  194. ->exception(function() use ($invoker, & $call) {
  195. $invoker->offsetUnset($call = rand(0, PHP_INT_MAX), function() {});
  196. }
  197. )
  198. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  199. ->hasMessage('There is no closure defined for call ' . $call)
  200. ;
  201. $invoker->setClosure(function() {});
  202. $this->assert
  203. ->boolean($invoker->closureIsSetForCall(0))->isTrue()
  204. ->object($invoker->offsetUnset(0))->isIdenticalTo($invoker)
  205. ->boolean($invoker->closureIsSetForCall(0))->isFalse()
  206. ;
  207. }
  208. public function testInvoke()
  209. {
  210. $invoker = new adapter\invoker();
  211. $this->assert
  212. ->exception(function() use ($invoker) {
  213. $invoker->invoke();
  214. }
  215. )
  216. ->isInstanceOf('mageekguy\atoum\exceptions\logic\invalidArgument')
  217. ->hasMessage('There is no closure defined for call 0')
  218. ;
  219. $invoker->setClosure(function($string) { return md5($string); });
  220. $invoker->setClosure(function() use (& $md5) { return $md5 = uniqid(); }, 1);
  221. $invoker->setClosure(function() use (& $md5) { return $md5 = uniqid(); }, $call = rand(2, PHP_INT_MAX));
  222. $this->assert
  223. ->string($invoker->invoke(array($string = uniqid())))->isEqualTo(md5($string))
  224. ->string($invoker->invoke(array($string = uniqid()), 0))->isEqualTo(md5($string))
  225. ->string($invoker->invoke(array($string = uniqid()), 1))->isEqualTo($md5)
  226. ->string($invoker->invoke(array($string = uniqid())))->isEqualTo(md5($string))
  227. ->string($invoker->invoke(array($string = uniqid()), 0))->isEqualTo(md5($string))
  228. ->string($invoker->invoke(array($string = uniqid()), $call))->isEqualTo($md5)
  229. ;
  230. }
  231. public function testAtCall()
  232. {
  233. $invoker = new adapter\invoker();
  234. $defaultReturn = uniqid();
  235. $invoker->setClosure(function () use ($defaultReturn) { return $defaultReturn; }, 0);
  236. $this->assert
  237. ->variable($invoker->getCurrentCall())->isNull()
  238. ->object($invoker->atCall($call = rand(1, PHP_INT_MAX)))->isIdenticalTo($invoker)
  239. ->integer($invoker->getCurrentCall())->isEqualTo($call)
  240. ;
  241. }
  242. }