2011-01-13 14:49:56 +00:00
|
|
|
<?php defined('SYSPATH') or die ('No direct script access.');
|
|
|
|
/**
|
|
|
|
* PHPUnit Kohana web based test runner
|
|
|
|
*
|
2011-05-16 12:47:16 +00:00
|
|
|
* @package Kohana/UnitTest
|
2011-01-13 14:49:56 +00:00
|
|
|
* @author Kohana Team
|
|
|
|
* @author BRMatt <matthew@sigswitch.com>
|
2011-05-16 12:47:16 +00:00
|
|
|
* @author Paul Banks
|
2011-01-13 14:49:56 +00:00
|
|
|
* @copyright (c) 2008-2009 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license
|
|
|
|
*/
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
class Controller_UnitTest extends Controller_Template
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Whether the archive module is available
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $cc_archive_available = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unittest config
|
2011-05-16 12:47:16 +00:00
|
|
|
* @var Config
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
protected $config = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The uri by which the report uri will be executed
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $report_uri = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The uri by which the run action will be executed
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $run_uri = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the XDEBUG extension loaded?
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $xdebug_loaded = FALSE;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $template = 'unittest/layout';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads test suite
|
|
|
|
*/
|
|
|
|
public function before()
|
|
|
|
{
|
|
|
|
parent::before();
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
if ( ! Unittest_tests::enabled())
|
2011-01-13 14:49:56 +00:00
|
|
|
{
|
|
|
|
// 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',
|
2011-05-16 12:47:16 +00:00
|
|
|
array(':uri' => $this->request->uri()));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent the whitelist from being autoloaded, but allow the blacklist
|
|
|
|
// to be loaded
|
2011-05-16 12:47:16 +00:00
|
|
|
Unittest_Tests::configure_environment(FALSE);
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
$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'));
|
2011-05-16 12:47:16 +00:00
|
|
|
$this->run_uri = $route->uri(array('action' => 'run'));
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
// Switch used to disable cc settings
|
2011-05-16 12:47:16 +00:00
|
|
|
$this->xdebug_loaded = extension_loaded('xdebug');
|
2011-01-13 14:49:56 +00:00
|
|
|
$this->cc_archive_enabled = class_exists('Archive');
|
|
|
|
|
|
|
|
Kohana_View::set_global('xdebug_enabled', $this->xdebug_loaded);
|
|
|
|
Kohana_View::set_global('cc_archive_enabled', $this->cc_archive_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles index page for /unittest/ and /unittest/index/
|
|
|
|
*/
|
|
|
|
public function action_index()
|
|
|
|
{
|
|
|
|
$this->template->body = View::factory('unittest/index')
|
|
|
|
->set('run_uri', $this->run_uri)
|
|
|
|
->set('report_uri', $this->report_uri)
|
|
|
|
->set('whitelistable_items', $this->get_whitelistable_items())
|
2011-05-16 12:47:16 +00:00
|
|
|
->set('groups', $this->get_groups_list(Unittest_tests::suite()));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles report generation
|
|
|
|
*/
|
|
|
|
public function action_report()
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$suite = Unittest_tests::suite();
|
2011-01-13 14:49:56 +00:00
|
|
|
$temp_path = rtrim($this->config->temp_path, '/').'/';
|
2011-05-16 12:47:16 +00:00
|
|
|
$group = (array) Arr::get($_GET, 'group', array());
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
// Stop unittest from interpretting "all groups" as "no groups"
|
|
|
|
if (empty($group) OR empty($group[0]))
|
|
|
|
{
|
|
|
|
$group = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Arr::get($_GET, 'use_whitelist', FALSE))
|
|
|
|
{
|
|
|
|
$this->whitelist(Arr::get($_GET, 'whitelist', array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$runner = new Kohana_Unittest_Runner($suite);
|
|
|
|
|
|
|
|
// If the user wants to download a report
|
|
|
|
if ($this->cc_archive_enabled AND Arr::get($_GET, 'archive') === '1')
|
|
|
|
{
|
|
|
|
// $report is the actual directory of the report,
|
|
|
|
// $folder is the name component of directory
|
|
|
|
list($report, $folder) = $runner->generate_report($group, $temp_path);
|
|
|
|
|
|
|
|
$archive = Archive::factory('zip');
|
|
|
|
|
|
|
|
// TODO: Include the test results?
|
|
|
|
$archive->add($report, 'report', TRUE);
|
|
|
|
|
|
|
|
$filename = $folder.'.zip';
|
|
|
|
|
|
|
|
$archive->save($temp_path.$filename);
|
|
|
|
|
|
|
|
// It'd be nice to clear up afterwards but by deleting the report dir we corrupt the archive
|
|
|
|
// And once the archive has been sent to the user Request stops the script so we can't delete anything
|
|
|
|
// It'll be up to the user to delete files periodically
|
|
|
|
$this->request->send_file($temp_path.$filename, $filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$folder = trim($this->config->cc_report_path, '/').'/';
|
2011-05-16 12:47:16 +00:00
|
|
|
$path = DOCROOT.$folder;
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
$this->request->redirect(URL::site($folder.'index.html', $this->request));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles test running interface
|
|
|
|
*/
|
|
|
|
public function action_run()
|
|
|
|
{
|
|
|
|
$this->template->body = View::factory('unittest/results');
|
|
|
|
|
|
|
|
// Get the test suite and work out which groups we're testing
|
2011-05-16 12:47:16 +00:00
|
|
|
$suite = Unittest_tests::suite();
|
2011-01-13 14:49:56 +00:00
|
|
|
$group = (array) Arr::get($_GET, 'group', array());
|
|
|
|
|
|
|
|
|
|
|
|
// Stop phpunit from interpretting "all groups" as "no groups"
|
|
|
|
if (empty($group) OR empty($group[0]))
|
|
|
|
{
|
|
|
|
$group = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only collect code coverage if the user asked for it
|
|
|
|
$collect_cc = (bool) Arr::get($_GET, 'collect_cc', FALSE);
|
|
|
|
|
|
|
|
if ($collect_cc AND Arr::get($_GET, 'use_whitelist', FALSE))
|
|
|
|
{
|
|
|
|
$whitelist = $this->whitelist(Arr::get($_GET, 'whitelist', array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$runner = new Kohana_Unittest_Runner($suite);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$runner->run($group, $collect_cc);
|
|
|
|
|
|
|
|
if ($collect_cc)
|
|
|
|
{
|
|
|
|
$this->template->body->set('coverage', $runner->calculate_cc_percentage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($whitelist))
|
|
|
|
{
|
|
|
|
$this->template->body->set('coverage_explanation', $this->nice_whitelist_explanation($whitelist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Kohana_Exception $e)
|
|
|
|
{
|
|
|
|
// Code coverage is not allowed, possibly xdebug disabled?
|
|
|
|
// TODO: Tell the user this?
|
|
|
|
$runner->run($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show some results
|
|
|
|
$this->template->body
|
|
|
|
->set('results', $runner->results)
|
2011-05-16 12:47:16 +00:00
|
|
|
->set('totals', $runner->totals)
|
|
|
|
->set('time', $this->nice_time($runner->time))
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
// Sets group to the currently selected group, or default all groups
|
2011-05-16 12:47:16 +00:00
|
|
|
->set('group', Arr::get($this->get_groups_list($suite), reset($group), 'All groups'))
|
2011-01-13 14:49:56 +00:00
|
|
|
->set('groups', $this->get_groups_list($suite))
|
|
|
|
|
2011-05-16 12:47:16 +00:00
|
|
|
->set('run_uri', $this->request->uri())
|
|
|
|
->set('report_uri', $this->report_uri.url::query())
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
// Whitelist related stuff
|
|
|
|
->set('whitelistable_items', $this->get_whitelistable_items())
|
2011-05-16 12:47:16 +00:00
|
|
|
->set('whitelisted_items', isset($whitelist) ? array_keys($whitelist) : array())
|
|
|
|
->set('whitelist', ! empty($whitelist));
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of groups from the test suite, sorted with 'All groups' prefixed
|
|
|
|
*
|
|
|
|
* @return array Array of groups in the test suite
|
|
|
|
*/
|
|
|
|
protected function get_groups_list($suite)
|
|
|
|
{
|
|
|
|
// Make groups aray suitable for drop down
|
|
|
|
$groups = $suite->getGroups();
|
|
|
|
if (count($groups) > 0)
|
|
|
|
{
|
|
|
|
sort($groups);
|
|
|
|
$groups = array_combine($groups, $groups);
|
|
|
|
}
|
|
|
|
return array('' => 'All Groups') + $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of items that are whitelistable
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_whitelistable_items()
|
|
|
|
{
|
|
|
|
static $whitelist;
|
|
|
|
|
|
|
|
if (count($whitelist))
|
|
|
|
return $whitelist;
|
|
|
|
|
|
|
|
$whitelist = array();
|
|
|
|
|
|
|
|
$whitelist['k_app'] = 'Application';
|
|
|
|
|
|
|
|
$k_modules = array_keys(Kohana::modules());
|
|
|
|
|
|
|
|
$whitelist += array_map('ucfirst', array_combine($k_modules, $k_modules));
|
|
|
|
|
|
|
|
$whitelist['k_sys'] = 'Kohana Core';
|
|
|
|
|
|
|
|
return $whitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whitelists a specified set of modules specified by the user
|
|
|
|
*
|
|
|
|
* @param array $modules
|
|
|
|
*/
|
|
|
|
protected function whitelist(array $modules)
|
|
|
|
{
|
|
|
|
$k_modules = Kohana::modules();
|
|
|
|
$whitelist = array();
|
|
|
|
|
|
|
|
// Make sure our whitelist is valid
|
|
|
|
foreach ($modules as $item)
|
|
|
|
{
|
|
|
|
if (isset($k_modules[$item]))
|
|
|
|
{
|
|
|
|
$whitelist[$item] = $k_modules[$item];
|
|
|
|
}
|
|
|
|
elseif ($item === 'k_app')
|
|
|
|
{
|
|
|
|
$whitelist[$item] = APPPATH;
|
|
|
|
}
|
|
|
|
elseif ($item === 'k_sys')
|
|
|
|
{
|
|
|
|
$whitelist[$item] = SYSPATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($whitelist))
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
Unittest_tests::whitelist($whitelist);
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $whitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prettifies the list of whitelisted modules
|
|
|
|
*
|
|
|
|
* @param array Array of whitelisted items
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function nice_whitelist_explanation(array $whitelist)
|
|
|
|
{
|
|
|
|
$items = array_intersect_key($this->get_whitelistable_items(), $whitelist);
|
|
|
|
|
|
|
|
return implode(', ', $items);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function nice_time($time)
|
|
|
|
{
|
|
|
|
$parts = array();
|
|
|
|
|
|
|
|
if ($time > DATE::DAY)
|
|
|
|
{
|
|
|
|
$parts[] = floor($time/DATE::DAY).'d';
|
|
|
|
$time = $time % DATE::DAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($time > DATE::HOUR)
|
|
|
|
{
|
|
|
|
$parts[] = floor($time/DATE::HOUR).'h';
|
|
|
|
$time = $time % DATE::HOUR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($time > DATE::MINUTE)
|
|
|
|
{
|
|
|
|
$parts[] = floor($time/DATE::MINUTE).'m';
|
|
|
|
$time = $time % DATE::MINUTE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($time > 0)
|
|
|
|
{
|
|
|
|
$parts[] = round($time, 1).'s';
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $parts);
|
|
|
|
}
|
|
|
|
} // End Controller_PHPUnit
|