Adding tests for ShellDispatcher, Shell, ExtractTask and TestTask.

Adding console test group.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8096 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
davidpersson 2009-03-13 16:06:18 +00:00
parent 36d3540c19
commit b184d0705d
5 changed files with 485 additions and 108 deletions

View file

@ -445,6 +445,8 @@ class ShellDispatcherTest extends UnitTestCase {
$expected .= "\n/";
$this->assertPattern($expected, $Dispatcher->stdout);
// Shells need to be returned ordered
// See Configure::__list/Folder::read
$expected = "/ ROOT(\\\|\/)cake(\\\|\/)tests(\\\|\/)test_app(\\\|\/)plugins(\\\|\/)test_plugin_two(\\\|\/)vendors(\\\|\/)shells:";
$expected .= "\n\t example";
$expected .= "\n\t welcome";
@ -461,6 +463,8 @@ class ShellDispatcherTest extends UnitTestCase {
$expected .= "\n/";
$this->assertPattern($expected, $Dispatcher->stdout);
// Shells need to be returned ordered
// See Configure::__list/Folder::read
$expected = "/ ROOT(\\\|\/)cake(\\\|\/)console(\\\|\/)libs:";
$expected .= "\n\t acl";
$expected .= "\n\t api";

View file

@ -3,7 +3,7 @@
/**
* Test Case for Shell
*
*
* Long description for file
*
* PHP versions 4 and 5
*
@ -16,15 +16,15 @@
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs.
* @package cake.tests
* @subpackage cake.tests.cases.console.libs
* @since CakePHP v 1.2.0.7726
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', 'Shell');
App::import('Core', array('Shell', 'Folder'));
if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
@ -37,37 +37,80 @@ if (!class_exists('ShellDispatcher')) {
ob_end_clean();
}
Mock::generatePartial(
'ShellDispatcher', 'TestShellMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
/**
* Test Shell class
* TestShell class
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
* @subpackage cake.tests.cases.console.libs
*/
class TestShell extends Shell {
}
Mock::generate('ShellDispatcher');
/**
* Test case class for shell
* TestAppleTask class
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
* @subpackage cake.tests.cases.console.libs
*/
class TestAppleTask extends Shell {
}
/**
* TestBananaTask class
*
* @package cake.tests
* @subpackage cake.tests.cases.console.libs
*/
class TestBananaTask extends Shell {
}
/**
* ShellTest class
*
* @package cake.tests
* @subpackage cake.tests.cases.console.libs
*/
class ShellTest extends CakeTestCase {
/**
* Fixtures used in this test case
*
* @var array
* @access public
*/
class CakeShellTestCase extends CakeTestCase {
var $fixtures = array('core.post', 'core.comment');
/**
* setup
* setUp method
*
* @return void
**/
* @access public
*/
function setUp() {
$this->Dispatcher =& new MockShellDispatcher();
$this->Dispatcher =& new TestShellMockShellDispatcher();
$this->Shell =& new TestShell($this->Dispatcher);
}
/**
* undocumented function
* tearDown method
*
* @return void
* @access public
*/
function tearDown() {
ClassRegistry::flush();
}
/**
* testConstruct method
*
* @return void
* @access public
*/
function testConstruct() {
$this->assertIsA($this->Shell->Dispatch, 'TestShellMockShellDispatcher');
$this->assertEqual($this->Shell->name, 'TestShell');
$this->assertEqual($this->Shell->alias, 'TestShell');
}
/**
* testInitialize method
*
* @return void
* @access public
@ -84,43 +127,255 @@ class CakeShellTestCase extends CakeTestCase {
$this->Shell->initialize();
$this->assertTrue(isset($this->Shell->TestPluginPost));
$this->assertTrue(is_a($this->Shell->TestPluginPost, 'TestPluginPost'));
$this->assertIsA($this->Shell->TestPluginPost, 'TestPluginPost');
$this->assertEqual($this->Shell->modelClass, 'TestPluginPost');
$this->Shell->uses = array('Comment');
$this->Shell->initialize();
$this->assertTrue(isset($this->Shell->Comment));
$this->assertTrue(is_a($this->Shell->Comment, 'Comment'));
$this->assertIsA($this->Shell->Comment, 'Comment');
$this->assertEqual($this->Shell->modelClass, 'Comment');
$this->Shell->uses = true;
$this->Shell->initialize();
$this->assertTrue(isset($this->Shell->AppModel));
$this->assertIsA($this->Shell->AppModel, 'AppModel');
Configure::write('pluginPaths', $_back['pluginPaths']);
Configure::write('modelPaths', $_back['modelPaths']);
}
/**
* testOut method
*
* @return void
* @access public
*/
function testOut() {
$this->Shell->Dispatch->expectAt(0, 'stdout', array('Just a test', true));
$this->Shell->out('Just a test');
$this->Shell->Dispatch->expectAt(1, 'stdout', array("Just\na\ntest\n", true));
$this->Shell->out(array('Just', 'a', 'test'));
}
/**
* Test Loading of Tasks
* testIn method
*
* @return void
**/
* @access public
*/
function testIn() {
$this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
$this->Shell->Dispatch->expectAt(0, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
$result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
$this->assertEqual($result, 'n');
$this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'Y');
$this->Shell->Dispatch->expectAt(1, 'getInput', array('Just a test?', array('y', 'n'), 'n'));
$result = $this->Shell->in('Just a test?', array('y', 'n'), 'n');
$this->assertEqual($result, 'Y');
$this->Shell->Dispatch->setReturnValueAt(2, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(2, 'getInput', array('Just a test?', 'y,n', 'n'));
$result = $this->Shell->in('Just a test?', 'y,n', 'n');
$this->assertEqual($result, 'y');
$this->Shell->Dispatch->setReturnValueAt(3, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(3, 'getInput', array('Just a test?', 'y/n', 'n'));
$result = $this->Shell->in('Just a test?', 'y/n', 'n');
$this->assertEqual($result, 'y');
$this->Shell->Dispatch->setReturnValueAt(4, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(4, 'getInput', array('Just a test?', 'y', 'y'));
$result = $this->Shell->in('Just a test?', 'y', 'y');
$this->assertEqual($result, 'y');
$this->Shell->interactive = false;
$result = $this->Shell->in('Just a test?', 'y/n', 'n');
$this->assertEqual($result, 'n');
}
/**
* testLoadTasks method
*
* @return void
* @access public
*/
function testLoadTasks() {
$this->assertTrue($this->Shell->loadTasks());
$this->Shell->tasks = null;
$this->assertTrue($this->Shell->loadTasks());
$this->Shell->tasks = false;
$this->assertTrue($this->Shell->loadTasks());
$this->Shell->tasks = true;
$this->assertTrue($this->Shell->loadTasks());
$this->Shell->tasks = array();
$this->assertTrue($this->Shell->loadTasks());
// Fatal Error
// $this->Shell->tasks = 'TestIDontExist';
// $this->assertFalse($this->Shell->loadTasks());
// $this->assertFalse(isset($this->Shell->TestIDontExist));
$this->Shell->tasks = 'TestApple';
$this->assertTrue($this->Shell->loadTasks());
$this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
$this->Shell->tasks = 'TestBanana';
$this->assertTrue($this->Shell->loadTasks());
$this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
$this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
unset($this->Shell->ShellTestApple, $this->Shell->TestBanana);
$this->Shell->tasks = array('TestApple', 'TestBanana');
$this->assertTrue($this->Shell->loadTasks());
$this->assertIsA($this->Shell->TestApple, 'TestAppleTask');
$this->assertIsA($this->Shell->TestBanana, 'TestBananaTask');
}
/**
* test ShortPath
* testShortPath method
*
* @return void
**/
* @access public
*/
function testShortPath() {
$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'cd' . DS ;
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = $expected = DS . 'tmp' . DS . 'ab' . DS . 'index.php';
$this->assertEqual($this->Shell->shortPath($path), $expected);
// Shell::shortPath needs Folder::realpath
// $path = DS . 'tmp' . DS . 'ab' . DS . '..' . DS . 'cd';
// $expected = DS . 'tmp' . DS . 'cd';
// $this->assertEqual($this->Shell->shortPath($path), $expected);
$path = DS . 'tmp' . DS . 'ab' . DS . DS . 'cd';
$expected = DS . 'tmp' . DS . 'ab' . DS . 'cd';
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = 'tmp' . DS . 'ab';
$expected = 'tmp' . DS . 'ab';
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = 'tmp' . DS . 'ab';
$expected = 'tmp' . DS . 'ab';
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = APP;
$expected = DS . basename(APP) . DS;
$this->assertEqual($this->Shell->shortPath($path), $expected);
$path = APP . 'index.php';
$expected = DS . basename(APP) . DS . 'index.php';
$this->assertEqual($this->Shell->shortPath($path), $expected);
}
/**
* test File creation
* testCreateFile method
*
* @return void
**/
function createFile() {
* @access public
*/
function testCreateFile() {
$this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s Not supported on Windows');
$path = TMP . 'shell_test';
$file = $path . DS . 'file1.php';
new Folder($path, true);
$this->Shell->interactive = false;
$contents = "<?php\necho 'test';\n\$te = 'st';\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$contents = "<?php\necho 'another test';\n\$te = 'st';\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$this->Shell->interactive = true;
$this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
$this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?', '*'));
$contents = "<?php\necho 'yet another test';\n\$te = 'st';\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertFalse($result);
$this->assertTrue(file_exists($file));
$this->assertNotEqual(file_get_contents($file), $contents);
$this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?', '*'));
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$Folder = new Folder($path);
$Folder->delete();
}
/**
* testCreateFileWindows method
*
* @return void
* @access public
*/
function testCreateFileWindows() {
$this->skipUnless(DIRECTORY_SEPARATOR === '\\', '%s Supported on Windows only');
$path = TMP . 'shell_test';
$file = $path . DS . 'file1.php';
new Folder($path, true);
$this->Shell->interactive = false;
$contents = "<?php\necho 'test';\r\n\$te = 'st';\r\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$contents = "<?php\necho 'another test';\r\n\$te = 'st';\r\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$this->Shell->interactive = true;
$this->Shell->Dispatch->setReturnValueAt(0, 'getInput', 'n');
$this->Shell->Dispatch->expectAt(1, 'stdout', array('File exists, overwrite?'));
$contents = "<?php\necho 'yet another test';\r\n\$te = 'st';\r\n?>";
$result = $this->Shell->createFile($file, $contents);
$this->assertFalse($result);
$this->assertTrue(file_exists($file));
$this->assertNotEqual(file_get_contents($file), $contents);
$this->Shell->Dispatch->setReturnValueAt(1, 'getInput', 'y');
$this->Shell->Dispatch->expectAt(3, 'stdout', array('File exists, overwrite?'));
$result = $this->Shell->createFile($file, $contents);
$this->assertTrue($result);
$this->assertTrue(file_exists($file));
$this->assertEqual(file_get_contents($file), $contents);
$Folder = new Folder($path);
$Folder->delete();
}
}
?>

View file

@ -3,7 +3,7 @@
/**
* Test Case for i18n extraction shell task
*
*
* Long description for file
*
* PHP versions 4 and 5
*
@ -16,15 +16,15 @@
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs.
* @package cake.tests
* @subpackage cake.tests.cases.console.libs.tasks
* @since CakePHP v 1.2.0.7726
* @version $Revision: 7838 $
* @modifiedby $LastChangedBy: DarkAngelBGE $
* @lastmodified $Date: 2008-11-07 05:41:52 -0500 (Fri, 07 Nov 2008) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Core', 'Shell');
App::import('Core', array('Shell', 'Folder'));
if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
@ -41,45 +41,96 @@ if (!class_exists('ExtractTask')) {
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'extract.php';
}
class TestExtractShellDispatcher extends ShellDispatcher {
function _initEnvironment() {
}
function stdout($string, $newline = true) {
}
function stderr($string) {
}
function _stop($status = 0) {
$this->stopped = 'Stopped with status: ' . $status;
}
}
class MockExtractTast extends ExtractTask {
function searchDirectory($path = null) {
return parent::__searchDirectory($path);
}
}
Mock::generatePartial(
'ShellDispatcher', 'TestExtractTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
/**
* ExtractTaskTest class
*
* @package cake.tests
* @subpackage cake.tests.cases.console.libs.tasks
*/
class ExtractTaskTest extends CakeTestCase {
/**
* setUp method
*
* @return void
* @access public
*/
function setUp() {
$this->dispatcher = new TestExtractShellDispatcher();
$this->task = new MockExtractTast($this->dispatcher);
mkdir(TMP . '/extract_test');
$this->Dispatcher =& new TestExtractTaskMockShellDispatcher();
$this->Task =& new ExtractTask($this->Dispatcher);
}
function testDirectorySearching() {
$this->assertIdentical($this->task->searchDirectory(TMP . '/extract_test'), array());
}
/**
* tearDown method
*
* @return void
* @access public
*/
function tearDown() {
unset($this->task, $this->dispatcher);
rmdir(TMP . '/extract_test');
ClassRegistry::flush();
}
/**
* testExecute method
*
* @return void
* @access public
*/
function testExecute() {
$path = TMP . 'extract_task_test';
$folder1 = $path . DS . 'locale';
new Folder($path, true);
new Folder($folder1, true);
$this->Task->interactive = false;
$this->Task->params['path'] = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'pages';
$this->Task->params['output'] = $path . DS;
$this->Task->Dispatch->expectNever('stderr');
$this->Task->Dispatch->expectNever('_stop');
$this->Task->execute();
$this->assertTrue(file_exists($path . DS . 'default.pot'));
$result = file_get_contents($path . DS . 'default.pot');
$pattern = '/"Content-Type\: text\/plain; charset\=utf-8/';
$this->assertPattern($pattern, $result);
$pattern = '/"Content-Transfer-Encoding\: 8bit/';
$this->assertPattern($pattern, $result);
$pattern = '/"Plural-Forms\: nplurals\=INTEGER; plural\=EXPRESSION;/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Your tmp directory is writable."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Your tmp directory is NOT writable."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "The %s is being used for caching. To change the config edit ';
$pattern .= 'APP\/config\/core.php "\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Your cache is NOT working. Please check ';
$pattern .= 'the settings in APP\/config\/core.php"\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Your database configuration file is present."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Your database configuration file is NOT present."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Rename config\/database.php.default to ';
$pattern .= 'config\/database.php"\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Cake is able to connect to the database."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Cake is NOT able to connect to the database."\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "Editing this Page"\nmsgstr ""\n/';
$this->assertPattern($pattern, $result);
$pattern = '/msgid "To change the content of this page, edit: %s.*To change its layout, ';
$pattern .= 'edit: %s.*You can also add some CSS styles for your pages at: %s"\nmsgstr ""/s';
$this->assertPattern($pattern, $result);
$Folder = new Folder($path);
$Folder->delete();
}
}
?>

View file

@ -3,7 +3,7 @@
/**
* Test Case for test generation shell task
*
*
* Long description for file
*
* PHP versions 4 and 5
*
@ -16,8 +16,8 @@
* @filesource
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs.
* @package cake.tests
* @subpackage cake.tests.cases.console.libs.tasks
* @since CakePHP v 1.2.0.7726
* @version $Revision$
* @modifiedby $LastChangedBy$
@ -41,51 +41,60 @@ if (!class_exists('TestTask')) {
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'test.php';
}
class TestTestShellDispatcher extends ShellDispatcher {
function _initEnvironment() {
}
function stdout($string, $newline = true) {
}
function stderr($string) {
}
function getInput($prompt, $options, $default) {
}
function _stop($status = 0) {
$this->stopped = 'Stopped with status: ' . $status;
}
}
Mock::generatePartial('TestTask', 'MockTestTask', array('createFile', 'out', 'in'));
Mock::generatePartial(
'ShellDispatcher', 'TestTestTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);
Mock::generatePartial(
'TestTask', 'MockTestTask',
array('in', 'out', 'createFile')
);
/**
* TestTaskTest class
*
* @package cake.tests
* @subpackage cake.tests.cases.console.libs.tasks
*/
class TestTaskTest extends CakeTestCase {
/**
* setUp method
*
* @return void
* @access public
*/
function setUp() {
$this->dispatcher = new TestTestShellDispatcher();
$this->task = new MockTestTask($this->dispatcher);
$this->Dispatcher =& new TestTestTaskMockShellDispatcher();
$this->Task =& new MockTestTask($this->Dispatcher);
$this->Task->Dispatch = new $this->Dispatcher;
}
/**
* tearDown method
*
* @return void
* @access public
*/
function tearDown() {
ClassRegistry::flush();
}
/**
* Test that file path generation doesn't continuously append paths.
*
*
* @access public
* @return void
*/
function testFilePathGeneration () {
$this->task->setReturnValue('in', 'y');
$this->task->expectAt(0, 'createFile', array(TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php', '*'));
$this->task->bake('Model', 'MyClass');
$this->task->expectAt(1, 'createFile', array(TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php', '*'));
$this->task->bake('Model', 'MyClass');
}
$file = TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php';
function tearDown() {
unset($this->task, $this->dispatcher);
$this->Task->Dispatch->expectNever('stderr');
$this->Task->Dispatch->expectNever('_stop');
$this->Task->setReturnValueAt(0, 'in', 'y');
$this->Task->expectAt(0, 'createFile', array($file, '*'));
$this->Task->bake('Model', 'MyClass');
$this->Task->setReturnValueAt(1, 'in', 'y');
$this->Task->expectAt(1, 'createFile', array($file, '*'));
$this->Task->bake('Model', 'MyClass');
}
}
?>

View file

@ -0,0 +1,58 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.groups
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
/** AllConsoleGroupTest
*
* This test group will run all console tests
*
* @package cake.tests
* @subpackage cake.tests.groups
*/
/**
* AllConsoleGroupTest class
*
* @package cake
* @subpackage cake.tests.groups
*/
class AllConsoleGroupTest extends GroupTest {
/**
* label property
*
* @var string 'All core cache engines'
* @access public
*/
var $label = 'All console tests';
/**
* AllConsoleGroupTest method
*
* @access public
* @return void
*/
function AllConsoleGroupTest() {
TestManager::addTestCasesFromDirectory($this, CORE_TEST_CASES . DS . 'console');
}
}
?>