Skip to content

Commit

Permalink
Updating the rest of the controller task test case to use phpunit.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 30, 2010
1 parent ca5fb9f commit 5ef9103
Showing 1 changed file with 84 additions and 93 deletions.
177 changes: 84 additions & 93 deletions cake/tests/cases/console/libs/tasks/controller.test.php
Expand Up @@ -81,7 +81,7 @@ public function startTest() {
'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment'
));
$this->Task = $this->getMock('ControllerTask',
array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
array(&$this->Dispatcher)
);
$this->Task->name = 'ControllerTask';
Expand All @@ -94,7 +94,7 @@ public function startTest() {
array(&$this->Dispatcher)
);
$this->Task->Project = $this->getMock('ProjectTask',
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest'),
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix'),
array(&$this->Dispatcher)
);
$this->Task->Test = $this->getMock('TestTask', array(), array(&$this->Dispatcher));
Expand Down Expand Up @@ -324,7 +324,7 @@ public function testBakeWithPlugin() {
*
* @return void
*/
public function xxtestBakeActionsUsingSessions() {
public function testBakeActionsUsingSessions() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Testing bakeActions requires Article, Comment & Tag Model to be undefined. %s');
if ($skip) {
Expand All @@ -350,7 +350,7 @@ public function xxtestBakeActionsUsingSessions() {

$this->assertTrue(strpos($result, 'function delete($id = null)') !== false);
$this->assertTrue(strpos($result, 'if ($this->Article->delete($id))') !== false);
$this->assertTrue(strpos($result, "\$this->Session->setFlash(__('Article deleted', true));") !== false);
$this->assertTrue(strpos($result, "\$this->Session->setFlash(__('Article deleted'));") !== false);

$result = $this->Task->bakeActions('Articles', 'admin_', true);

Expand All @@ -366,7 +366,7 @@ public function xxtestBakeActionsUsingSessions() {
*
* @return void
*/
public function xxtestBakeActionsWithNoSessions() {
public function testBakeActionsWithNoSessions() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Testing bakeActions requires Article, Tag, Comment Models to be undefined. %s');
if ($skip) {
Expand Down Expand Up @@ -402,7 +402,7 @@ public function xxtestBakeActionsWithNoSessions() {
*
* @return void
*/
public function xxtestBakeTest() {
public function testBakeTest() {
$this->Task->plugin = 'ControllerTest';
$this->Task->connection = 'test_suite';
$this->Task->interactive = false;
Expand All @@ -420,23 +420,26 @@ public function xxtestBakeTest() {
*
* @return void
*/
public function xxtestInteractive() {
public function testInteractive() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path';
$this->Task->expects($this->any())->method('in')->will($this->returnValue('1'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y')); // build interactive
$this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n')); // build no scaffolds
$this->Task->expects($this->at(3))->method('in')->will($this->returnValue('y')); // build normal methods
$this->Task->expects($this->at(4))->method('in')->will($this->returnValue('n')); // build admin methods
$this->Task->expects($this->at(5))->method('in')->will($this->returnValue('n')); // helpers?
$this->Task->expects($this->at(6))->method('in')->will($this->returnValue('n')); // components?
$this->Task->expects($this->at(7))->method('in')->will($this->returnValue('y')); // use sessions
$this->Task->expects($this->at(8))->method('in')->will($this->returnValue('y')); // looks good

$this->Task->execute();
$this->Task->path = '/my/path/';

$this->Task->expects($this->at(8))->method('in')->will($this->returnValue('1'));
$this->Task->expects($this->at(12))->method('in')->will($this->returnValue('y')); // build interactive
$this->Task->expects($this->at(13))->method('in')->will($this->returnValue('n')); // build no scaffolds
$this->Task->expects($this->at(14))->method('in')->will($this->returnValue('y')); // build normal methods
$this->Task->expects($this->at(15))->method('in')->will($this->returnValue('n')); // build admin methods
$this->Task->expects($this->at(16))->method('in')->will($this->returnValue('n')); // helpers?
$this->Task->expects($this->at(17))->method('in')->will($this->returnValue('n')); // components?
$this->Task->expects($this->at(18))->method('in')->will($this->returnValue('y')); // use sessions
$this->Task->expects($this->at(25))->method('in')->will($this->returnValue('y')); // looks good

$filename = '/my/path/articles_controller.php';
$this->Task->expects($this->at(0))->method('createFile')->with($filename, new PatternExpectation('/class ArticlesController/'));
$this->Task->expects($this->once())->method('createFile')->with(
$filename,
new PHPUnit_Framework_Constraint_PCREMatch('/class ArticlesController/')
);
$this->Task->execute();
}

/**
Expand All @@ -445,35 +448,40 @@ public function xxtestInteractive() {
* @return void
* @access public
*/
function xxtestInteractiveAdminMethodsNotInteractive() {
function testInteractiveAdminMethodsNotInteractive() {
$this->Task->connection = 'test_suite';
$this->Task->interactive = true;
$this->Task->path = '/my/path';
$this->Task->setReturnValue('in', '1');
$this->Task->setReturnValueAt(1, 'in', 'y'); // build interactive
$this->Task->setReturnValueAt(2, 'in', 'n'); // build no scaffolds
$this->Task->setReturnValueAt(3, 'in', 'y'); // build normal methods
$this->Task->setReturnValueAt(4, 'in', 'y'); // build admin methods
$this->Task->setReturnValueAt(5, 'in', 'n'); // helpers?
$this->Task->setReturnValueAt(6, 'in', 'n'); // components?
$this->Task->setReturnValueAt(7, 'in', 'y'); // use sessions
$this->Task->setReturnValueAt(8, 'in', 'y'); // looks good
$this->Task->setReturnValue('createFile', true);
$this->Task->Project->setReturnValue('getPrefix', 'admin_');
$this->Task->path = '/my/path/';

$result = $this->Task->execute();
$this->assertPattern('/admin_index/', $result);
$this->Task->expects($this->at(8))->method('in')->will($this->returnValue('1'));
$this->Task->expects($this->at(12))->method('in')->will($this->returnValue('y')); // build interactive
$this->Task->expects($this->at(13))->method('in')->will($this->returnValue('n')); // build no scaffolds
$this->Task->expects($this->at(14))->method('in')->will($this->returnValue('y')); // build normal methods
$this->Task->expects($this->at(15))->method('in')->will($this->returnValue('y')); // build admin methods
$this->Task->expects($this->at(16))->method('in')->will($this->returnValue('n')); // helpers?
$this->Task->expects($this->at(17))->method('in')->will($this->returnValue('n')); // components?
$this->Task->expects($this->at(18))->method('in')->will($this->returnValue('y')); // use sessions
$this->Task->expects($this->at(25))->method('in')->will($this->returnValue('y')); // looks good
$this->Task->Project->expects($this->any())
->method('getPrefix')
->will($this->returnValue('admin_'));

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticlesController/')));
$this->Task->expects($this->once())->method('createFile')->with(
$filename,
new PHPUnit_Framework_Constraint_PCREMatch('/class ArticlesController/')
)->will($this->returnValue(true));

$result = $this->Task->execute();
$this->assertPattern('/admin_index/', $result);
}

/**
* test that execute runs all when the first arg == all
*
* @return void
*/
public function xxtestExecuteIntoAll() {
public function testExecuteIntoAll() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute into all could not be run as an Article, Tag or Comment model was already loaded. %s');
if ($skip) {
Expand All @@ -483,12 +491,14 @@ public function xxtestExecuteIntoAll() {
$this->Task->path = '/my/path/';
$this->Task->args = array('all');

$this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));
$this->Task->expects($this->any())->method('_checkUnitTest')->will($this->returnValue(true));
$this->Task->Test->expectCallCount('bake', 1);
$this->Task->Test->expects($this->once())->method('bake');

$filename = '/my/path/articles_controller.php';
$this->Task->expects($this->at(0))->method('createFile')->with($filename, new PatternExpectation('/class ArticlesController/'));
$this->Task->expects($this->once())->method('createFile')->with(
$filename,
new PHPUnit_Framework_Constraint_PCREMatch('/class ArticlesController/')
)->will($this->returnValue(true));

$this->Task->execute();
}
Expand All @@ -498,7 +508,7 @@ public function xxtestExecuteIntoAll() {
*
* @return void
*/
public function xxtestExecuteWithController() {
public function testExecuteWithController() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
Expand All @@ -509,61 +519,44 @@ public function xxtestExecuteWithController() {
$this->Task->args = array('Articles');

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new PatternExpectation('/\$scaffold/')
));
$this->Task->expects($this->once())->method('createFile')->with(
$filename,
new PHPUnit_Framework_Constraint_PCREMatch('/\$scaffold/')
);

$this->Task->execute();
}

/**
* data provider for testExecuteWithControllerNameVariations
*
* @return void
*/
static function nameVariations() {
return array(
array('Articles'), array('Article'), array('article'), array('articles')
);
}
/**
* test that both plural and singular forms work for controller baking.
*
* @dataProvider nameVariations
* @return void
*/
public function xxtestExecuteWithControllerNameVariations() {
public function testExecuteWithControllerNameVariations($name) {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
return;
}
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('Articles');

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new PatternExpectation('/\$scaffold/')
));
$this->Task->args = array($name);

$this->Task->execute();

$this->Task->args = array('Article');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(1, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();

$this->Task->args = array('article');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(2, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));

$this->Task->args = array('articles');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(3, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();

$this->Task->args = array('Articles');
$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(4, 'createFile', array(
$filename, new PatternExpectation('/class ArticlesController/')
));
$this->Task->execute();
$this->Task->expects($this->once())->method('createFile')->with(
$filename, new PHPUnit_Framework_Constraint_PCREMatch('/\$scaffold/')
);
$this->Task->execute();
}

Expand All @@ -572,7 +565,7 @@ public function xxtestExecuteWithControllerNameVariations() {
*
* @return void
*/
public function xxtestExecuteWithPublicParam() {
public function testExecuteWithPublicParam() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
Expand All @@ -583,10 +576,10 @@ public function xxtestExecuteWithPublicParam() {
$this->Task->args = array('Articles', 'public');

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new NoPatternExpectation('/var \$scaffold/')
));

$expected = new PHPUnit_Framework_Constraint_Not(new PHPUnit_Framework_Constraint_PCREMatch('/\$scaffold/'));
$this->Task->expects($this->once())->method('createFile')->with(
$filename, $expected
);
$this->Task->execute();
}

Expand All @@ -595,7 +588,7 @@ public function xxtestExecuteWithPublicParam() {
*
* @return void
*/
public function xxtestExecuteWithControllerAndBoth() {
public function testExecuteWithControllerAndBoth() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
Expand All @@ -607,10 +600,9 @@ public function xxtestExecuteWithControllerAndBoth() {
$this->Task->args = array('Articles', 'public', 'admin');

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new PatternExpectation('/admin_index/')
));

$this->Task->expects($this->once())->method('createFile')->with(
$filename, new PHPUnit_Framework_Constraint_PCREMatch('/admin_index/')
);
$this->Task->execute();
}

Expand All @@ -619,7 +611,7 @@ public function xxtestExecuteWithControllerAndBoth() {
*
* @return void
*/
public function xxtestExecuteWithControllerAndAdmin() {
public function testExecuteWithControllerAndAdmin() {
$skip = $this->skipIf(!defined('ARTICLE_MODEL_CREATED'),
'Execute with scaffold param requires no Article, Tag or Comment model to be defined. %s');
if ($skip) {
Expand All @@ -631,10 +623,9 @@ public function xxtestExecuteWithControllerAndAdmin() {
$this->Task->args = array('Articles', 'admin');

$filename = '/my/path/articles_controller.php';
$this->Task->expectAt(0, 'createFile', array(
$filename, new PatternExpectation('/admin_index/')
));

$this->Task->expects($this->once())->method('createFile')->with(
$filename, new PHPUnit_Framework_Constraint_PCREMatch('/admin_index/')
);
$this->Task->execute();
}
}

0 comments on commit 5ef9103

Please sign in to comment.