[ZF-8906] Zend_Validate_Identical:
[zend.git] / tests / Zend / LoaderTest.php
blobfe711a3243658cd640d142099ba3f156729327cf
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
23 // Call Zend_LoaderTest::main() if this source file is executed directly.
24 if (!defined('PHPUnit_MAIN_METHOD')) {
25 define('PHPUnit_MAIN_METHOD', 'Zend_LoaderTest::main');
28 /**
29 * Test helper
31 require_once dirname(__FILE__) . '/../TestHelper.php';
33 /**
34 * Zend_Loader
36 require_once 'Zend/Loader.php';
38 /**
39 * Zend_Loader_Autoloader
41 require_once 'Zend/Loader/Autoloader.php';
43 /**
44 * @category Zend
45 * @package Zend_Loader
46 * @subpackage UnitTests
47 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
49 * @group Zend_Loader
51 class Zend_LoaderTest extends PHPUnit_Framework_TestCase
53 /**
54 * Runs the test methods of this class.
56 * @return void
58 public static function main()
60 require_once "PHPUnit/TextUI/TestRunner.php";
62 $suite = new PHPUnit_Framework_TestSuite("Zend_LoaderTest");
63 $result = PHPUnit_TextUI_TestRunner::run($suite);
66 public function setUp()
68 // Store original autoloaders
69 $this->loaders = spl_autoload_functions();
70 if (!is_array($this->loaders)) {
71 // spl_autoload_functions does not return empty array when no
72 // autoloaders registered...
73 $this->loaders = array();
76 // Store original include_path
77 $this->includePath = get_include_path();
79 $this->error = null;
80 $this->errorHandler = null;
81 Zend_Loader_Autoloader::resetInstance();
84 public function tearDown()
86 if ($this->errorHandler !== null) {
87 restore_error_handler();
90 // Restore original autoloaders
91 $loaders = spl_autoload_functions();
92 if (is_array($loaders)) {
93 foreach ($loaders as $loader) {
94 spl_autoload_unregister($loader);
98 if (is_array($this->loaders)) {
99 foreach ($this->loaders as $loader) {
100 spl_autoload_register($loader);
104 // Retore original include_path
105 set_include_path($this->includePath);
107 // Reset autoloader instance so it doesn't affect other tests
108 Zend_Loader_Autoloader::resetInstance();
111 public function setErrorHandler()
113 set_error_handler(array($this, 'handleErrors'), E_USER_NOTICE);
114 $this->errorHandler = true;
117 public function handleErrors($errno, $errstr)
119 $this->error = $errstr;
123 * Tests that a class can be loaded from a well-formed PHP file
125 public function testLoaderClassValid()
127 $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
129 Zend_Loader::loadClass('Class1', $dir);
132 public function testLoaderInterfaceViaLoadClass()
134 try {
135 Zend_Loader::loadClass('Zend_Controller_Dispatcher_Interface');
136 } catch (Zend_Exception $e) {
137 $this->fail('Loading interfaces should not fail');
141 public function testLoaderLoadClassWithDotDir()
143 $dirs = array('.');
144 try {
145 Zend_Loader::loadClass('Zend_Version', $dirs);
146 } catch (Zend_Exception $e) {
147 $this->fail('Loading from dot should not fail');
152 * Tests that an exception is thrown when a file is loaded but the
153 * class is not found within the file
155 public function testLoaderClassNonexistent()
157 $dir = implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR);
159 try {
160 Zend_Loader::loadClass('ClassNonexistent', $dir);
161 $this->fail('Zend_Exception was expected but never thrown.');
162 } catch (Zend_Exception $e) {
163 $this->assertRegExp('/file(.*)does not exist or class(.*)not found/i', $e->getMessage());
168 * Tests that an exception is thrown if the $dirs argument is
169 * not a string or an array.
171 public function testLoaderInvalidDirs()
173 try {
174 Zend_Loader::loadClass('Zend_Invalid_Dirs', new stdClass());
175 $this->fail('Zend_Exception was expected but never thrown.');
176 } catch (Zend_Exception $e) {
177 $this->assertEquals('Directory argument must be a string or an array', $e->getMessage());
182 * Tests that a class can be loaded from the search directories.
184 public function testLoaderClassSearchDirs()
186 $dirs = array();
187 foreach (array('_testDir1', '_testDir2') as $dir) {
188 $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
191 // throws exception on failure
192 Zend_Loader::loadClass('Class1', $dirs);
193 Zend_Loader::loadClass('Class2', $dirs);
197 * Tests that a class locatedin a subdirectory can be loaded from the search directories
199 public function testLoaderClassSearchSubDirs()
201 $dirs = array();
202 foreach (array('_testDir1', '_testDir2') as $dir) {
203 $dirs[] = implode(array(dirname(__FILE__), '_files', $dir), DIRECTORY_SEPARATOR);
206 // throws exception on failure
207 Zend_Loader::loadClass('Class1_Subclass2', $dirs);
211 * Tests that the security filter catches illegal characters.
213 public function testLoaderClassIllegalFilename()
215 try {
216 Zend_Loader::loadClass('/path/:to/@danger');
217 $this->fail('Zend_Exception was expected but never thrown.');
218 } catch (Zend_Exception $e) {
219 $this->assertRegExp('/security(.*)filename/i', $e->getMessage());
224 * Tests that loadFile() finds a file in the include_path when $dirs is null
226 public function testLoaderFileIncludePathEmptyDirs()
228 $saveIncludePath = get_include_path();
229 set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
231 $this->assertTrue(Zend_Loader::loadFile('Class3.php', null));
233 set_include_path($saveIncludePath);
237 * Tests that loadFile() finds a file in the include_path when $dirs is non-null
238 * This was not working vis-a-vis ZF-1174
240 public function testLoaderFileIncludePathNonEmptyDirs()
242 $saveIncludePath = get_include_path();
243 set_include_path(implode(array($saveIncludePath, implode(array(dirname(__FILE__), '_files', '_testDir1'), DIRECTORY_SEPARATOR)), PATH_SEPARATOR));
245 $this->assertTrue(Zend_Loader::loadFile('Class4.php', implode(PATH_SEPARATOR, array('foo', 'bar'))));
247 set_include_path($saveIncludePath);
251 * Tests that isReadable works
253 public function testLoaderIsReadable()
255 $this->assertTrue(Zend_Loader::isReadable(__FILE__));
256 $this->assertFalse(Zend_Loader::isReadable(__FILE__ . '.foobaar'));
258 // test that a file in include_path gets loaded, see ZF-2985
259 $this->assertTrue(Zend_Loader::isReadable('Zend/Controller/Front.php'), get_include_path());
263 * Tests that autoload works for valid classes and interfaces
265 public function testLoaderAutoloadLoadsValidClasses()
267 $this->setErrorHandler();
268 $this->assertEquals('Zend_Db_Profiler_Exception', Zend_Loader::autoload('Zend_Db_Profiler_Exception'));
269 $this->assertContains('deprecated', $this->error);
270 $this->error = null;
271 $this->assertEquals('Zend_Auth_Storage_Interface', Zend_Loader::autoload('Zend_Auth_Storage_Interface'));
272 $this->assertContains('deprecated', $this->error);
276 * Tests that autoload returns false on invalid classes
278 public function testLoaderAutoloadFailsOnInvalidClasses()
280 $this->setErrorHandler();
281 $this->assertFalse(Zend_Loader::autoload('Zend_FooBar_Magic_Abstract'));
282 $this->assertContains('deprecated', $this->error);
285 public function testLoaderRegisterAutoloadRegisters()
287 if (!function_exists('spl_autoload_register')) {
288 $this->markTestSkipped("spl_autoload not installed on this PHP installation");
291 $this->setErrorHandler();
292 Zend_Loader::registerAutoload();
293 $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 = $function[0];
300 if ($class == 'Zend_Loader_Autoloader') {
301 $found = true;
302 spl_autoload_unregister($function);
303 break;
307 $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
310 public function testLoaderRegisterAutoloadExtendedClassNeedsAutoloadMethod()
312 if (!function_exists('spl_autoload_register')) {
313 $this->markTestSkipped("spl_autoload not installed on this PHP installation");
316 $this->setErrorHandler();
317 Zend_Loader::registerAutoload('Zend_Loader_MyLoader');
318 $this->assertContains('deprecated', $this->error);
320 $autoloaders = spl_autoload_functions();
321 $expected = array('Zend_Loader_MyLoader', 'autoload');
322 $found = false;
323 foreach ($autoloaders as $function) {
324 if ($expected == $function) {
325 $found = true;
326 break;
329 $this->assertFalse($found, "Failed to register Zend_Loader_MyLoader::autoload() with spl_autoload");
331 spl_autoload_unregister($expected);
334 public function testLoaderRegisterAutoloadExtendedClassWithAutoloadMethod()
336 if (!function_exists('spl_autoload_register')) {
337 $this->markTestSkipped("spl_autoload not installed on this PHP installation");
340 $this->setErrorHandler();
341 Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
342 $this->assertContains('deprecated', $this->error);
344 $autoloaders = spl_autoload_functions();
345 $found = false;
346 foreach ($autoloaders as $function) {
347 if (is_array($function)) {
348 $class = $function[0];
349 if ($class == 'Zend_Loader_Autoloader') {
350 $found = true;
351 break;
355 $this->assertTrue($found, "Failed to register Zend_Loader_Autoloader with spl_autoload");
357 $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
358 $found = false;
359 $expected = array('Zend_Loader_MyOverloader', 'autoload');
360 $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register My_Loader_MyOverloader with Zend_Loader_Autoloader: ' . var_export($autoloaders, 1));
362 // try to instantiate a class that is known not to be loaded
363 $obj = new Zend_Loader_AutoloadableClass();
365 // now it should be loaded
366 $this->assertTrue(class_exists('Zend_Loader_AutoloadableClass'),
367 'Expected Zend_Loader_AutoloadableClass to be loaded');
369 // and we verify it is the correct type
370 $this->assertType('Zend_Loader_AutoloadableClass', $obj,
371 'Expected to instantiate Zend_Loader_AutoloadableClass, got '.get_class($obj));
373 spl_autoload_unregister($function);
376 public function testLoaderRegisterAutoloadFailsWithoutSplAutoload()
378 if (function_exists('spl_autoload_register')) {
379 $this->markTestSkipped("spl_autoload() is installed on this PHP installation; cannot test for failure");
382 try {
383 Zend_Loader::registerAutoload();
384 $this->fail('registerAutoload should fail without spl_autoload');
385 } catch (Zend_Exception $e) {
389 public function testLoaderRegisterAutoloadInvalidClass()
391 if (!function_exists('spl_autoload_register')) {
392 $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
395 $this->setErrorHandler();
396 try {
397 Zend_Loader::registerAutoload('stdClass');
398 $this->fail('registerAutoload should fail without spl_autoload');
399 } catch (Zend_Exception $e) {
400 $this->assertEquals('The class "stdClass" does not have an autoload() method', $e->getMessage());
401 $this->assertContains('deprecated', $this->error);
405 public function testLoaderUnregisterAutoload()
407 if (!function_exists('spl_autoload_register')) {
408 $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
411 $this->setErrorHandler();
412 Zend_Loader::registerAutoload('Zend_Loader_MyOverloader');
413 $this->assertContains('deprecated', $this->error);
415 $expected = array('Zend_Loader_MyOverloader', 'autoload');
416 $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
417 $this->assertTrue(in_array($expected, $autoloaders, true), 'Failed to register autoloader');
419 Zend_Loader::registerAutoload('Zend_Loader_MyOverloader', false);
420 $autoloaders = Zend_Loader_Autoloader::getInstance()->getAutoloaders();
421 $this->assertFalse(in_array($expected, $autoloaders, true), 'Failed to unregister autoloader');
423 foreach (spl_autoload_functions() as $function) {
424 if (is_array($function)) {
425 $class = $function[0];
426 if ($class == 'Zend_Loader_Autoloader') {
427 spl_autoload_unregister($function);
428 break;
435 * @group ZF-6605
437 public function testRegisterAutoloadShouldEnableZendLoaderAutoloaderAsFallbackAutoloader()
439 if (!function_exists('spl_autoload_register')) {
440 $this->markTestSkipped("spl_autoload() not installed on this PHP installation");
443 $this->setErrorHandler();
444 Zend_Loader::registerAutoload();
445 $this->assertContains('deprecated', $this->error);
447 $autoloader = Zend_Loader_Autoloader::getInstance();
448 $this->assertTrue($autoloader->isFallbackAutoloader());
450 foreach (spl_autoload_functions() as $function) {
451 if (is_array($function)) {
452 $class = $function[0];
453 if ($class == 'Zend_Loader_Autoloader') {
454 spl_autoload_unregister($function);
455 break;
462 * @group ZF-8200
464 public function testLoadClassShouldAllowLoadingPhpNamespacedClasses()
466 if (version_compare(PHP_VERSION, '5.3.0') < 0) {
467 $this->markTestSkipped('PHP < 5.3.0 does not support namespaces');
469 Zend_Loader::loadClass('\Zfns\Foo', array(dirname(__FILE__) . '/Loader/_files'));
473 * @group ZF-7271
474 * @group ZF-8913
476 public function testIsReadableShouldHonorStreamDefinitions()
478 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
479 $this->markTestSkipped();
482 $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
483 $phar = new Phar($pharFile, 0, 'zlt.phar');
484 $incPath = 'phar://zlt.phar'
485 . PATH_SEPARATOR . $this->includePath;
486 set_include_path($incPath);
487 $this->assertTrue(Zend_Loader::isReadable('User.php'));
488 unset($phar);
492 * @group ZF-8913
494 public function testIsReadableShouldNotLockWhenTestingForNonExistantFileInPhar()
496 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
497 $this->markTestSkipped();
500 $pharFile = dirname(__FILE__) . '/Loader/_files/Zend_LoaderTest.phar';
501 $phar = new Phar($pharFile, 0, 'zlt.phar');
502 $incPath = 'phar://zlt.phar'
503 . PATH_SEPARATOR . $this->includePath;
504 set_include_path($incPath);
505 $this->assertFalse(Zend_Loader::isReadable('does-not-exist'));
506 unset($phar);
510 * @group ZF-7271
512 public function testExplodeIncludePathProperlyIdentifiesStreamSchemes()
514 if (PATH_SEPARATOR != ':') {
515 $this->markTestSkipped();
517 $path = 'phar://zlt.phar:/var/www:.:filter://[a-z]:glob://*';
518 $paths = Zend_Loader::explodeIncludePath($path);
519 $this->assertSame(array(
520 'phar://zlt.phar',
521 '/var/www',
522 '.',
523 'filter://[a-z]',
524 'glob://*',
525 ), $paths);
529 * @group ZF-9100
531 public function testIsReadableShouldReturnTrueForAbsolutePaths()
533 set_include_path(dirname(__FILE__) . '../../');
534 $path = dirname(__FILE__);
535 $this->assertTrue(Zend_Loader::isReadable($path));
539 * @group ZF-9263
540 * @group ZF-9166
541 * @group ZF-9306
543 public function testIsReadableShouldFailEarlyWhenProvidedInvalidWindowsAbsolutePath()
545 if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
546 $this->markTestSkipped('Windows-only test');
548 $path = 'C:/this/file/should/not/exist.php';
549 $this->assertFalse(Zend_Loader::isReadable($path));
553 * In order to play nice with spl_autoload, an autoload callback should
554 * *not* emit errors (exceptions are okay). ZF-2923 requests that this
555 * behavior be applied, which counters the previous request in ZF-2463.
557 * As it is, the new behavior *will* hide parse and other errors. However,
558 * a fatal error *will* be raised in such situations, which is as
559 * appropriate or more appropriate than raising an exception.
561 * NOTE: Removed from test suite, as autoload functionality in Zend_Loader
562 * is now deprecated.
564 * @see http://framework.zend.com/issues/browse/ZF-2463
565 * @group ZF-2923
566 * @return void
567 public function testLoaderAutoloadShouldHideParseError()
569 if (isset($_SERVER['OS']) && strstr($_SERVER['OS'], 'Win')) {
570 $this->markTestSkipped(__METHOD__ . ' does not work on Windows');
572 $command = 'php -d include_path='
573 . escapeshellarg(get_include_path())
574 . ' Zend/Loader/AutoloadDoesNotHideParseError.php 2>&1';
575 $output = shell_exec($command);
576 $this->assertTrue(empty($output));
581 // Call Zend_LoaderTest::main() if this source file is executed directly.
582 if (PHPUnit_MAIN_METHOD === 'Zend_LoaderTest::main') {
583 Zend_LoaderTest::main();