LoaderTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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-2008 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_LoaderTest::main() if this source file is executed directly.
  23. if (!defined('PHPUnit_MAIN_METHOD')) {
  24. define('PHPUnit_MAIN_METHOD', 'Zend_LoaderTest::main');
  25. }
  26. /**
  27. * Test helper
  28. */
  29. require_once dirname(__FILE__) . '/../TestHelper.php';
  30. /**
  31. * Zend_Loader
  32. */
  33. require_once 'Zend/Loader.php';
  34. /**
  35. * Zend_Loader_Autoloader
  36. */
  37. require_once 'Zend/Loader/Autoloader.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Loader
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_LoaderTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. require_once "PHPUnit/TextUI/TestRunner.php";
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_LoaderTest");
  56. $result = PHPUnit_TextUI_TestRunner::run($suite);
  57. }
  58. public function setUp()
  59. {
  60. $this->error = null;
  61. $this->errorHandler = null;
  62. Zend_Loader_Autoloader::resetInstance();
  63. }
  64. public function tearDown()
  65. {
  66. if ($this->errorHandler !== null) {
  67. restore_error_handler();
  68. }
  69. }
  70. public function setErrorHandler()
  71. {
  72. set_error_handler(array($this, 'handleErrors'), E_USER_NOTICE);
  73. $this->errorHandler = true;
  74. }
  75. public function handleErrors($errno, $errstr)
  76. {
  77. $this->error = $errstr;
  78. }
  79. /**
  80. * Tests that a class can be loaded from a well-formed PHP file
  81. */
  82. public function testLoaderClassValid()
  83. {
  84. $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
  85. Zend_Loader::loadClass('Class1', $dir);
  86. }
  87. public function testLoaderInterfaceViaLoadClass()
  88. {
  89. try {
  90. Zend_Loader::loadClass('Zend_Controller_Dispatcher_Interface');
  91. } catch (Zend_Exception $e) {
  92. $this->fail('Loading interfaces should not fail');
  93. }
  94. }
  95. public function testLoaderLoadClassWithDotDir()
  96. {
  97. $dirs = array('.');
  98. try {
  99. Zend_Loader::loadClass('Zend_Version', $dirs);
  100. } catch (Zend_Exception $e) {
  101. $this->fail('Loading from dot should not fail');
  102. }
  103. }
  104. /**
  105. * Tests that an exception is thrown when a file is loaded but the
  106. * class is not found within the file
  107. */
  108. public function testLoaderClassNonexistent()
  109. {
  110. $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
  111. try {
  112. Zend_Loader::loadClass('ClassNonexistent', $dir);
  113. $this->fail('Zend_Exception was expected but never thrown.');
  114. } catch (Zend_Exception $e) {
  115. $this->assertRegExp('/file(.*)does not exist or class(.*)not found/i', $e->getMessage());
  116. }
  117. }
  118. /**
  119. * Tests that an exception is thrown if the $dirs argument is
  120. * not a string or an array.
  121. */
  122. public function testLoaderInvalidDirs()
  123. {
  124. try {
  125. Zend_Loader::loadClass('Zend_Invalid_Dirs', new stdClass());
  126. $this->fail('Zend_Exception was expected but never thrown.');
  127. } catch (Zend_Exception $e) {
  128. $this->assertEquals('Directory argument must be a string or an array', $e->getMessage());
  129. }
  130. }
  131. /**
  132. * Tests that a class can be loaded from the search directories.
  133. */
  134. public function testLoaderClassSearchDirs()
  135. {
  136. $dirs = array();
  137. foreach (array('_testDir1', '_testDir2') as $dir) {
  138. $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
  139. }
  140. // throws exception on failure
  141. Zend_Loader::loadClass('Class1', $dirs);
  142. Zend_Loader::loadClass('Class2', $dirs);
  143. }
  144. /**
  145. * Tests that a class locatedin a subdirectory can be loaded from the search directories
  146. */
  147. public function testLoaderClassSearchSubDirs()
  148. {
  149. $dirs = array();
  150. foreach (array('_testDir1', '_testDir2') as $dir) {
  151. $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
  152. }
  153. // throws exception on failure
  154. Zend_Loader::loadClass('Class1_Subclass2', $dirs);
  155. }
  156. /**
  157. * Tests that the security filter catches illegal characters.
  158. */
  159. public function testLoaderClassIllegalFilename()
  160. {
  161. try {
  162. Zend_Loader::loadClass('/path/:to/@danger');
  163. $this->fail('Zend_Exception was expected but never thrown.');
  164. } catch (Zend_Exception $e) {
  165. $this->assertRegExp('/security(.*)filename/i', $e->getMessage());
  166. }
  167. }
  168. /**
  169. * Tests that loadFile() finds a file in the include_path when $dirs is null
  170. */
  171. public function testLoaderFileIncludePathEmptyDirs()
  172. {
  173. $saveIncludePath = get_include_path();
  174. set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
  175. $this->assertTrue(Zend_Loader::loadFile('Class3.php', null));
  176. set_include_path($saveIncludePath);
  177. }
  178. /**
  179. * Tests that loadFile() finds a file in the include_path when $dirs is non-null
  180. * This was not working vis-a-vis ZF-1174
  181. */
  182. public function testLoaderFileIncludePathNonEmptyDirs()
  183. {
  184. $saveIncludePath = get_include_path();
  185. set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
  186. $this->assertTrue(Zend_Loader::loadFile('Class4.php', implode(PATH_SEPARATOR, array('foo', 'bar'))));
  187. set_include_path($saveIncludePath);
  188. }
  189. /**
  190. * Tests that isReadable works
  191. */
  192. public function testLoaderIsReadable()
  193. {
  194. $this->assertTrue(Zend_Loader::isReadable(__FILE__));
  195. $this->assertFalse(Zend_Loader::isReadable(__FILE__ . '.foobaar'));
  196. // test that a file in include_path gets loaded, see ZF-2985
  197. $this->assertTrue(Zend_Loader::isReadable('Zend/Controller/Front.php'));
  198. }
  199. /**
  200. * Tests that autoload works for valid classes and interfaces
  201. */
  202. public function testLoaderAutoloadLoadsValidClasses()
  203. {
  204. $this->setErrorHandler();
  205. $this->assertEquals('Zend_Db_Profiler_Exception', Zend_Loader::autoload('Zend_Db_Profiler_Exception'));
  206. $this->assertContains('deprecated', $this->error);
  207. $this->error = null;
  208. $this->assertEquals('Zend_Auth_Storage_Interface', Zend_Loader::autoload('Zend_Auth_Storage_Interface'));
  209. $this->assertContains('deprecated', $this->error);
  210. }
  211. /**
  212. * Tests that autoload returns false on invalid classes
  213. */
  214. public function testLoaderAutoloadFailsOnInvalidClasses()
  215. {
  216. $this->setErrorHandler();
  217. $this->assertFalse(Zend_Loader::autoload('Zend_FooBar_Magic_Abstract'));
  218. $this->assertContains('deprecated', $this->error);
  219. }
  220. public function testLoaderRegisterAutoloadRegisters()
  221. {
  222. if (!function_exists('spl_autoload_register')) {
  223. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  224. }
  225. $this->setErrorHandler();
  226. Zend_Loader::registerAutoload();
  227. $this->assertContains('deprecated', $this->error);
  228. $autoloaders = spl_autoload_functions();
  229. $found = false;
  230. foreach($autoloaders as $function) {
  231. if (is_array($function)) {
  232. $class = array_shift($function);
  233. if ($class == 'Zend_Loader_Autoloader') {
  234. $found = true;
  235. spl_autoload_unregister($function);
  236. break;
  237. }
  238. }
  239. }
  240. $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
  241. }
  242. public function testLoaderRegisterAutoloadExtendedClassNeedsAutoloadMethod()
  243. {
  244. if (!function_exists('spl_autoload_register')) {
  245. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  246. }
  247. $this->setErrorHandler();
  248. Zend_Loader::registerAutoload('Zend_Loader_MyLoader');
  249. $this->assertContains('deprecated', $this->error);
  250. $autoloaders = spl_autoload_functions();
  251. $expected = array('Zend_Loader_MyLoader', 'autoload');
  252. $found = false;
  253. foreach ($autoloaders as $function) {
  254. if ($expected == $function) {
  255. $found = true;
  256. break;
  257. }
  258. }
  259. $this->assertFalse($found, "Failed to register Zend_Loader_MyLoader::autoload() with spl_autoload");
  260. spl_autoload_unregister($expected);
  261. }
  262. public function testLoaderRegisterAutoloadExtendedClassWithAutoloadMethod()
  263. {
  264. if (!function_exists('spl_autoload_register')) {
  265. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  266. }
  267. $this->setErrorHandler();
  268. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
  269. $this->assertContains('deprecated', $this->error);
  270. $autoloaders = spl_autoload_functions();
  271. $found = false;
  272. foreach ($autoloaders as $function) {
  273. if (is_array($function)) {
  274. $class = array_shift($function);
  275. if ($class == 'Zend_Loader_Autoloader') {
  276. $found = true;
  277. break;
  278. }
  279. }
  280. }
  281. $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
  282. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  283. $found = false;
  284. $expected = array('Zend_Loader_MyOverloader', 'autoload');
  285. $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register My_Loader_MyOverloader with Zend_Loader_Autoloader: ' . var_export($autoloaders, 1));
  286. // try to instantiate a class that is known not to be loaded
  287. $obj = new Zend_Loader_AutoloadableClass();
  288. // now it should be loaded
  289. $this->assertTrue(class_exists('Zend_Loader_AutoloadableClass'),
  290. 'Expected Zend_Loader_AutoloadableClass to be loaded');
  291. // and we verify it is the correct type
  292. $this->assertType('Zend_Loader_AutoloadableClass', $obj,
  293. 'Expected to instantiate Zend_Loader_AutoloadableClass, got '.get_class($obj));
  294. spl_autoload_unregister($function);
  295. }
  296. public function testLoaderRegisterAutoloadFailsWithoutSplAutoload()
  297. {
  298. if (function_exists('spl_autoload_register')) {
  299. $this->markTestSkipped("spl_autoload() is installed on this PHP installation; cannot test for failure");
  300. }
  301. try {
  302. Zend_Loader::registerAutoload();
  303. $this->fail('registerAutoload should fail without spl_autoload');
  304. } catch (Zend_Exception $e) {
  305. }
  306. }
  307. public function testLoaderRegisterAutoloadInvalidClass()
  308. {
  309. if (!function_exists('spl_autoload_register')) {
  310. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  311. }
  312. $this->setErrorHandler();
  313. try {
  314. Zend_Loader::registerAutoload('stdClass');
  315. $this->fail('registerAutoload should fail without spl_autoload');
  316. } catch (Zend_Exception $e) {
  317. $this->assertEquals('The class "stdClass" does not have an autoload() method', $e->getMessage());
  318. $this->assertContains('deprecated', $this->error);
  319. }
  320. }
  321. public function testLoaderUnregisterAutoload()
  322. {
  323. if (!function_exists('spl_autoload_register')) {
  324. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  325. }
  326. $this->setErrorHandler();
  327. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
  328. $this->assertContains('deprecated', $this->error);
  329. $expected = array('Zend_Loader_MyOverloader', 'autoload');
  330. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  331. $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register autoloader');
  332. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader', false);
  333. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  334. $this->assertFalse(in_array($expected, $autoloaders, true), 'Failed to unregister autoloader');
  335. foreach (spl_autoload_functions() as $function) {
  336. if (is_array($function)) {
  337. $class = array_shift($function);
  338. if ($class == 'Zend_Loader_Autoloader') {
  339. spl_autoload_unregister($function);
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. /**
  346. * @group ZF-6605
  347. */
  348. public function testRegisterAutoloadShouldEnableZendLoaderAutoloaderAsFallbackAutoloader()
  349. {
  350. if (!function_exists('spl_autoload_register')) {
  351. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  352. }
  353. $this->setErrorHandler();
  354. Zend_Loader::registerAutoload();
  355. $this->assertContains('deprecated', $this->error);
  356. $autoloader = Zend_Loader_Autoloader::getInstance();
  357. $this->assertTrue($autoloader->isFallbackAutoloader());
  358. foreach (spl_autoload_functions() as $function) {
  359. if (is_array($function)) {
  360. $class = array_shift($function);
  361. if ($class == 'Zend_Loader_Autoloader') {
  362. spl_autoload_unregister($function);
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * In order to play nice with spl_autoload, an autoload callback should
  370. * *not* emit errors (exceptions are okay). ZF-2923 requests that this
  371. * behavior be applied, which counters the previous request in ZF-2463.
  372. *
  373. * As it is, the new behavior *will* hide parse and other errors. However,
  374. * a fatal error *will* be raised in such situations, which is as
  375. * appropriate or more appropriate than raising an exception.
  376. *
  377. * NOTE: Removed from test suite, as autoload functionality in Zend_Loader
  378. * is now deprecated.
  379. *
  380. * @see http://framework.zend.com/issues/browse/ZF-2463
  381. * @group ZF-2923
  382. * @return void
  383. public function testLoaderAutoloadShouldHideParseError()
  384. {
  385. if (isset($_SERVER['OS']) && strstr($_SERVER['OS'], 'Win')) {
  386. $this->markTestSkipped(__METHOD__ . ' does not work on Windows');
  387. }
  388. $command = 'php -d include_path='
  389. . escapeshellarg(get_include_path())
  390. . ' Zend/Loader/AutoloadDoesNotHideParseError.php 2>&1';
  391. $output = shell_exec($command);
  392. $this->assertTrue(empty($output));
  393. }
  394. */
  395. }
  396. // Call Zend_LoaderTest::main() if this source file is executed directly.
  397. if (PHPUnit_MAIN_METHOD === 'Zend_LoaderTest::main') {
  398. Zend_LoaderTest::main();
  399. }