Skip to content

Commit

Permalink
Merge branch '2.0-phpunit' into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 14, 2010
2 parents c9079c8 + 2a3e64b commit 4759b7a
Show file tree
Hide file tree
Showing 23 changed files with 844 additions and 206 deletions.
8 changes: 7 additions & 1 deletion cake/console/cake.php
Expand Up @@ -155,9 +155,15 @@ function __initConstants() {
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
define('DS', DIRECTORY_SEPARATOR);
define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
define('DISABLE_DEFAULT_ERROR_HANDLING', false);
define('CAKEPHP_SHELL', true);
if (!defined('CORE_PATH')) {
if (function_exists('ini_set') && ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ini_get('include_path'))) {
define('CORE_PATH', null);
} else {
define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
}
}
}
require_once(CORE_PATH . 'cake' . DS . 'basics.php');
}
Expand Down
223 changes: 171 additions & 52 deletions cake/console/libs/testsuite.php
Expand Up @@ -42,28 +42,25 @@ public function initialize() {
} else {
define('TEST_CAKE_CORE_INCLUDE_PATH', CAKE_CORE_INCLUDE_PATH);
}
$params = $this->parseArgs();

$this->_dispatcher = new CakeTestSuiteDispatcher();
$this->_dispatcher->setParams($params);
$this->_dispatcher->loadTestFramework();
require_once CAKE . 'tests' . DS . 'lib' . DS . 'test_manager.php';
}

/**
* Parse the CLI options into an array CakeTestDispatcher can use.
*
* @return array Array of params for CakeTestDispatcher
*/
public function parseArgs() {
protected function parseArgs() {
if (empty($this->args)) {
return;
}
$params = array(
'app' => false,
'plugin' => null,
'output' => 'text',
'codeCoverage' => false,
'filter' => false,
'case' => null
);

$category = $this->args[0];
Expand All @@ -75,20 +72,32 @@ public function parseArgs() {
}

if (isset($this->args[1])) {
$params['case'] = Inflector::underscore($this->args[1]) . '.test.php';
}
if (isset($this->args[2]) && $this->args[2] == 'cov') {
$params['codeCoverage'] = true;
$params['case'] = Inflector::underscore($this->args[1]);
}
if (isset($this->params['filter'])) {
$params['filter'] = $this->params['filter'];
}
if (isset($this->params['coverage'])) {
$params['codeCoverage'] = true;
$this->params['-filter'] = $this->params['filter'];
}
return $params;
}

/**
* Converts the options passed to the shell as options for the PHPUnit cli runner
*
* @return array Array of params for CakeTestDispatcher
*/
protected function runnerOptions() {
$options = array();
foreach ($this->params as $param => $value) {
if ($param[0] === '-') {
$options[] = '-' . $param;
if (is_string($value)) {
$options[] = $value;
}
}
}
return $options;
}

/**
* Main entry point to this shell
*
Expand All @@ -98,16 +107,86 @@ public function main() {
$this->out(__('CakePHP Test Shell'));
$this->hr();

if (count($this->args) == 0) {
$this->error(__('Sorry, you did not pass any arguments!'));
$args = $this->parseArgs();

if (empty($args['case'])) {
$this->available();
}

$this->run($args, $this->runnerOptions());
}

/**
* Runs the test case from $runnerArgs
*
* @param array $runnerArgs list of arguments as obtained from parseArgs()
* @param array $options list of options as constructed by runnerOptions()
* @return void
*/
protected function run($runnerArgs, $options = array()) {
require_once CAKE . 'tests' . DS . 'lib' . DS . 'test_runner.php';
$testCli = new TestRunner($runnerArgs);
$testCli->run($options);
}

/**
* Shows a list of available test cases and gives the option to run one of them
*
* @return void
*/
public function available() {
$params = $this->parseArgs();
$testCases = TestManager::getTestCaseList($params);
$app = $params['app'];
$plugin = $params['plugin'];

$title = "Core Test Cases:";
$category = 'core';
if ($app) {
$title = "App Test Cases:";
$category = 'app';
} elseif ($plugin) {
$title = Inflector::humanize($plugin) . " Test Cases:";
$category = $plugin;
}

$result = $this->_dispatcher->dispatch();
$exit = 0;
if ($result instanceof PHPUnit_Framework_TestResult) {
$exit = ($result->errorCount() + $result->failureCount()) > 0;
if (empty($testCases)) {
$this->out(__("No test cases available \n\n"));
$this->help();
$this->_stop();
}

$this->out($title);
$i = 1;
$cases = array();
foreach ($testCases as $testCaseFile => $testCase) {
$case = explode(DS, str_replace('.test.php', '', $testCase));
$case[count($case) - 1] = Inflector::camelize($case[count($case) - 1]);
$case = implode('/', $case);
$this->out("[$i] $case");
$cases[$i] = $case;
$i++;
}

while ($choice = $this->in(__('What test case would you like to run?'), null, 'q')) {
if (is_numeric($choice) && isset($cases[$choice])) {
$this->args[0] = $category;
$this->args[1] = $cases[$choice];
$this->run($this->parseArgs(), $this->runnerOptions());
break;
}

if (is_string($choice) && in_array($choice, $cases)) {
$this->args[0] = $category;
$this->args[1] = $choice;
$this->run($this->parseArgs(), $this->runnerOptions());
break;
}

if ($choice == 'q') {
break;
}
}
$this->_stop($exit);
}

/**
Expand All @@ -116,36 +195,76 @@ public function main() {
* @return void
*/
public function help() {
$help = <<<TEXT
Usage:
-----
cake testsuite <category> <file> [params]
- category - "app", "core" or name of a plugin
- file - file name with folder prefix and without the test.php suffix.
Params:
-------
-filter
The -filter option allows you supply a pattern that is used to match
test method names. This can be a regular expression.
-coverage
Enable code coverage for this run.
Examples:
---------
cake testsuite app behaviors/debuggable;
cake testsuite app models/my_model;
cake testsuite app controllers/my_controller
cake testsuite core libs/file
cake testsuite core libs/router
cake testsuite core libs/set
cake testsuite bugs models/bug
// for the plugin 'bugs' and its test case 'models/bug'
TEXT;
$this->out($help);
$this->out('CakePHP Testsuite:');
$this->hr();

$this->out('The CakPHP Testsuite allows you to run test cases from the command line');
$this->out('If run with no command line arguments, a list of available core test cases will be shown');
$this->hr();

$this->out("Usage: cake testuite <category> <file> [params]");
$this->out("\t- category: app, core or name of a plugin");
$this->out("\t- file: file name with folder prefix and without the test.php suffix");
$this->hr();

$this->out("Usage: cake testuite available <category> [params]");
$this->out("\t Shows a list of available testcases for the specified category");
$this->out("\t Params list will be used for running the selected test case");
$this->hr();

$this->out('Examples:');
$this->out('cake testsuite app models/my_model');
$this->out("cake testsuite app controllers/my_controller \n");
$this->out('cake testsuite core libs/file');
$this->out("cake testsuite core libs/set \n");
$this->out('cake testsuite bugs models/bug -- for the plugin bugs and its test case models/bug');
$this->hr();

$this->out('Params:');
$this->out("--log-junit <file> Log test execution in JUnit XML format to file.");
$this->out("--log-json <file> Log test execution in JSON format.");

$this->out("--coverage-html <dir> Generate code coverage report in HTML format.");
$this->out("--coverage-clover <file> Write code coverage data in Clover XML format.");
$this->out("--coverage-source <dir> Write code coverage / source data in XML format.");

$this->out("--story-html <file> Write Story/BDD results in HTML format to file.");
$this->out("--story-text <file> Write Story/BDD results in Text format to file.");

$this->out("--testdox-html <file> Write agile documentation in HTML format to file.");
$this->out("--testdox-text <file> Write agile documentation in Text format to file.");

$this->out("--filter <pattern> Filter which tests to run.");
$this->out("--group ... Only runs tests from the specified group(s).");
$this->out("--exclude-group ... Exclude tests from the specified group(s).");
$this->out("--filter <pattern> Filter which tests to run.");
$this->out("--loader <loader> TestSuiteLoader implementation to use.");
$this->out("--repeat <times> Runs the test(s) repeatedly.");

$this->out("--story Report test execution progress in Story/BDD format.");
$this->out("--tap Report test execution progress in TAP format.");
$this->out("--testdox Report test execution progress in TestDox format.");

$this->out("--colors Use colors in output.");
$this->out("--stderr Write to STDERR instead of STDOUT.");
$this->out("--stop-on-failure Stop execution upon first error or failure.");
$this->out("--verbose Output more verbose information.");
$this->out("--wait Waits for a keystroke after each test.");

$this->out("--skeleton-class Generate Unit class for UnitTest in UnitTest.php.");
$this->out("--skeleton-test Generate UnitTest class for Unit in Unit.php.");

$this->out("--process-isolation Run each test in a separate PHP process.");
$this->out("--no-globals-backup Do not backup and restore \$GLOBALS for each test.");
$this->out("--static-backup Backup and restore static attributes for each test.");
$this->out("--syntax-check Try to check source files for syntax errors.");

$this->out("--bootstrap <file> A \"bootstrap\" PHP file that is run before the tests.");
$this->out("--configuration <file> Read configuration from XML file.");
$this->out("--no-configuration Ignore default configuration file (phpunit.xml).");
$this->out("--include-path <path(s)> Prepend PHP's include_path with given path(s).");

$this->out("-d key[=value] Sets a php.ini value. \n");
}

}
23 changes: 17 additions & 6 deletions cake/dispatcher.php
Expand Up @@ -556,7 +556,7 @@ public function asset($url) {
);

if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
header('HTTP/1.1 404 Not Found');
$this->header('HTTP/1.1 404 Not Found');
return $this->_stop();
} elseif ($isCss) {
include WWW_ROOT . DS . $filters['css'];
Expand Down Expand Up @@ -622,11 +622,11 @@ protected function _deliverAsset($assetFile, $ext) {
}
}

header("Date: " . date("D, j M Y G:i:s ", filemtime($assetFile)) . 'GMT');
header('Content-type: ' . $contentType);
header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
header("Cache-Control: cache");
header("Pragma: cache");
$this->header("Date: " . date("D, j M Y G:i:s ", filemtime($assetFile)) . 'GMT');
$this->header('Content-type: ' . $contentType);
$this->header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
$this->header("Cache-Control: cache");
$this->header("Pragma: cache");

if ($ext === 'css' || $ext === 'js') {
include($assetFile);
Expand All @@ -639,4 +639,15 @@ protected function _deliverAsset($assetFile, $ext) {
ob_end_flush();
}
}

/**
* Sends the specified headers to the client
*
* @param string $header header to send
* @todo Refactor this to use a response object or similar
* @return void
*/
public function header($header) {
header($header);
}
}
4 changes: 4 additions & 0 deletions cake/libs/view/helper.php
Expand Up @@ -20,6 +20,10 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

if (!class_exists('Router')) {
App::import('Core', 'Router');
}

/**
* Abstract base class for all other Helpers in CakePHP.
* Provides common methods and features.
Expand Down
1 change: 1 addition & 0 deletions cake/tests/cases/console/libs/bake.test.php
Expand Up @@ -19,6 +19,7 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::import('Shell', 'Shell', false);
App::import('Core', 'Controller');

if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/console/libs/schema.test.php
Expand Up @@ -283,7 +283,7 @@ public function testGenerateSnapshot() {
$this->Shell->Schema->expects($this->at(0))->method('write')->will($this->returnValue(true));

$this->Shell->Schema->expects($this->at(1))->method('read');
$this->Shell->Schema->expects($this->at(1))->method('write')->with(array('schema data', 'file' => 'schema_1.php'));
$this->Shell->Schema->expects($this->at(1))->method('write')->with(array('schema data', 'file' => 'schema_0.php'));

$this->Shell->generate();
}
Expand Down Expand Up @@ -406,7 +406,7 @@ public function testCreateWithTableArgs() {
$this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
$this->Shell->create();

$db =& ConnectionManager::getDataSource('test_suite');
$db =& ConnectionManager::getDataSource('test');
$sources = $db->listSources();
$this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
$this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources));
Expand Down

0 comments on commit 4759b7a

Please sign in to comment.