LoaderTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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-2010 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Loader
  45. */
  46. class Zend_LoaderTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @return void
  52. */
  53. public static function main()
  54. {
  55. require_once "PHPUnit/TextUI/TestRunner.php";
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_LoaderTest");
  57. $result = PHPUnit_TextUI_TestRunner::run($suite);
  58. }
  59. public function setUp()
  60. {
  61. // Store original autoloaders
  62. $this->loaders = spl_autoload_functions();
  63. if (!is_array($this->loaders)) {
  64. // spl_autoload_functions does not return empty array when no
  65. // autoloaders registered...
  66. $this->loaders = array();
  67. }
  68. // Store original include_path
  69. $this->includePath = get_include_path();
  70. $this->error = null;
  71. $this->errorHandler = null;
  72. Zend_Loader_Autoloader::resetInstance();
  73. }
  74. public function tearDown()
  75. {
  76. if ($this->errorHandler !== null) {
  77. restore_error_handler();
  78. }
  79. // Restore original autoloaders
  80. $loaders = spl_autoload_functions();
  81. if (is_array($loaders)) {
  82. foreach ($loaders as $loader) {
  83. spl_autoload_unregister($loader);
  84. }
  85. }
  86. if (is_array($this->loaders)) {
  87. foreach ($this->loaders as $loader) {
  88. spl_autoload_register($loader);
  89. }
  90. }
  91. // Retore original include_path
  92. set_include_path($this->includePath);
  93. // Reset autoloader instance so it doesn't affect other tests
  94. Zend_Loader_Autoloader::resetInstance();
  95. }
  96. public function setErrorHandler()
  97. {
  98. set_error_handler(array($this, 'handleErrors'), E_USER_NOTICE);
  99. $this->errorHandler = true;
  100. }
  101. public function handleErrors($errno, $errstr)
  102. {
  103. $this->error = $errstr;
  104. }
  105. /**
  106. * Tests that a class can be loaded from a well-formed PHP file
  107. */
  108. public function testLoaderClassValid()
  109. {
  110. $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
  111. Zend_Loader::loadClass('Class1', $dir);
  112. }
  113. public function testLoaderInterfaceViaLoadClass()
  114. {
  115. try {
  116. Zend_Loader::loadClass('Zend_Controller_Dispatcher_Interface');
  117. } catch (Zend_Exception $e) {
  118. $this->fail('Loading interfaces should not fail');
  119. }
  120. }
  121. public function testLoaderLoadClassWithDotDir()
  122. {
  123. $dirs = array('.');
  124. try {
  125. Zend_Loader::loadClass('Zend_Version', $dirs);
  126. } catch (Zend_Exception $e) {
  127. $this->fail('Loading from dot should not fail');
  128. }
  129. }
  130. /**
  131. * Tests that an exception is thrown when a file is loaded but the
  132. * class is not found within the file
  133. */
  134. public function testLoaderClassNonexistent()
  135. {
  136. $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
  137. try {
  138. Zend_Loader::loadClass('ClassNonexistent', $dir);
  139. $this->fail('Zend_Exception was expected but never thrown.');
  140. } catch (Zend_Exception $e) {
  141. $this->assertRegExp('/file(.*)does not exist or class(.*)not found/i', $e->getMessage());
  142. }
  143. }
  144. /**
  145. * Tests that an exception is thrown if the $dirs argument is
  146. * not a string or an array.
  147. */
  148. public function testLoaderInvalidDirs()
  149. {
  150. try {
  151. Zend_Loader::loadClass('Zend_Invalid_Dirs', new stdClass());
  152. $this->fail('Zend_Exception was expected but never thrown.');
  153. } catch (Zend_Exception $e) {
  154. $this->assertEquals('Directory argument must be a string or an array', $e->getMessage());
  155. }
  156. }
  157. /**
  158. * Tests that a class can be loaded from the search directories.
  159. */
  160. public function testLoaderClassSearchDirs()
  161. {
  162. $dirs = array();
  163. foreach (array('_testDir1', '_testDir2') as $dir) {
  164. $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
  165. }
  166. // throws exception on failure
  167. Zend_Loader::loadClass('Class1', $dirs);
  168. Zend_Loader::loadClass('Class2', $dirs);
  169. }
  170. /**
  171. * Tests that a class locatedin a subdirectory can be loaded from the search directories
  172. */
  173. public function testLoaderClassSearchSubDirs()
  174. {
  175. $dirs = array();
  176. foreach (array('_testDir1', '_testDir2') as $dir) {
  177. $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
  178. }
  179. // throws exception on failure
  180. Zend_Loader::loadClass('Class1_Subclass2', $dirs);
  181. }
  182. /**
  183. * Tests that the security filter catches illegal characters.
  184. */
  185. public function testLoaderClassIllegalFilename()
  186. {
  187. try {
  188. Zend_Loader::loadClass('/path/:to/@danger');
  189. $this->fail('Zend_Exception was expected but never thrown.');
  190. } catch (Zend_Exception $e) {
  191. $this->assertRegExp('/security(.*)filename/i', $e->getMessage());
  192. }
  193. }
  194. /**
  195. * Tests that loadFile() finds a file in the include_path when $dirs is null
  196. */
  197. public function testLoaderFileIncludePathEmptyDirs()
  198. {
  199. $saveIncludePath = get_include_path();
  200. set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
  201. $this->assertTrue(Zend_Loader::loadFile('Class3.php', null));
  202. set_include_path($saveIncludePath);
  203. }
  204. /**
  205. * Tests that loadFile() finds a file in the include_path when $dirs is non-null
  206. * This was not working vis-a-vis ZF-1174
  207. */
  208. public function testLoaderFileIncludePathNonEmptyDirs()
  209. {
  210. $saveIncludePath = get_include_path();
  211. set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
  212. $this->assertTrue(Zend_Loader::loadFile('Class4.php', implode(PATH_SEPARATOR, array('foo', 'bar'))));
  213. set_include_path($saveIncludePath);
  214. }
  215. /**
  216. * Tests that isReadable works
  217. */
  218. public function testLoaderIsReadable()
  219. {
  220. $this->assertTrue(Zend_Loader::isReadable(__FILE__));
  221. $this->assertFalse(Zend_Loader::isReadable(__FILE__ . '.foobaar'));
  222. // test that a file in include_path gets loaded, see ZF-2985
  223. $this->assertTrue(Zend_Loader::isReadable('Zend/Controller/Front.php'), get_include_path());
  224. }
  225. /**
  226. * Tests that autoload works for valid classes and interfaces
  227. */
  228. public function testLoaderAutoloadLoadsValidClasses()
  229. {
  230. $this->setErrorHandler();
  231. $this->assertEquals('Zend_Db_Profiler_Exception', Zend_Loader::autoload('Zend_Db_Profiler_Exception'));
  232. $this->assertContains('deprecated', $this->error);
  233. $this->error = null;
  234. $this->assertEquals('Zend_Auth_Storage_Interface', Zend_Loader::autoload('Zend_Auth_Storage_Interface'));
  235. $this->assertContains('deprecated', $this->error);
  236. }
  237. /**
  238. * Tests that autoload returns false on invalid classes
  239. */
  240. public function testLoaderAutoloadFailsOnInvalidClasses()
  241. {
  242. $this->setErrorHandler();
  243. $this->assertFalse(Zend_Loader::autoload('Zend_FooBar_Magic_Abstract'));
  244. $this->assertContains('deprecated', $this->error);
  245. }
  246. public function testLoaderRegisterAutoloadRegisters()
  247. {
  248. if (!function_exists('spl_autoload_register')) {
  249. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  250. }
  251. $this->setErrorHandler();
  252. Zend_Loader::registerAutoload();
  253. $this->assertContains('deprecated', $this->error);
  254. $autoloaders = spl_autoload_functions();
  255. $found = false;
  256. foreach($autoloaders as $function) {
  257. if (is_array($function)) {
  258. $class = $function[0];
  259. if ($class == 'Zend_Loader_Autoloader') {
  260. $found = true;
  261. spl_autoload_unregister($function);
  262. break;
  263. }
  264. }
  265. }
  266. $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
  267. }
  268. public function testLoaderRegisterAutoloadExtendedClassNeedsAutoloadMethod()
  269. {
  270. if (!function_exists('spl_autoload_register')) {
  271. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  272. }
  273. $this->setErrorHandler();
  274. Zend_Loader::registerAutoload('Zend_Loader_MyLoader');
  275. $this->assertContains('deprecated', $this->error);
  276. $autoloaders = spl_autoload_functions();
  277. $expected = array('Zend_Loader_MyLoader', 'autoload');
  278. $found = false;
  279. foreach ($autoloaders as $function) {
  280. if ($expected == $function) {
  281. $found = true;
  282. break;
  283. }
  284. }
  285. $this->assertFalse($found, "Failed to register Zend_Loader_MyLoader::autoload() with spl_autoload");
  286. spl_autoload_unregister($expected);
  287. }
  288. public function testLoaderRegisterAutoloadExtendedClassWithAutoloadMethod()
  289. {
  290. if (!function_exists('spl_autoload_register')) {
  291. $this->markTestSkipped("spl_autoload not installed on this PHP installation");
  292. }
  293. $this->setErrorHandler();
  294. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
  295. $this->assertContains('deprecated', $this->error);
  296. $autoloaders = spl_autoload_functions();
  297. $found = false;
  298. foreach ($autoloaders as $function) {
  299. if (is_array($function)) {
  300. $class = $function[0];
  301. if ($class == 'Zend_Loader_Autoloader') {
  302. $found = true;
  303. break;
  304. }
  305. }
  306. }
  307. $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
  308. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  309. $found = false;
  310. $expected = array('Zend_Loader_MyOverloader', 'autoload');
  311. $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register My_Loader_MyOverloader with Zend_Loader_Autoloader: ' . var_export($autoloaders, 1));
  312. // try to instantiate a class that is known not to be loaded
  313. $obj = new Zend_Loader_AutoloadableClass();
  314. // now it should be loaded
  315. $this->assertTrue(class_exists('Zend_Loader_AutoloadableClass'),
  316. 'Expected Zend_Loader_AutoloadableClass to be loaded');
  317. // and we verify it is the correct type
  318. $this->assertType('Zend_Loader_AutoloadableClass', $obj,
  319. 'Expected to instantiate Zend_Loader_AutoloadableClass, got '.get_class($obj));
  320. spl_autoload_unregister($function);
  321. }
  322. public function testLoaderRegisterAutoloadFailsWithoutSplAutoload()
  323. {
  324. if (function_exists('spl_autoload_register')) {
  325. $this->markTestSkipped("spl_autoload() is installed on this PHP installation; cannot test for failure");
  326. }
  327. try {
  328. Zend_Loader::registerAutoload();
  329. $this->fail('registerAutoload should fail without spl_autoload');
  330. } catch (Zend_Exception $e) {
  331. }
  332. }
  333. public function testLoaderRegisterAutoloadInvalidClass()
  334. {
  335. if (!function_exists('spl_autoload_register')) {
  336. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  337. }
  338. $this->setErrorHandler();
  339. try {
  340. Zend_Loader::registerAutoload('stdClass');
  341. $this->fail('registerAutoload should fail without spl_autoload');
  342. } catch (Zend_Exception $e) {
  343. $this->assertEquals('The class "stdClass" does not have an autoload() method', $e->getMessage());
  344. $this->assertContains('deprecated', $this->error);
  345. }
  346. }
  347. public function testLoaderUnregisterAutoload()
  348. {
  349. if (!function_exists('spl_autoload_register')) {
  350. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  351. }
  352. $this->setErrorHandler();
  353. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
  354. $this->assertContains('deprecated', $this->error);
  355. $expected = array('Zend_Loader_MyOverloader', 'autoload');
  356. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  357. $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register autoloader');
  358. Zend_Loader::registerAutoload('Zend_Loader_MyOverloader', false);
  359. $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
  360. $this->assertFalse(in_array($expected, $autoloaders, true), 'Failed to unregister autoloader');
  361. foreach (spl_autoload_functions() as $function) {
  362. if (is_array($function)) {
  363. $class = $function[0];
  364. if ($class == 'Zend_Loader_Autoloader') {
  365. spl_autoload_unregister($function);
  366. break;
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. * @group ZF-6605
  373. */
  374. public function testRegisterAutoloadShouldEnableZendLoaderAutoloaderAsFallbackAutoloader()
  375. {
  376. if (!function_exists('spl_autoload_register')) {
  377. $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
  378. }
  379. $this->setErrorHandler();
  380. Zend_Loader::registerAutoload();
  381. $this->assertContains('deprecated', $this->error);
  382. $autoloader = Zend_Loader_Autoloader::getInstance();
  383. $this->assertTrue($autoloader->isFallbackAutoloader());
  384. foreach (spl_autoload_functions() as $function) {
  385. if (is_array($function)) {
  386. $class = $function[0];
  387. if ($class == 'Zend_Loader_Autoloader') {
  388. spl_autoload_unregister($function);
  389. break;
  390. }
  391. }
  392. }
  393. }
  394. /**
  395. * @group ZF-8200
  396. */
  397. public function testLoadClassShouldAllowLoadingPhpNamespacedClasses()
  398. {
  399. if (version_compare(PHP_VERSION, '5.3.0') < 0) {
  400. $this->markTestSkipped('PHP < 5.3.0 does not support namespaces');
  401. }
  402. Zend_Loader::loadClass('\Zfns\Foo', array(dirname(__FILE__) . '/Loader/_files'));
  403. }
  404. /**
  405. * @group ZF-7271
  406. * @group ZF-8913
  407. */
  408. public function testIsReadableShouldHonorStreamDefinitions()
  409. {
  410. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  411. $this->markTestSkipped();
  412. }
  413. $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
  414. $phar = new Phar($pharFile, 0, 'zlt.phar');
  415. $incPath = 'phar://zlt.phar'
  416. . PATH_SEPARATOR . $this->includePath;
  417. set_include_path($incPath);
  418. $this->assertTrue(Zend_Loader::isReadable('User.php'));
  419. unset($phar);
  420. }
  421. /**
  422. * @group ZF-8913
  423. */
  424. public function testIsReadableShouldNotLockWhenTestingForNonExistantFileInPhar()
  425. {
  426. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  427. $this->markTestSkipped();
  428. }
  429. $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
  430. $phar = new Phar($pharFile, 0, 'zlt.phar');
  431. $incPath = 'phar://zlt.phar'
  432. . PATH_SEPARATOR . $this->includePath;
  433. set_include_path($incPath);
  434. $this->assertFalse(Zend_Loader::isReadable('does-not-exist'));
  435. unset($phar);
  436. }
  437. /**
  438. * @group ZF-7271
  439. */
  440. public function testExplodeIncludePathProperlyIdentifiesStreamSchemes()
  441. {
  442. if (PATH_SEPARATOR != ':') {
  443. $this->markTestSkipped();
  444. }
  445. $path = 'phar://zlt.phar:/var/www:.:filter://[a-z]:glob://*';
  446. $paths = Zend_Loader::explodeIncludePath($path);
  447. $this->assertSame(array(
  448. 'phar://zlt.phar',
  449. '/var/www',
  450. '.',
  451. 'filter://[a-z]',
  452. 'glob://*',
  453. ), $paths);
  454. }
  455. /**
  456. * @group ZF-9100
  457. */
  458. public function testIsReadableShouldReturnTrueForAbsolutePaths()
  459. {
  460. set_include_path(dirname(__FILE__) . '../../');
  461. $path = dirname(__FILE__);
  462. $this->assertTrue(Zend_Loader::isReadable($path));
  463. }
  464. /**
  465. * @group ZF-9263
  466. * @group ZF-9166
  467. * @group ZF-9306
  468. */
  469. public function testIsReadableShouldFailEarlyWhenProvidedInvalidWindowsAbsolutePath()
  470. {
  471. if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
  472. $this->markTestSkipped('Windows-only test');
  473. }
  474. $path = 'C:/this/file/should/not/exist.php';
  475. $this->assertFalse(Zend_Loader::isReadable($path));
  476. }
  477. /**
  478. * In order to play nice with spl_autoload, an autoload callback should
  479. * *not* emit errors (exceptions are okay). ZF-2923 requests that this
  480. * behavior be applied, which counters the previous request in ZF-2463.
  481. *
  482. * As it is, the new behavior *will* hide parse and other errors. However,
  483. * a fatal error *will* be raised in such situations, which is as
  484. * appropriate or more appropriate than raising an exception.
  485. *
  486. * NOTE: Removed from test suite, as autoload functionality in Zend_Loader
  487. * is now deprecated.
  488. *
  489. * @see http://framework.zend.com/issues/browse/ZF-2463
  490. * @group ZF-2923
  491. * @return void
  492. public function testLoaderAutoloadShouldHideParseError()
  493. {
  494. if (isset($_SERVER['OS']) && strstr($_SERVER['OS'], 'Win')) {
  495. $this->markTestSkipped(__METHOD__ . ' does not work on Windows');
  496. }
  497. $command = 'php -d include_path='
  498. . escapeshellarg(get_include_path())
  499. . ' Zend/Loader/AutoloadDoesNotHideParseError.php 2>&1';
  500. $output = shell_exec($command);
  501. $this->assertTrue(empty($output));
  502. }
  503. */
  504. }
  505. // Call Zend_LoaderTest::main() if this source file is executed directly.
  506. if (PHPUnit_MAIN_METHOD === 'Zend_LoaderTest::main') {
  507. Zend_LoaderTest::main();
  508. }