Added KH 3.3.0
This commit is contained in:
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
/**
|
||||
* TestCase for testing a database
|
||||
*
|
||||
* @package Kohana/UnitTest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
abstract class Kohana_Unittest_Database_TestCase extends PHPUnit_Extensions_Database_TestCase {
|
||||
|
||||
/**
|
||||
* Whether we should enable work arounds to make the tests compatible with phpunit 3.4
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $_assert_type_compatability = NULL;
|
||||
|
||||
/**
|
||||
* Make sure PHPUnit backs up globals
|
||||
* @var boolean
|
||||
*/
|
||||
protected $backupGlobals = FALSE;
|
||||
|
||||
/**
|
||||
* A set of unittest helpers that are shared between normal / database
|
||||
* testcases
|
||||
* @var Kohana_Unittest_Helpers
|
||||
*/
|
||||
protected $_helpers = NULL;
|
||||
|
||||
/**
|
||||
* A default set of environment to be applied before each test
|
||||
* @var array
|
||||
*/
|
||||
protected $environmentDefault = array();
|
||||
|
||||
/**
|
||||
* The kohana database connection that PHPUnit should use for this test
|
||||
* @var string
|
||||
*/
|
||||
protected $_database_connection = 'default';
|
||||
|
||||
/**
|
||||
* Creates a predefined environment using the default environment
|
||||
*
|
||||
* Extending classes that have their own setUp() should call
|
||||
* parent::setUp()
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
if(self::$_assert_type_compatability === NULL)
|
||||
{
|
||||
if( ! class_exists('PHPUnit_Runner_Version'))
|
||||
{
|
||||
require_once 'PHPUnit/Runner/Version.php';
|
||||
}
|
||||
|
||||
self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<=');
|
||||
}
|
||||
|
||||
$this->_helpers = new Kohana_Unittest_Helpers;
|
||||
|
||||
$this->setEnvironment($this->environmentDefault);
|
||||
|
||||
return parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the original environment overriden with setEnvironment()
|
||||
*
|
||||
* Extending classes that have their own tearDown()
|
||||
* should call parent::tearDown()
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
$this->_helpers->restore_environment();
|
||||
|
||||
return parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a connection to the unittesting database
|
||||
*
|
||||
* @return PDO
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
// Get the unittesting db connection
|
||||
$config = Kohana::$config->load('database.'.$this->_database_connection);
|
||||
|
||||
if($config['type'] !== 'pdo')
|
||||
{
|
||||
$config['connection']['dsn'] = $config['type'].':'.
|
||||
'host='.$config['connection']['hostname'].';'.
|
||||
'dbname='.$config['connection']['database'];
|
||||
}
|
||||
|
||||
$pdo = new PDO(
|
||||
$config['connection']['dsn'],
|
||||
$config['connection']['username'],
|
||||
$config['connection']['password']
|
||||
);
|
||||
|
||||
return $this->createDefaultDBConnection($pdo, $config['connection']['database']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a connection to the unittest database
|
||||
*
|
||||
* @return Kohana_Database The database connection
|
||||
*/
|
||||
public function getKohanaConnection()
|
||||
{
|
||||
return Database::instance(Kohana::$config->load('unittest')->db_connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all kohana related cache files in the cache directory
|
||||
*/
|
||||
public function cleanCacheDir()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::clean_cache_dir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that replaces all occurences of '/' with
|
||||
* the OS-specific directory separator
|
||||
*
|
||||
* @param string $path The path to act on
|
||||
* @return string
|
||||
*/
|
||||
public function dirSeparator($path)
|
||||
{
|
||||
return Kohana_Unittest_Helpers::dir_separator($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows easy setting & backing up of enviroment config
|
||||
*
|
||||
* Option types are checked in the following order:
|
||||
*
|
||||
* * Server Var
|
||||
* * Static Variable
|
||||
* * Config option
|
||||
*
|
||||
* @param array $environment List of environment to set
|
||||
*/
|
||||
public function setEnvironment(array $environment)
|
||||
{
|
||||
return $this->_helpers->set_environment($environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for internet connectivity
|
||||
*
|
||||
* @return boolean Whether an internet connection is available
|
||||
*/
|
||||
public function hasInternet()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::has_internet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertInstanceOf($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return parent::assertInstanceOf($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is not of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertNotInstanceOf($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertNotType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return self::assertNotInstanceOf($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertInternalType($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return parent::assertInternalType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is not of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertNotInternalType($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertNotType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return self::assertNotInternalType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
}
|
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Unit testing helpers
|
||||
*/
|
||||
class Kohana_Unittest_Helpers {
|
||||
/**
|
||||
* Static variable used to work out whether we have an internet
|
||||
* connection
|
||||
* @see has_internet
|
||||
* @var boolean
|
||||
*/
|
||||
static protected $_has_internet = NULL;
|
||||
|
||||
/**
|
||||
* Check for internet connectivity
|
||||
*
|
||||
* @return boolean Whether an internet connection is available
|
||||
*/
|
||||
public static function has_internet()
|
||||
{
|
||||
if ( ! isset(self::$_has_internet))
|
||||
{
|
||||
// The @ operator is used here to avoid DNS errors when there is no connection.
|
||||
$sock = @fsockopen("www.google.com", 80, $errno, $errstr, 1);
|
||||
|
||||
self::$_has_internet = (bool) $sock ? TRUE : FALSE;
|
||||
}
|
||||
|
||||
return self::$_has_internet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function which replaces the "/" to OS-specific delimiter
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
static public function dir_separator($path)
|
||||
{
|
||||
return str_replace('/', DIRECTORY_SEPARATOR, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all cache files from the kohana cache dir
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static public function clean_cache_dir()
|
||||
{
|
||||
$cache_dir = opendir(Kohana::$cache_dir);
|
||||
|
||||
while ($dir = readdir($cache_dir))
|
||||
{
|
||||
// Cache files are split into directories based on first two characters of hash
|
||||
if ($dir[0] !== '.' AND strlen($dir) === 2)
|
||||
{
|
||||
$dir = self::dir_separator(Kohana::$cache_dir.'/'.$dir.'/');
|
||||
|
||||
$cache = opendir($dir);
|
||||
|
||||
while ($file = readdir($cache))
|
||||
{
|
||||
if ($file[0] !== '.')
|
||||
{
|
||||
unlink($dir.$file);
|
||||
}
|
||||
}
|
||||
|
||||
closedir($cache);
|
||||
|
||||
rmdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
closedir($cache_dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Backup of the environment variables
|
||||
* @see set_environment
|
||||
* @var array
|
||||
*/
|
||||
protected $_environment_backup = array();
|
||||
|
||||
/**
|
||||
* Allows easy setting & backing up of enviroment config
|
||||
*
|
||||
* Option types are checked in the following order:
|
||||
*
|
||||
* * Server Var
|
||||
* * Static Variable
|
||||
* * Config option
|
||||
*
|
||||
* @param array $environment List of environment to set
|
||||
*/
|
||||
public function set_environment(array $environment)
|
||||
{
|
||||
if ( ! count($environment))
|
||||
return FALSE;
|
||||
|
||||
foreach ($environment as $option => $value)
|
||||
{
|
||||
$backup_needed = ! array_key_exists($option, $this->_environment_backup);
|
||||
|
||||
// Handle changing superglobals
|
||||
if (in_array($option, array('_GET', '_POST', '_SERVER', '_FILES')))
|
||||
{
|
||||
// For some reason we need to do this in order to change the superglobals
|
||||
global $$option;
|
||||
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = $$option;
|
||||
}
|
||||
|
||||
// PHPUnit makes a backup of superglobals automatically
|
||||
$$option = $value;
|
||||
}
|
||||
// If this is a static property i.e. Html::$windowed_urls
|
||||
elseif (strpos($option, '::$') !== FALSE)
|
||||
{
|
||||
list($class, $var) = explode('::$', $option, 2);
|
||||
|
||||
$class = new ReflectionClass($class);
|
||||
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = $class->getStaticPropertyValue($var);
|
||||
}
|
||||
|
||||
$class->setStaticPropertyValue($var, $value);
|
||||
}
|
||||
// If this is an environment variable
|
||||
elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option]))
|
||||
{
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : '';
|
||||
}
|
||||
|
||||
$_SERVER[$option] = $value;
|
||||
}
|
||||
// Else we assume this is a config option
|
||||
else
|
||||
{
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = Kohana::$config->load($option);
|
||||
}
|
||||
|
||||
list($group, $var) = explode('.', $option, 2);
|
||||
|
||||
Kohana::$config->load($group)->set($var, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the environment to the original state
|
||||
*
|
||||
* @chainable
|
||||
* @return Kohana_Unittest_Helpers $this
|
||||
*/
|
||||
public function restore_environment()
|
||||
{
|
||||
$this->set_environment($this->_environment_backup);
|
||||
}
|
||||
}
|
@@ -0,0 +1,261 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* A version of the stock PHPUnit testcase that includes some extra helpers
|
||||
* and default settings
|
||||
*/
|
||||
abstract class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* Whether we should enable work arounds to make the tests compatible with phpunit 3.4
|
||||
* @var boolean
|
||||
*/
|
||||
protected static $_assert_type_compatability = NULL;
|
||||
|
||||
/**
|
||||
* Make sure PHPUnit backs up globals
|
||||
* @var boolean
|
||||
*/
|
||||
protected $backupGlobals = FALSE;
|
||||
|
||||
/**
|
||||
* A set of unittest helpers that are shared between normal / database
|
||||
* testcases
|
||||
* @var Kohana_Unittest_Helpers
|
||||
*/
|
||||
protected $_helpers = NULL;
|
||||
|
||||
/**
|
||||
* A default set of environment to be applied before each test
|
||||
* @var array
|
||||
*/
|
||||
protected $environmentDefault = array();
|
||||
|
||||
/**
|
||||
* Creates a predefined environment using the default environment
|
||||
*
|
||||
* Extending classes that have their own setUp() should call
|
||||
* parent::setUp()
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
if(self::$_assert_type_compatability === NULL)
|
||||
{
|
||||
if( ! class_exists('PHPUnit_Runner_Version'))
|
||||
{
|
||||
require_once 'PHPUnit/Runner/Version.php';
|
||||
}
|
||||
|
||||
self::$_assert_type_compatability = version_compare(PHPUnit_Runner_Version::id(), '3.5.0', '<=');
|
||||
}
|
||||
|
||||
$this->_helpers = new Unittest_Helpers;
|
||||
|
||||
$this->setEnvironment($this->environmentDefault);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores the original environment overriden with setEnvironment()
|
||||
*
|
||||
* Extending classes that have their own tearDown()
|
||||
* should call parent::tearDown()
|
||||
*/
|
||||
public function tearDown()
|
||||
{
|
||||
$this->_helpers->restore_environment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all kohana related cache files in the cache directory
|
||||
*/
|
||||
public function cleanCacheDir()
|
||||
{
|
||||
return Unittest_Helpers::clean_cache_dir();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function that replaces all occurences of '/' with
|
||||
* the OS-specific directory separator
|
||||
*
|
||||
* @param string $path The path to act on
|
||||
* @return string
|
||||
*/
|
||||
public function dirSeparator($path)
|
||||
{
|
||||
return Unittest_Helpers::dir_separator($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows easy setting & backing up of enviroment config
|
||||
*
|
||||
* Option types are checked in the following order:
|
||||
*
|
||||
* * Server Var
|
||||
* * Static Variable
|
||||
* * Config option
|
||||
*
|
||||
* @param array $environment List of environment to set
|
||||
*/
|
||||
public function setEnvironment(array $environment)
|
||||
{
|
||||
return $this->_helpers->set_environment($environment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for internet connectivity
|
||||
*
|
||||
* @return boolean Whether an internet connection is available
|
||||
*/
|
||||
public function hasInternet()
|
||||
{
|
||||
return Unittest_Helpers::has_internet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertInstanceOf($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return parent::assertInstanceOf($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return parent::assertAttributeInstanceOf($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is not of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertNotInstanceOf($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertNotType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return self::assertNotInstanceOf($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeNotInstanceOf($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertInternalType($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return parent::assertInternalType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeInternalType($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeInternalType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a variable is not of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param mixed $actual
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertNotInternalType($expected, $actual, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertNotType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
return self::assertNotInternalType($expected, $actual, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an attribute is of a given type.
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $attributeName
|
||||
* @param mixed $classOrObject
|
||||
* @param string $message
|
||||
* @since Method available since Release 3.5.0
|
||||
*/
|
||||
public static function assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message = '')
|
||||
{
|
||||
if(self::$_assert_type_compatability)
|
||||
{
|
||||
return self::assertAttributeNotType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
|
||||
return self::assertAttributeNotInternalType($expected, $attributeName, $classOrObject, $message);
|
||||
}
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* A version of the stock PHPUnit testsuite that supports whitelisting and
|
||||
* blacklisting for code coverage filter
|
||||
*/
|
||||
abstract class Kohana_Unittest_TestSuite extends PHPUnit_Framework_TestSuite
|
||||
{
|
||||
/**
|
||||
* Holds the details of files that should be white and blacklisted for
|
||||
* code coverage
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_filter_calls = array(
|
||||
'addFileToBlacklist' => array(),
|
||||
'addDirectoryToBlacklist' => array(),
|
||||
'addFileToWhitelist' => array());
|
||||
|
||||
/**
|
||||
* Runs the tests and collects their result in a TestResult.
|
||||
*
|
||||
* @param PHPUnit_Framework_TestResult $result
|
||||
* @param mixed $filter
|
||||
* @param array $groups
|
||||
* @param array $excludeGroups
|
||||
* @param boolean $processIsolation
|
||||
* @return PHPUnit_Framework_TestResult
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function run(PHPUnit_Framework_TestResult $result = NULL, $filter = FALSE, array $groups = array(), array $excludeGroups = array(), $processIsolation = FALSE)
|
||||
{
|
||||
|
||||
// Get the code coverage filter from the suite's result object
|
||||
$coverage = $result->getCodeCoverage();
|
||||
|
||||
if ($coverage)
|
||||
{
|
||||
$coverage_filter = $coverage->filter();
|
||||
|
||||
// Apply the white and blacklisting
|
||||
foreach ($this->_filter_calls as $method => $args)
|
||||
{
|
||||
foreach ($args as $arg)
|
||||
{
|
||||
$coverage_filter->$method($arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return parent::run($result, $filter, $groups, $excludeGroups, $processIsolation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a file to be added to the code coverage blacklist when the suite runs
|
||||
* @param string $file
|
||||
*/
|
||||
public function addFileToBlacklist($file)
|
||||
{
|
||||
$this->_filter_calls['addFileToBlacklist'][] = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a directory to be added to the code coverage blacklist when the suite runs
|
||||
* @param string $dir
|
||||
*/
|
||||
public function addDirectoryToBlacklist($dir)
|
||||
{
|
||||
$this->_filter_calls['addDirectoryToBlacklist'][] = $dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queues a file to be added to the code coverage whitelist when the suite runs
|
||||
* @param string $file
|
||||
*/
|
||||
public function addFileToWhitelist($file)
|
||||
{
|
||||
$this->_filter_calls['addFileToWhitelist'][] = $file;
|
||||
}
|
||||
}
|
@@ -0,0 +1,267 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* PHPUnit testsuite for kohana application
|
||||
*
|
||||
* @package Kohana/UnitTest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class Kohana_Unittest_Tests {
|
||||
static protected $cache = array();
|
||||
|
||||
/**
|
||||
* Loads test files if they cannot be found by kohana
|
||||
* @param <type> $class
|
||||
*/
|
||||
static function autoload($class)
|
||||
{
|
||||
$file = str_replace('_', '/', $class);
|
||||
|
||||
if ($file = Kohana::find_file('tests', $file))
|
||||
{
|
||||
require_once $file;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the environment for testing
|
||||
*
|
||||
* Does the following:
|
||||
*
|
||||
* * Loads the phpunit framework (for the web ui)
|
||||
* * Restores exception phpunit error handlers (for cli)
|
||||
* * registeres an autoloader to load test files
|
||||
*/
|
||||
static public function configure_environment($do_whitelist = TRUE, $do_blacklist = TRUE)
|
||||
{
|
||||
restore_exception_handler();
|
||||
restore_error_handler();
|
||||
|
||||
spl_autoload_register(array('Unittest_tests', 'autoload'));
|
||||
|
||||
Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? array() : $cache;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the test suite for kohana
|
||||
*
|
||||
* @return Unittest_TestSuite
|
||||
*/
|
||||
static function suite()
|
||||
{
|
||||
static $suite = NULL;
|
||||
|
||||
if ($suite instanceof PHPUnit_Framework_TestSuite)
|
||||
{
|
||||
return $suite;
|
||||
}
|
||||
|
||||
Unittest_Tests::configure_environment();
|
||||
|
||||
$suite = new Unittest_TestSuite;
|
||||
|
||||
// Load the whitelist and blacklist for code coverage
|
||||
$config = Kohana::$config->load('unittest');
|
||||
|
||||
if ($config->use_whitelist)
|
||||
{
|
||||
Unittest_Tests::whitelist(NULL, $suite);
|
||||
}
|
||||
|
||||
if (count($config['blacklist']))
|
||||
{
|
||||
Unittest_Tests::blacklist($config->blacklist, $suite);
|
||||
}
|
||||
|
||||
// Add tests
|
||||
$files = Kohana::list_files('tests');
|
||||
self::addTests($suite, $files);
|
||||
|
||||
return $suite;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add files to test suite $suite
|
||||
*
|
||||
* Uses recursion to scan subdirectories
|
||||
*
|
||||
* @param Unittest_TestSuite $suite The test suite to add to
|
||||
* @param array $files Array of files to test
|
||||
*/
|
||||
static function addTests(Unittest_TestSuite $suite, array $files)
|
||||
{
|
||||
|
||||
foreach ($files as $path => $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
{
|
||||
if ($path != 'tests'.DIRECTORY_SEPARATOR.'test_data')
|
||||
{
|
||||
self::addTests($suite, $file);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Make sure we only include php files
|
||||
if (is_file($file) AND substr($file, -strlen(EXT)) === EXT)
|
||||
{
|
||||
// The default PHPUnit TestCase extension
|
||||
if ( ! strpos($file, 'TestCase'.EXT))
|
||||
{
|
||||
$suite->addTestFile($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
require_once($file);
|
||||
}
|
||||
|
||||
$suite->addFileToBlacklist($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blacklist a set of files in PHPUnit code coverage
|
||||
*
|
||||
* @param array $blacklist_items A set of files to blacklist
|
||||
* @param Unittest_TestSuite $suite The test suite
|
||||
*/
|
||||
static public function blacklist(array $blacklist_items, Unittest_TestSuite $suite = NULL)
|
||||
{
|
||||
foreach ($blacklist_items as $item)
|
||||
{
|
||||
if (is_dir($item))
|
||||
{
|
||||
$suite->addDirectoryToBlacklist($item);
|
||||
}
|
||||
else
|
||||
{
|
||||
$suite->addFileToBlacklist($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the whitelist
|
||||
*
|
||||
* If no directories are provided then the function'll load the whitelist
|
||||
* set in the config file
|
||||
*
|
||||
* @param array $directories Optional directories to whitelist
|
||||
* @param Unittest_Testsuite $suite Suite to load the whitelist into
|
||||
*/
|
||||
static public function whitelist(array $directories = NULL, Unittest_TestSuite $suite = NULL)
|
||||
{
|
||||
if (empty($directories))
|
||||
{
|
||||
$directories = self::get_config_whitelist();
|
||||
}
|
||||
|
||||
if (count($directories))
|
||||
{
|
||||
foreach ($directories as & $directory)
|
||||
{
|
||||
$directory = realpath($directory).'/';
|
||||
}
|
||||
|
||||
// Only whitelist the "top" files in the cascading filesystem
|
||||
self::set_whitelist(Kohana::list_files('classes', $directories), $suite);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Works out the whitelist from the config
|
||||
* Used only on the CLI
|
||||
*
|
||||
* @returns array Array of directories to whitelist
|
||||
*/
|
||||
static protected function get_config_whitelist()
|
||||
{
|
||||
$config = Kohana::$config->load('unittest');
|
||||
$directories = array();
|
||||
|
||||
if ($config->whitelist['app'])
|
||||
{
|
||||
$directories['k_app'] = APPPATH;
|
||||
}
|
||||
|
||||
if ($modules = $config->whitelist['modules'])
|
||||
{
|
||||
$k_modules = Kohana::modules();
|
||||
|
||||
// Have to do this because kohana merges config...
|
||||
// If you want to include all modules & override defaults then TRUE must be the first
|
||||
// value in the modules array of your app/config/unittest file
|
||||
if (array_search(TRUE, $modules, TRUE) === (count($modules) - 1))
|
||||
{
|
||||
$modules = $k_modules;
|
||||
}
|
||||
elseif (array_search(FALSE, $modules, TRUE) === FALSE)
|
||||
{
|
||||
$modules = array_intersect_key($k_modules, array_combine($modules, $modules));
|
||||
}
|
||||
else
|
||||
{
|
||||
// modules are disabled
|
||||
$modules = array();
|
||||
}
|
||||
|
||||
$directories += $modules;
|
||||
}
|
||||
|
||||
if ($config->whitelist['system'])
|
||||
{
|
||||
$directories['k_sys'] = SYSPATH;
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively whitelists an array of files
|
||||
*
|
||||
* @param array $files Array of files to whitelist
|
||||
* @param Unittest_TestSuite $suite Suite to load the whitelist into
|
||||
*/
|
||||
static protected function set_whitelist($files, Unittest_TestSuite $suite = NULL)
|
||||
{
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
{
|
||||
self::set_whitelist($file, $suite);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ! isset(Unittest_tests::$cache[$file]))
|
||||
{
|
||||
$relative_path = substr($file, strrpos($file, 'classes'.DIRECTORY_SEPARATOR) + 8, -strlen(EXT));
|
||||
$cascading_file = Kohana::find_file('classes', $relative_path);
|
||||
|
||||
// The theory is that if this file is the highest one in the cascading filesystem
|
||||
// then it's safe to whitelist
|
||||
Unittest_tests::$cache[$file] = ($cascading_file === $file);
|
||||
}
|
||||
|
||||
if (Unittest_tests::$cache[$file])
|
||||
{
|
||||
if (isset($suite))
|
||||
{
|
||||
$suite->addFileToWhitelist($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToWhitelist($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Transparent extension for Kohana_Unittest_Database_TestCase
|
||||
*
|
||||
* Provides some unittest helpers and allows a kohana database connection to be
|
||||
* used to connect to the database
|
||||
*
|
||||
* @package Kohana/UnitTest
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
abstract class Unittest_Database_TestCase extends Kohana_Unittest_Database_TestCase
|
||||
{
|
||||
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Unittest_Helpers extends Kohana_Unittest_Helpers {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
abstract class Unittest_TestCase extends Kohana_Unittest_TestCase {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class Unittest_TestSuite extends Kohana_Unittest_TestSuite {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Unittest_Tests extends Kohana_Unittest_Tests {}
|
Reference in New Issue
Block a user