ResourceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Loader_Autoloader_ResourceTest::main');
  24. }
  25. /**
  26. * @see Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @see Zend_Loader_Autoloader_Resource
  31. */
  32. require_once 'Zend/Loader/Autoloader/Resource.php';
  33. /**
  34. * @see Zend_Loader_Autoloader_Interface
  35. */
  36. require_once 'Zend/Loader/Autoloader/Interface.php';
  37. /** Zend_Config */
  38. require_once 'Zend/Config.php';
  39. /**
  40. * @category Zend
  41. * @package Zend_Loader
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @group Zend_Loader
  46. */
  47. class Zend_Loader_Autoloader_ResourceTest extends PHPUnit_Framework_TestCase
  48. {
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. // Store original autoloaders
  57. $this->loaders = spl_autoload_functions();
  58. if (!is_array($this->loaders)) {
  59. // spl_autoload_functions does not return empty array when no
  60. // autoloaders registered...
  61. $this->loaders = array();
  62. }
  63. // Store original include_path
  64. $this->includePath = get_include_path();
  65. Zend_Loader_Autoloader::resetInstance();
  66. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  67. // initialize 'error' member for tests that utilize error handling
  68. $this->error = null;
  69. $this->loader = new Zend_Loader_Autoloader_Resource(array(
  70. 'namespace' => 'FooBar',
  71. 'basePath' => realpath(dirname(__FILE__) . '/_files'),
  72. ));
  73. }
  74. public function tearDown()
  75. {
  76. // Restore original autoloaders
  77. $loaders = spl_autoload_functions();
  78. foreach ($loaders as $loader) {
  79. spl_autoload_unregister($loader);
  80. }
  81. foreach ($this->loaders as $loader) {
  82. spl_autoload_register($loader);
  83. }
  84. // Retore original include_path
  85. set_include_path($this->includePath);
  86. // Reset autoloader instance so it doesn't affect other tests
  87. Zend_Loader_Autoloader::resetInstance();
  88. }
  89. /**
  90. * @expectedException Zend_Loader_Exception
  91. */
  92. public function testAutoloaderInstantiationShouldRaiseExceptionWithoutNamespace()
  93. {
  94. $loader = new Zend_Loader_Autoloader_Resource(array('basePath' => dirname(__FILE__)));
  95. }
  96. /**
  97. * @expectedException Zend_Loader_Exception
  98. */
  99. public function testAutoloaderInstantiationShouldRaiseExceptionWithoutBasePath()
  100. {
  101. $loader = new Zend_Loader_Autoloader_Resource(array('namespace' => 'Foo'));
  102. }
  103. /**
  104. * @expectedException Zend_Loader_Exception
  105. */
  106. public function testAutoloaderInstantiationShouldRaiseExceptionWhenInvalidOptionsTypeProvided()
  107. {
  108. $loader = new Zend_Loader_Autoloader_Resource('foo');
  109. }
  110. public function testAutoloaderConstructorShouldAcceptZendConfigObject()
  111. {
  112. $config = new Zend_Config(array('namespace' => 'Foo', 'basePath' => dirname(__FILE__)));
  113. $loader = new Zend_Loader_Autoloader_Resource($config);
  114. }
  115. public function testAutoloaderShouldAllowRetrievingNamespace()
  116. {
  117. $this->assertEquals('FooBar', $this->loader->getNamespace());
  118. }
  119. public function testAutoloaderShouldAllowRetrievingBasePath()
  120. {
  121. $this->assertEquals(realpath(dirname(__FILE__) . '/_files'), $this->loader->getBasePath());
  122. }
  123. public function testNoResourceTypesShouldBeRegisteredByDefault()
  124. {
  125. $resourceTypes = $this->loader->getResourceTypes();
  126. $this->assertTrue(is_array($resourceTypes));
  127. $this->assertTrue(empty($resourceTypes));
  128. }
  129. /**
  130. * @expectedException Zend_Loader_Exception
  131. */
  132. public function testInitialResourceTypeDefinitionShouldRequireNamespace()
  133. {
  134. $this->loader->addResourceType('foo', 'foo');
  135. }
  136. /**
  137. * @expectedException Zend_Loader_Exception
  138. */
  139. public function testPassingNonStringPathWhenAddingResourceTypeShouldRaiseAnException()
  140. {
  141. $this->loader->addResourceType('foo', array('foo'), 'Foo');
  142. }
  143. public function testAutoloaderShouldAllowAddingArbitraryResourceTypes()
  144. {
  145. $this->loader->addResourceType('models', 'models', 'Model');
  146. $resources = $this->loader->getResourceTypes();
  147. $this->assertTrue(array_key_exists('models', $resources));
  148. $this->assertEquals($this->loader->getNamespace() . '_Model', $resources['models']['namespace']);
  149. $this->assertContains('/models', $resources['models']['path']);
  150. }
  151. public function testAutoloaderShouldAllowAddingResettingResourcePaths()
  152. {
  153. $this->loader->addResourceType('models', 'models', 'Model');
  154. $this->loader->addResourceType('models', 'apis');
  155. $resources = $this->loader->getResourceTypes();
  156. $this->assertNotContains('/models', $resources['models']['path']);
  157. $this->assertContains('/apis', $resources['models']['path']);
  158. }
  159. public function testAutoloaderShouldSupportAddingMultipleResourceTypesAtOnce()
  160. {
  161. $this->loader->addResourceTypes(array(
  162. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  163. 'form' => array('path' => 'forms', 'namespace' => 'Form'),
  164. ));
  165. $resources = $this->loader->getResourceTypes();
  166. $this->assertContains('model', array_keys($resources));
  167. $this->assertContains('form', array_keys($resources));
  168. }
  169. /**
  170. * @expectedException Zend_Loader_Exception
  171. */
  172. public function testAddingMultipleResourceTypesShouldRaiseExceptionWhenReceivingNonArrayItem()
  173. {
  174. $this->loader->addResourceTypes(array('foo' => 'bar'));
  175. }
  176. /**
  177. * @expectedException Zend_Loader_Exception
  178. */
  179. public function testAddingMultipleResourceTypesShouldRaiseExceptionWhenMissingResourcePath()
  180. {
  181. $this->loader->addResourceTypes(array('model' => array('namespace' => 'Model')));
  182. }
  183. public function testSetResourceTypesShouldOverwriteExistingResourceTypes()
  184. {
  185. $this->loader->addResourceTypes(array(
  186. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  187. 'form' => array('path' => 'forms', 'namespace' => 'Form'),
  188. ));
  189. $this->loader->setResourceTypes(array(
  190. 'view' => array('path' => 'views', 'namespace' => 'View'),
  191. 'layout' => array('path' => 'layouts', 'namespace' => 'Layout'),
  192. ));
  193. $resources = $this->loader->getResourceTypes();
  194. $this->assertNotContains('model', array_keys($resources));
  195. $this->assertNotContains('form', array_keys($resources));
  196. $this->assertContains('view', array_keys($resources));
  197. $this->assertContains('layout', array_keys($resources));
  198. }
  199. public function testHasResourceTypeShouldReturnFalseWhenTypeNotDefined()
  200. {
  201. $this->assertFalse($this->loader->hasResourceType('model'));
  202. }
  203. public function testHasResourceTypeShouldReturnTrueWhenTypeIsDefined()
  204. {
  205. $this->loader->addResourceTypes(array(
  206. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  207. ));
  208. $this->assertTrue($this->loader->hasResourceType('model'));
  209. }
  210. public function testRemoveResourceTypeShouldRemoveResourceFromList()
  211. {
  212. $this->loader->addResourceTypes(array(
  213. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  214. 'form' => array('path' => 'forms', 'namespace' => 'Form'),
  215. ));
  216. $this->loader->removeResourceType('form');
  217. $resources = $this->loader->getResourceTypes();
  218. $this->assertContains('model', array_keys($resources));
  219. $this->assertNotContains('form', array_keys($resources));
  220. }
  221. public function testAutoloaderShouldAllowSettingDefaultResourceType()
  222. {
  223. $this->loader->addResourceTypes(array(
  224. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  225. ));
  226. $this->loader->setDefaultResourceType('model');
  227. $this->assertEquals('model', $this->loader->getDefaultResourceType());
  228. }
  229. public function testSettingDefaultResourceTypeToUndefinedTypeShouldHaveNoEffect()
  230. {
  231. $this->loader->setDefaultResourceType('model');
  232. $this->assertNull($this->loader->getDefaultResourceType());
  233. }
  234. /**
  235. * @expectedException Zend_Loader_Exception
  236. */
  237. public function testLoadShouldRaiseExceptionWhenNotTypePassedAndNoDefaultSpecified()
  238. {
  239. $this->loader->load('Foo');
  240. }
  241. /**
  242. * @expectedException Zend_Loader_Exception
  243. */
  244. public function testLoadShouldRaiseExceptionWhenResourceTypeDoesNotExist()
  245. {
  246. $this->loader->load('Foo', 'model');
  247. }
  248. public function testLoadShouldReturnObjectOfExpectedClass()
  249. {
  250. $this->loader->addResourceTypes(array(
  251. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  252. ));
  253. $object = $this->loader->load('ZendLoaderAutoloaderResourceTest', 'model');
  254. $this->assertTrue($object instanceof FooBar_Model_ZendLoaderAutoloaderResourceTest);
  255. }
  256. public function testSuccessiveCallsToLoadSameResourceShouldReturnSameObject()
  257. {
  258. $this->loader->addResourceTypes(array(
  259. 'form' => array('path' => 'forms', 'namespace' => 'Form'),
  260. ));
  261. $object = $this->loader->load('ZendLoaderAutoloaderResourceTest', 'form');
  262. $this->assertTrue($object instanceof FooBar_Form_ZendLoaderAutoloaderResourceTest);
  263. $test = $this->loader->load('ZendLoaderAutoloaderResourceTest', 'form');
  264. $this->assertSame($object, $test);
  265. }
  266. public function testAutoloadShouldAllowEmptyNamespacing()
  267. {
  268. $loader = new Zend_Loader_Autoloader_Resource(array(
  269. 'namespace' => '',
  270. 'basePath' => realpath(dirname(__FILE__) . '/_files'),
  271. ));
  272. $loader->addResourceTypes(array(
  273. 'service' => array('path' => 'services', 'namespace' => 'Service'),
  274. ));
  275. $test = $loader->load('ZendLoaderAutoloaderResourceTest', 'service');
  276. $this->assertTrue($test instanceof Service_ZendLoaderAutoloaderResourceTest);
  277. }
  278. public function testPassingClassOfDifferentNamespaceToAutoloadShouldReturnFalse()
  279. {
  280. $this->assertFalse($this->loader->autoload('Foo_Bar_Baz'));
  281. }
  282. public function testPassingClassWithoutBothComponentAndClassSegmentsToAutoloadShouldReturnFalse()
  283. {
  284. $this->assertFalse($this->loader->autoload('FooBar_Baz'));
  285. }
  286. public function testPassingClassWithUnmatchedResourceTypeToAutoloadShouldReturnFalse()
  287. {
  288. $this->assertFalse($this->loader->autoload('FooBar_Baz_Bat'));
  289. }
  290. /**
  291. * @expectedException Zend_Loader_Exception
  292. */
  293. public function testMethodOverloadingShouldRaiseExceptionForNonGetterMethodCalls()
  294. {
  295. $this->loader->lalalalala();
  296. }
  297. /**
  298. * @expectedException Zend_Loader_Exception
  299. */
  300. public function testMethodOverloadingShouldRaiseExceptionWhenRequestedResourceDoesNotExist()
  301. {
  302. $this->loader->getModel('Foo');
  303. }
  304. /**
  305. * @expectedException Zend_Loader_Exception
  306. */
  307. public function testMethodOverloadingShouldRaiseExceptionWhenNoArgumentPassed()
  308. {
  309. $this->loader->addResourceTypes(array(
  310. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  311. ));
  312. $this->loader->getModel();
  313. }
  314. public function testMethodOverloadingShouldReturnObjectOfExpectedType()
  315. {
  316. $this->loader->addResourceTypes(array(
  317. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  318. ));
  319. $test = $this->loader->getModel('ZendLoaderAutoloaderResourceMethodOverloading');
  320. $this->assertTrue($test instanceof FooBar_Model_ZendLoaderAutoloaderResourceMethodOverloading);
  321. }
  322. /**
  323. * @group ZF-7473
  324. */
  325. public function testAutoloaderShouldReceiveNamespaceWithTrailingUnderscore()
  326. {
  327. $al = Zend_Loader_Autoloader::getInstance();
  328. $loaders = $al->getNamespaceAutoloaders('FooBar');
  329. $this->assertTrue(empty($loaders));
  330. $loaders = $al->getNamespaceAutoloaders('FooBar_');
  331. $this->assertFalse(empty($loaders));
  332. $loader = array_shift($loaders);
  333. $this->assertSame($this->loader, $loader);
  334. }
  335. /**
  336. * @group ZF-7501
  337. */
  338. public function testAutoloaderShouldTrimResourceTypePathsForTrailingPathSeparator()
  339. {
  340. $this->loader->addResourceType('models', 'models/', 'Model');
  341. $resources = $this->loader->getResourceTypes();
  342. $this->assertEquals($this->loader->getBasePath() . '/models', $resources['models']['path']);
  343. }
  344. /**
  345. * @group ZF-6727
  346. */
  347. public function testAutoloaderResourceGetClassPath()
  348. {
  349. $this->loader->addResourceTypes(array(
  350. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  351. ));
  352. $path = $this->loader->getClassPath('FooBar_Model_Class_Model');
  353. // if true we have // in path
  354. $this->assertFalse(strpos($path, '//'));
  355. }
  356. /**
  357. * @group ZF-8364
  358. * @group ZF-6727
  359. */
  360. public function testAutoloaderResourceGetClassPathReturnFalse()
  361. {
  362. $this->loader->addResourceTypes(array(
  363. 'model' => array('path' => 'models', 'namespace' => 'Model'),
  364. ));
  365. $path = $this->loader->autoload('Something_Totally_Wrong');
  366. $this->assertFalse($path);
  367. }
  368. /**
  369. * @group ZF-10836
  370. */
  371. public function testConstructorAcceptsNamespaceKeyInAnyOrder()
  372. {
  373. // namespace is after resourceTypes - fails in ZF 1.11.1
  374. $data = array(
  375. 'basePath' => 'path/to/some/directory',
  376. 'resourceTypes' => array(
  377. 'acl' => array(
  378. 'path' => 'acls/',
  379. 'namespace' => 'Acl',
  380. )
  381. ),
  382. 'namespace' => 'My'
  383. );
  384. $loader1 = new Zend_Loader_Autoloader_Resource($data);
  385. // namespace is defined before resourceTypes - always worked as expected
  386. $data = array(
  387. 'basePath' => 'path/to/some/directory',
  388. 'namespace' => 'My',
  389. 'resourceTypes' => array(
  390. 'acl' => array(
  391. 'path' => 'acls/',
  392. 'namespace' => 'Acl',
  393. )
  394. )
  395. );
  396. $loader2 = new Zend_Loader_Autoloader_Resource($data);
  397. // Check that autoloaders are configured the same
  398. $this->assertEquals($loader1, $loader2);
  399. }
  400. /**
  401. * @group ZF-11219
  402. */
  403. public function testMatchesMultiLevelNamespaces()
  404. {
  405. $this->loader->setNamespace('Foo_Bar')
  406. ->setBasePath(dirname(__FILE__) . '/_files')
  407. ->addResourceType('model', 'models', 'Model');
  408. $path = $this->loader->getClassPath('Foo_Bar_Model_Baz');
  409. $this->assertEquals(dirname(__FILE__) . '/_files/models/Baz.php', $path, var_export($path, 1));
  410. }
  411. }
  412. if (PHPUnit_MAIN_METHOD == 'Zend_Loader_Autoloader_ResourceTest::main') {
  413. Zend_Loader_Autoloader_ResourceTest::main();
  414. }