Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ without having to manually edit each one:
```bash
UPDATE_TEST_COMPARISON_FILES=1 vendor/bin/phpunit
```

Generated PHP registered by command tests is checked with `php -l` before cleanup.
A dedicated integration test also bakes classes, a complete CRUD stack, and plugin
skeletons and checks them against the CakePHP coding standard and the project's
Rector rules. PHPCS uses the generated files' intended application paths so
test-fixture exclusions cannot hide violations. Rector runs in dry-run mode and
fails if it proposes changes; generated files are never rewritten by the checks:

```bash
vendor/bin/phpunit tests/TestCase/GeneratedCodeTest.php
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"require-dev": {
"cakephp/cakephp-codesniffer": "^6.0",
"cakephp/debug_kit": "^6.0",
"phpunit/phpunit": "^13.0"
"phpunit/phpunit": "^13.0",
"rector/rector": "~2.7.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,9 @@ public function generateUses(string $type, string $fullClassName): array
if ($type === 'Behavior') {
$uses[] = 'Cake\ORM\Table';
}
$uses[] = $fullClassName;
if (!in_array($type, ['Controller', 'Command'], true)) {
$uses[] = $fullClassName;
}

return $uses;
}
Expand Down
1 change: 0 additions & 1 deletion templates/bake/Controller/component.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace: "#{namespace}\\Controller\\Component",
classImports: [
'Cake\\Controller\\Component',
'Cake\\Controller\\ComponentRegistry',
],
}) }}

Expand Down
6 changes: 5 additions & 1 deletion templates/bake/Controller/controller.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
#}
{% set classImports = ((plugin or prefix) ? ["#{baseNamespace}\\Controller\\AppController"] : [])|merge(routingImports) %}
{% if 'delete' in actions %}
{%~ set classImports = classImports|merge(['Cake\\Http\\Response']) %}
{% endif %}
{{ element('Bake.file_header', {
namespace: "#{namespace}\\Controller#{prefix}",
classImports: ((plugin or prefix) ? ["#{baseNamespace}\\Controller\\AppController"] : [])|merge(routingImports),
classImports: classImports,
}) }}
{% set has_login = 'login' in actions and Bake.hasPlugin('Authentication') %}
{% set include_initialize = components or helpers or has_login %}
Expand Down
1 change: 0 additions & 1 deletion templates/bake/Model/behavior.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace: "#{namespace}\\Model\\Behavior",
classImports: [
'Cake\\ORM\\Behavior',
'Cake\\ORM\\Table',
],
}) }}

Expand Down
9 changes: 8 additions & 1 deletion templates/bake/Model/table.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
#}
{% set annotations = DocBlock.buildTableAnnotations(associations, associationInfo, behaviors, entity, namespace) %}
{% set generatedFunctions = ['initialize'] %}
{% set classImports = ['Cake\\ORM\\Table'] %}
{% if validation %}
{%~ set classImports = classImports|merge(['Cake\\Validation\\Validator']) %}
{% endif %}
{% if rulesChecker %}
{%~ set classImports = classImports|merge(['Cake\\ORM\\RulesChecker']) %}
{% endif %}
{{ element('Bake.file_header', {
namespace: fileBuilder.namespace,
classImports: fileBuilder.classImports(['Cake\\ORM\\Query\\SelectQuery', 'Cake\\ORM\\RulesChecker', 'Cake\\ORM\\Table', 'Cake\\Validation\\Validator']),
classImports: fileBuilder.classImports(classImports),
}) }}

{{ DocBlock.classDescription(name, 'Model', annotations)|raw }}
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/Plugin/src/Plugin.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class {{ name }}Plugin extends BasePlugin
$routes->plugin(
'{{ plugin }}',
['path' => '/{{ routePath }}'],
function (RouteBuilder $builder) {
function (RouteBuilder $builder): void {
// Add custom routes here
{% if not attributeRouting %}

$builder->fallbacks();
{% endif %}
}
},
);
parent::routes($routes);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/bake/Plugin/tests/bootstrap.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ if (file_exists($root . '/config/bootstrap.php')) {
use Cake\TestSuite\Fixture\SchemaLoader;

// Load a schema dump file.
(new SchemaLoader())->loadSqlFiles('tests/schema.sql', 'test');
new SchemaLoader()->loadSqlFiles('tests/schema.sql', 'test');
2 changes: 1 addition & 1 deletion templates/bake/View/cell.twig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class {{ name }}Cell extends Cell
*
* @return void
*/
public function display()
public function display(): void
{
}
}
1 change: 0 additions & 1 deletion templates/bake/View/helper.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace: "#{namespace}\\View\\Helper",
classImports: [
'Cake\\View\\Helper',
'Cake\\View\\View',
],
}) }}

Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/delete.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
/**
* Delete method
*
* @param string|null $id {{ singularHumanName }} id.
* @param string|int $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
{% if attributeRouting %}
#[Route('/delete/{id}')]
{% endif %}
public function delete($id = null)
public function delete(int|string $id): ?Response
{
$this->request->allowMethod(['post', 'delete']);
${{ singularName }} = $this->{{ currentModelName }}->get($id);
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
/**
* Edit method
*
* @param string|null $id {{ singularHumanName }} id.
* @param string|int $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
{% if attributeRouting %}
#[Route('/edit/{id}')]
{% endif %}
public function edit($id = null)
public function edit(int|string $id)
{
${{ singularName }} = $this->{{ currentModelName }}->get($id, contain: {{ Bake.exportArray(belongsToMany)|raw }});
{% if Bake.hasPlugin('Authorization') %}
Expand Down
4 changes: 2 additions & 2 deletions templates/bake/element/Controller/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
/**
* View method
*
* @param string|null $id {{ singularHumanName }} id.
* @param string|int $id {{ singularHumanName }} id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
{% if attributeRouting %}
#[Route('/view/{id}')]
{% endif %}
public function view($id = null)
public function view(int|string $id)
{
${{ singularName }} = $this->{{ currentModelName }}->get($id, contain: {{ Bake.exportArray(allAssociations)|raw }});
{% if Bake.hasPlugin('Authorization') %}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Command/AllCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function testExecuteWithPrefix(): void
$path . 'Controller/Admin/ProductsController.php',
);
$this->assertFileContains(
'use Bake\Test\App\Controller\Admin\ProductsController;',
'@link \Bake\Test\App\Controller\Admin\ProductsController',
$testsPath . 'TestCase/Controller/Admin/ProductsControllerTest.php',
);
$this->assertOutputContains('Bake All complete');
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Command/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,10 @@ public function testGenerateUses(): void
$this->assertEquals($expected, $result);

$result = $command->generateUses('Controller', 'App\Controller\PostsController');
$expected = [
'App\Controller\PostsController',
];
$this->assertEquals($expected, $result);
$this->assertSame([], $result);

$result = $command->generateUses('Command', 'App\Command\ExampleCommand');
$this->assertSame([], $result);

$result = $command->generateUses('Helper', 'App\View\Helper\FormHelper');
$expected = [
Expand Down
Loading
Loading