Commits

Pascal Kriete committed 2b84e69

Adding early bootstrap ideas for core test suite

Comments (0)

Files changed (10)

+<?php
+
+ini_set('display_errors', 1);
+error_reporting(E_ALL | E_STRICT);
+
+if ( ! defined('PROJECT_BASE'))
+{
+	define('PROJECT_BASE',	realpath(dirname(__FILE__).'/../').'/');
+
+	define('BASEPATH',		PROJECT_BASE.'system/');
+	define('APPPATH',		PROJECT_BASE.'application/');
+}
+// define('EXT', '.php');
+
+// @todo provide a way to set various config options
+
+
+
+// set up a highly controlled CI environment
+require_once './lib/common.php';
+require_once './lib/ci_testcase.php';

tests/codeigniter/Setup_test.php

+<?php
+
+class Setup_test extends PHPUnit_Framework_TestCase {
+	
+	function testNonsense()
+	{
+		$this->markTestIncomplete('not implemented');
+		// ensure that our bootstrapped test environment
+		// is a good representation of an isolated CI install
+		//die('here');
+	}
+	
+}

tests/codeigniter/core/Config_test.php

+<?php
+
+class Config_test extends CodeIgniterTestCase {
+
+	public function setUp()
+	{
+		$cls =& $this->ci_core_class('cfg');
+		
+		$stub = $this->getMock($cls, NULL, array(), '', FALSE);
+		
+	 	//I would prefer this, but it currently
+		// does not work as when you try to pass
+		// null to setMethods it fails on an internal
+		// function call that expects an array =(
+		/*
+		$stub = $this->getMockBuilder($cls)
+					 ->disableOriginalConstructor()
+					 ->setMethods(null)
+					 ->getMock();
+		*/
+
+		
+		// set predictable config values
+		$stub->config = array(
+			'index_page'		=> 'index.php',
+			'base_url'			=> 'http://example.com/',
+			'subclass_prefix'	=> 'MY_'
+		);
+		
+		$this->config = $stub;
+	}
+	
+	// --------------------------------------------------------------------
+
+	public function testItem()
+	{
+		$this->assertEquals('http://example.com/', $this->config->item('base_url'));
+
+		// Bad Config value
+		$this->assertFalse($this->config->item('no_good_item'));
+		
+		// Index
+		$this->assertFalse($this->config->item('no_good_item', 'bad_index'));
+		$this->assertFalse($this->config->item('no_good_item', 'default'));
+	}
+	
+	// --------------------------------------------------------------------
+	
+	public function testSetItem()
+	{
+		$this->assertFalse($this->config->item('not_yet_set'));
+		
+		$this->config->set_item('not_yet_set', 'is set');
+		
+		$this->assertEquals('is set', $this->config->item('not_yet_set'));
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testSlashItem()
+	{
+		// Bad Config value
+		$this->assertFalse($this->config->slash_item('no_good_item'));
+		
+		$this->assertEquals('http://example.com/', $this->config->slash_item('base_url'));
+
+		$this->assertEquals('MY_/', $this->config->slash_item('subclass_prefix'));
+	}
+
+	// --------------------------------------------------------------------
+
+	public function testSiteUrl()
+	{
+		$this->assertEquals('http://example.com/index.php', $this->config->site_url());
+		
+		$base_url = $this->config->item('base_url');
+		
+		$this->config->set_item('base_url', '');
+		
+		$q_string = $this->config->item('enable_query_strings');
+		
+		$this->config->set_item('enable_query_strings', FALSE);
+
+		$this->assertEquals('/index.php/test', $this->config->site_url('test'));
+		$this->assertEquals('/index.php/test/1', $this->config->site_url(array('test', '1')));
+		
+		$this->config->set_item('enable_query_strings', TRUE);
+
+		$this->assertEquals('/index.php?test', $this->config->site_url('test'));
+		$this->assertEquals('/index.php?0=test&1=1', $this->config->site_url(array('test', '1')));
+		
+		$this->config->set_item('base_url', $base_url);
+
+		$this->assertEquals('http://example.com/index.php?test', $this->config->site_url('test'));
+		
+		// back to home base
+		$this->config->set_item('enable_query_strings', $q_string);				
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testSystemUrl()
+	{
+		$this->assertEquals('http://example.com/system/', $this->config->system_url());
+	}
+
+}

tests/codeigniter/core/Lang_test.php

+<?php
+
+class Lang_test extends CodeIgniterTestCase {
+	
+	protected $lang;
+	
+	public function setUp()
+	{
+		$cls = $this->ci_core_class('lang');
+		$this->lang = new $cls;
+	}
+	
+	// --------------------------------------------------------------------
+	
+	public function testLoad()
+	{
+		// get_config needs work
+		$this->markTestIncomplete('get_config needs work');
+		//$this->assertTrue($this->lang->load('profiler'));
+	}
+	
+	// --------------------------------------------------------------------
+
+	public function testLine()
+	{
+		$this->markTestIncomplete('get_config needs work');
+		
+		$this->assertEquals('URI STRING', $this->lang->line('profiler_uri_string'));
+	}
+	
+}

tests/codeigniter/core/Loader_test.php

+<?php
+
+class Loader_test extends CodeIgniterTestCase {
+	
+	private $ci_obj;
+	
+	public function setUp()
+	{
+		// Instantiate a new loader
+		$cls = $this->ci_core_class('load');
+		$this->_loader = new $cls;
+		
+		// mock up a ci instance
+		$this->ci_obj = new StdClass;
+		
+		// Fix get_instance()
+		CodeIgniterTestCase::$test_instance =& $this;
+		$this->ci_instance($this->ci_obj);
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testLibrary()
+	{
+		// Mock up a config object until we
+		// figure out how to test the library configs
+		$config = $this->getMock('CI_Config', NULL, array(), '', FALSE);
+		$config->expects($this->any())
+			   ->method('load')
+			   ->will($this->returnValue(TRUE));
+		
+		// Add the mock to our stdClass
+		$this->ci_set_instance_var('config', $config);
+		
+		// Test loading as an array.
+		$this->assertEquals(NULL, $this->_loader->library(array('table')));
+		$this->assertTrue(class_exists('CI_Table'), 'Table class exists');
+		$this->assertAttributeInstanceOf('CI_Table', 'table', $this->ci_obj);
+		
+		// Test no lib given
+		$this->assertEquals(FALSE, $this->_loader->library());
+		
+		// Test a string given to params
+		$this->assertEquals(NULL, $this->_loader->library('table', ' '));
+	}	
+	
+	// --------------------------------------------------------------------
+	
+	public function testModels()
+	{
+		// Test loading as an array.
+		$this->assertEquals(NULL, $this->_loader->model(array('foobar')));
+		
+		// Test no model given
+		$this->assertEquals(FALSE, $this->_loader->model(''));
+		
+		// Test a string given to params
+		$this->assertEquals(NULL, $this->_loader->model('foobar', ' '));		
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testDatabase()
+	{
+		$this->assertEquals(NULL, $this->_loader->database());
+		$this->assertEquals(NULL, $this->_loader->dbutil());		
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testView()
+	{
+		// I'm not entirely sure this is the proper way to handle this.
+		// So, let's revist it, m'kay?
+		try 
+		{
+			 $this->_loader->view('foo');
+		}
+		catch (Exception $expected)
+		{
+			return;
+		}
+	}
+
+	// --------------------------------------------------------------------
+
+	public function testFile()
+	{
+		// I'm not entirely sure this is the proper way to handle this.
+		// So, let's revist it, m'kay?
+		try 
+		{
+			 $this->_loader->file('foo');
+		}
+		catch (Exception $expected)
+		{
+			return;
+		}		
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testVars()
+	{
+		$vars = array(
+			'foo'	=> 'bar'
+		);
+		
+		$this->assertEquals(NULL, $this->_loader->vars($vars));
+		$this->assertEquals(NULL, $this->_loader->vars('foo', 'bar'));
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function testHelper()
+	{
+		$this->assertEquals(NULL, $this->_loader->helper('array'));
+		$this->assertEquals(NULL, $this->_loader->helper('bad'));
+	}
+	
+	// --------------------------------------------------------------------
+
+	public function testHelpers()
+	{
+		$this->assertEquals(NULL, $this->_loader->helpers(array('file', 'array', 'string')));
+	}
+	
+	// --------------------------------------------------------------------
+	
+	// public function testLanguage()
+	// {
+	// 	$this->assertEquals(NULL, $this->_loader->language('test'));
+	// }	
+
+	// --------------------------------------------------------------------
+
+	public function testLoadConfig()
+	{
+		$this->assertEquals(NULL, $this->_loader->config('config', FALSE, TRUE));
+	}
+	
+	
+	
+}

tests/codeigniter/helpers/Array_helper_test.php

+<?php
+
+// OLD TEST FORMAT: DO NOT COPY
+
+require_once(BASEPATH.'helpers/array_helper.php');
+
+class Array_helper_test extends PHPUnit_Framework_TestCase
+{
+	public function setUp()
+	{
+		$this->my_array = array(
+			'foo'		=> 'bar',
+			'sally'		=> 'jim',
+			'maggie'	=> 'bessie',
+			'herb'		=> 'cook'
+		);
+	}
+	
+	// ------------------------------------------------------------------------
+	
+	public function testElementWithExistingItem()
+	{	
+		$this->assertEquals(FALSE, element('testing', $this->my_array));
+		
+		$this->assertEquals('not set', element('testing', $this->my_array, 'not set'));
+		
+		$this->assertEquals('bar', element('foo', $this->my_array));
+	}
+	
+	// ------------------------------------------------------------------------	
+
+	public function testRandomElement()
+	{
+		// Send a string, not an array to random_element
+		$this->assertEquals('my string', random_element('my string'));
+		
+		// Test sending an array
+		$this->assertEquals(TRUE, in_array(random_element($this->my_array), $this->my_array));
+	}
+
+	// ------------------------------------------------------------------------	
+	
+	public function testElements()
+	{
+		$this->assertEquals(TRUE, is_array(elements('test', $this->my_array)));
+		$this->assertEquals(TRUE, is_array(elements('foo', $this->my_array)));
+	}
+
+}

tests/codeigniter/libraries/Parser_test.php

+<?php
+
+// OLD TEST FORMAT: DO NOT COPY
+
+class Parser_test extends PHPUnit_Framework_TestCase
+{
+	static $cls;
+	protected $parser;
+	
+	public static function setUpBeforeClass()
+	{
+		$CI = get_instance();
+		$CI->load->library('parser');
+		self::$cls = get_class($CI->parser);
+	}
+
+	// --------------------------------------------------------------------
+	
+	public function setUp()
+	{
+		$cls = self::$cls;
+		$this->parser = new $cls;
+	}
+	// --------------------------------------------------------------------
+	
+	public function testSetDelimiters()
+	{
+		// Make sure default delimiters are there
+		$this->assertEquals('{', $this->parser->l_delim);
+		$this->assertEquals('}', $this->parser->r_delim);
+		
+		// Change them to square brackets
+		$this->parser->set_delimiters('[', ']');
+		
+		// Make sure they changed
+		$this->assertEquals('[', $this->parser->l_delim);
+		$this->assertEquals(']', $this->parser->r_delim);
+		
+		// Reset them
+		$this->parser->set_delimiters();
+		
+		// Make sure default delimiters are there
+		$this->assertEquals('{', $this->parser->l_delim);
+		$this->assertEquals('}', $this->parser->r_delim);
+	}
+	
+	// --------------------------------------------------------------------
+	
+	public function testParseSimpleString()
+	{
+		$data = array(
+			'title' => 'Page Title',
+			'body' => 'Lorem ipsum dolor sit amet.'
+		);
+		
+		$template = "{title}\n{body}";
+		
+		$result = implode("\n", $data);
+		
+		$this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));
+	}
+	
+	// --------------------------------------------------------------------
+
+	public function testParse()
+	{
+		$this->_parse_no_template();
+		$this->_parse_var_pair();
+		$this->_mismatched_var_pair();
+	}
+
+	// --------------------------------------------------------------------
+
+	private function _parse_no_template()
+	{
+		$this->assertFalse($this->parser->parse_string('', '', TRUE));
+	}
+
+	// --------------------------------------------------------------------
+
+	private function _parse_var_pair()
+	{
+		$data = array(
+			'title'		=> 'Super Heroes',
+			'powers'	=> array(
+					array(
+						'invisibility'	=> 'yes',
+						'flying'		=> 'no'),
+			)
+		);
+		
+		$template = "{title}\n{powers}{invisibility}\n{flying}{/powers}";
+		
+		$result = "Super Heroes\nyes\nno";
+		
+		$this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));	
+	}
+
+	// --------------------------------------------------------------------
+
+	private function _mismatched_var_pair()
+	{
+		$data = array(
+			'title'		=> 'Super Heroes',
+			'powers'	=> array(
+					array(
+						'invisibility'	=> 'yes',
+						'flying'		=> 'no'),
+			)
+		);
+		
+		$template = "{title}\n{powers}{invisibility}\n{flying}";
+		
+		$result = "Super Heroes\n{powers}{invisibility}\n{flying}";
+		
+		$this->assertEquals($result, $this->parser->parse_string($template, $data, TRUE));			
+	}
+
+	// --------------------------------------------------------------------
+
+	// --------------------------------------------------------------------
+
+	// --------------------------------------------------------------------
+
+}

tests/lib/ci_testcase.php

+<?php
+
+
+// Need a way to change dependencies (core libs and laoded libs)
+// Need a way to set the CI class
+
+class CodeIgniterTestCase extends PHPUnit_Framework_TestCase {
+		
+	public $ci_instance;
+	public static $test_instance;
+	public static $global_map = array(
+		'benchmark'	=> 'bm',
+		'config'	=> 'cfg',
+		'hooks'		=> 'ext',
+		'utf8'		=> 'uni',
+		'router'	=> 'rtr',
+		'output'	=> 'out',
+		'security'	=> 'sec',
+		'input'		=> 'in',
+		'lang'		=> 'lang',
+		
+		// @todo the loader is an edge case
+		'loader'	=> 'load'
+	);
+	
+	function __construct()
+	{
+		parent::__construct();
+	}
+	
+	// --------------------------------------------------------------------
+	
+	// Change what get_instance returns
+	function ci_instance($obj)
+	{
+		$this->ci_instance = $obj;
+	}
+	
+	// --------------------------------------------------------------------
+	
+	function ci_set_instance_var($name, $obj)
+	{
+		$this->ci_instance->$name =& $obj;
+	}
+	
+	// --------------------------------------------------------------------
+	
+	// Set a class to a mock before it is loaded
+	function ci_library($name)
+	{
+		
+	}
+
+	// --------------------------------------------------------------------
+
+	/**
+	 * Grab a core class
+	 *
+	 * Loads the correct core class without extensions
+	 * and returns a reference to the class name in the
+	 * globals array with the correct key. This way the
+	 * test can modify the variable it assigns to and
+	 * still maintain the global.
+	 */
+	function &ci_core_class($name)
+	{
+		$name = strtolower($name);
+		
+		if (isset(self::$global_map[$name]))
+		{
+			$class_name = ucfirst($name);
+			$global_name = self::$global_map[$name];
+		}
+		elseif (in_array($name, self::$global_map))
+		{
+			$class_name = ucfirst(array_search($name, self::$global_map));
+			$global_name = $name;
+		}
+		else
+		{
+			throw new Exception('Not a valid core class.');
+		}
+		
+		if ( ! class_exists('CI_'.$class_name))
+		{
+			require_once BASEPATH.'core/'.$class_name.'.php';
+		}
+		
+		$GLOBALS[strtoupper($global_name)] = 'CI_'.$class_name;
+		return $GLOBALS[strtoupper($global_name)];
+	}
+	
+	// --------------------------------------------------------------------
+	
+	// convenience function for global mocks
+	function ci_set_core_class($name, $obj)
+	{
+		$orig =& $this->ci_core_class($name);
+		$orig = $obj;
+	}
+	
+	// --------------------------------------------------------------------
+	
+	static function ci_config($item)
+	{
+		return '';
+	}
+}
+
+// EOF
+<?php
+
+// Set up the global CI functions in their most minimal core representation
+
+function &get_instance() 
+{
+	$test = CodeIgniterTestCase::$test_instance;
+	return $test->ci_instance;
+}
+
+// Config Stuff | @todo High priority!
+// --------------------------------------------------------------------
+
+function get_config() { die('implement me'); }
+
+function config_item($item)
+{
+	return CodeIgniterTestCase::ci_config($item);
+}
+
+// --------------------------------------------------------------------
+
+function load_class($class, $directory = 'libraries', $prefix = 'CI_')
+{
+	if ($directory != 'core' OR $prefix != 'CI_')
+	{
+		throw new Exception('Not Implemented: Non-core load_class()');
+	}
+	
+	$test = CodeIgniterTestCase::$test_instance;
+	
+	$obj =& $test->ci_core_class($class);
+	
+	if (is_string($obj))
+	{
+		throw new Exception('Bad Isolation: Use ci_set_core_class to set '.$class.'');
+	}
+	
+	return $obj;
+}
+
+// This is sort of meh. Should probably be mocked up with
+// controllable output, so that we can test some of our
+// security code. The function itself will be tested in the
+// bootstrap testsuite.
+// --------------------------------------------------------------------
+
+function remove_invisible_characters($str, $url_encoded = TRUE)
+{
+	$non_displayables = array();
+	
+	// every control character except newline (dec 10)
+	// carriage return (dec 13), and horizontal tab (dec 09)
+	
+	if ($url_encoded)
+	{
+		$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
+		$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
+	}
+	
+	$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127
+
+	do
+	{
+		$str = preg_replace($non_displayables, '', $str, -1, $count);
+	}
+	while ($count);
+
+	return $str;
+}
+
+
+// Clean up error messages
+// --------------------------------------------------------------------
+
+function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
+{
+	throw new Exception('CI Error: '.$message);
+}
+
+function show_404($page = '', $log_error = TRUE)
+{
+	throw new Exception('CI Error: 404');
+}
+
+function _exception_handler($severity, $message, $filepath, $line)
+{
+	throw new Exception('CI Exception: '.$message.' | '.$filepath.' | '.$line);
+}
+
+
+// We assume a few things about our environment ...
+// --------------------------------------------------------------------
+
+function is_php($version = '5.0.0')
+{
+	return ! (version_compare(PHP_VERSION, $version) < 0);
+}
+
+function is_really_writable($file)
+{
+	return is_writable($file);
+}
+
+function is_loaded()
+{
+	throw new Exception('Bad Isolation: mock up environment');
+}
+
+function log_message($level = 'error', $message, $php_error = FALSE)
+{
+	return TRUE;
+}
+
+function set_status_header($code = 200, $text = '')
+{
+	return TRUE;
+}
+
+// EOF
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit 
+	bootstrap="Bootstrap.php"
+	colors="true">
+	<testsuites>
+		<testsuite name="CodeIgniter Core Test Suite">
+			<file>codeigniter/Setup_test.php</file>
+			<directory suffix="test.php">codeigniter/core</directory>
+			
+			<!-- We'll worry about these later ...
+			<directory suffix="test.php">codeigniter/libraries</directory>
+			<directory suffix="test.php">codeigniter/helpers</directory>
+			-->
+		</testsuite>
+	</testsuites>
+</phpunit>