Skip to content

Commit e17166c

Browse files
committed
Fix up problems with all() and unskip/remove tests.
1 parent 9e41ec8 commit e17166c

File tree

2 files changed

+81
-194
lines changed

2 files changed

+81
-194
lines changed

src/Console/Command/Task/ModelTask.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ public function execute() {
115115
* @return void
116116
*/
117117
public function generate($name) {
118-
$table = $this->getTable();
119-
118+
$table = $this->getTable($name);
120119
$model = $this->getTableObject($name, $table);
121120
$associations = $this->getAssociations($model);
122121
$primaryKey = $this->getPrimaryKey($model);
@@ -141,14 +140,13 @@ public function generate($name) {
141140
*/
142141
public function all() {
143142
$this->listAll($this->connection, false);
144-
$unitTestExists = $this->_checkUnitTest();
145143
foreach ($this->_tables as $table) {
146144
if (in_array($table, $this->skipTables)) {
147145
continue;
148146
}
149-
$modelClass = Inflector::classify($table);
147+
$modelClass = $this->_modelName($table);
150148
$this->out(__d('cake_console', 'Baking %s', $modelClass));
151-
$this->generate($table);
149+
$this->generate($modelClass);
152150
}
153151
}
154152

@@ -205,7 +203,7 @@ public function getAssociations(Table $table) {
205203
*/
206204
public function findBelongsTo($model, $associations) {
207205
$schema = $model->schema();
208-
$primary = $schema->primaryKey();
206+
$primary = (array)$schema->primaryKey();
209207
foreach ($schema->columns() as $fieldName) {
210208
$offset = strpos($fieldName, '_id');
211209
if (!in_array($fieldName, $primary) && $fieldName !== 'parent_id' && $offset !== false) {
@@ -629,11 +627,11 @@ protected function _getAllTables() {
629627
*
630628
* @return string.
631629
*/
632-
public function getTable() {
630+
public function getTable($name) {
633631
if (isset($this->params['table'])) {
634632
return $this->params['table'];
635633
}
636-
return Inflector::tableize($this->args[0]);
634+
return Inflector::tableize($name);
637635
}
638636

639637
/**

tests/TestCase/Console/Command/Task/ModelTaskTest.php

Lines changed: 75 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,14 @@ public function testListAllConnection() {
121121
* @return void
122122
*/
123123
public function testGetTable() {
124-
$this->Task->args[0] = 'BakeArticle';
125-
$result = $this->Task->getTable();
124+
$result = $this->Task->getTable('BakeArticle');
126125
$this->assertEquals('bake_articles', $result);
127126

128-
$this->Task->args[0] = 'BakeArticles';
129-
$result = $this->Task->getTable();
127+
$result = $this->Task->getTable('BakeArticles');
130128
$this->assertEquals('bake_articles', $result);
131129

132-
$this->Task->args[0] = 'Article';
133130
$this->Task->params['table'] = 'bake_articles';
134-
$result = $this->Task->getTable();
131+
$result = $this->Task->getTable('Article');
135132
$this->assertEquals('bake_articles', $result);
136133
}
137134

@@ -673,164 +670,69 @@ public function testExecuteWithNamedModelVariations($name) {
673670
* @return void
674671
*/
675672
public function testExecuteIntoAll() {
676-
$this->markTestIncomplete('Not done here yet');
677-
$count = count($this->Task->listAll('test'));
673+
$count = count($this->Task->listAll());
678674
if ($count != count($this->fixtures)) {
679675
$this->markTestSkipped('Additional tables detected.');
680676
}
681677

682678
$this->Task->connection = 'test';
683679
$this->Task->path = '/my/path/';
684-
$this->Task->args = array('all');
685-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
686-
687-
$this->Task->Fixture->expects($this->exactly(5))->method('bake');
688-
$this->Task->Test->expects($this->exactly(5))->method('bake');
689-
690-
$filename = '/my/path/BakeArticle.php';
691-
$this->Task->expects($this->at(1))->method('createFile')
692-
->with($filename, $this->stringContains('class BakeArticle'));
693-
694-
$filename = '/my/path/BakeArticlesBakeTag.php';
695-
$this->Task->expects($this->at(2))->method('createFile')
696-
->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
697-
698-
$filename = '/my/path/BakeComment.php';
699-
$this->Task->expects($this->at(3))->method('createFile')
700-
->with($filename, $this->stringContains('class BakeComment'));
701-
702-
$filename = '/my/path/BakeComment.php';
703-
$this->Task->expects($this->at(3))->method('createFile')
704-
->with($filename, $this->stringContains('public $primaryKey = \'otherid\';'));
705-
706-
$filename = '/my/path/BakeTag.php';
707-
$this->Task->expects($this->at(4))->method('createFile')
708-
->with($filename, $this->stringContains('class BakeTag'));
709-
710-
$filename = '/my/path/BakeTag.php';
711-
$this->Task->expects($this->at(4))->method('createFile')
712-
->with($filename, $this->logicalNot($this->stringContains('public $primaryKey')));
713-
714-
$filename = '/my/path/CategoryThread.php';
715-
$this->Task->expects($this->at(5))->method('createFile')
716-
->with($filename, $this->stringContains('class CategoryThread'));
717-
718-
$this->Task->execute();
719-
720-
$this->assertEquals(count(ClassRegistry::keys()), 0);
721-
$this->assertEquals(count(ClassRegistry::mapKeys()), 0);
722-
}
723-
724-
/**
725-
* test that odd tablenames aren't inflected back from modelname
726-
*
727-
* @return void
728-
*/
729-
public function testExecuteIntoAllOddTables() {
730-
$this->markTestIncomplete('Not done here yet');
731-
$out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
732-
$in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
733-
$this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
734-
array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
735-
array($out, $out, $in)
736-
);
737-
$this->_setupOtherMocks();
738-
739-
$this->Task->connection = 'test';
740-
$this->Task->path = '/my/path/';
741-
$this->Task->args = array('all');
742-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
743-
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
744-
$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
745-
$this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
746-
$this->Task->expects($this->at(3))->method('bake')->with($object, false)->will($this->returnValue(true));
747-
$this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
680+
$this->Task->args = ['all'];
748681

749-
$this->Task->execute();
682+
$this->Task->Fixture->expects($this->exactly(6))
683+
->method('bake');
684+
$this->Task->Test->expects($this->exactly(6))
685+
->method('bake');
750686

751-
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
752-
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
753-
$this->Task = $this->getMock('ModelTask',
754-
array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
755-
array($out, $out, $in)
756-
);
757-
$this->_setupOtherMocks();
687+
$filename = '/my/path/Table/BakeArticlesTable.php';
688+
$this->Task->expects($this->at(0))
689+
->method('createFile')
690+
->with($filename, $this->stringContains('class BakeArticlesTable extends'));
758691

759-
$this->Task->connection = 'test';
760-
$this->Task->path = '/my/path/';
761-
$this->Task->args = array('all');
762-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
763-
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('bake_odd')));
764-
$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
765-
$this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
766-
$this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
767-
$this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
768-
$this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
769-
770-
$filename = '/my/path/BakeOdd.php';
771-
$this->Task->expects($this->once())->method('createFile')
772-
->with($filename, $this->stringContains('class BakeOdd'));
692+
$filename = '/my/path/Entity/BakeArticle.php';
693+
$this->Task->expects($this->at(1))
694+
->method('createFile')
695+
->with($filename, $this->stringContains('class BakeArticle extends'));
773696

774-
$filename = '/my/path/BakeOdd.php';
775-
$this->Task->expects($this->once())->method('createFile')
776-
->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
697+
$filename = '/my/path/Table/BakeArticlesBakeTagsTable.php';
698+
$this->Task->expects($this->at(2))
699+
->method('createFile')
700+
->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
777701

778-
$this->Task->execute();
779-
}
702+
$filename = '/my/path/Entity/BakeArticlesBakeTag.php';
703+
$this->Task->expects($this->at(3))
704+
->method('createFile')
705+
->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
780706

781-
/**
782-
* test that odd tablenames aren't inflected back from modelname
783-
*
784-
* @return void
785-
*/
786-
public function testExecuteIntoBakeOddTables() {
787-
$this->markTestIncomplete('Not done here yet');
788-
$out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
789-
$in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
790-
$this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
791-
array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'bake', 'bakeFixture'),
792-
array($out, $out, $in)
793-
);
794-
$this->_setupOtherMocks();
707+
$filename = '/my/path/Table/BakeCommentsTable.php';
708+
$this->Task->expects($this->at(4))
709+
->method('createFile')
710+
->with($filename, $this->stringContains('class BakeCommentsTable extends'));
795711

796-
$this->Task->connection = 'test';
797-
$this->Task->path = '/my/path/';
798-
$this->Task->args = array('BakeOdd');
799-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
800-
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
801-
$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
802-
$this->Task->expects($this->once())->method('_getModelObject')->with('BakeOdd', 'bake_odd')->will($this->returnValue($object));
803-
$this->Task->expects($this->once())->method('bake')->with($object, false)->will($this->returnValue(true));
804-
$this->Task->expects($this->once())->method('bakeFixture')->with('BakeOdd', 'bake_odd');
712+
$filename = '/my/path/Entity/BakeComment.php';
713+
$this->Task->expects($this->at(5))
714+
->method('createFile')
715+
->with($filename, $this->stringContains('class BakeComment extends'));
805716

806-
$this->Task->execute();
717+
$filename = '/my/path/Table/BakeTagsTable.php';
718+
$this->Task->expects($this->at(6))
719+
->method('createFile')
720+
->with($filename, $this->stringContains('class BakeTagsTable extends'));
807721

808-
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
809-
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
810-
$this->Task = $this->getMock('ModelTask',
811-
array('in', 'err', '_stop', '_checkUnitTest', 'getAllTables', '_getModelObject', 'doAssociations', 'doValidation', 'doActsAs', 'createFile'),
812-
array($out, $out, $in)
813-
);
814-
$this->_setupOtherMocks();
722+
$filename = '/my/path/Entity/BakeTag.php';
723+
$this->Task->expects($this->at(7))
724+
->method('createFile')
725+
->with($filename, $this->stringContains('class BakeTag extends'));
815726

816-
$this->Task->connection = 'test';
817-
$this->Task->path = '/my/path/';
818-
$this->Task->args = array('BakeOdd');
819-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
820-
$this->Task->expects($this->once())->method('getAllTables')->will($this->returnValue(array('articles', 'bake_odd')));
821-
$object = new Model(array('name' => 'BakeOdd', 'table' => 'bake_odd', 'ds' => 'test'));
822-
$this->Task->expects($this->once())->method('_getModelObject')->will($this->returnValue($object));
823-
$this->Task->expects($this->once())->method('doAssociations')->will($this->returnValue(array()));
824-
$this->Task->expects($this->once())->method('doValidation')->will($this->returnValue(array()));
825-
$this->Task->expects($this->once())->method('doActsAs')->will($this->returnValue(array()));
826-
827-
$filename = '/my/path/BakeOdd.php';
828-
$this->Task->expects($this->once())->method('createFile')
829-
->with($filename, $this->stringContains('class BakeOdd'));
727+
$filename = '/my/path/Table/CategoryThreadsTable.php';
728+
$this->Task->expects($this->at(8))
729+
->method('createFile')
730+
->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
830731

831-
$filename = '/my/path/BakeOdd.php';
832-
$this->Task->expects($this->once())->method('createFile')
833-
->with($filename, $this->stringContains('public $useTable = \'bake_odd\''));
732+
$filename = '/my/path/Entity/CategoryThread.php';
733+
$this->Task->expects($this->at(9))
734+
->method('createFile')
735+
->with($filename, $this->stringContains('class CategoryThread extends'));
834736

835737
$this->Task->execute();
836738
}
@@ -841,58 +743,45 @@ public function testExecuteIntoBakeOddTables() {
841743
* @return void
842744
*/
843745
public function testSkipTablesAndAll() {
844-
$this->markTestIncomplete('Not done here yet');
845746
$count = count($this->Task->listAll('test'));
846747
if ($count != count($this->fixtures)) {
847748
$this->markTestSkipped('Additional tables detected.');
848749
}
849750

850751
$this->Task->connection = 'test';
851752
$this->Task->path = '/my/path/';
852-
$this->Task->args = array('all');
853-
$this->Task->expects($this->once())->method('_checkUnitTest')->will($this->returnValue(true));
854-
$this->Task->skipTables = array('bake_tags');
855-
856-
$this->Task->Fixture->expects($this->exactly(4))->method('bake');
857-
$this->Task->Test->expects($this->exactly(4))->method('bake');
753+
$this->Task->args = ['all'];
754+
$this->Task->skipTables = ['bake_tags'];
858755

859-
$filename = '/my/path/BakeArticle.php';
860-
$this->Task->expects($this->at(1))->method('createFile')
861-
->with($filename, $this->stringContains('class BakeArticle'));
862-
863-
$filename = '/my/path/BakeArticlesBakeTag.php';
864-
$this->Task->expects($this->at(2))->method('createFile')
865-
->with($filename, $this->stringContains('class BakeArticlesBakeTag'));
756+
$this->Task->Fixture->expects($this->exactly(5))
757+
->method('bake');
758+
$this->Task->Test->expects($this->exactly(5))
759+
->method('bake');
866760

867-
$filename = '/my/path/BakeComment.php';
868-
$this->Task->expects($this->at(3))->method('createFile')
869-
->with($filename, $this->stringContains('class BakeComment'));
761+
$filename = '/my/path/Entity/BakeArticle.php';
762+
$this->Task->expects($this->at(1))
763+
->method('createFile')
764+
->with($filename);
870765

871-
$filename = '/my/path/CategoryThread.php';
872-
$this->Task->expects($this->at(4))->method('createFile')
873-
->with($filename, $this->stringContains('class CategoryThread'));
766+
$filename = '/my/path/Entity/BakeArticlesBakeTag.php';
767+
$this->Task->expects($this->at(3))
768+
->method('createFile')
769+
->with($filename);
874770

875-
$this->Task->execute();
876-
}
771+
$filename = '/my/path/Entity/BakeComment.php';
772+
$this->Task->expects($this->at(5))
773+
->method('createFile')
774+
->with($filename);
877775

878-
/**
879-
* test using bake interactively with a table that does not exist.
880-
*
881-
* @return void
882-
*/
883-
public function testForcedExecuteWithNonExistantTableName() {
884-
$this->markTestIncomplete('Not done here yet');
885-
$this->Task->connection = 'test';
886-
$this->Task->path = '/my/path/';
776+
$filename = '/my/path/Entity/CategoryThread.php';
777+
$this->Task->expects($this->at(7))
778+
->method('createFile')
779+
->with($filename);
887780

888-
$this->Task->expects($this->any())->method('in')
889-
->will($this->onConsecutiveCalls(
890-
'Foobar', // Or type in the name of the model
891-
'y', // Do you want to use this table
892-
'y', // Doesn't exist, continue anyway?
893-
'id', // Primary key
894-
'y' // Looks good?
895-
));
781+
$filename = '/my/path/Entity/NumberTree.php';
782+
$this->Task->expects($this->at(9))
783+
->method('createFile')
784+
->with($filename);
896785

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

0 commit comments

Comments
 (0)