Autoloader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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\Autoloader as SUT;
  37. use Hoa\Test;
  38. /**
  39. * Class \Hoa\Consistency\Test\Unit\Autoloader.
  40. *
  41. * Test suite of the autoloader.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Autoloader extends Test\Unit\Suite
  47. {
  48. public function case_add_namespace_prepend()
  49. {
  50. $this
  51. ->given(
  52. $autoloader = new SUT(),
  53. $prefix = 'Foo\Bar\\',
  54. $baseDirectoryA = 'Source/Foo/Bar/',
  55. $baseDirectoryB = 'Source/Foo/Bar/'
  56. )
  57. ->when(
  58. $autoloader->addNamespace($prefix, $baseDirectoryA),
  59. $result = $autoloader->addNamespace($prefix, $baseDirectoryB)
  60. )
  61. ->then
  62. ->boolean($autoloader->hasBaseDirectory($prefix))
  63. ->isTrue()
  64. ->array($autoloader->getBaseDirectories($prefix))
  65. ->isEqualTo([
  66. $baseDirectoryB,
  67. $baseDirectoryA
  68. ]);
  69. }
  70. public function case_add_namespace_append()
  71. {
  72. $this
  73. ->given(
  74. $autoloader = new SUT(),
  75. $prefix = 'Foo\Bar\\',
  76. $baseDirectoryA = 'Source/Foo/Bar/',
  77. $baseDirectoryB = 'Source/Foo/Bar/'
  78. )
  79. ->when(
  80. $autoloader->addNamespace($prefix, $baseDirectoryA),
  81. $result = $autoloader->addNamespace($prefix, $baseDirectoryB)
  82. )
  83. ->then
  84. ->boolean($autoloader->hasBaseDirectory($prefix))
  85. ->isTrue()
  86. ->array($autoloader->getBaseDirectories($prefix))
  87. ->isEqualTo([
  88. $baseDirectoryA,
  89. $baseDirectoryB
  90. ]);
  91. }
  92. public function case_add_namespace_with_invalid_prefix()
  93. {
  94. $this
  95. ->given(
  96. $autoloader = new SUT(),
  97. $prefix = '\\\\Foo\Bar',
  98. $baseDirectory = 'Source/Foo/Bar/'
  99. )
  100. ->when($result = $autoloader->addNamespace($prefix, $baseDirectory))
  101. ->then
  102. ->boolean($autoloader->hasBaseDirectory('Foo\Bar\\'))
  103. ->isTrue()
  104. ->array($autoloader->getBaseDirectories('Foo\Bar\\'))
  105. ->isEqualTo([$baseDirectory]);
  106. }
  107. public function case_add_namespace_with_invalid_base_directory()
  108. {
  109. $this
  110. ->given(
  111. $autoloader = new SUT(),
  112. $prefix = 'Foo\Bar\\',
  113. $baseDirectory = 'Source/Foo/Bar'
  114. )
  115. ->when($result = $autoloader->addNamespace($prefix, $baseDirectory))
  116. ->then
  117. ->boolean($autoloader->hasBaseDirectory('Foo\Bar\\'))
  118. ->isTrue()
  119. ->array($autoloader->getBaseDirectories('Foo\Bar\\'))
  120. ->isEqualTo(['Source/Foo/Bar/']);
  121. }
  122. public function case_add_namespace_with_crazy_invalid_base_directory()
  123. {
  124. $this
  125. ->given(
  126. $autoloader = new SUT(),
  127. $prefix = 'Foo\Bar\\',
  128. $baseDirectory = 'Source/Foo/Bar/////'
  129. )
  130. ->when($result = $autoloader->addNamespace($prefix, $baseDirectory))
  131. ->then
  132. ->boolean($autoloader->hasBaseDirectory('Foo\Bar\\'))
  133. ->isTrue()
  134. ->array($autoloader->getBaseDirectories('Foo\Bar\\'))
  135. ->isEqualTo(['Source/Foo/Bar/']);
  136. }
  137. public function case_load()
  138. {
  139. $this
  140. ->given(
  141. $autoloader = new \Mock\Hoa\Consistency\Autoloader(),
  142. $autoloader->addNamespace('Foo\Bar\\', 'Source/Foo/Bar/'),
  143. $this->calling($autoloader)->requireFile = function ($file) {
  144. return $file;
  145. }
  146. )
  147. ->when($result = $autoloader->load('Foo\Bar\Baz\Qux'))
  148. ->then
  149. ->string($result)
  150. ->isEqualTo('Source/Foo/Bar/Baz/Qux.php');
  151. }
  152. public function case_load_invalid_entity()
  153. {
  154. $this
  155. ->given($autoloader = new SUT())
  156. ->when($result = $autoloader->load('Foo'))
  157. ->then
  158. ->variable($result)
  159. ->isNull();
  160. }
  161. public function case_load_flex_entity()
  162. {
  163. $self = $this;
  164. $this
  165. ->given(
  166. $autoloader = new \Mock\Hoa\Consistency\Autoloader(),
  167. $autoloader->addNamespace('Foo\Bar\\', 'Source/Foo/'),
  168. $this->calling($autoloader)->runAutoloaderStack = function ($entity) use ($self, &$called) {
  169. $called = true;
  170. $self
  171. ->string($entity)
  172. ->isEqualTo('Foo\Bar\Baz\Baz');
  173. return;
  174. },
  175. $autoloader->register()
  176. )
  177. ->when($result = $autoloader->load('Foo\Bar\Baz'))
  178. ->then
  179. ->variable($result)
  180. ->isNull()
  181. ->boolean($called)
  182. ->isTrue();
  183. }
  184. public function case_load_unmapped_flex_entity()
  185. {
  186. $self = $this;
  187. $this
  188. ->given(
  189. $autoloader = new \Mock\Hoa\Consistency\Autoloader(),
  190. $this->calling($autoloader)->runAutoloaderStack = function ($entity) use ($self, &$called) {
  191. $called = true;
  192. return;
  193. },
  194. $autoloader->register()
  195. )
  196. ->when($result = $autoloader->load('Foo\Bar\Baz'))
  197. ->then
  198. ->variable($result)
  199. ->isNull()
  200. ->variable($called)
  201. ->isNull();
  202. }
  203. public function case_require_existing_file()
  204. {
  205. $this
  206. ->given(
  207. $autoloader = new SUT(),
  208. $this->function->file_exists = true,
  209. $constantName = 'HOA_TEST_' . uniqid(),
  210. $filename = 'hoa://Test/Vfs/Foo?type=file',
  211. file_put_contents($filename, '<?php define("' . $constantName . '", "BAR");')
  212. )
  213. ->when($result = $autoloader->requireFile($filename))
  214. ->then
  215. ->boolean($result)
  216. ->isTrue()
  217. ->string(constant($constantName))
  218. ->isEqualTo('BAR');
  219. }
  220. public function case_require_not_existing_file()
  221. {
  222. $this
  223. ->given(
  224. $autoloader = new SUT(),
  225. $this->function->file_exists = false
  226. )
  227. ->when($result = $autoloader->requireFile('/hoa/flatland'))
  228. ->then
  229. ->boolean($result)
  230. ->isFalse();
  231. }
  232. public function case_has_not_base_directory()
  233. {
  234. $this
  235. ->given($autoloader = new SUT())
  236. ->when($result = $autoloader->hasBaseDirectory('foo'))
  237. ->then
  238. ->boolean($result)
  239. ->isFalse();
  240. }
  241. public function case_get_base_undeclared_namespace_prefix()
  242. {
  243. $this
  244. ->given($autoloader = new SUT())
  245. ->when($result = $autoloader->getBaseDirectories('foo'))
  246. ->then
  247. ->array($result)
  248. ->isEmpty();
  249. }
  250. public function case_dnew()
  251. {
  252. $this
  253. ->given($classname = 'Hoa\Consistency\Autoloader')
  254. ->when($result = SUT::dnew($classname))
  255. ->then
  256. ->object($result)
  257. ->isInstanceOf($classname);
  258. }
  259. public function case_dnew_unknown_class()
  260. {
  261. $this
  262. ->given($this->function->spl_autoload_call = null)
  263. ->exception(function () {
  264. SUT::dnew('Foo');
  265. })
  266. ->isInstanceOf('ReflectionException');
  267. }
  268. public function case_get_loaded_classes()
  269. {
  270. $this
  271. ->given(
  272. $declaredClasses = get_declared_classes(),
  273. $this->function->get_declared_classes = $declaredClasses
  274. )
  275. ->when($result = SUT::getLoadedClasses())
  276. ->then
  277. ->array($result)
  278. ->isEqualTo($declaredClasses);
  279. }
  280. public function case_register()
  281. {
  282. $self = $this;
  283. $this
  284. ->given($autoloader = new SUT())
  285. ->when($result = $autoloader->register())
  286. ->then
  287. ->boolean($result)
  288. ->isTrue()
  289. ->array($autoloader->getRegisteredAutoloaders())
  290. ->isEqualTo(spl_autoload_functions());
  291. }
  292. public function case_unregister()
  293. {
  294. $this
  295. ->given(
  296. $autoloader = new SUT(),
  297. $oldRegisteredAutoloaders = $autoloader->getRegisteredAutoloaders()
  298. )
  299. ->when($result = $autoloader->register())
  300. ->then
  301. ->boolean($result)
  302. ->isTrue()
  303. ->integer(count($autoloader->getRegisteredAutoloaders()))
  304. ->isEqualTo(count($oldRegisteredAutoloaders) + 1)
  305. ->when($result = $autoloader->unregister())
  306. ->then
  307. ->boolean($result)
  308. ->isTrue()
  309. ->array($autoloader->getRegisteredAutoloaders())
  310. ->isEqualTo($oldRegisteredAutoloaders);
  311. }
  312. }