LoaderTest.php 16 KB

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