From 98a323914124d23845ca3043008181cce432d38d Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 9 Jun 2010 20:15:37 -0400 Subject: [PATCH] Updating TestTask test case to use PHPUnit. --- .../cases/console/libs/tasks/test.test.php | 101 ++++++++++-------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/cake/tests/cases/console/libs/tasks/test.test.php b/cake/tests/cases/console/libs/tasks/test.test.php index 23c6c3e204e..7cd6080204f 100644 --- a/cake/tests/cases/console/libs/tasks/test.test.php +++ b/cake/tests/cases/console/libs/tasks/test.test.php @@ -37,16 +37,6 @@ require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'test.php'; require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php'; - -Mock::generatePartial( - 'ShellDispatcher', 'TestTestTaskMockShellDispatcher', - array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment') -); -Mock::generatePartial( - 'TestTask', 'MockTestTask', - array('in', '_stop', 'err', 'out', 'createFile', 'isLoadableClass') -); - /** * Test Article model * @@ -258,11 +248,15 @@ class TestTaskTest extends CakeTestCase { * @return void */ public function startTest() { - $this->Dispatcher =& new TestTestTaskMockShellDispatcher(); + $this->Dispatcher = $this->getMock('ShellDispatcher', array( + 'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment' + )); + $this->Task = $this->getMock('TestTask', + array('in', 'err', 'createFile', '_stop', 'isLoadableClass'), + array(&$this->Dispatcher) + ); $this->Dispatcher->shellPaths = App::path('shells'); - $this->Task =& new MockTestTask($this->Dispatcher); $this->Task->name = 'TestTask'; - $this->Task->Dispatch =& $this->Dispatcher; $this->Task->Template =& new TemplateTask($this->Dispatcher); } @@ -280,21 +274,24 @@ public function endTest() { * * @return void */ - public function testFilePathGeneration() { - $file = TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php'; + public function testFilePathGenerationModelRepeated() { + $this->Task->Dispatch->expects($this->never())->method('stderr'); + $this->Task->Dispatch->expects($this->never())->method('_stop'); - $this->Task->Dispatch->expectNever('stderr'); - $this->Task->Dispatch->expectNever('_stop'); + $file = TESTS . 'cases' . DS . 'models' . DS . 'my_class.test.php'; - $this->Task->setReturnValue('in', 'y'); - $this->Task->expectAt(0, 'createFile', array($file, '*')); - $this->Task->bake('Model', 'MyClass'); + $this->Task->expects($this->at(1))->method('createFile') + ->with($file, new PHPUnit_Framework_Constraint_IsAnything()); - $this->Task->expectAt(1, 'createFile', array($file, '*')); - $this->Task->bake('Model', 'MyClass'); + $this->Task->expects($this->at(3))->method('createFile') + ->with($file, new PHPUnit_Framework_Constraint_IsAnything()); $file = TESTS . 'cases' . DS . 'controllers' . DS . 'comments_controller.test.php'; - $this->Task->expectAt(2, 'createFile', array($file, '*')); + $this->Task->expects($this->at(5))->method('createFile') + ->with($file, new PHPUnit_Framework_Constraint_IsAnything()); + + $this->Task->bake('Model', 'MyClass'); + $this->Task->bake('Model', 'MyClass'); $this->Task->bake('Controller', 'Comments'); } @@ -344,11 +341,12 @@ public function testFixtureArrayGenerationFromController() { * @return void */ public function testGetObjectType() { - $this->Task->expectOnce('_stop'); - $this->Task->setReturnValueAt(0, 'in', 'q'); + $this->Task->expects($this->once())->method('_stop'); + $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('q')); + $this->Task->expects($this->at(2))->method('in')->will($this->returnValue(2)); + $this->Task->getObjectType(); - $this->Task->setReturnValueAt(1, 'in', 2); $result = $this->Task->getObjectType(); $this->assertEqual($result, $this->Task->classTypes[1]); } @@ -388,11 +386,12 @@ public function testGetClassName() { if ($skip) { return; } - $this->Task->setReturnValueAt(0, 'in', 'MyCustomClass'); + $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('MyCustomClass')); + $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(1)); + $result = $this->Task->getClassName('Model'); $this->assertEqual($result, 'MyCustomClass'); - $this->Task->setReturnValueAt(1, 'in', 1); $result = $this->Task->getClassName('Model'); $options = App::objects('model'); $this->assertEqual($result, $options[0]); @@ -404,8 +403,10 @@ public function testGetClassName() { * @return void */ public function testGetUserFixtures() { - $this->Task->setReturnValueAt(0, 'in', 'y'); - $this->Task->setReturnValueAt(1, 'in', 'app.pizza, app.topping, app.side_dish'); + $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y')); + $this->Task->expects($this->at(1))->method('in') + ->will($this->returnValue('app.pizza, app.topping, app.side_dish')); + $result = $this->Task->getUserFixtures(); $expected = array('app.pizza', 'app.topping', 'app.side_dish'); $this->assertEqual($result, $expected); @@ -440,8 +441,8 @@ public function testGetRealClassname() { * @return void */ public function testBakeModelTest() { - $this->Task->setReturnValue('createFile', true); - $this->Task->setReturnValue('isLoadableClass', true); + $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true)); + $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true)); $result = $this->Task->bake('Model', 'TestTaskArticle'); @@ -458,9 +459,7 @@ public function testBakeModelTest() { $this->assertPattern('/function testDoSomethingElse\(\)/i', $result); $this->assertPattern("/'app\.test_task_article'/", $result); - if (PHP5) { - $this->assertPattern("/'plugin\.test_task\.test_task_comment'/", $result); - } + $this->assertPattern("/'plugin\.test_task\.test_task_comment'/", $result); $this->assertPattern("/'app\.test_task_tag'/", $result); $this->assertPattern("/'app\.articles_tag'/", $result); } @@ -473,8 +472,8 @@ public function testBakeModelTest() { * @return void */ public function testBakeControllerTest() { - $this->Task->setReturnValue('createFile', true); - $this->Task->setReturnValue('isLoadableClass', true); + $this->Task->expects($this->once())->method('createFile')->will($this->returnValue(true)); + $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true)); $result = $this->Task->bake('Controller', 'TestTaskComments'); @@ -493,9 +492,7 @@ public function testBakeControllerTest() { $this->assertPattern('/unset\(\$this->TestTaskComments\)/', $result); $this->assertPattern("/'app\.test_task_article'/", $result); - if (PHP5) { - $this->assertPattern("/'plugin\.test_task\.test_task_comment'/", $result); - } + $this->assertPattern("/'plugin\.test_task\.test_task_comment'/", $result); $this->assertPattern("/'app\.test_task_tag'/", $result); $this->assertPattern("/'app\.articles_tag'/", $result); } @@ -538,7 +535,9 @@ public function testBakeWithPlugin() { $this->Task->plugin = 'TestTest'; $path = APP . 'plugins' . DS . 'test_test' . DS . 'tests' . DS . 'cases' . DS . 'helpers' . DS . 'form.test.php'; - $this->Task->expectAt(0, 'createFile', array($path, '*')); + $this->Task->expects($this->once())->method('createFile') + ->with($path, new PHPUnit_Framework_Constraint_IsAnything()); + $this->Task->bake('Helper', 'Form'); } @@ -583,9 +582,13 @@ public function testTestCaseFileName() { */ public function testExecuteWithOneArg() { $this->Task->args[0] = 'Model'; - $this->Task->setReturnValueAt(0, 'in', 'TestTaskTag'); - $this->Task->setReturnValue('isLoadableClass', true); - $this->Task->expectAt(0, 'createFile', array('*', new PatternExpectation('/class TestTaskTagTestCase extends CakeTestCase/'))); + $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag')); + $this->Task->expects($this->once())->method('isLoadableClass')->will($this->returnValue(true)); + $this->Task->expects($this->once())->method('createFile') + ->with( + new PHPUnit_Framework_Constraint_IsAnything(), + new PHPUnit_Framework_Constraint_PCREMatch('/class TestTaskTagTestCase extends CakeTestCase/') + ); $this->Task->execute(); } @@ -596,9 +599,13 @@ public function testExecuteWithOneArg() { */ public function testExecuteWithTwoArgs() { $this->Task->args = array('Model', 'TestTaskTag'); - $this->Task->setReturnValueAt(0, 'in', 'TestTaskTag'); - $this->Task->setReturnValue('isLoadableClass', true); - $this->Task->expectAt(0, 'createFile', array('*', new PatternExpectation('/class TestTaskTagTestCase extends CakeTestCase/'))); + $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('TestTaskTag')); + $this->Task->expects($this->once())->method('createFile') + ->with( + new PHPUnit_Framework_Constraint_IsAnything(), + new PHPUnit_Framework_Constraint_PCREMatch('/class TestTaskTagTestCase extends CakeTestCase/') + ); + $this->Task->expects($this->any())->method('isLoadableClass')->will($this->returnValue(true)); $this->Task->execute(); } }