From 73ad3043a2c9a6fc0a62c39d1f7b8d208bc16004 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 6 Oct 2010 00:15:51 -0400 Subject: [PATCH] Fixing more console tests to use new internals. Making TaskCollection pass the stdout, stdin, stderr to Tasks they create. This allows for more flexible dependency injection and makes testing easier. --- cake/console/libs/task_collection.php | 4 ++- cake/console/libs/tasks/fixture.php | 4 +-- .../console/libs/tasks/controller.test.php | 14 +++++----- .../console/libs/tasks/db_config.test.php | 16 +++++++---- .../cases/console/libs/tasks/fixture.test.php | 28 +++++++++---------- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/cake/console/libs/task_collection.php b/cake/console/libs/task_collection.php index b0fc6aea1..a3b162166 100644 --- a/cake/console/libs/task_collection.php +++ b/cake/console/libs/task_collection.php @@ -60,7 +60,9 @@ class TaskCollection extends ObjectCollection { } } - $this->_loaded[$name] = new $taskClass($this->_Shell); + $this->_loaded[$name] = new $taskClass( + $this->_Shell, $this->_Shell->stdout, $this->_Shell->stderr, $this->_Shell->stdin + ); if ($enable === true) { $this->_enabled[] = $name; } diff --git a/cake/console/libs/tasks/fixture.php b/cake/console/libs/tasks/fixture.php index 1cc9f5ae7..fd04b6287 100644 --- a/cake/console/libs/tasks/fixture.php +++ b/cake/console/libs/tasks/fixture.php @@ -54,8 +54,8 @@ class FixtureTask extends BakeTask { * Override initialize * */ - public function __construct(&$dispatch) { - parent::__construct($dispatch); + public function __construct(&$dispatch, $stdout = null, $stderr = null, $stdin = null) { + parent::__construct($dispatch, $stdout, $stderr, $stdin); $this->path = $this->params['working'] . DS . 'tests' . DS . 'fixtures' . DS; } diff --git a/cake/tests/cases/console/libs/tasks/controller.test.php b/cake/tests/cases/console/libs/tasks/controller.test.php index e9a980e23..6c469a195 100644 --- a/cake/tests/cases/console/libs/tasks/controller.test.php +++ b/cake/tests/cases/console/libs/tasks/controller.test.php @@ -67,12 +67,12 @@ class ControllerTaskTest extends CakeTestCase { * @return void */ public function setUp() { - $this->Dispatcher = $this->getMock('ShellDispatcher', array( - 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment', 'clear' - )); + $this->Dispatcher = $this->getMock('ShellDispatcher', array('_stop', '_initEnvironment')); + $out = $this->getMock('ConsoleOutput'); + $in = $this->getMock('ConsoleInput'); $this->Task = $this->getMock('ControllerTask', array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'), - array(&$this->Dispatcher) + array(&$this->Dispatcher, $out, $out, $in) ); $this->Task->name = 'ControllerTask'; $this->Task->Dispatch->shellPaths = App::path('shells'); @@ -81,13 +81,13 @@ class ControllerTaskTest extends CakeTestCase { $this->Task->Model = $this->getMock('ModelTask', array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest'), - array(&$this->Dispatcher) + array(&$this->Dispatcher, $out, $out, $in) ); $this->Task->Project = $this->getMock('ProjectTask', array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix'), - array(&$this->Dispatcher) + array(&$this->Dispatcher, $out, $out, $in) ); - $this->Task->Test = $this->getMock('TestTask', array(), array(&$this->Dispatcher)); + $this->Task->Test = $this->getMock('TestTask', array(), array(&$this->Dispatcher, $out, $out, $in)); } /** diff --git a/cake/tests/cases/console/libs/tasks/db_config.test.php b/cake/tests/cases/console/libs/tasks/db_config.test.php index 6b1aee614..1ea686daf 100644 --- a/cake/tests/cases/console/libs/tasks/db_config.test.php +++ b/cake/tests/cases/console/libs/tasks/db_config.test.php @@ -60,12 +60,13 @@ class DbConfigTaskTest extends CakeTestCase { */ public function setUp() { parent::setUp(); - $this->Dispatcher = $this->getMock('ShellDispatcher', array( - 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment', 'clear' - )); + $out = $this->getMock('ConsoleOutput'); + $in = $this->getMock('ConsoleInput'); + + $this->Dispatcher = $this->getMock('ShellDispatcher', array('_stop', '_initEnvironment')); $this->Task = $this->getMock('DbConfigTask', array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest', '_verify'), - array(&$this->Dispatcher) + array(&$this->Dispatcher, $out, $out, $in) ); $this->Task->Dispatch->shellPaths = App::path('shells'); @@ -114,7 +115,12 @@ class DbConfigTaskTest extends CakeTestCase { */ public function testExecuteIntoInteractive() { $this->Task->initialize(); - $this->Task = $this->getMock('DbConfigTask', array('in', '_stop', 'createFile'), array(&$this->Dispatcher)); + + $out = $this->getMock('ConsoleOutput'); + $this->Task = $this->getMock( + 'DbConfigTask', + array('in', '_stop', 'createFile'), array(&$this->Dispatcher, $out, $out) + ); $this->Task->expects($this->once())->method('_stop'); $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('default')); //name diff --git a/cake/tests/cases/console/libs/tasks/fixture.test.php b/cake/tests/cases/console/libs/tasks/fixture.test.php index 98e5409b9..6e25291c2 100644 --- a/cake/tests/cases/console/libs/tasks/fixture.test.php +++ b/cake/tests/cases/console/libs/tasks/fixture.test.php @@ -19,10 +19,6 @@ */ App::import('Shell', 'Shell', false); -if (!defined('DISABLE_AUTO_DISPATCH')) { - define('DISABLE_AUTO_DISPATCH', true); -} - require_once CAKE . 'console' . DS . 'shell_dispatcher.php'; require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php'; require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php'; @@ -50,18 +46,19 @@ class FixtureTaskTest extends CakeTestCase { */ public function setUp() { parent::setUp(); - $this->Dispatcher = $this->getMock('ShellDispatcher', array( - 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment', 'clear' - )); + $out = $this->getMock('ConsoleOutput'); + $in = $this->getMock('ConsoleInput'); + + $this->Dispatcher = $this->getMock('ShellDispatcher', array('_stop', '_initEnvironment')); $this->Task = $this->getMock('FixtureTask', - array('in', 'err', 'createFile', '_stop'), - array(&$this->Dispatcher) + array('in', 'err', 'createFile', '_stop', 'clear'), + array(&$this->Dispatcher, $out, $out, $in) ); $this->Task->Model = $this->getMock('Shell', - array('in', 'out', 'erro', 'createFile', 'getName', 'getTable', 'listAll'), - array(&$this->Dispatcher) + array('in', 'out', 'error', 'createFile', 'getName', 'getTable', 'listAll'), + array(&$this->Dispatcher, $out, $out, $in) ); - $this->Task->Template =& new TemplateTask($this->Dispatcher); + $this->Task->Template = new TemplateTask($this->Dispatcher, $out, $out, $in); $this->Task->Dispatch->shellPaths = App::path('shells'); $this->Task->Template->initialize(); } @@ -82,8 +79,11 @@ class FixtureTaskTest extends CakeTestCase { * @return void */ public function testConstruct() { + $out = $this->getMock('ConsoleOutput'); + $in = $this->getMock('ConsoleInput'); + $this->Dispatcher->params['working'] = DS . 'my' . DS . 'path'; - $Task = new FixtureTask($this->Dispatcher); + $Task = new FixtureTask($this->Dispatcher, $out, $out, $in); $expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS; $this->assertEqual($Task->path, $expected); @@ -137,7 +137,7 @@ class FixtureTaskTest extends CakeTestCase { * * @return void */ - public function testImportRecordsFromDatabaseWithConditions() { + public function testImportRecordsFromDatabaseWithConditionsPoo() { $this->Task->interactive = true; $this->Task->expects($this->at(0))->method('in') ->will($this->returnValue('WHERE 1=1 LIMIT 10'));