LoaderTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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-2015 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. * Zend_Loader
  28. */
  29. require_once 'Zend/Loader.php';
  30. /**
  31. * Zend_Loader_Autoloader
  32. */
  33. require_once 'Zend/Loader/Autoloader.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Loader
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Loader
  41. */
  42. class Zend_LoaderTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_LoaderTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. public function setUp()
  55. {
  56. // Store original autoloaders
  57. $this->loaders = spl_autoload_functions();
  58. if (!is_array($this->loaders)) {
  59. // spl_autoload_functions does not return empty array when no
  60. // autoloaders registered...
  61. $this->loaders = array();
  62. }
  63. // Store original include_path
  64. $this->includePath = get_include_path();
  65. $this->error = null;
  66. $this->errorHandler = null;
  67. Zend_Loader_Autoloader::resetInstance();
  68. }
  69. public function tearDown()
  70. {
  71. if ($this->errorHandler !== null) {
  72. restore_error_handler();
  73. }
  74. // Restore original autoloaders
  75. $loaders = spl_autoload_functions();
  76. if (is_array($loaders)) {
  77. foreach ($loaders as $loader) {
  78. spl_autoload_unregister($loader);
  79. }
  80. }
  81. if (is_array($this->loaders)) {
  82. foreach ($this->loaders as $loader) {
  83. spl_autoload_register($loader);
  84. }
  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'), get_include_path());
  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 = $function[0];
  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 = $function[0];
  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->assertTrue($obj instanceof Zend_Loader_AutoloadableClass,
  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 = $function[0];
  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 = $function[0];
  382. if ($class == 'Zend_Loader_Autoloader') {
  383. spl_autoload_unregister($function);
  384. break;
  385. }
  386. }
  387. }
  388. }
  389. /**
  390. * @group ZF-8200
  391. */
  392. public function testLoadClassShouldAllowLoadingPhpNamespacedClasses()
  393. {
  394. if (version_compare(PHP_VERSION, '5.3.0') < 0) {
  395. $this->markTestSkipped('PHP < 5.3.0 does not support namespaces');
  396. }
  397. Zend_Loader::loadClass('\Zfns\Foo', array(dirname(__FILE__) . '/Loader/_files'));
  398. }
  399. /**
  400. * @group ZF-7271
  401. * @group ZF-8913
  402. */
  403. public function testIsReadableShouldHonorStreamDefinitions()
  404. {
  405. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  406. $this->markTestSkipped();
  407. }
  408. $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
  409. $phar = new Phar($pharFile, 0, 'zlt.phar');
  410. $incPath = 'phar://zlt.phar'
  411. . PATH_SEPARATOR . $this->includePath;
  412. set_include_path($incPath);
  413. $this->assertTrue(Zend_Loader::isReadable('User.php'));
  414. unset($phar);
  415. }
  416. /**
  417. * @group ZF-8913
  418. */
  419. public function testIsReadableShouldNotLockWhenTestingForNonExistantFileInPhar()
  420. {
  421. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  422. $this->markTestSkipped();
  423. }
  424. $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
  425. $phar = new Phar($pharFile, 0, 'zlt.phar');
  426. $incPath = 'phar://zlt.phar'
  427. . PATH_SEPARATOR . $this->includePath;
  428. set_include_path($incPath);
  429. $this->assertFalse(Zend_Loader::isReadable('does-not-exist'));
  430. unset($phar);
  431. }
  432. /**
  433. * @group ZF-7271
  434. */
  435. public function testExplodeIncludePathProperlyIdentifiesStreamSchemes()
  436. {
  437. if (PATH_SEPARATOR != ':') {
  438. $this->markTestSkipped();
  439. }
  440. $path = 'phar://zlt.phar:/var/www:.:filter://[a-z]:glob://*';
  441. $paths = Zend_Loader::explodeIncludePath($path);
  442. $this->assertSame(array(
  443. 'phar://zlt.phar',
  444. '/var/www',
  445. '.',
  446. 'filter://[a-z]',
  447. 'glob://*',
  448. ), $paths);
  449. }
  450. /**
  451. * @group ZF-9100
  452. */
  453. public function testIsReadableShouldReturnTrueForAbsolutePaths()
  454. {
  455. set_include_path(dirname(__FILE__) . '../../');
  456. $path = dirname(__FILE__);
  457. $this->assertTrue(Zend_Loader::isReadable($path));
  458. }
  459. /**
  460. * @group ZF-9263
  461. * @group ZF-9166
  462. * @group ZF-9306
  463. */
  464. public function testIsReadableShouldFailEarlyWhenProvidedInvalidWindowsAbsolutePath()
  465. {
  466. if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
  467. $this->markTestSkipped('Windows-only test');
  468. }
  469. $path = 'C:/this/file/should/not/exist.php';
  470. $this->assertFalse(Zend_Loader::isReadable($path));
  471. }
  472. /**
  473. * In order to play nice with spl_autoload, an autoload callback should
  474. * *not* emit errors (exceptions are okay). ZF-2923 requests that this
  475. * behavior be applied, which counters the previous request in ZF-2463.
  476. *
  477. * As it is, the new behavior *will* hide parse and other errors. However,
  478. * a fatal error *will* be raised in such situations, which is as
  479. * appropriate or more appropriate than raising an exception.
  480. *
  481. * NOTE: Removed from test suite, as autoload functionality in Zend_Loader
  482. * is now deprecated.
  483. *
  484. * @see http://framework.zend.com/issues/browse/ZF-2463
  485. * @group ZF-2923
  486. * @return void
  487. public function testLoaderAutoloadShouldHideParseError()
  488. {
  489. if (isset($_SERVER['OS']) && strstr($_SERVER['OS'], 'Win')) {
  490. $this->markTestSkipped(__METHOD__ . ' does not work on Windows');
  491. }
  492. $command = 'php -d include_path='
  493. . escapeshellarg(get_include_path())
  494. . ' Zend/Loader/AutoloadDoesNotHideParseError.php 2>&1';
  495. $output = shell_exec($command);
  496. $this->assertTrue(empty($output));
  497. }
  498. */
  499. }
  500. // Call Zend_LoaderTest::main() if this source file is executed directly.
  501. if (PHPUnit_MAIN_METHOD === 'Zend_LoaderTest::main') {
  502. Zend_LoaderTest::main();
  503. }