diff --git a/src/Shell/Task/ModelTask.php b/src/Shell/Task/ModelTask.php index ce65b0ae7c5..baded30546c 100644 --- a/src/Shell/Task/ModelTask.php +++ b/src/Shell/Task/ModelTask.php @@ -646,6 +646,14 @@ public function bakeTable($model, array $data = []) { $filename = $path . 'Table' . DS . $name . 'Table.php'; $this->out("\n" . sprintf('Baking table class for %s...', $name), 1, Shell::QUIET); $this->createFile($filename, $out); + + // Work around composer caching that classes/files do not exist. + // Check for the file as it might not exist in tests. + if (file_exists($filename)) { + require_once($filename); + } + TableRegistry::clear(); + $emptyFile = $path . 'Table' . DS . 'empty'; $this->_deleteEmptyFile($emptyFile); return $out; diff --git a/src/Shell/Task/ViewTask.php b/src/Shell/Task/ViewTask.php index dace404bcda..4e2d68c23a0 100644 --- a/src/Shell/Task/ViewTask.php +++ b/src/Shell/Task/ViewTask.php @@ -357,6 +357,11 @@ public function getContent($action, $vars = null) { $vars = $this->_loadController(); } + if (empty($vars['primaryKey'])) { + $this->error('Cannot generate views for models with no primary key'); + return false; + } + $this->Template->set('action', $action); $this->Template->set('plugin', $this->plugin); $this->Template->set($vars); diff --git a/tests/TestCase/Shell/Task/ViewTaskTest.php b/tests/TestCase/Shell/Task/ViewTaskTest.php index 4d0907e3dae..3f67970b5b4 100644 --- a/tests/TestCase/Shell/Task/ViewTaskTest.php +++ b/tests/TestCase/Shell/Task/ViewTaskTest.php @@ -102,7 +102,7 @@ public function setUp() { parent::setUp(); Configure::write('App.namespace', 'TestApp'); - $this->_setupTask(['in', 'err', 'createFile', '_stop']); + $this->_setupTask(['in', 'err', 'error', 'createFile', '_stop']); TableRegistry::get('ViewTaskComments', [ 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable', @@ -312,6 +312,33 @@ public function testGetContent() { $this->assertContains('$testViewModel->body', $result); } +/** + * Test getContent with no pk + * + * @return void + */ + public function testGetContentWithNoPrimaryKey() { + $vars = array( + 'modelClass' => 'TestViewModel', + 'schema' => TableRegistry::get('ViewTaskComments')->schema(), + 'primaryKey' => [], + 'displayField' => 'name', + 'singularVar' => 'testViewModel', + 'pluralVar' => 'testViewModels', + 'singularHumanName' => 'Test View Model', + 'pluralHumanName' => 'Test View Models', + 'fields' => ['id', 'name', 'body'], + 'associations' => [], + 'keyFields' => [], + ); + $this->Task->expects($this->once()) + ->method('error') + ->with($this->stringContains('Cannot generate views for models')); + + $result = $this->Task->getContent('view', $vars); + $this->assertFalse($result); + } + /** * test getContent() using a routing prefix action. *