PluginLoaderTest.php 16 KB

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