LoaderTest.php 14 KB

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