Xcallable.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Consistency\Test\Unit;
  36. use Hoa\Consistency\Xcallable as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Consistency\Test\Unit\Xcallable.
  40. *
  41. * Test suite of the xcallable class.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Xcallable extends Test\Unit\Suite
  47. {
  48. public function case_form_function()
  49. {
  50. $this
  51. ->when($result = new SUT('strtoupper'))
  52. ->then
  53. ->string($result('foo'))
  54. ->isEqualTo('FOO')
  55. ->string($result->getValidCallback())
  56. ->isEqualTo('strtoupper')
  57. ->string($result->getHash())
  58. ->isEqualTo('function#strtoupper')
  59. ->isEqualTo($result . '')
  60. ->object($reflection = $result->getReflection())
  61. ->isInstanceOf('ReflectionFunction')
  62. ->string($reflection->getName())
  63. ->isEqualTo('strtoupper');
  64. }
  65. public function case_form_class___method()
  66. {
  67. $this
  68. ->when($result = new SUT(__CLASS__ . '::strtoupper'))
  69. ->then
  70. ->string($result('foo'))
  71. ->isEqualTo('FOO')
  72. ->array($result->getValidCallback())
  73. ->isEqualTo([__CLASS__, 'strtoupper'])
  74. ->string($result->getHash())
  75. ->isEqualTo('class#' . __CLASS__ . '::strtoupper')
  76. ->isEqualTo($result . '')
  77. ->object($reflection = $result->getReflection())
  78. ->isInstanceOf('ReflectionMethod')
  79. ->string($reflection->getName())
  80. ->isEqualTo('strtoupper');
  81. }
  82. public function case_form_class_method()
  83. {
  84. $this
  85. ->when($result = new SUT(__CLASS__, 'strtoupper'))
  86. ->then
  87. ->string($result('foo'))
  88. ->isEqualTo('FOO')
  89. ->array($result->getValidCallback())
  90. ->isEqualTo([__CLASS__, 'strtoupper'])
  91. ->string($result->getHash())
  92. ->isEqualTo('class#' . __CLASS__ . '::strtoupper')
  93. ->isEqualTo($result . '')
  94. ->object($reflection = $result->getReflection())
  95. ->isInstanceOf('ReflectionMethod')
  96. ->string($reflection->getName())
  97. ->isEqualTo('strtoupper');
  98. }
  99. public function case_form_object_method()
  100. {
  101. $this
  102. ->when($result = new SUT($this, 'strtolower'))
  103. ->then
  104. ->string($result('FOO'))
  105. ->isEqualTo('foo')
  106. ->array($result->getValidCallback())
  107. ->isEqualTo([$this, 'strtolower'])
  108. ->string($result->getHash())
  109. ->matches(
  110. '/^object\([^:]+\)#' .
  111. preg_quote(__CLASS__) .
  112. '::strtolower$/'
  113. )
  114. ->isEqualTo($result . '')
  115. ->object($reflection = $result->getReflection())
  116. ->isInstanceOf('ReflectionMethod')
  117. ->string($reflection->getName())
  118. ->isEqualTo('strtolower');
  119. }
  120. public function case_form_object_invoke()
  121. {
  122. $this
  123. ->when($result = new SUT($this))
  124. ->then
  125. ->string($result('foo'))
  126. ->isEqualTo('FOO')
  127. ->array($result->getValidCallback())
  128. ->isEqualTo([$this, '__invoke'])
  129. ->string($result->getHash())
  130. ->matches(
  131. '/^object\([^:]+\)#' .
  132. preg_quote(__CLASS__) .
  133. '::__invoke$/'
  134. )
  135. ->isEqualTo($result . '')
  136. ->object($reflection = $result->getReflection())
  137. ->isInstanceOf('ReflectionMethod')
  138. ->string($reflection->getName())
  139. ->isEqualTo('__invoke');
  140. }
  141. public function case_form_closure()
  142. {
  143. $this
  144. ->given(
  145. $closure = function ($string) {
  146. return strtoupper($string);
  147. }
  148. )
  149. ->when($result = new SUT($closure))
  150. ->then
  151. ->string($result('foo'))
  152. ->isEqualTo('FOO')
  153. ->object($result->getValidCallback())
  154. ->isIdenticalTo($closure)
  155. ->string($result->getHash())
  156. ->matches('/^closure\([^:]+\)$/')
  157. ->isEqualTo($result . '')
  158. ->object($reflection = $result->getReflection())
  159. ->isInstanceOf('ReflectionFunction')
  160. ->string($reflection->getName())
  161. ->isEqualTo('Hoa\Consistency\Test\Unit\{closure}');
  162. }
  163. public function case_form_array_of_class_method()
  164. {
  165. $this
  166. ->when($result = new SUT([__CLASS__, 'strtoupper']))
  167. ->then
  168. ->string($result('foo'))
  169. ->isEqualTo('FOO')
  170. ->array($result->getValidCallback())
  171. ->isEqualTo([__CLASS__, 'strtoupper'])
  172. ->string($result->getHash())
  173. ->isEqualTo('class#' . __CLASS__ . '::strtoupper')
  174. ->isEqualTo($result . '')
  175. ->object($reflection = $result->getReflection())
  176. ->isInstanceOf('ReflectionMethod')
  177. ->string($reflection->getName())
  178. ->isEqualTo('strtoupper');
  179. }
  180. public function case_form_array_of_object_method()
  181. {
  182. $this
  183. ->when($result = new SUT([$this, 'strtolower']))
  184. ->then
  185. ->string($result('FOO'))
  186. ->isEqualTo('foo')
  187. ->array($result->getValidCallback())
  188. ->isEqualTo([$this, 'strtolower'])
  189. ->string($result->getHash())
  190. ->matches(
  191. '/^object\([^:]+\)#' .
  192. preg_quote(__CLASS__) .
  193. '::strtolower$/'
  194. )
  195. ->isEqualTo($result . '')
  196. ->object($reflection = $result->getReflection())
  197. ->isInstanceOf('ReflectionMethod')
  198. ->string($reflection->getName())
  199. ->isEqualTo('strtolower');
  200. }
  201. public function case_form_able_not_a_string()
  202. {
  203. $this
  204. ->exception(function () {
  205. new SUT(__CLASS__, 123);
  206. })
  207. ->isInstanceOf('Hoa\Consistency\Exception');
  208. }
  209. public function case_form_function_not_defined()
  210. {
  211. $this
  212. ->exception(function () {
  213. new SUT('__hoa_test_undefined_function__');
  214. })
  215. ->isInstanceOf('Hoa\Consistency\Exception');
  216. }
  217. public function case_form_able_cannot_be_deduced()
  218. {
  219. $this
  220. ->given($this->function->method_exists = false)
  221. ->exception(function () {
  222. new SUT($this);
  223. })
  224. ->isInstanceOf('Hoa\Consistency\Exception');
  225. }
  226. public function case_invoke()
  227. {
  228. $this
  229. ->given(
  230. $callable = new SUT(
  231. function ($x, $y, $z) {
  232. return [$x, $y, $z];
  233. }
  234. )
  235. )
  236. ->when($result = $callable(7, [4.2], 'foo'))
  237. ->then
  238. ->array($result)
  239. ->isEqualTo([7, [4.2], 'foo']);
  240. }
  241. public function case_distribute_arguments()
  242. {
  243. $this
  244. ->given(
  245. $callable = new SUT(
  246. function ($x, $y, $z) {
  247. return [$x, $y, $z];
  248. }
  249. )
  250. )
  251. ->when($result = $callable->distributeArguments([7, [4.2], 'foo']))
  252. ->then
  253. ->array($result)
  254. ->isEqualTo([7, [4.2], 'foo']);
  255. }
  256. protected function _get_valid_callback_stream_xxx($argument, $method)
  257. {
  258. $this
  259. ->given(
  260. $stream = new \Mock\Hoa\Stream\IStream\Out(),
  261. $arguments = [$argument],
  262. $xcallable = new SUT($stream)
  263. )
  264. ->when($result = $xcallable->getValidCallback($arguments))
  265. ->then
  266. ->array($result)
  267. ->isEqualTo([$stream, $method]);
  268. }
  269. public function case_get_valid_callback_stream_character()
  270. {
  271. return $this->_get_valid_callback_stream_xxx('f', 'writeCharacter');
  272. }
  273. public function case_get_valid_callback_stream_string()
  274. {
  275. return $this->_get_valid_callback_stream_xxx('foo', 'writeString');
  276. }
  277. public function case_get_valid_callback_stream_boolean()
  278. {
  279. return $this->_get_valid_callback_stream_xxx(true, 'writeBoolean');
  280. }
  281. public function case_get_valid_callback_stream_integer()
  282. {
  283. return $this->_get_valid_callback_stream_xxx(7, 'writeInteger');
  284. }
  285. public function case_get_valid_callback_stream_array()
  286. {
  287. return $this->_get_valid_callback_stream_xxx([4, 2], 'writeArray');
  288. }
  289. public function case_get_valid_callback_stream_float()
  290. {
  291. return $this->_get_valid_callback_stream_xxx(4.2, 'writeFloat');
  292. }
  293. public function case_get_valid_callback_stream_other()
  294. {
  295. return $this->_get_valid_callback_stream_xxx($this, 'writeAll');
  296. }
  297. public static function strtoupper($string)
  298. {
  299. return strtoupper($string);
  300. }
  301. public function strtolower($string)
  302. {
  303. return strtolower($string);
  304. }
  305. public function __invoke($string)
  306. {
  307. return strtoupper($string);
  308. }
  309. public function __toString()
  310. {
  311. return 'hello';
  312. }
  313. }