diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Bootstrap.php | 7 | ||||
-rw-r--r-- | tests/codeigniter/core/Common_test.php | 2 | ||||
-rw-r--r-- | tests/codeigniter/core/Loader_test.php | 38 | ||||
-rw-r--r-- | tests/codeigniter/core/URI_test.php | 33 | ||||
-rw-r--r-- | tests/mocks/autoloader.php | 33 | ||||
-rw-r--r-- | tests/mocks/ci_testcase.php (renamed from tests/lib/ci_testcase.php) | 5 | ||||
-rw-r--r-- | tests/mocks/core/common.php (renamed from tests/lib/common.php) | 2 | ||||
-rw-r--r-- | tests/mocks/core/loader.php | 33 | ||||
-rw-r--r-- | tests/mocks/core/uri.php | 27 |
9 files changed, 99 insertions, 81 deletions
diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 39c24b219..62c7d0d52 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -6,16 +6,15 @@ error_reporting(E_ALL | E_STRICT); $dir = realpath(dirname(__FILE__)); - // Path constants define('PROJECT_BASE', realpath($dir.'/../').'/'); define('BASEPATH', PROJECT_BASE.'system/'); define('APPPATH', PROJECT_BASE.'application/'); define('VIEWPATH', PROJECT_BASE.''); - // Prep our test environment -require_once $dir.'/lib/common.php'; -require_once $dir.'/lib/ci_testcase.php'; +include_once $dir.'/mocks/core/common.php'; +include_once $dir.'/mocks/autoloader.php'; +spl_autoload_register('autoload'); unset($dir);
\ No newline at end of file diff --git a/tests/codeigniter/core/Common_test.php b/tests/codeigniter/core/Common_test.php index cec12982d..29b512d8a 100644 --- a/tests/codeigniter/core/Common_test.php +++ b/tests/codeigniter/core/Common_test.php @@ -1,7 +1,5 @@ <?php -require_once(BASEPATH.'helpers/email_helper.php'); - class Common_test extends CI_TestCase { diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index b86fd3477..43008651e 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -1,38 +1,5 @@ <?php -require_once 'vfsStream/vfsStream.php'; -require_once BASEPATH.'/core/Loader.php'; - -class Extended_Loader extends CI_Loader { - - /** - * Since we use paths to load up models, views, etc, we need the ability to - * mock up the file system so when core tests are run, we aren't mucking - * in the application directory. this will give finer grained control over - * these tests. So yeah, while this looks odd, I need to overwrite protected - * class vars in the loader. So here we go... - * - * @covers CI_Loader::__construct() - */ - public function __construct() - { - vfsStreamWrapper::register(); - vfsStreamWrapper::setRoot(new vfsStreamDirectory('application')); - - $this->models_dir = vfsStream::newDirectory('models')->at(vfsStreamWrapper::getRoot()); - $this->libs_dir = vfsStream::newDirectory('libraries')->at(vfsStreamWrapper::getRoot()); - $this->helpers_dir = vfsStream::newDirectory('helpers')->at(vfsStreamWrapper::getRoot()); - $this->views_dir = vfsStream::newDirectory('views')->at(vfsStreamWrapper::getRoot()); - - $this->_ci_ob_level = ob_get_level(); - $this->_ci_library_paths = array(vfsStream::url('application').'/', BASEPATH); - $this->_ci_helper_paths = array(vfsStream::url('application').'/', BASEPATH); - $this->_ci_model_paths = array(vfsStream::url('application').'/'); - $this->_ci_view_paths = array(vfsStream::url('application').'/views/' => TRUE); - } -} - - class Loader_test extends CI_TestCase { private $ci_obj; @@ -40,7 +7,7 @@ class Loader_test extends CI_TestCase { public function set_up() { // Instantiate a new loader - $this->load = new Extended_Loader(); + $this->load = new Mock_Core_Loader(); // mock up a ci instance $this->ci_obj = new StdClass; @@ -265,7 +232,4 @@ class Loader_test extends CI_TestCase { // -------------------------------------------------------------------- - - - } diff --git a/tests/codeigniter/core/URI_test.php b/tests/codeigniter/core/URI_test.php index 40252aa14..e340ddf73 100644 --- a/tests/codeigniter/core/URI_test.php +++ b/tests/codeigniter/core/URI_test.php @@ -1,41 +1,10 @@ <?php -require BASEPATH.'core/URI.php'; - -/** - * Extend the URI class - * - override contructor - * - override CLI detection - */ -class URI_extended extends CI_URI { - - public function __construct() - { - $test = CI_TestCase::instance(); - $cls =& $test->ci_core_class('cfg'); - - // set predictable config values - $test->ci_set_config(array( - 'index_page' => 'index.php', - 'base_url' => 'http://example.com/', - 'subclass_prefix' => 'MY_' - )); - - $this->config = new $cls; - - } - - protected function _is_cli_request() - { - return FALSE; - } -} - class URI_test extends CI_TestCase { public function set_up() { - $this->uri = new URI_extended(); + $this->uri = new Mock_Core_URI(); } // -------------------------------------------------------------------- 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 diff --git a/tests/lib/ci_testcase.php b/tests/mocks/ci_testcase.php index afccee017..5c83974b3 100644 --- a/tests/lib/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -1,9 +1,5 @@ <?php - -// Need a way to change dependencies (core libs and laoded libs) -// Need a way to set the CI class - class CI_TestCase extends PHPUnit_Framework_TestCase { protected $ci_config; @@ -20,7 +16,6 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { 'security' => 'sec', 'input' => 'in', 'lang' => 'lang', - // @todo the loader is an edge case 'loader' => 'load', 'model' => 'model' ); diff --git a/tests/lib/common.php b/tests/mocks/core/common.php index 4a832587d..fc94d7fff 100644 --- a/tests/lib/common.php +++ b/tests/mocks/core/common.php @@ -129,4 +129,4 @@ function set_status_header($code = 200, $text = '') return TRUE; } -// EOF +// EOF
\ No newline at end of file diff --git a/tests/mocks/core/loader.php b/tests/mocks/core/loader.php new file mode 100644 index 000000000..115ccc3de --- /dev/null +++ b/tests/mocks/core/loader.php @@ -0,0 +1,33 @@ +<?php + +require_once 'vfsStream/vfsStream.php'; +require_once BASEPATH.'/core/Loader.php'; + +class Mock_Core_Loader extends CI_Loader { + + /** + * Since we use paths to load up models, views, etc, we need the ability to + * mock up the file system so when core tests are run, we aren't mucking + * in the application directory. this will give finer grained control over + * these tests. So yeah, while this looks odd, I need to overwrite protected + * class vars in the loader. So here we go... + * + * @covers CI_Loader::__construct() + */ + public function __construct() + { + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('application')); + + $this->models_dir = vfsStream::newDirectory('models')->at(vfsStreamWrapper::getRoot()); + $this->libs_dir = vfsStream::newDirectory('libraries')->at(vfsStreamWrapper::getRoot()); + $this->helpers_dir = vfsStream::newDirectory('helpers')->at(vfsStreamWrapper::getRoot()); + $this->views_dir = vfsStream::newDirectory('views')->at(vfsStreamWrapper::getRoot()); + + $this->_ci_ob_level = ob_get_level(); + $this->_ci_library_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_helper_paths = array(vfsStream::url('application').'/', BASEPATH); + $this->_ci_model_paths = array(vfsStream::url('application').'/'); + $this->_ci_view_paths = array(vfsStream::url('application').'/views/' => TRUE); + } +}
\ No newline at end of file diff --git a/tests/mocks/core/uri.php b/tests/mocks/core/uri.php new file mode 100644 index 000000000..207c7dc41 --- /dev/null +++ b/tests/mocks/core/uri.php @@ -0,0 +1,27 @@ +<?php + +require BASEPATH.'core/URI.php'; + +class Mock_Core_URI extends CI_URI { + + public function __construct() + { + $test = CI_TestCase::instance(); + $cls =& $test->ci_core_class('cfg'); + + // set predictable config values + $test->ci_set_config(array( + 'index_page' => 'index.php', + 'base_url' => 'http://example.com/', + 'subclass_prefix' => 'MY_' + )); + + $this->config = new $cls; + + } + + protected function _is_cli_request() + { + return FALSE; + } +}
\ No newline at end of file |