Skip to content

Commit

Permalink
Merge pull request #659 from cakephp/bugfix/controller-test-case
Browse files Browse the repository at this point in the history
Fix baking controller tests without models.
  • Loading branch information
markstory authored Mar 16, 2020
2 parents 4e3e688 + 3f8a377 commit d47ec5f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use ReflectionClass;
use UnexpectedValueException;

/**
* Command class for generating test files.
Expand Down Expand Up @@ -488,7 +489,14 @@ protected function _processModel(Table $subject): void
*/
protected function _processController(Controller $subject): void
{
$models = [$subject->loadModel()->getAlias()];
try {
$model = $subject->loadModel();
} catch (UnexpectedValueException $exception) {
// No fixtures needed or possible
return;
}

$models = [$model->getAlias()];
foreach ($models as $model) {
[, $model] = pluginSplit($model);
$this->_processModel($subject->{$model});
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Command/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@ public function testBakeControllerTest()
$this->assertSameAsFile(__FUNCTION__ . '.php', file_get_contents($this->generatedFiles[0]));
}

/**
* test baking controller test files
*
* @return void
*/
public function testBakeControllerWithoutModelTest()
{
$this->generatedFiles = [
ROOT . 'tests/TestCase/Controller/NoModelControllerTest.php',
];
$this->exec('bake test controller NoModelController');

$this->assertExitCode(Command::CODE_SUCCESS);
$this->assertFilesExist($this->generatedFiles);
$this->assertSameAsFile(__FUNCTION__ . '.php', file_get_contents($this->generatedFiles[0]));
}

/**
* test baking controller test files
*
Expand Down
28 changes: 28 additions & 0 deletions tests/comparisons/Test/testBakeControllerWithoutModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace Bake\Test\App\Test\TestCase\Controller;

use Bake\Test\App\Controller\NoModelController;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;

/**
* Bake\Test\App\Controller\NoModelController Test Case
*
* @uses \Bake\Test\App\Controller\NoModelController
*/
class NoModelControllerTest extends TestCase
{
use IntegrationTestTrait;

/**
* Test index method
*
* @return void
*/
public function testIndex(): void
{
$this->markTestIncomplete('Not implemented yet.');
}
}
38 changes: 38 additions & 0 deletions tests/test_app/App/Controller/NoModelController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\App\Controller;

/**
* NoModelController class
*/
class NoModelController extends AppController
{
/**
* @var string
*/
protected $modelClass = '';

/**
* Index method.
*
* @return void
*/
public function index()
{
$this->set('test', 'value');
}
}

0 comments on commit d47ec5f

Please sign in to comment.