/standard/tags/release-1.5.0/tests/Zend/Loader/PluginLoaderTest.php

https://github.com/bhaumik25/zend-framework · PHP · 348 lines · 288 code · 31 blank · 29 comment · 3 complexity · be474f99c6fc4a82274d157d5db37167 MD5 · raw file

  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. /**
  17. * Runs the test methods of this class.
  18. *
  19. * @return void
  20. */
  21. public static function main()
  22. {
  23. require_once "PHPUnit/TextUI/TestRunner.php";
  24. $suite = new PHPUnit_Framework_TestSuite("Zend_Loader_PluginLoaderTest");
  25. $result = PHPUnit_TextUI_TestRunner::run($suite);
  26. }
  27. /**
  28. * Sets up the fixture, for example, open a network connection.
  29. * This method is called before a test is executed.
  30. *
  31. * @return void
  32. */
  33. public function setUp()
  34. {
  35. $this->libPath = realpath(dirname(__FILE__) . '/../../../library');
  36. $this->key = null;
  37. }
  38. /**
  39. * Tears down the fixture, for example, close a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. $this->clearStaticPaths();
  47. }
  48. public function clearStaticPaths()
  49. {
  50. if (null !== $this->key) {
  51. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  52. $loader->clearPaths();
  53. }
  54. }
  55. public function testAddPrefixPathNonStatically()
  56. {
  57. $loader = new Zend_Loader_PluginLoader();
  58. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  59. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  60. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  61. $paths = $loader->getPaths();
  62. $this->assertEquals(2, count($paths));
  63. $this->assertTrue(array_key_exists('Zend_View_', $paths));
  64. $this->assertTrue(array_key_exists('Zend_Loader_', $paths));
  65. $this->assertEquals(1, count($paths['Zend_View_']));
  66. $this->assertEquals(2, count($paths['Zend_Loader_']));
  67. }
  68. public function testAddPrefixPathStatically()
  69. {
  70. $this->key = 'foobar';
  71. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  72. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  73. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  74. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  75. $paths = $loader->getPaths();
  76. $this->assertEquals(2, count($paths));
  77. $this->assertTrue(array_key_exists('Zend_View_', $paths));
  78. $this->assertTrue(array_key_exists('Zend_Loader_', $paths));
  79. $this->assertEquals(1, count($paths['Zend_View_']));
  80. $this->assertEquals(2, count($paths['Zend_Loader_']));
  81. }
  82. public function testAddPrefixPathThrowsExceptionWithNonStringPrefix()
  83. {
  84. $loader = new Zend_Loader_PluginLoader();
  85. try {
  86. $loader->addPrefixPath(array(), $this->libPath);
  87. $this->fail('addPrefixPath() should throw exception with non-string prefix');
  88. } catch (Exception $e) {
  89. }
  90. }
  91. public function testAddPrefixPathThrowsExceptionWithNonStringPath()
  92. {
  93. $loader = new Zend_Loader_PluginLoader();
  94. try {
  95. $loader->addPrefixPath('Foo_Bar', array());
  96. $this->fail('addPrefixPath() should throw exception with non-string path');
  97. } catch (Exception $e) {
  98. }
  99. }
  100. public function testRemoveAllPathsForGivenPrefixNonStatically()
  101. {
  102. $loader = new Zend_Loader_PluginLoader();
  103. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  104. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  105. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  106. $paths = $loader->getPaths('Zend_Loader');
  107. $this->assertEquals(2, count($paths));
  108. $loader->removePrefixPath('Zend_Loader');
  109. $this->assertFalse($loader->getPaths('Zend_Loader'));
  110. }
  111. public function testRemoveAllPathsForGivenPrefixStatically()
  112. {
  113. $this->key = 'foobar';
  114. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  115. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  116. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  117. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  118. $paths = $loader->getPaths('Zend_Loader');
  119. $this->assertEquals(2, count($paths));
  120. $loader->removePrefixPath('Zend_Loader');
  121. $this->assertFalse($loader->getPaths('Zend_Loader'));
  122. }
  123. public function testRemovePrefixPathThrowsExceptionIfPrefixNotRegistered()
  124. {
  125. $loader = new Zend_Loader_PluginLoader();
  126. try {
  127. $loader->removePrefixPath('Foo_Bar');
  128. $this->fail('Removing non-existent prefix should throw an exception');
  129. } catch (Exception $e) {
  130. }
  131. }
  132. public function testRemovePrefixPathThrowsExceptionIfPrefixPathPairNotRegistered()
  133. {
  134. $loader = new Zend_Loader_PluginLoader();
  135. $loader->addPrefixPath('Foo_Bar', realpath(dirname(__FILE__)));
  136. $paths = $loader->getPaths();
  137. $this->assertTrue(isset($paths['Foo_Bar_']));
  138. try {
  139. $loader->removePrefixPath('Foo_Bar', $this->libPath);
  140. $this->fail('Removing non-existent prefix/path pair should throw an exception');
  141. } catch (Exception $e) {
  142. }
  143. }
  144. public function testClearPathsNonStaticallyClearsPathArray()
  145. {
  146. $loader = new Zend_Loader_PluginLoader();
  147. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  148. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  149. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  150. $paths = $loader->getPaths();
  151. $this->assertEquals(2, count($paths));
  152. $loader->clearPaths();
  153. $paths = $loader->getPaths();
  154. $this->assertEquals(0, count($paths));
  155. }
  156. public function testClearPathsStaticallyClearsPathArray()
  157. {
  158. $this->key = 'foobar';
  159. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  160. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  161. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  162. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  163. $paths = $loader->getPaths();
  164. $this->assertEquals(2, count($paths));
  165. $loader->clearPaths();
  166. $paths = $loader->getPaths();
  167. $this->assertEquals(0, count($paths));
  168. }
  169. public function testClearPathsWithPrefixNonStaticallyClearsPathArray()
  170. {
  171. $loader = new Zend_Loader_PluginLoader();
  172. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  173. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  174. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  175. $paths = $loader->getPaths();
  176. $this->assertEquals(2, count($paths));
  177. $loader->clearPaths('Zend_Loader');
  178. $paths = $loader->getPaths();
  179. $this->assertEquals(1, count($paths));
  180. }
  181. public function testClearPathsWithPrefixStaticallyClearsPathArray()
  182. {
  183. $this->key = 'foobar';
  184. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  185. $loader->addPrefixPath('Zend_View', $this->libPath . '/Zend/View')
  186. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend/Loader')
  187. ->addPrefixPath('Zend_Loader', $this->libPath . '/Zend');
  188. $paths = $loader->getPaths();
  189. $this->assertEquals(2, count($paths));
  190. $loader->clearPaths('Zend_Loader');
  191. $paths = $loader->getPaths();
  192. $this->assertEquals(1, count($paths));
  193. }
  194. public function testGetClassNameNonStaticallyReturnsFalseWhenClassNotLoaded()
  195. {
  196. $loader = new Zend_Loader_PluginLoader();
  197. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  198. $this->assertFalse($loader->getClassName('FormElement'));
  199. }
  200. public function testGetClassNameStaticallyReturnsFalseWhenClassNotLoaded()
  201. {
  202. $this->key = 'foobar';
  203. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  204. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  205. $this->assertFalse($loader->getClassName('FormElement'));
  206. }
  207. public function testLoadPluginNonStaticallyLoadsClass()
  208. {
  209. $loader = new Zend_Loader_PluginLoader();
  210. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  211. try {
  212. $className = $loader->load('FormButton');
  213. } catch (Exception $e) {
  214. $paths = $loader->getPaths();
  215. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  216. }
  217. $this->assertEquals('Zend_View_Helper_FormButton', $className);
  218. $this->assertTrue(class_exists('Zend_View_Helper_FormButton', false));
  219. $this->assertTrue($loader->isLoaded('FormButton'));
  220. }
  221. public function testLoadPluginStaticallyLoadsClass()
  222. {
  223. $this->key = 'foobar';
  224. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  225. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  226. try {
  227. $className = $loader->load('FormRadio');
  228. } catch (Exception $e) {
  229. $paths = $loader->getPaths();
  230. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  231. }
  232. $this->assertEquals('Zend_View_Helper_FormRadio', $className);
  233. $this->assertTrue(class_exists('Zend_View_Helper_FormRadio', false));
  234. $this->assertTrue($loader->isLoaded('FormRadio'));
  235. }
  236. public function testLoadThrowsExceptionIfFileFoundInPrefixButClassNotLoaded()
  237. {
  238. $loader = new Zend_Loader_PluginLoader();
  239. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  240. try {
  241. $className = $loader->load('DocType');
  242. $this->fail('Invalid prefix for a path should throw an exception');
  243. } catch (Exception $e) {
  244. }
  245. }
  246. public function testLoadThrowsExceptionIfNoHelperClassLoaded()
  247. {
  248. $loader = new Zend_Loader_PluginLoader();
  249. $loader->addPrefixPath('Foo_Helper', $this->libPath . '/Zend/View/Helper');
  250. try {
  251. $className = $loader->load('FooBarBazBat');
  252. $this->fail('Not finding a helper should throw an exception');
  253. } catch (Exception $e) {
  254. }
  255. }
  256. public function testGetClassAfterNonStaticLoadReturnsResolvedClassName()
  257. {
  258. $loader = new Zend_Loader_PluginLoader();
  259. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  260. try {
  261. $className = $loader->load('FormSelect');
  262. } catch (Exception $e) {
  263. $paths = $loader->getPaths();
  264. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  265. }
  266. $this->assertEquals($className, $loader->getClassName('FormSelect'));
  267. $this->assertEquals('Zend_View_Helper_FormSelect', $loader->getClassName('FormSelect'));
  268. }
  269. public function testGetClassAfterStaticLoadReturnsResolvedClassName()
  270. {
  271. $this->key = 'foobar';
  272. $loader = new Zend_Loader_PluginLoader(array(), $this->key);
  273. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  274. try {
  275. $className = $loader->load('FormCheckbox');
  276. } catch (Exception $e) {
  277. $paths = $loader->getPaths();
  278. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  279. }
  280. $this->assertEquals($className, $loader->getClassName('FormCheckbox'));
  281. $this->assertEquals('Zend_View_Helper_FormCheckbox', $loader->getClassName('FormCheckbox'));
  282. }
  283. public function testClassFilesAreSearchedInLifoOrder()
  284. {
  285. $loader = new Zend_Loader_PluginLoader(array());
  286. $loader->addPrefixPath('Zend_View_Helper', $this->libPath . '/Zend/View/Helper');
  287. $loader->addPrefixPath('ZfTest', dirname(__FILE__) . '/_files/ZfTest');
  288. try {
  289. $className = $loader->load('FormSubmit');
  290. } catch (Exception $e) {
  291. $paths = $loader->getPaths();
  292. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  293. }
  294. $this->assertEquals($className, $loader->getClassName('FormSubmit'));
  295. $this->assertEquals('ZfTest_FormSubmit', $loader->getClassName('FormSubmit'));
  296. }
  297. /**
  298. * @issue ZF-2741
  299. */
  300. public function testWin32UnderscoreSpacedShortNamesWillLoad()
  301. {
  302. $loader = new Zend_Loader_PluginLoader(array());
  303. $loader->addPrefixPath('Zend_Filter', $this->libPath . '/Zend/Filter');
  304. try {
  305. // Plugin loader will attempt to load "c:\path\to\library/Zend/Filter/Word\UnderscoreToDash.php"
  306. $className = $loader->load('Word_UnderscoreToDash');
  307. } catch (Exception $e) {
  308. $paths = $loader->getPaths();
  309. $this->fail(sprintf("Failed loading helper; paths: %s", var_export($paths, 1)));
  310. }
  311. $this->assertEquals($className, $loader->getClassName('Word_UnderscoreToDash'));
  312. }
  313. }
  314. // Call Zend_Loader_PluginLoaderTest::main() if this source file is executed directly.
  315. if (PHPUnit_MAIN_METHOD === 'Zend_Loader_PluginLoaderTest::main') {
  316. Zend_Loader_PluginLoaderTest::main();
  317. }