2
0

LoaderTest.php 17 KB

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