PluginLoaderTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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-2010 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-2010 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 testAddPrefixPathMultipleTimes()
  106. {
  107. $loader = new Zend_Loader_PluginLoader();
  108. $loader->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  109. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader');
  110. $paths = $loader->getPaths();
  111. $this->assertType('array', $paths);
  112. $this->assertEquals(1, count($paths['Zend_Loader_']));
  113. }
  114. public function testAddPrefixPathStatically()
  115. {
  116. $this->key = 'foobar';
  117. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  118. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  119. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  120. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  121. $paths = $loader->getPaths();
  122. $this->assertEquals(2, count($paths));
  123. $this->assertTrue(array_key_exists('Zend_View_', $paths));
  124. $this->assertTrue(array_key_exists('Zend_Loader_', $paths));
  125. $this->assertEquals(1, count($paths['Zend_View_']));
  126. $this->assertEquals(2, count($paths['Zend_Loader_']));
  127. }
  128. public function testAddPrefixPathThrowsExceptionWithNonStringPrefix()
  129. {
  130. $loader = new Zend_Loader_PluginLoader();
  131. try {
  132. $loader->addPrefixPath(array(), $this->libPath);
  133. $this->fail('addPrefixPath() should throw exception with non-string prefix');
  134. } catch (Exception $e) {
  135. }
  136. }
  137. public function testAddPrefixPathThrowsExceptionWithNonStringPath()
  138. {
  139. $loader = new Zend_Loader_PluginLoader();
  140. try {
  141. $loader->addPrefixPath('Foo_Bar', array());
  142. $this->fail('addPrefixPath() should throw exception with non-string path');
  143. } catch (Exception $e) {
  144. }
  145. }
  146. public function testRemoveAllPathsForGivenPrefixNonStatically()
  147. {
  148. $loader = new Zend_Loader_PluginLoader();
  149. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  150. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  151. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  152. $paths = $loader->getPaths('Zend_Loader');
  153. $this->assertEquals(2, count($paths));
  154. $loader->removePrefixPath('Zend_Loader');
  155. $this->assertFalse($loader->getPaths('Zend_Loader'));
  156. }
  157. public function testRemoveAllPathsForGivenPrefixStatically()
  158. {
  159. $this->key = 'foobar';
  160. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  161. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  162. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  163. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  164. $paths = $loader->getPaths('Zend_Loader');
  165. $this->assertEquals(2, count($paths));
  166. $loader->removePrefixPath('Zend_Loader');
  167. $this->assertFalse($loader->getPaths('Zend_Loader'));
  168. }
  169. public function testRemovePrefixPathThrowsExceptionIfPrefixNotRegistered()
  170. {
  171. $loader = new Zend_Loader_PluginLoader();
  172. try {
  173. $loader->removePrefixPath('Foo_Bar');
  174. $this->fail('Removing non-existent prefix should throw an exception');
  175. } catch (Exception $e) {
  176. }
  177. }
  178. public function testRemovePrefixPathThrowsExceptionIfPrefixPathPairNotRegistered()
  179. {
  180. $loader = new Zend_Loader_PluginLoader();
  181. $loader->addPrefixPath('Foo_Bar', realpath(dirname(__FILE__)));
  182. $paths = $loader->getPaths();
  183. $this->assertTrue(isset($paths['Foo_Bar_']));
  184. try {
  185. $loader->removePrefixPath('Foo_Bar', $this->libPath);
  186. $this->fail('Removing non-existent prefix/path pair should throw an exception');
  187. } catch (Exception $e) {
  188. }
  189. }
  190. public function testClearPathsNonStaticallyClearsPathArray()
  191. {
  192. $loader = new Zend_Loader_PluginLoader();
  193. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  194. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  195. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  196. $paths = $loader->getPaths();
  197. $this->assertEquals(2, count($paths));
  198. $loader->clearPaths();
  199. $paths = $loader->getPaths();
  200. $this->assertEquals(0, count($paths));
  201. }
  202. public function testClearPathsStaticallyClearsPathArray()
  203. {
  204. $this->key = 'foobar';
  205. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  206. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  207. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  208. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  209. $paths = $loader->getPaths();
  210. $this->assertEquals(2, count($paths));
  211. $loader->clearPaths();
  212. $paths = $loader->getPaths();
  213. $this->assertEquals(0, count($paths));
  214. }
  215. public function testClearPathsWithPrefixNonStaticallyClearsPathArray()
  216. {
  217. $loader = new Zend_Loader_PluginLoader();
  218. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  219. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  220. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  221. $paths = $loader->getPaths();
  222. $this->assertEquals(2, count($paths));
  223. $loader->clearPaths('Zend_Loader');
  224. $paths = $loader->getPaths();
  225. $this->assertEquals(1, count($paths));
  226. }
  227. public function testClearPathsWithPrefixStaticallyClearsPathArray()
  228. {
  229. $this->key = 'foobar';
  230. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  231. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  232. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  233. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  234. $paths = $loader->getPaths();
  235. $this->assertEquals(2, count($paths));
  236. $loader->clearPaths('Zend_Loader');
  237. $paths = $loader->getPaths();
  238. $this->assertEquals(1, count($paths));
  239. }
  240. public function testGetClassNameNonStaticallyReturnsFalseWhenClassNotLoaded()
  241. {
  242. $loader = new Zend_Loader_PluginLoader();
  243. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  244. $this->assertFalse($loader->getClassName('FormElement'));
  245. }
  246. public function testGetClassNameStaticallyReturnsFalseWhenClassNotLoaded()
  247. {
  248. $this->key = 'foobar';
  249. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  250. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  251. $this->assertFalse($loader->getClassName('FormElement'));
  252. }
  253. public function testLoadPluginNonStaticallyLoadsClass()
  254. {
  255. $loader = new Zend_Loader_PluginLoader();
  256. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  257. try {
  258. $className = $loader->load('FormButton');
  259. } catch (Exception $e) {
  260. $paths = $loader->getPaths();
  261. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  262. }
  263. $this->assertEquals('Zend_View_Helper_FormButton', $className);
  264. $this->assertTrue(class_exists('Zend_View_Helper_FormButton', false));
  265. $this->assertTrue($loader->isLoaded('FormButton'));
  266. }
  267. public function testLoadPluginStaticallyLoadsClass()
  268. {
  269. $this->key = 'foobar';
  270. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  271. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  272. try {
  273. $className = $loader->load('FormRadio');
  274. } catch (Exception $e) {
  275. $paths = $loader->getPaths();
  276. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  277. }
  278. $this->assertEquals('Zend_View_Helper_FormRadio', $className);
  279. $this->assertTrue(class_exists('Zend_View_Helper_FormRadio', false));
  280. $this->assertTrue($loader->isLoaded('FormRadio'));
  281. }
  282. public function testLoadThrowsExceptionIfFileFoundInPrefixButClassNotLoaded()
  283. {
  284. $loader = new Zend_Loader_PluginLoader();
  285. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  286. try {
  287. $className = $loader->load('Doctype');
  288. $this->fail('Invalid prefix for a path should throw an exception');
  289. } catch (Exception $e) {
  290. }
  291. }
  292. public function testLoadThrowsExceptionIfNoHelperClassLoaded()
  293. {
  294. $loader = new Zend_Loader_PluginLoader();
  295. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  296. try {
  297. $className = $loader->load('FooBarBazBat');
  298. $this->fail('Not finding a helper should throw an exception');
  299. } catch (Exception $e) {
  300. }
  301. }
  302. public function testGetClassAfterNonStaticLoadReturnsResolvedClassName()
  303. {
  304. $loader = new Zend_Loader_PluginLoader();
  305. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  306. try {
  307. $className = $loader->load('FormSelect');
  308. } catch (Exception $e) {
  309. $paths = $loader->getPaths();
  310. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  311. }
  312. $this->assertEquals($className, $loader->getClassName('FormSelect'));
  313. $this->assertEquals('Zend_View_Helper_FormSelect', $loader->getClassName('FormSelect'));
  314. }
  315. public function testGetClassAfterStaticLoadReturnsResolvedClassName()
  316. {
  317. $this->key = 'foobar';
  318. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  319. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  320. try {
  321. $className = $loader->load('FormCheckbox');
  322. } catch (Exception $e) {
  323. $paths = $loader->getPaths();
  324. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  325. }
  326. $this->assertEquals($className, $loader->getClassName('FormCheckbox'));
  327. $this->assertEquals('Zend_View_Helper_FormCheckbox', $loader->getClassName('FormCheckbox'));
  328. }
  329. public function testClassFilesAreSearchedInLifoOrder()
  330. {
  331. $loader = new Zend_Loader_PluginLoader(array());
  332. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  333. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  334. try {
  335. $className = $loader->load('FormSubmit');
  336. } catch (Exception $e) {
  337. $paths = $loader->getPaths();
  338. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  339. }
  340. $this->assertEquals($className, $loader->getClassName('FormSubmit'));
  341. $this->assertEquals('ZfTest_FormSubmit', $loader->getClassName('FormSubmit'));
  342. }
  343. /**
  344. * @issue ZF-2741
  345. */
  346. public function testWin32UnderscoreSpacedShortNamesWillLoad()
  347. {
  348. $loader = new Zend_Loader_PluginLoader(array());
  349. $loader->addPrefixPath('Zend_Filter', $this->libPath . '/Zend/Filter');
  350. try {
  351. // Plugin loader will attempt to load "c:\path\to\library/Zend/Filter/Word\UnderscoreToDash.php"
  352. $className = $loader->load('Word_UnderscoreToDash');
  353. } catch (Exception $e) {
  354. $paths = $loader->getPaths();
  355. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  356. }
  357. $this->assertEquals($className, $loader->getClassName('Word_UnderscoreToDash'));
  358. }
  359. /**
  360. * @group ZF-4670
  361. */
  362. public function testIncludeCacheShouldBeNullByDefault()
  363. {
  364. $this->assertNull(Zend_Loader_PluginLoader::getIncludeFileCache());
  365. }
  366. /**
  367. * @group ZF-4670
  368. */
  369. public function testPluginLoaderShouldAllowSpecifyingIncludeFileCache()
  370. {
  371. $cacheFile = $this->_includeCache;
  372. $this->testIncludeCacheShouldBeNullByDefault();
  373. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  374. $this->assertEquals($cacheFile, Zend_Loader_PluginLoader::getIncludeFileCache());
  375. }
  376. /**
  377. * @group ZF-4670
  378. * @expectedException Zend_Loader_PluginLoader_Exception
  379. */
  380. public function testPluginLoaderShouldThrowExceptionWhenPathDoesNotExist()
  381. {
  382. $cacheFile = dirname(__FILE__) . '/_filesDoNotExist/includeCache.inc.php';
  383. $this->testIncludeCacheShouldBeNullByDefault();
  384. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  385. $this->fail('Should not allow specifying invalid cache file path');
  386. }
  387. /**
  388. * @group ZF-4670
  389. */
  390. public function testPluginLoaderShouldAppendIncludeCacheWhenClassIsFound()
  391. {
  392. $cacheFile = $this->_includeCache;
  393. Zend_Loader_PluginLoader::setIncludeFileCache($cacheFile);
  394. $loader = new Zend_Loader_PluginLoader(array());
  395. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  396. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  397. try {
  398. $className = $loader->load('CacheTest');
  399. } catch (Exception $e) {
  400. $paths = $loader->getPaths();
  401. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  402. }
  403. $this->assertTrue(file_exists($cacheFile));
  404. $cache = file_get_contents($cacheFile);
  405. $this->assertContains('CacheTest.php', $cache);
  406. }
  407. /**
  408. * @group ZF-5208
  409. */
  410. public function testStaticRegistryNamePersistsInDifferentLoaderObjects()
  411. {
  412. $loader1 = new Zend_Loader_PluginLoader(array(), "PluginLoaderStaticNamespace");
  413. $loader1->addPrefixPath("Zend_View_Helper", "Zend/View/Helper");
  414. $loader2 = new Zend_Loader_PluginLoader(array(), "PluginLoaderStaticNamespace");
  415. $this->assertEquals(array(
  416. "Zend_View_Helper_" => array("Zend/View/Helper/"),
  417. ), $loader2->getPaths());
  418. }
  419. /**
  420. * @group ZF-4697
  421. */
  422. public function testClassFilesGrabCorrectPathForLoadedClasses()
  423. {
  424. require_once 'Zend/View/Helper/DeclareVars.php';
  425. $reflection = new ReflectionClass('Zend_View_Helper_DeclareVars');
  426. $expected = $reflection->getFileName();
  427. $loader = new Zend_Loader_PluginLoader(array());
  428. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  429. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  430. try {
  431. // Class in /Zend/View/Helper and not in /_files/ZfTest
  432. $className = $loader->load('DeclareVars');
  433. } catch (Exception $e) {
  434. $paths = $loader->getPaths();
  435. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  436. }
  437. $classPath = $loader->getClassPath('DeclareVars');
  438. $this->assertContains($expected, $classPath);
  439. }
  440. /**
  441. * @group ZF-7350
  442. */
  443. public function testPrefixesEndingInBackslashDenoteNamespacedClasses()
  444. {
  445. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  446. $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
  447. return;
  448. }
  449. $loader = new Zend_Loader_PluginLoader(array());
  450. $loader->addPrefixPath('Zfns\\', dirname(__FILE__) . '/_files/Zfns');
  451. try {
  452. $className = $loader->load('Foo');
  453. } catch (Exception $e) {
  454. $paths = $loader->getPaths();
  455. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  456. }
  457. $this->assertEquals('Zfns\\Foo', $className);
  458. $this->assertEquals('Zfns\\Foo', $loader->getClassName('Foo'));
  459. }
  460. }
  461. // Call Zend_Loader_PluginLoaderTest::main() if this source file is executed directly.
  462. if (PHPUnit_MAIN_METHOD === 'Zend_Loader_PluginLoaderTest::main') {
  463. Zend_Loader_PluginLoaderTest::main();
  464. }