PluginLoaderTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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-2009 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. // Call Zend_Loader_PluginLoaderTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_Loader_PluginLoaderTest::main');
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../../TestHelper.php';
  30. require_once 'Zend/Loader/PluginLoader.php';
  31. /**
  32. * Test class for Zend_Loader_PluginLoader.
  33. *
  34. * @category Zend
  35. * @package Zend_Loader
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_Loader
  40. */
  41. class Zend_Loader_PluginLoaderTest extends PHPUnit_Framework_TestCase
  42. {
  43. protected $_includeCache;
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. require_once "PHPUnit/TextUI/TestRunner.php";
  52. $suite = new PHPUnit_Framework_TestSuite("Zend_Loader_PluginLoaderTest");
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. /**
  56. * Sets up the fixture, for example, open a network connection.
  57. * This method is called before a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function setUp()
  62. {
  63. if (file_exists($this->_includeCache)) {
  64. unlink($this->_includeCache);
  65. }
  66. Zend_Loader_PluginLoader::setIncludeFileCache(null);
  67. $this->_includeCache = dirname(__FILE__) . '/_files/includeCache.inc.php';
  68. $this->libPath = realpath(dirname(__FILE__) . '/../../../library');
  69. $this->key = null;
  70. }
  71. /**
  72. * Tears down the fixture, for example, close a network connection.
  73. * This method is called after a test is executed.
  74. *
  75. * @return void
  76. */
  77. public function tearDown()
  78. {
  79. $this->clearStaticPaths();
  80. Zend_Loader_PluginLoader::setIncludeFileCache(null);
  81. if (file_exists($this->_includeCache)) {
  82. unlink($this->_includeCache);
  83. }
  84. }
  85. public function clearStaticPaths()
  86. {
  87. if (null !== $this->key) {
  88. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  89. $loader->clearPaths();
  90. }
  91. }
  92. public function testAddPrefixPathNonStatically()
  93. {
  94. $loader = new Zend_Loader_PluginLoader();
  95. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  96. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  97. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  98. $paths = $loader->getPaths();
  99. $this->assertEquals(2, count($paths));
  100. $this->assertTrue(array_key_exists('Zend_View_', $paths));
  101. $this->assertTrue(array_key_exists('Zend_Loader_', $paths));
  102. $this->assertEquals(1, count($paths['Zend_View_']));
  103. $this->assertEquals(2, count($paths['Zend_Loader_']));
  104. }
  105. public function testAddPrefixPathStatically()
  106. {
  107. $this->key = 'foobar';
  108. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  109. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  110. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  111. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  112. $paths = $loader->getPaths();
  113. $this->assertEquals(2, count($paths));
  114. $this->assertTrue(array_key_exists('Zend_View_', $paths));
  115. $this->assertTrue(array_key_exists('Zend_Loader_', $paths));
  116. $this->assertEquals(1, count($paths['Zend_View_']));
  117. $this->assertEquals(2, count($paths['Zend_Loader_']));
  118. }
  119. public function testAddPrefixPathThrowsExceptionWithNonStringPrefix()
  120. {
  121. $loader = new Zend_Loader_PluginLoader();
  122. try {
  123. $loader->addPrefixPath(array(), $this->libPath);
  124. $this->fail('addPrefixPath() should throw exception with non-string prefix');
  125. } catch (Exception $e) {
  126. }
  127. }
  128. public function testAddPrefixPathThrowsExceptionWithNonStringPath()
  129. {
  130. $loader = new Zend_Loader_PluginLoader();
  131. try {
  132. $loader->addPrefixPath('Foo_Bar', array());
  133. $this->fail('addPrefixPath() should throw exception with non-string path');
  134. } catch (Exception $e) {
  135. }
  136. }
  137. public function testRemoveAllPathsForGivenPrefixNonStatically()
  138. {
  139. $loader = new Zend_Loader_PluginLoader();
  140. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  141. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  142. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  143. $paths = $loader->getPaths('Zend_Loader');
  144. $this->assertEquals(2, count($paths));
  145. $loader->removePrefixPath('Zend_Loader');
  146. $this->assertFalse($loader->getPaths('Zend_Loader'));
  147. }
  148. public function testRemoveAllPathsForGivenPrefixStatically()
  149. {
  150. $this->key = 'foobar';
  151. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  152. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  153. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  154. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  155. $paths = $loader->getPaths('Zend_Loader');
  156. $this->assertEquals(2, count($paths));
  157. $loader->removePrefixPath('Zend_Loader');
  158. $this->assertFalse($loader->getPaths('Zend_Loader'));
  159. }
  160. public function testRemovePrefixPathThrowsExceptionIfPrefixNotRegistered()
  161. {
  162. $loader = new Zend_Loader_PluginLoader();
  163. try {
  164. $loader->removePrefixPath('Foo_Bar');
  165. $this->fail('Removing non-existent prefix should throw an exception');
  166. } catch (Exception $e) {
  167. }
  168. }
  169. public function testRemovePrefixPathThrowsExceptionIfPrefixPathPairNotRegistered()
  170. {
  171. $loader = new Zend_Loader_PluginLoader();
  172. $loader->addPrefixPath('Foo_Bar', realpath(dirname(__FILE__)));
  173. $paths = $loader->getPaths();
  174. $this->assertTrue(isset($paths['Foo_Bar_']));
  175. try {
  176. $loader->removePrefixPath('Foo_Bar', $this->libPath);
  177. $this->fail('Removing non-existent prefix/path pair should throw an exception');
  178. } catch (Exception $e) {
  179. }
  180. }
  181. public function testClearPathsNonStaticallyClearsPathArray()
  182. {
  183. $loader = new Zend_Loader_PluginLoader();
  184. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  185. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  186. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  187. $paths = $loader->getPaths();
  188. $this->assertEquals(2, count($paths));
  189. $loader->clearPaths();
  190. $paths = $loader->getPaths();
  191. $this->assertEquals(0, count($paths));
  192. }
  193. public function testClearPathsStaticallyClearsPathArray()
  194. {
  195. $this->key = 'foobar';
  196. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  197. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  198. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  199. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  200. $paths = $loader->getPaths();
  201. $this->assertEquals(2, count($paths));
  202. $loader->clearPaths();
  203. $paths = $loader->getPaths();
  204. $this->assertEquals(0, count($paths));
  205. }
  206. public function testClearPathsWithPrefixNonStaticallyClearsPathArray()
  207. {
  208. $loader = new Zend_Loader_PluginLoader();
  209. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  210. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  211. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  212. $paths = $loader->getPaths();
  213. $this->assertEquals(2, count($paths));
  214. $loader->clearPaths('Zend_Loader');
  215. $paths = $loader->getPaths();
  216. $this->assertEquals(1, count($paths));
  217. }
  218. public function testClearPathsWithPrefixStaticallyClearsPathArray()
  219. {
  220. $this->key = 'foobar';
  221. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  222. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  223. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  224. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  225. $paths = $loader->getPaths();
  226. $this->assertEquals(2, count($paths));
  227. $loader->clearPaths('Zend_Loader');
  228. $paths = $loader->getPaths();
  229. $this->assertEquals(1, count($paths));
  230. }
  231. public function testGetClassNameNonStaticallyReturnsFalseWhenClassNotLoaded()
  232. {
  233. $loader = new Zend_Loader_PluginLoader();
  234. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  235. $this->assertFalse($loader->getClassName('FormElement'));
  236. }
  237. public function testGetClassNameStaticallyReturnsFalseWhenClassNotLoaded()
  238. {
  239. $this->key = 'foobar';
  240. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  241. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  242. $this->assertFalse($loader->getClassName('FormElement'));
  243. }
  244. public function testLoadPluginNonStaticallyLoadsClass()
  245. {
  246. $loader = new Zend_Loader_PluginLoader();
  247. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  248. try {
  249. $className = $loader->load('FormButton');
  250. } catch (Exception $e) {
  251. $paths = $loader->getPaths();
  252. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  253. }
  254. $this->assertEquals('Zend_View_Helper_FormButton', $className);
  255. $this->assertTrue(class_exists('Zend_View_Helper_FormButton', false));
  256. $this->assertTrue($loader->isLoaded('FormButton'));
  257. }
  258. public function testLoadPluginStaticallyLoadsClass()
  259. {
  260. $this->key = 'foobar';
  261. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  262. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  263. try {
  264. $className = $loader->load('FormRadio');
  265. } catch (Exception $e) {
  266. $paths = $loader->getPaths();
  267. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  268. }
  269. $this->assertEquals('Zend_View_Helper_FormRadio', $className);
  270. $this->assertTrue(class_exists('Zend_View_Helper_FormRadio', false));
  271. $this->assertTrue($loader->isLoaded('FormRadio'));
  272. }
  273. public function testLoadThrowsExceptionIfFileFoundInPrefixButClassNotLoaded()
  274. {
  275. $loader = new Zend_Loader_PluginLoader();
  276. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  277. try {
  278. $className = $loader->load('Doctype');
  279. $this->fail('Invalid prefix for a path should throw an exception');
  280. } catch (Exception $e) {
  281. }
  282. }
  283. public function testLoadThrowsExceptionIfNoHelperClassLoaded()
  284. {
  285. $loader = new Zend_Loader_PluginLoader();
  286. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  287. try {
  288. $className = $loader->load('FooBarBazBat');
  289. $this->fail('Not finding a helper should throw an exception');
  290. } catch (Exception $e) {
  291. }
  292. }
  293. public function testGetClassAfterNonStaticLoadReturnsResolvedClassName()
  294. {
  295. $loader = new Zend_Loader_PluginLoader();
  296. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  297. try {
  298. $className = $loader->load('FormSelect');
  299. } catch (Exception $e) {
  300. $paths = $loader->getPaths();
  301. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  302. }
  303. $this->assertEquals($className, $loader->getClassName('FormSelect'));
  304. $this->assertEquals('Zend_View_Helper_FormSelect', $loader->getClassName('FormSelect'));
  305. }
  306. public function testGetClassAfterStaticLoadReturnsResolvedClassName()
  307. {
  308. $this->key = 'foobar';
  309. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  310. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  311. try {
  312. $className = $loader->load('FormCheckbox');
  313. } catch (Exception $e) {
  314. $paths = $loader->getPaths();
  315. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  316. }
  317. $this->assertEquals($className, $loader->getClassName('FormCheckbox'));
  318. $this->assertEquals('Zend_View_Helper_FormCheckbox', $loader->getClassName('FormCheckbox'));
  319. }
  320. public function testClassFilesAreSearchedInLifoOrder()
  321. {
  322. $loader = new Zend_Loader_PluginLoader(array());
  323. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  324. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  325. try {
  326. $className = $loader->load('FormSubmit');
  327. } catch (Exception $e) {
  328. $paths = $loader->getPaths();
  329. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  330. }
  331. $this->assertEquals($className, $loader->getClassName('FormSubmit'));
  332. $this->assertEquals('ZfTest_FormSubmit', $loader->getClassName('FormSubmit'));
  333. }
  334. /**
  335. * @issue ZF-2741
  336. */
  337. public function testWin32UnderscoreSpacedShortNamesWillLoad()
  338. {
  339. $loader = new Zend_Loader_PluginLoader(array());
  340. $loader->addPrefixPath('Zend_Filter', $this->libPath . '/Zend/Filter');
  341. try {
  342. // Plugin loader will attempt to load "c:\path\to\library/Zend/Filter/Word\UnderscoreToDash.php"
  343. $className = $loader->load('Word_UnderscoreToDash');
  344. } catch (Exception $e) {
  345. $paths = $loader->getPaths();
  346. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  347. }
  348. $this->assertEquals($className, $loader->getClassName('Word_UnderscoreToDash'));
  349. }
  350. /**
  351. * @group ZF-4670
  352. */
  353. public function testIncludeCacheShouldBeNullByDefault()
  354. {
  355. $this->assertNull(Zend_Loader_PluginLoader::getIncludeFileCache());
  356. }
  357. /**
  358. * @group ZF-4670
  359. */
  360. public function testPluginLoaderShouldAllowSpecifyingIncludeFileCache()
  361. {
  362. $cacheFile = $this->_includeCache;
  363. $this->testIncludeCacheShouldBeNullByDefault();
  364. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  365. $this->assertEquals($cacheFile, Zend_Loader_PluginLoader::getIncludeFileCache());
  366. }
  367. /**
  368. * @group ZF-4670
  369. * @expectedException Zend_Loader_PluginLoader_Exception
  370. */
  371. public function testPluginLoaderShouldThrowExceptionWhenPathDoesNotExist()
  372. {
  373. $cacheFile = dirname(__FILE__) . '/_filesDoNotExist/includeCache.inc.php';
  374. $this->testIncludeCacheShouldBeNullByDefault();
  375. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  376. $this->fail('Should not allow specifying invalid cache file path');
  377. }
  378. /**
  379. * @group ZF-4670
  380. */
  381. public function testPluginLoaderShouldAppendIncludeCacheWhenClassIsFound()
  382. {
  383. $cacheFile = $this->_includeCache;
  384. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  385. $loader = new Zend_Loader_PluginLoader(array());
  386. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  387. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  388. try {
  389. $className = $loader->load('CacheTest');
  390. } catch (Exception $e) {
  391. $paths = $loader->getPaths();
  392. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  393. }
  394. $this->assertTrue(file_exists($cacheFile));
  395. $cache = file_get_contents($cacheFile);
  396. $this->assertContains('CacheTest.php', $cache);
  397. }
  398. /**
  399. * @group ZF-5208
  400. */
  401. public function testStaticRegistryNamePersistsInDifferentLoaderObjects()
  402. {
  403. $loader1 = new Zend_Loader_PluginLoader(array(), "PluginLoaderStaticNamespace");
  404. $loader1->addPrefixPath("Zend_View_Helper", "Zend/View/Helper");
  405. $loader2 = new Zend_Loader_PluginLoader(array(), "PluginLoaderStaticNamespace");
  406. $this->assertEquals(array(
  407. "Zend_View_Helper_" => array("Zend/View/Helper/"),
  408. ), $loader2->getPaths());
  409. }
  410. }
  411. // Call Zend_Loader_PluginLoaderTest::main() if this source file is executed directly.
  412. if (PHPUnit_MAIN_METHOD === 'Zend_Loader_PluginLoaderTest::main') {
  413. Zend_Loader_PluginLoaderTest::main();
  414. }