diff options
Diffstat (limited to 'tests/mocks/autoloader.php')
-rw-r--r-- | tests/mocks/autoloader.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/mocks/autoloader.php b/tests/mocks/autoloader.php new file mode 100644 index 000000000..442389f81 --- /dev/null +++ b/tests/mocks/autoloader.php @@ -0,0 +1,33 @@ +<?php + +// This autoloader provide convinient way to working with mock object +// make the test looks natural. This autoloader support cascade file loading as well +// within mocks directory. +// +// Prototype : +// +// include_once('Mock_Core_Common') // Will load ./mocks/core/common.php +// $mock_loader = new Mock_Core_Loader(); // Will load ./mocks/core/loader.php +// $mock_database_driver = new Mock_Database_Driver(); // Will load ./mocks/database/driver.php +function autoload($class) +{ + $class = (strpos($class, 'Mock_') === 0) ? str_replace(array('Mock_', '_'), array('', DIRECTORY_SEPARATOR), $class) : $class; + $dir = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR; + $file = $dir.strtolower($class).'.php'; + + if ( ! file_exists($file)) + { + $trace = debug_backtrace(); + + // If the autoload call came from `class_exists`, we skipped + // and return FALSE + if ($trace[2]['function'] == 'class_exists') + { + return FALSE; + } + + throw new InvalidArgumentException("Unable to load $class."); + } + + include_once($file); +}
\ No newline at end of file |