Consistency.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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\Consistency as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Consistency\Test\Unit\Consistency.
  40. *
  41. * Test suite of the consistency class.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Consistency extends Test\Unit\Suite
  47. {
  48. protected function _entity_exists_with_xxx($class, $interface, $trait)
  49. {
  50. $this
  51. ->given(
  52. $this->function->class_exists = $class,
  53. $this->function->interface_exists = $interface,
  54. $this->function->trait_exists = $trait
  55. )
  56. ->when($result = SUT::entityExists('foo'))
  57. ->then
  58. ->boolean($result)
  59. ->isTrue();
  60. }
  61. public function case_entity_exists_with_class()
  62. {
  63. return $this->_entity_exists_with_xxx(true, false, false);
  64. }
  65. public function case_entity_exists_with_interface()
  66. {
  67. return $this->_entity_exists_with_xxx(false, true, false);
  68. }
  69. public function case_entity_exists_with_trait()
  70. {
  71. return $this->_entity_exists_with_xxx(false, false, true);
  72. }
  73. public function case_entity_does_not_exists()
  74. {
  75. $this
  76. ->given(
  77. $this->function->class_exists = false,
  78. $this->function->interface_exists = false,
  79. $this->function->trait_exists = false
  80. )
  81. ->when($result = SUT::entityExists('foo'))
  82. ->then
  83. ->boolean($result)
  84. ->isFalse();
  85. }
  86. public function case_get_entity_shortest_name()
  87. {
  88. $this
  89. ->when($result = SUT::getEntityShortestName('Foo\Bar\Bar'))
  90. ->then
  91. ->string($result)
  92. ->isEqualTo('Foo\Bar');
  93. }
  94. public function case_get_entity_shortest_name_with_already_the_shortest()
  95. {
  96. $this
  97. ->when($result = SUT::getEntityShortestName('Foo\Bar'))
  98. ->then
  99. ->string($result)
  100. ->isEqualTo('Foo\Bar');
  101. }
  102. public function case_get_entity_shortest_name_with_no_namespace()
  103. {
  104. $this
  105. ->when($result = SUT::getEntityShortestName('Foo'))
  106. ->then
  107. ->string($result)
  108. ->isEqualTo('Foo');
  109. }
  110. public function case_is_keyword()
  111. {
  112. $this
  113. ->given(
  114. $keywords = [
  115. '__HALT_COMPILER',
  116. 'abstract',
  117. 'and',
  118. 'array',
  119. 'as',
  120. 'bool',
  121. 'break',
  122. 'callable',
  123. 'case',
  124. 'catch',
  125. 'class',
  126. 'clone',
  127. 'const',
  128. 'continue',
  129. 'declare',
  130. 'default',
  131. 'die',
  132. 'do',
  133. 'echo',
  134. 'else',
  135. 'elseif',
  136. 'empty',
  137. 'enddeclare',
  138. 'endfor',
  139. 'endforeach',
  140. 'endif',
  141. 'endswitch',
  142. 'endwhile',
  143. 'eval',
  144. 'exit',
  145. 'extends',
  146. 'false',
  147. 'final',
  148. 'float',
  149. 'for',
  150. 'foreach',
  151. 'function',
  152. 'global',
  153. 'goto',
  154. 'if',
  155. 'implements',
  156. 'include',
  157. 'include_once',
  158. 'instanceof',
  159. 'insteadof',
  160. 'int',
  161. 'interface',
  162. 'isset',
  163. 'list',
  164. 'mixed',
  165. 'namespace',
  166. 'new',
  167. 'null',
  168. 'numeric',
  169. 'object',
  170. 'or',
  171. 'print',
  172. 'private',
  173. 'protected',
  174. 'public',
  175. 'require',
  176. 'require_once',
  177. 'resource',
  178. 'return',
  179. 'static',
  180. 'string',
  181. 'switch',
  182. 'throw',
  183. 'trait',
  184. 'true',
  185. 'try',
  186. 'unset',
  187. 'use',
  188. 'var',
  189. 'void',
  190. 'while',
  191. 'xor',
  192. 'yield',
  193. '__CLASS__',
  194. '__DIR__',
  195. '__FILE__',
  196. '__FUNCTION__',
  197. '__LINE__',
  198. '__METHOD__',
  199. '__NAMESPACE__',
  200. '__TRAIT__'
  201. ]
  202. )
  203. ->when(function () use ($keywords) {
  204. foreach ($keywords as $keyword) {
  205. $this
  206. ->boolean(SUT::isKeyword($keyword))
  207. ->isTrue();
  208. }
  209. });
  210. }
  211. public function case_is_identifier()
  212. {
  213. $this
  214. ->given($_identifier = $this->realdom->regex('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x80-\xff]*$#'))
  215. ->when(function () use ($_identifier) {
  216. foreach ($this->sampleMany($_identifier, 1000) as $identifier) {
  217. $this
  218. ->boolean(SUT::isIdentifier($identifier))
  219. ->isTrue();
  220. }
  221. });
  222. }
  223. public function case_register_shutdown_function()
  224. {
  225. $self = $this;
  226. $this
  227. ->given(
  228. $callable = function () {
  229. },
  230. $this->function->register_shutdown_function = function ($_callable) use (&$called, $self, &$callable) {
  231. $called = true;
  232. $self
  233. ->variable($_callable)
  234. ->isEqualTo($callable);
  235. return true;
  236. }
  237. )
  238. ->when($result = SUT::registerShutdownFunction($callable))
  239. ->then
  240. ->boolean($result)
  241. ->isTrue();
  242. }
  243. public function case_get_php_binary_with_constant()
  244. {
  245. $this
  246. ->given($this->constant->PHP_BINARY = '/foo/php')
  247. ->when($result = SUT::getPHPBinary())
  248. ->then
  249. ->string($result)
  250. ->isEqualTo('/foo/php');
  251. }
  252. public function case_get_php_binary_with_server()
  253. {
  254. $this
  255. ->given(
  256. $this->function->defined = false,
  257. $_SERVER['_'] = '/bar/php'
  258. )
  259. ->when($result = SUT::getPHPBinary())
  260. ->then
  261. ->string($result)
  262. ->isEqualTo('/bar/php');
  263. }
  264. public function case_get_php_binary_with_bin_directory()
  265. {
  266. unset($_SERVER['_']);
  267. $this
  268. ->given(
  269. $this->function->defined = false,
  270. $this->function->file_exists = true,
  271. $this->function->realpath = '/baz/php'
  272. )
  273. ->when($result = SUT::getPHPBinary())
  274. ->then
  275. ->string($result)
  276. ->isEqualTo('/baz/php');
  277. }
  278. public function case_uuid()
  279. {
  280. $this
  281. ->given($this->function->mt_rand = 42)
  282. ->when($result = SUT::uuid())
  283. ->then
  284. ->string($result)
  285. ->isEqualTo('002a002a-002a-402a-802a-002a002a002a');
  286. }
  287. public function case_uuid_all_differents()
  288. {
  289. $this
  290. ->when(function () {
  291. $uuids = [];
  292. for ($i = 0; $i < 10000; ++$i) {
  293. $uuids[] = SUT::uuid();
  294. }
  295. $this
  296. ->integer(count($uuids))
  297. ->isEqualTo(count(array_unique($uuids)));
  298. });
  299. }
  300. }