Upgrade to KH 3.1.3.1
This commit is contained in:
@@ -25,46 +25,12 @@ And watch the gitorious magic...
|
||||
|
||||
Of course, you can always download the code from the [github project](http://github.com/kohana/unittest) as an archive.
|
||||
|
||||
The following instructions will assume you've moved it to `modules/unittest`, if you haven't then you should update all paths accordingly.
|
||||
## Running the tests
|
||||
|
||||
**Step 1**: Enable this module in your bootstrap file:
|
||||
$ phpunit --bootstrap=modules/unittest/bootstrap.php {tests}
|
||||
|
||||
/**
|
||||
* Enable modules. Modules are referenced by a relative or absolute path.
|
||||
*/
|
||||
Kohana::modules(array(
|
||||
'unittest' => MODPATH.'unittest', // PHPUnit integration
|
||||
));
|
||||
Where `{tests}` can either be a path to a folder of tests, or a path to the the `tests.php` (`modules/unittest/tests.php`)
|
||||
|
||||
**Step 2**: In your app's bootstrap file modify the lines where the request is handled, which by default looks like:
|
||||
Please see the guide pages for more info. An example of how we run the tests for the kohana project can be found in the [phing build script](https://github.com/kohana/kohana/blob/3.1/master/build.xml#L172).
|
||||
|
||||
/**
|
||||
* Execute the main request using PATH_INFO. If no URI source is specified,
|
||||
* the URI will be automatically detected.
|
||||
*/
|
||||
echo Request::instance($_SERVER['PATH_INFO'])
|
||||
->execute()
|
||||
->send_headers()
|
||||
->response;
|
||||
|
||||
To:
|
||||
|
||||
if ( ! defined('SUPPRESS_REQUEST'))
|
||||
{
|
||||
/**
|
||||
* Execute the main request using PATH_INFO. If no URI source is specified,
|
||||
* the URI will be automatically detected.
|
||||
*/
|
||||
echo Request::instance($_SERVER['PATH_INFO'])
|
||||
->execute()
|
||||
->send_headers()
|
||||
->response;
|
||||
}
|
||||
|
||||
**Step 3**: Create a folder called `unittest` in your app's cache dir (`APPPATH/cache`). If you don't want to use this path for storing generated reports, skip this step and change the config file.
|
||||
|
||||
Note: make sure the settings in `config/unittest.php` are correct for your environment. If they aren't, copy the file to `application/config/unittest.php` and change the values accordingly.
|
||||
|
||||
**Step 4**: Start testing!
|
||||
|
||||
You can find more info and tutorials in the [guide/](http://github.com/kohana/unittest/tree/master/guide/) directory.
|
||||
If you're looking for more info on running the core kohana tests then please see our [dev wiki](https://github.com/kohana/kohana/wiki/Unit-Testing-Kohana)
|
104
includes/kohana/modules/unittest/bootstrap.php
Normal file
104
includes/kohana/modules/unittest/bootstrap.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* The directory in which your application specific resources are located.
|
||||
* The application directory must contain the bootstrap.php file.
|
||||
*
|
||||
* @see http://kohanaframework.org/guide/about.install#application
|
||||
*/
|
||||
$application = 'application';
|
||||
|
||||
/**
|
||||
* The directory in which your modules are located.
|
||||
*
|
||||
* @see http://kohanaframework.org/guide/about.install#modules
|
||||
*/
|
||||
$modules = 'modules';
|
||||
|
||||
/**
|
||||
* The directory in which the Kohana resources are located. The system
|
||||
* directory must contain the classes/kohana.php file.
|
||||
*
|
||||
* @see http://kohanaframework.org/guide/about.install#system
|
||||
*/
|
||||
$system = 'system';
|
||||
|
||||
/**
|
||||
* The default extension of resource files. If you change this, all resources
|
||||
* must be renamed to use the new extension.
|
||||
*
|
||||
* @see http://kohanaframework.org/guide/about.install#ext
|
||||
*/
|
||||
define('EXT', '.php');
|
||||
|
||||
/**
|
||||
* Set the PHP error reporting level. If you set this in php.ini, you remove this.
|
||||
* @see http://php.net/error_reporting
|
||||
*
|
||||
* When developing your application, it is highly recommended to enable notices
|
||||
* and strict warnings. Enable them by using: E_ALL | E_STRICT
|
||||
*
|
||||
* In a production environment, it is safe to ignore notices and strict warnings.
|
||||
* Disable them by using: E_ALL ^ E_NOTICE
|
||||
*
|
||||
* When using a legacy application with PHP >= 5.3, it is recommended to disable
|
||||
* deprecated notices. Disable with: E_ALL & ~E_DEPRECATED
|
||||
*/
|
||||
error_reporting(E_ALL | E_STRICT);
|
||||
|
||||
/**
|
||||
* End of standard configuration! Changing any of the code below should only be
|
||||
* attempted by those with a working knowledge of Kohana internals.
|
||||
*
|
||||
* @see http://kohanaframework.org/guide/using.configuration
|
||||
*/
|
||||
|
||||
// Set the full path to the docroot
|
||||
define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
|
||||
|
||||
// Make the application relative to the docroot
|
||||
if ( ! is_dir($application) AND is_dir(DOCROOT.$application))
|
||||
{
|
||||
$application = DOCROOT.$application;
|
||||
}
|
||||
|
||||
// Make the modules relative to the docroot
|
||||
if ( ! is_dir($modules) AND is_dir(DOCROOT.$modules))
|
||||
{
|
||||
$modules = DOCROOT.$modules;
|
||||
}
|
||||
|
||||
// Make the system relative to the docroot
|
||||
if ( ! is_dir($system) AND is_dir(DOCROOT.$system))
|
||||
{
|
||||
$system = DOCROOT.$system;
|
||||
}
|
||||
|
||||
// Define the absolute paths for configured directories
|
||||
define('APPPATH', realpath($application).DIRECTORY_SEPARATOR);
|
||||
define('MODPATH', realpath($modules).DIRECTORY_SEPARATOR);
|
||||
define('SYSPATH', realpath($system).DIRECTORY_SEPARATOR);
|
||||
|
||||
// Clean up the configuration vars
|
||||
unset($application, $modules, $system);
|
||||
|
||||
/**
|
||||
* Define the start time of the application, used for profiling.
|
||||
*/
|
||||
if ( ! defined('KOHANA_START_TIME'))
|
||||
{
|
||||
define('KOHANA_START_TIME', microtime(TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the memory usage at the start of the application, used for profiling.
|
||||
*/
|
||||
if ( ! defined('KOHANA_START_MEMORY'))
|
||||
{
|
||||
define('KOHANA_START_MEMORY', memory_get_usage());
|
||||
}
|
||||
|
||||
// Bootstrap the application
|
||||
require APPPATH.'bootstrap'.EXT;
|
||||
|
||||
// Enable the unittest module
|
||||
Kohana::modules(Kohana::modules() + array('unittest' => MODPATH.'unittest'));
|
@@ -2,15 +2,15 @@
|
||||
/**
|
||||
* PHPUnit Kohana web based test runner
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @package Kohana/UnitTest
|
||||
* @author Kohana Team
|
||||
* @author BRMatt <matthew@sigswitch.com>
|
||||
* @author Paul Banks
|
||||
* @author Paul Banks
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
|
||||
Class Controller_UnitTest extends Controller_Template
|
||||
class Controller_UnitTest extends Controller_Template
|
||||
{
|
||||
/**
|
||||
* Whether the archive module is available
|
||||
@@ -20,7 +20,7 @@ Class Controller_UnitTest extends Controller_Template
|
||||
|
||||
/**
|
||||
* Unittest config
|
||||
* @var Kohana_Config
|
||||
* @var Config
|
||||
*/
|
||||
protected $config = NULL;
|
||||
|
||||
@@ -55,28 +55,28 @@ Class Controller_UnitTest extends Controller_Template
|
||||
{
|
||||
parent::before();
|
||||
|
||||
if ( ! Kohana_Tests::enabled())
|
||||
if ( ! Unittest_tests::enabled())
|
||||
{
|
||||
// Pretend this is a normal 404 error...
|
||||
$this->status = 404;
|
||||
|
||||
throw new Kohana_Request_Exception('Unable to find a route to match the URI: :uri',
|
||||
array(':uri' => $this->request->uri));
|
||||
array(':uri' => $this->request->uri()));
|
||||
}
|
||||
|
||||
// Prevent the whitelist from being autoloaded, but allow the blacklist
|
||||
// to be loaded
|
||||
Kohana_Tests::configure_environment(FALSE);
|
||||
Unittest_Tests::configure_environment(FALSE);
|
||||
|
||||
$this->config = Kohana::config('unittest');
|
||||
|
||||
// This just stops some very very long lines
|
||||
$route = Route::get('unittest');
|
||||
$this->report_uri = $route->uri(array('action' => 'report'));
|
||||
$this->run_uri = $route->uri(array('action' => 'run'));
|
||||
$this->run_uri = $route->uri(array('action' => 'run'));
|
||||
|
||||
// Switch used to disable cc settings
|
||||
$this->xdebug_loaded = extension_loaded('xdebug');
|
||||
$this->xdebug_loaded = extension_loaded('xdebug');
|
||||
$this->cc_archive_enabled = class_exists('Archive');
|
||||
|
||||
Kohana_View::set_global('xdebug_enabled', $this->xdebug_loaded);
|
||||
@@ -92,7 +92,7 @@ Class Controller_UnitTest extends Controller_Template
|
||||
->set('run_uri', $this->run_uri)
|
||||
->set('report_uri', $this->report_uri)
|
||||
->set('whitelistable_items', $this->get_whitelistable_items())
|
||||
->set('groups', $this->get_groups_list(Kohana_Tests::suite()));
|
||||
->set('groups', $this->get_groups_list(Unittest_tests::suite()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,16 +102,14 @@ Class Controller_UnitTest extends Controller_Template
|
||||
{
|
||||
// Fairly foolproof
|
||||
if ( ! $this->config->cc_report_path AND ! class_exists('Archive'))
|
||||
{
|
||||
throw new Kohana_Exception('Cannot generate report');
|
||||
}
|
||||
|
||||
// We don't want to use the HTML layout, we're sending the user 100111011100110010101100
|
||||
$this->auto_render = FALSE;
|
||||
|
||||
$suite = Kohana_Tests::suite();
|
||||
$suite = Unittest_tests::suite();
|
||||
$temp_path = rtrim($this->config->temp_path, '/').'/';
|
||||
$group = (array) Arr::get($_GET, 'group', array());
|
||||
$group = (array) Arr::get($_GET, 'group', array());
|
||||
|
||||
// Stop unittest from interpretting "all groups" as "no groups"
|
||||
if (empty($group) OR empty($group[0]))
|
||||
@@ -150,21 +148,17 @@ Class Controller_UnitTest extends Controller_Template
|
||||
else
|
||||
{
|
||||
$folder = trim($this->config->cc_report_path, '/').'/';
|
||||
$path = DOCROOT.$folder;
|
||||
$path = DOCROOT.$folder;
|
||||
|
||||
if ( ! file_exists($path))
|
||||
{
|
||||
throw new Kohana_Exception('Report directory :dir does not exist', array(':dir' => $path));
|
||||
}
|
||||
|
||||
if ( ! is_writable($path))
|
||||
{
|
||||
throw new Kohana_Exception('Script doesn\'t have permission to write to report dir :dir ', array(':dir' => $path));
|
||||
}
|
||||
|
||||
$runner->generate_report($group, $path, FALSE);
|
||||
|
||||
$this->request->redirect(URL::base(FALSE, TRUE).$folder.'index.html');
|
||||
$this->request->redirect(URL::site($folder.'index.html', $this->request));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +170,7 @@ Class Controller_UnitTest extends Controller_Template
|
||||
$this->template->body = View::factory('unittest/results');
|
||||
|
||||
// Get the test suite and work out which groups we're testing
|
||||
$suite = Kohana_Tests::suite();
|
||||
$suite = Unittest_tests::suite();
|
||||
$group = (array) Arr::get($_GET, 'group', array());
|
||||
|
||||
|
||||
@@ -220,19 +214,20 @@ Class Controller_UnitTest extends Controller_Template
|
||||
// Show some results
|
||||
$this->template->body
|
||||
->set('results', $runner->results)
|
||||
->set('totals', $runner->totals)
|
||||
->set('time', $this->nice_time($runner->time))
|
||||
->set('totals', $runner->totals)
|
||||
->set('time', $this->nice_time($runner->time))
|
||||
|
||||
// Sets group to the currently selected group, or default all groups
|
||||
->set('group', Arr::get($this->get_groups_list($suite), reset($group), 'All groups'))
|
||||
->set('group', Arr::get($this->get_groups_list($suite), reset($group), 'All groups'))
|
||||
->set('groups', $this->get_groups_list($suite))
|
||||
|
||||
->set('report_uri', $this->report_uri.url::query())
|
||||
->set('run_uri', $this->request->uri())
|
||||
->set('report_uri', $this->report_uri.url::query())
|
||||
|
||||
// Whitelist related stuff
|
||||
->set('whitelistable_items', $this->get_whitelistable_items())
|
||||
->set('whitelisted_items', isset($whitelist) ? array_keys($whitelist) : array())
|
||||
->set('whitelist', ! empty($whitelist));
|
||||
->set('whitelisted_items', isset($whitelist) ? array_keys($whitelist) : array())
|
||||
->set('whitelist', ! empty($whitelist));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,9 +257,7 @@ Class Controller_UnitTest extends Controller_Template
|
||||
static $whitelist;
|
||||
|
||||
if (count($whitelist))
|
||||
{
|
||||
return $whitelist;
|
||||
}
|
||||
|
||||
$whitelist = array();
|
||||
|
||||
@@ -308,7 +301,7 @@ Class Controller_UnitTest extends Controller_Template
|
||||
|
||||
if (count($whitelist))
|
||||
{
|
||||
Kohana_Tests::whitelist($whitelist);
|
||||
Unittest_tests::whitelist($whitelist);
|
||||
}
|
||||
|
||||
return $whitelist;
|
||||
|
@@ -0,0 +1,291 @@
|
||||
<?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
|
||||
*/
|
||||
// @codingStandardsIgnoreFile
|
||||
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 = TRUE;
|
||||
|
||||
/**
|
||||
* 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 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('database')
|
||||
->{Kohana::config('unittest')->db_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('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);
|
||||
}
|
||||
}
|
@@ -1,16 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Unit testing helpers
|
||||
*
|
||||
* @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_Helpers
|
||||
{
|
||||
class Kohana_Unittest_Helpers {
|
||||
/**
|
||||
* Static variable used to work out whether we have an internet
|
||||
* connection
|
||||
@@ -57,7 +50,7 @@ class Kohana_Unittest_Helpers
|
||||
{
|
||||
$cache_dir = opendir(Kohana::$cache_dir);
|
||||
|
||||
while($dir = readdir($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)
|
||||
@@ -66,7 +59,7 @@ class Kohana_Unittest_Helpers
|
||||
|
||||
$cache = opendir($dir);
|
||||
|
||||
while($file = readdir($cache))
|
||||
while ($file = readdir($cache))
|
||||
{
|
||||
if ($file[0] !== '.')
|
||||
{
|
||||
@@ -116,7 +109,7 @@ class Kohana_Unittest_Helpers
|
||||
// For some reason we need to do this in order to change the superglobals
|
||||
global $$option;
|
||||
|
||||
if($backup_needed)
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = $$option;
|
||||
}
|
||||
@@ -139,9 +132,9 @@ class Kohana_Unittest_Helpers
|
||||
$class->setStaticPropertyValue($var, $value);
|
||||
}
|
||||
// If this is an environment variable
|
||||
elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option]))
|
||||
elseif (preg_match('/^[A-Z_-]+$/D', $option) OR isset($_SERVER[$option]))
|
||||
{
|
||||
if($backup_needed)
|
||||
if ($backup_needed)
|
||||
{
|
||||
$this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : '';
|
||||
}
|
||||
|
@@ -1,16 +1,16 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHPUnit test runner for kohana
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @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_Runner implements PHPUnit_Framework_TestListener
|
||||
{
|
||||
class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener {
|
||||
/**
|
||||
* Results
|
||||
* @var array
|
||||
@@ -145,7 +145,7 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
$executed += $stat['locExecuted'];
|
||||
}
|
||||
|
||||
return $executable > 0 ? ($executed / $executable) * 100 : 100;
|
||||
return ($executable > 0) ? ($executed * 100 / $executable) : 100;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
@@ -160,9 +160,7 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
public function generate_report(array $groups, $temp_path, $create_sub_dir = TRUE)
|
||||
{
|
||||
if ( ! is_writable($temp_path))
|
||||
{
|
||||
throw new Kohana_Exception('Temp path :path does not exist or is not writable by the webserver', array(':path' => $temp_path));
|
||||
}
|
||||
|
||||
$folder_path = $temp_path;
|
||||
|
||||
@@ -174,11 +172,11 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
do
|
||||
{
|
||||
$folder_name = date('Y-m-d_H:i:s')
|
||||
.( ! empty($groups) ? '['.implode(',', $groups).']' : '')
|
||||
.($count > 0 ? '('.$count.')' : '');
|
||||
.(empty($groups) ? '' : ('['.implode(',', $groups).']'))
|
||||
.(($count > 0) ? ('('.$count.')') : '');
|
||||
++$count;
|
||||
}
|
||||
while(is_dir($folder_path.$folder_name));
|
||||
while (is_dir($folder_path.$folder_name));
|
||||
|
||||
$folder_path .= $folder_name;
|
||||
|
||||
@@ -209,11 +207,9 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
public function run(array $groups = array(), $collect_cc = FALSE)
|
||||
{
|
||||
if ($collect_cc AND ! extension_loaded('xdebug'))
|
||||
{
|
||||
throw new Kohana_Exception('Code coverage cannot be collected because the xdebug extension is not loaded');
|
||||
}
|
||||
|
||||
$this->result->collectCodeCoverageInformation((bool) $collect_cc);
|
||||
$this->result->collectCodeCoverageInformation( (bool) $collect_cc);
|
||||
|
||||
// Run the tests.
|
||||
$this->suite->run($this->result, FALSE, $groups);
|
||||
@@ -221,43 +217,54 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
return $this;
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
$this->totals['errors']++;
|
||||
$this->current['result'] = 'errors';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
$this->totals['failures']++;
|
||||
$this->current['result'] = 'failures';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
$this->totals['incomplete']++;
|
||||
$this->current['result'] = 'incomplete';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
$this->totals['skipped']++;
|
||||
$this->current['result'] = 'skipped';
|
||||
$this->current['message'] = $test->getStatusMessage();
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function startTest(PHPUnit_Framework_Test $test)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
$this->current['name'] = $test->getName(FALSE);
|
||||
$this->current['description'] = $test->toString();
|
||||
$this->current['result'] = 'passed';
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function endTest(PHPUnit_Framework_Test $test, $time)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
// Add totals
|
||||
$this->totals['tests']++;
|
||||
@@ -280,11 +287,13 @@ Class Kohana_Unittest_Runner implements PHPUnit_Framework_TestListener
|
||||
$this->time += $time;
|
||||
}
|
||||
|
||||
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||
{
|
||||
}
|
||||
// @codingStandardsIgnoreStart
|
||||
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
// Parse test descriptions to make them look nicer
|
||||
foreach ($this->results as $case => $testresults)
|
||||
|
@@ -1,16 +1,18 @@
|
||||
<?php
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* TestCase for unittesting
|
||||
*
|
||||
* @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
|
||||
* A version of the stock PHPUnit testcase that includes some extra helpers
|
||||
* and default settings
|
||||
*/
|
||||
Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
// @codingStandardsIgnoreFile
|
||||
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
|
||||
@@ -38,7 +40,17 @@ Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->_helpers = new Kohana_Unittest_Helpers;
|
||||
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);
|
||||
}
|
||||
@@ -59,7 +71,7 @@ Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function cleanCacheDir()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::clean_cache_dir();
|
||||
return Unittest_Helpers::clean_cache_dir();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +83,7 @@ Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function dirSeparator($path)
|
||||
{
|
||||
return Kohana_Unittest_Helpers::dir_separator($path);
|
||||
return Unittest_Helpers::dir_separator($path);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,6 +109,138 @@ Abstract Class Kohana_Unittest_TestCase extends PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function hasInternet()
|
||||
{
|
||||
return Kohana_Unittest_Helpers::has_internet();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@@ -1,18 +1,25 @@
|
||||
<?php
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
/**
|
||||
* PHPUnit testsuite for kohana application
|
||||
*
|
||||
* @package Kohana/Unittest
|
||||
* @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_Tests
|
||||
{
|
||||
class Kohana_Unittest_Tests {
|
||||
static protected $cache = array();
|
||||
|
||||
/**
|
||||
* Flag to identify whether the installed version of phpunit
|
||||
* is greater than or equal to 3.5
|
||||
* @var boolean
|
||||
*/
|
||||
static protected $phpunit_v35 = FALSE;
|
||||
|
||||
/**
|
||||
* Loads test files if they cannot be found by kohana
|
||||
* @param <type> $class
|
||||
@@ -38,21 +45,32 @@ class Kohana_Tests
|
||||
*/
|
||||
static public function configure_environment($do_whitelist = TRUE, $do_blacklist = TRUE)
|
||||
{
|
||||
if ( ! class_exists('PHPUnit_Util_Filter', FALSE))
|
||||
// During a webui request we need to manually load PHPUnit
|
||||
if ( ! class_exists('PHPUnit_Util_Filter', FALSE) AND ! function_exists('phpunit_autoload'))
|
||||
{
|
||||
// Make sure the PHPUnit classes are available
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
try
|
||||
{
|
||||
include_once 'PHPUnit/Autoload.php';
|
||||
}
|
||||
catch (ErrorException $e)
|
||||
{
|
||||
include_once 'PHPUnit/Framework.php';
|
||||
}
|
||||
}
|
||||
|
||||
// Allow PHPUnit to handle exceptions and errors
|
||||
if (Kohana::$is_cli)
|
||||
{
|
||||
restore_exception_handler();
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
spl_autoload_register(array('Kohana_Tests', 'autoload'));
|
||||
spl_autoload_register(array('Unittest_tests', 'autoload'));
|
||||
|
||||
Kohana_Tests::$cache = ($cache = Kohana::cache('unittest_whitelist_cache')) === NULL ? array() : $cache;
|
||||
// As of PHPUnit v3.5 there are slight differences in the way files are black|whitelisted
|
||||
self::$phpunit_v35 = function_exists('phpunit_autoload');
|
||||
|
||||
Unittest_tests::$cache = (($cache = Kohana::cache('unittest_whitelist_cache')) === NULL) ? array() : $cache;
|
||||
|
||||
$config = Kohana::config('unittest');
|
||||
|
||||
@@ -63,17 +81,7 @@ class Kohana_Tests
|
||||
|
||||
if ($do_blacklist AND count($config['blacklist']))
|
||||
{
|
||||
foreach ($config->blacklist as $item)
|
||||
{
|
||||
if (is_dir($item))
|
||||
{
|
||||
PHPUnit_Util_Filter::addDirectoryToFilter($item);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToFilter($item);
|
||||
}
|
||||
}
|
||||
Unittest_tests::blacklist($config->blacklist);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +116,7 @@ class Kohana_Tests
|
||||
|
||||
$files = Kohana::list_files('tests');
|
||||
|
||||
$suite = new PHPUnit_Framework_TestSuite();
|
||||
$suite = new PHPUnit_Framework_TestSuite;
|
||||
|
||||
self::addTests($suite, $files);
|
||||
|
||||
@@ -123,8 +131,15 @@ class Kohana_Tests
|
||||
* @param PHPUnit_Framework_TestSuite $suite The test suite to add to
|
||||
* @param array $files Array of files to test
|
||||
*/
|
||||
// @codingStandardsIgnoreStart
|
||||
static function addTests(PHPUnit_Framework_TestSuite $suite, array $files)
|
||||
// @codingStandardsIgnoreEnd
|
||||
{
|
||||
if (self::$phpunit_v35)
|
||||
{
|
||||
$filter = PHP_CodeCoverage_Filter::getInstance();
|
||||
}
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
@@ -146,7 +161,53 @@ class Kohana_Tests
|
||||
require_once($file);
|
||||
}
|
||||
|
||||
PHPUnit_Util_Filter::addFileToFilter($file);
|
||||
if (isset($filter))
|
||||
{
|
||||
$filter->addFileToBlacklist($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToFilter($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blacklist a set of files in PHPUnit code coverage
|
||||
*
|
||||
* @param array A set of files to blacklist
|
||||
*/
|
||||
static public function blacklist(array $blacklist_items)
|
||||
{
|
||||
if (self::$phpunit_v35)
|
||||
{
|
||||
$filter = PHP_CodeCoverage_Filter::getInstance();
|
||||
|
||||
foreach ($blacklist_items as $item)
|
||||
{
|
||||
if (is_dir($item))
|
||||
{
|
||||
$filter->addDirectoryToBlacklist($item);
|
||||
}
|
||||
else
|
||||
{
|
||||
$filter->addFileToBlacklist($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($blacklist_items as $item)
|
||||
{
|
||||
if (is_dir($item))
|
||||
{
|
||||
PHPUnit_Util_Filter::addDirectoryToFilter($item);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToFilter($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,7 +230,7 @@ class Kohana_Tests
|
||||
|
||||
if (count($directories))
|
||||
{
|
||||
foreach ($directories as &$directory)
|
||||
foreach ($directories as & $directory)
|
||||
{
|
||||
$directory = realpath($directory).'/';
|
||||
}
|
||||
@@ -234,6 +295,11 @@ class Kohana_Tests
|
||||
*/
|
||||
static protected function set_whitelist($files)
|
||||
{
|
||||
if (self::$phpunit_v35)
|
||||
{
|
||||
$filter = PHP_CodeCoverage_Filter::getInstance();
|
||||
}
|
||||
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (is_array($file))
|
||||
@@ -242,19 +308,26 @@ class Kohana_Tests
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ! isset(Kohana_Tests::$cache[$file]))
|
||||
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
|
||||
Kohana_Tests::$cache[$file] = ($cascading_file === $file);
|
||||
Unittest_tests::$cache[$file] = ($cascading_file === $file);
|
||||
}
|
||||
|
||||
if (Kohana_Tests::$cache[$file])
|
||||
if (Unittest_tests::$cache[$file])
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToWhitelist($file);
|
||||
if (isset($filter))
|
||||
{
|
||||
$filter->addFileToWhitelist($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
PHPUnit_Util_Filter::addFileToWhitelist($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -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.');
|
||||
|
||||
class Unittest_Runner extends Kohana_Unittest_Runner {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Unittest_TestCase extends Kohana_Unittest_TestCase {}
|
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class Unittest_Tests extends Kohana_Unittest_Tests {}
|
@@ -18,7 +18,7 @@ return array(
|
||||
|
||||
// If you don't use a whitelist then only files included during the request will be counted
|
||||
// If you do, then only whitelisted items will be counted
|
||||
'use_whitelist' => FALSE,
|
||||
'use_whitelist' => TRUE,
|
||||
|
||||
// Items to whitelist, only used in cli
|
||||
// Web runner ui allows user to choose which items to whitelist
|
||||
|
23
includes/kohana/modules/unittest/config/userguide.php
Normal file
23
includes/kohana/modules/unittest/config/userguide.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
return array(
|
||||
// Leave this alone
|
||||
'modules' => array(
|
||||
|
||||
// This should be the path to this modules userguide pages, without the 'guide/'. Ex: '/guide/modulename/' would be 'modulename'
|
||||
'unittest' => array(
|
||||
|
||||
// Whether this modules userguide pages should be shown
|
||||
'enabled' => TRUE,
|
||||
|
||||
// The name that should show up on the userguide index page
|
||||
'name' => 'Unittest',
|
||||
|
||||
// A short description of this module, shown on the index page
|
||||
'description' => 'Kohana unit testing.',
|
||||
|
||||
// Copyright message, shown in the footer for this module
|
||||
'copyright' => '© 2008–2010 Kohana Team',
|
||||
)
|
||||
)
|
||||
);
|
@@ -1,5 +0,0 @@
|
||||
1. **UnitTest**
|
||||
- [Testing](unittest.testing)
|
||||
- [Mock Objects](unittest.mockobjects)
|
||||
- [Troubleshooting](unittest.troubleshooting)
|
||||
- [Testing workflows](unittest.testing_workflows)
|
2
includes/kohana/modules/unittest/guide/unittest/index.md
Normal file
2
includes/kohana/modules/unittest/guide/unittest/index.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# Unittest
|
||||
|
5
includes/kohana/modules/unittest/guide/unittest/menu.md
Normal file
5
includes/kohana/modules/unittest/guide/unittest/menu.md
Normal file
@@ -0,0 +1,5 @@
|
||||
## [UnitTest]()
|
||||
- [Testing](testing)
|
||||
- [Mock Objects](mockobjects)
|
||||
- [Troubleshooting](troubleshooting)
|
||||
- [Testing workflows](testing_workflows)
|
@@ -12,7 +12,7 @@ Just navigate to http://example.com/unittest. You may need to use http://example
|
||||
|
||||
If you're writing a test for your application, place it in "application/tests". Similarly, if you're writing a test for a module place it in modules/[modulefolder]/tests
|
||||
|
||||
Rather than tell you how to write tests I'll point you in the direction of the [PHPUnit Manual](http://www.phpunit.de/manual/3.4/en/index.html). One thing you should bear in mind when writing tests is that testcases should extend Kohana_Unittest_Testcase rathr than PHPUnit_Framework_TestCase.
|
||||
Rather than tell you how to write tests I'll point you in the direction of the [PHPUnit Manual](http://www.phpunit.de/manual/3.4/en/index.html). One thing you should bear in mind when writing tests is that testcases should extend Unittest_Testcase rather than PHPUnit_Framework_TestCase, doing so gives you access to useful kohana specific helpers such as `setEnvironment()`.
|
||||
|
||||
Here's a taster of some of the cool things you can do with phpunit:
|
||||
|
||||
@@ -24,7 +24,7 @@ Ordinarily you could use a foreach loop to iterate over an array of test data, h
|
||||
|
||||
<?php
|
||||
|
||||
Class ReallyCoolTest extends Kohana_Unittest_TestCase
|
||||
Class ReallyCoolTest extends Unittest_TestCase
|
||||
{
|
||||
function providerStrLen()
|
||||
{
|
||||
@@ -67,7 +67,7 @@ To allow users to selectively run tests you need to organise your tests into gro
|
||||
* @group somegroup
|
||||
* @group somegroup.morespecific
|
||||
*/
|
||||
Class AnotherReallyCoolTest extends Kohana_Unittest_TestCase
|
||||
Class AnotherReallyCoolTest extends Unittest_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests can also be grouped too!
|
@@ -4,7 +4,7 @@ Having unittests for your application is a nice idea, but unless you actually us
|
||||
|
||||
## Testing through the webui
|
||||
|
||||
The web ui is a fairly temporary solution, aimed at helping developers get into unittesting and code coverage. Eventually it's hoped that people migrate on to CI servers, but it's fine for calculating code coverage locally.
|
||||
The web ui is a fairly temporary solution, aimed at helping developers get into unittesting and code coverage. Eventually it's hoped that people migrate on to the termainl & CI servers.
|
||||
|
||||
To access it goto
|
||||
|
||||
@@ -34,15 +34,15 @@ You can also specify a custom test suite loader (enter the path to your tests.ph
|
||||
|
||||
## Looping shell
|
||||
|
||||
I personally prefer to do all of my development in an advanced text editor such as vim/gedit/np++.
|
||||
If you're developing in a text editor such as textmate, vim, gedit etc. chances are phpunit support isn't natively supported by your editor.
|
||||
|
||||
To test while I work I run tests in an infinte looping. It's very easy to setup and only takes a few commands to setup.
|
||||
On nix you can run the following commands in the terminal:
|
||||
In such situations you can run a simple bash script to loop over the tests every X seconds, here's an example script:
|
||||
|
||||
while(true) do clear; phpunit; sleep 8; done;
|
||||
|
||||
In my experience this gives you just enough time to see what's going wrong before the tests are rerun.
|
||||
It's also quite handy to store common phpunit settings (like path to the bootstrap) in a a phpunit xml file to reduce the amount that has to be written in order to start a loop.
|
||||
You will probably need to adjust the timeout (`sleep 8`) to suit your own workflow, but 8 seconds seems to be about enough time to see what's erroring before the tests are re-run.
|
||||
|
||||
In the above example we're using a phpunit.xml config file to specify all the unit testing settings & to reduce the complexity of the looping script.
|
||||
|
||||
## Continuous Integration (CI)
|
||||
|
@@ -1,20 +1,13 @@
|
||||
<?php
|
||||
|
||||
// If we're on the CLI then PHPUnit will already be loaded
|
||||
if (class_exists('PHPUnit_Util_Filter', FALSE))
|
||||
if (class_exists('PHPUnit_Util_Filter', FALSE) OR function_exists('phpunit_autoload'))
|
||||
{
|
||||
Kohana_Tests::configure_environment();
|
||||
Unittest_Tests::configure_environment();
|
||||
|
||||
// Stop kohana from processing the request
|
||||
define('SUPPRESS_REQUEST', TRUE);
|
||||
}
|
||||
elseif (Kohana_Tests::enabled())
|
||||
{
|
||||
// People shouldn't be running unit tests on their production server
|
||||
// so we assume that this _could_ be a web ui request on the dev server
|
||||
// and include phpunit so that modules realise that this could be a testing request
|
||||
require_once 'PHPUnit/Framework.php';
|
||||
}
|
||||
|
||||
Route::set('unittest', 'unittest(/<action>)')
|
||||
->defaults(array(
|
||||
|
@@ -1,17 +1,17 @@
|
||||
<?php
|
||||
|
||||
if( ! class_exists('Kohana'))
|
||||
if ( ! class_exists('Kohana'))
|
||||
{
|
||||
die('Please include the kohana bootstrap file (see README.markdown)');
|
||||
}
|
||||
|
||||
if($file = Kohana::find_file('classes', 'kohana/tests'))
|
||||
if ($file = Kohana::find_file('classes', 'unittest/tests'))
|
||||
{
|
||||
require_once $file;
|
||||
|
||||
// PHPUnit requires a test suite class to be in this file,
|
||||
// so we create a faux one that uses the kohana base
|
||||
Class TestSuite extends Kohana_Tests
|
||||
Class TestSuite extends Unittest_Tests
|
||||
{}
|
||||
}
|
||||
else
|
||||
|
@@ -2,7 +2,7 @@
|
||||
<div id="header" class="results">
|
||||
<fieldset id="results-options">
|
||||
<legend>Options</legend>
|
||||
<?php echo Form::open(NULL, array('method' => 'get'));?>
|
||||
<?php echo Form::open($run_uri, array('method' => 'get'));?>
|
||||
<?php echo Form::label('group', __('Switch Group')) ?>
|
||||
<?php echo Form::select('group', $groups, $group, array('id' => 'group'));?>
|
||||
<?php if ($xdebug_enabled): ?>
|
||||
@@ -21,7 +21,7 @@
|
||||
<?php echo Form::submit('run', 'Run');?>
|
||||
<?php echo Form::close();?>
|
||||
</fieldset>
|
||||
<h1><?php echo (is_null($group) ? __('All Groups') : __('Group').': ')?> <?php echo $group?></h1>
|
||||
<h1><?php echo is_null($group) ? __('All Groups') : (__('Group').': ') ?> <?php echo $group ?></h1>
|
||||
<span class="time"><?php echo __('Time') ?>: <b><?php echo $time?></b></span>
|
||||
<span class="summary">
|
||||
<?php echo __('Tests') ?> <b><?php echo $totals['tests']?></b>,
|
||||
@@ -32,13 +32,13 @@
|
||||
</span>
|
||||
<?php if ($xdebug_enabled AND isset($coverage)): ?>
|
||||
<span class="code_coverage">
|
||||
<?php $level_class = ($coverage > 75 ? 'excellent' : ($coverage > 35 ? 'ok' : 'terrible')) ?>
|
||||
<?php $level_class = ($coverage > 75) ? 'excellent' : (($coverage > 35) ? 'ok' : 'terrible') ?>
|
||||
<?php
|
||||
echo __('Tests covered :percent of the :codebase',
|
||||
array
|
||||
(
|
||||
':percent' => '<b class="'.$level_class.'">'.num::format($coverage, 2).'%</b>',
|
||||
':codebase' => ( ! empty($coverage_explanation) ? '<span title="'.$coverage_explanation.'" style="display:inline;">modules</span>' : 'codebase')
|
||||
':codebase' => empty($coverage_explanation) ? 'codebase' : ('<span title="'.$coverage_explanation.'" style="display:inline;">modules</span>'),
|
||||
)
|
||||
);
|
||||
?>,
|
||||
|
Reference in New Issue
Block a user