Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updating FixtureTask test case to use PHPUnit.
  • Loading branch information
markstory committed Jun 10, 2010
1 parent 98a3239 commit a91e8b8
Showing 1 changed file with 97 additions and 66 deletions.
163 changes: 97 additions & 66 deletions cake/tests/cases/console/libs/tasks/fixture.test.php
Expand Up @@ -33,21 +33,6 @@
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';

Mock::generatePartial(
'ShellDispatcher', 'TestFixtureTaskMockShellDispatcher',
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
);

Mock::generatePartial(
'FixtureTask', 'MockFixtureTask',
array('in', 'out', 'err', 'createFile', '_stop')
);

Mock::generatePartial(
'Shell', 'MockFixtureModelTask',
array('in', 'out', 'err', 'createFile', '_stop', 'getName', 'getTable', 'listAll')
);

/**
* FixtureTaskTest class
*
Expand All @@ -70,11 +55,18 @@ class FixtureTaskTest extends CakeTestCase {
* @return void
*/
public function startTest() {
$this->Dispatcher =& new TestFixtureTaskMockShellDispatcher();
$this->Task =& new MockFixtureTask();
$this->Task->Model =& new MockFixtureModelTask();
$this->Task->Dispatch =& $this->Dispatcher;
$this->Task->Template =& new TemplateTask($this->Task->Dispatch);
$this->Dispatcher = $this->getMock('ShellDispatcher', array(
'getInput', 'stdout', 'stderr', '_stop', '_initEnvironment'
));
$this->Task = $this->getMock('FixtureTask',
array('in', 'err', 'createFile', '_stop'),
array(&$this->Dispatcher)
);
$this->Task->Model = $this->getMock('Shell',
array('in', 'out', 'erro', 'createFile', 'getName', 'getTable', 'listAll'),
array(&$this->Dispatcher)
);
$this->Task->Template =& new TemplateTask($this->Dispatcher);
$this->Task->Dispatch->shellPaths = App::path('shells');
$this->Task->Template->initialize();
}
Expand All @@ -96,7 +88,7 @@ public function endTest() {
*/
public function testConstruct() {
$this->Dispatch->params['working'] = DS . 'my' . DS . 'path';
$Task =& new FixtureTask($this->Dispatch);
$Task = new FixtureTask($this->Dispatch);

$expected = DS . 'my' . DS . 'path' . DS . 'tests' . DS . 'fixtures' . DS;
$this->assertEqual($Task->path, $expected);
Expand All @@ -107,25 +99,39 @@ public function testConstruct() {
*
* @return void
*/
public function testImportOptions() {
$this->Task->setReturnValueAt(0, 'in', 'y');
$this->Task->setReturnValueAt(1, 'in', 'y');
public function testImportOptionsSchemaRecords() {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('y'));

$result = $this->Task->importOptions('Article');
$expected = array('schema' => 'Article', 'records' => true);
$this->assertEqual($result, $expected);
}

$this->Task->setReturnValueAt(2, 'in', 'n');
$this->Task->setReturnValueAt(3, 'in', 'n');
$this->Task->setReturnValueAt(4, 'in', 'n');
/**
* test importOptions choosing nothing.
*
* @return void
*/
public function testImportOptionsNothing() {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
$this->Task->expects($this->at(2))->method('in')->will($this->returnValue('n'));

$result = $this->Task->importOptions('Article');
$expected = array();
$this->assertEqual($result, $expected);

$this->Task->setReturnValueAt(5, 'in', 'n');
$this->Task->setReturnValueAt(6, 'in', 'n');
$this->Task->setReturnValueAt(7, 'in', 'y');
}

/**
* test importOptions choosing from Table.
*
* @return void
*/
public function testImportOptionsTable() {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('n'));
$this->Task->expects($this->at(1))->method('in')->will($this->returnValue('n'));
$this->Task->expects($this->at(2))->method('in')->will($this->returnValue('y'));
$result = $this->Task->importOptions('Article');
$expected = array('fromTable' => true);
$this->assertEqual($result, $expected);
Expand All @@ -138,10 +144,15 @@ public function testImportOptions() {
*/
public function testImportRecordsFromDatabaseWithConditions() {
$this->Task->interactive = true;
$this->Task->setReturnValueAt(0, 'in', 'WHERE 1=1 LIMIT 10');
$this->Task->expects($this->at(0))->method('in')
->will($this->returnValue('WHERE 1=1 LIMIT 10'));

$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$result = $this->Task->bake('Article', false, array('fromTable' => true, 'schema' => 'Article', 'records' => false));

$result = $this->Task->bake('Article', false, array(
'fromTable' => true, 'schema' => 'Article', 'records' => false
));

$this->assertPattern('/class ArticleFixture extends CakeTestFixture/', $result);
$this->assertPattern('/public \$records/', $result);
Expand All @@ -161,37 +172,39 @@ public function testExecuteWithNamedModel() {
$this->Task->path = '/my/path/';
$this->Task->args = array('article');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));

$this->Task->expects($this->at(0))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));

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

/**
* data provider for model name variations.
*
* @return array
*/
public static function modelNameProvider() {
return array(
array('article'), array('articles'), array('Articles'), array('Article')
);
}

/**
* test that execute passes runs bake depending with named model.
*
* @dataProvider modelNameProvider
* @return void
*/
public function testExecuteWithNamedModelVariations() {
public function testExecuteWithNamedModelVariations($modelName) {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';

$this->Task->args = array('article');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();

$this->Task->args = array('articles');
$this->Task->args = array($modelName);
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
$this->Task->expects($this->once())->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));

$this->Task->args = array('Articles');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();

$this->Task->args = array('Article');
$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
}

Expand All @@ -204,14 +217,17 @@ public function testExecuteIntoAll() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';
$this->Task->args = array('all');
$this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));
$this->Task->Model->expects($this->any())->method('listAll')
->will($this->returnValue(array('articles', 'comments')));

$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class ArticleFixture/')));
$this->Task->execute();
$this->Task->expects($this->at(0))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class ArticleFixture/'));

$filename = '/my/path/comment_fixture.php';
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class CommentFixture/')));
$this->Task->expects($this->at(1))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/class CommentFixture/'));

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

Expand All @@ -225,14 +241,19 @@ public function testAllWithCountAndRecordsFlags() {
$this->Task->path = '/my/path/';
$this->Task->args = array('all');
$this->Task->params = array('count' => 10, 'records' => true);
$this->Task->Model->setReturnValue('listAll', array('articles', 'comments'));

$this->Task->Model->expects($this->any())->method('listAll')
->will($this->returnValue(array('articles', 'comments')));

$filename = '/my/path/article_fixture.php';
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/title\' => \'Third Article\'/')));
$this->Task->expects($this->at(0))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/title\' => \'Third Article\'/'));

$filename = '/my/path/comment_fixture.php';
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/comment\' => \'First Comment for First Article/')));
$this->Task->expectCallCount('createFile', 2);
$this->Task->expects($this->at(1))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/comment\' => \'First Comment for First Article/'));
$this->Task->expects($this->exactly(2))->method('createFile');

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

Expand All @@ -245,12 +266,16 @@ public function testExecuteInteractive() {
$this->Task->connection = 'test_suite';
$this->Task->path = '/my/path/';

$this->Task->setReturnValue('in', 'y');
$this->Task->Model->setReturnValue('getName', 'Article');
$this->Task->Model->setReturnValue('getTable', 'articles', array('Article'));
$this->Task->expects($this->any())->method('in')->will($this->returnValue('y'));
$this->Task->Model->expects($this->any())->method('getName')->will($this->returnValue('Article'));
$this->Task->Model->expects($this->any())->method('getTable')
->with('Article')
->will($this->returnValue('articles'));

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

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

Expand Down Expand Up @@ -315,10 +340,14 @@ public function testGenerateFixtureFile() {
$this->Task->path = '/my/path/';
$filename = '/my/path/article_fixture.php';

$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
$this->Task->expects($this->at(0))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/Article/'));

$this->Task->expects($this->at(1))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/\<\?php(.*)\?\>/ms'));

$result = $this->Task->generateFixtureFile('Article', array());

$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/\<\?php(.*)\?\>/ms')));
$result = $this->Task->generateFixtureFile('Article', array());
}

Expand All @@ -333,7 +362,9 @@ public function testGeneratePluginFixtureFile() {
$this->Task->plugin = 'TestFixture';
$filename = APP . 'plugins' . DS . 'test_fixture' . DS . 'tests' . DS . 'fixtures' . DS . 'article_fixture.php';

$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/Article/')));
$this->Task->expects($this->at(0))->method('createFile')
->with($filename, new PHPUnit_Framework_Constraint_PCREMatch('/Article/'));

$result = $this->Task->generateFixtureFile('Article', array());
}
}

0 comments on commit a91e8b8

Please sign in to comment.