Skip to content

Commit

Permalink
Merge pull request #919 from cakephp/3.x-merge
Browse files Browse the repository at this point in the history
2.x => 3.x merge and fix tests
  • Loading branch information
markstory committed Apr 30, 2023
2 parents 18c9dab + 8605c9d commit 2c90e60
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/stale@v7
- uses: actions/stale@v8
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is stale because it has been open for 120 days with no activity. Remove the `stale` label or comment or this will be closed in 15 days'
Expand Down
2 changes: 1 addition & 1 deletion docs/en/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ dependencies in your templates you can include template overrides in your
application templates. These overrides work similar to overriding other plugin
templates.

#. Create a new directory **/templates/plugin/Bake/**.
#. Create a new directory **/templates/plugin/Bake/bake/**.
#. Copy any templates you want to override from
**vendor/cakephp/bake/templates/bake/** to matching files in your
application.
Expand Down
4 changes: 4 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Bake">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />
<arg value="ns"/>

<file>src/</file>
<file>tests/</file>

<rule ref="CakePHP" />

Expand Down
1 change: 1 addition & 0 deletions src/Command/BakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Cake\Event\Event;
use Cake\Event\EventManager;
use InvalidArgumentException;
use function Cake\Core\pluginSplit;

/**
* Base class for commands that bake can use.
Expand Down
16 changes: 12 additions & 4 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use function Cake\Core\pluginSplit;

/**
* Command for generating model files.
Expand Down Expand Up @@ -233,7 +234,7 @@ public function getAssociations(Table $table, Arguments $args, ConsoleIo $io): a
];

$primary = $table->getPrimaryKey();
$associations = $this->findBelongsTo($table, $associations);
$associations = $this->findBelongsTo($table, $associations, $args);

if (is_array($primary) && count($primary) > 1) {
$io->warning(
Expand Down Expand Up @@ -329,9 +330,10 @@ public function getAssociationInfo(Table $table): array
*
* @param \Cake\ORM\Table $model Database\Table instance of table being generated.
* @param array $associations Array of in progress associations
* @param \Cake\Console\Arguments|null $args CLI arguments
* @return array Associations with belongsTo added in.
*/
public function findBelongsTo(Table $model, array $associations): array
public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array
{
$schema = $model->getSchema();
foreach ($schema->columns() as $fieldName) {
Expand Down Expand Up @@ -362,11 +364,13 @@ public function findBelongsTo(Table $model, array $associations): array
get_class($associationTable) === Table::class &&
!in_array(Inflector::tableize($tmpModelName), $tables, true)
) {
$allowAliasRelations = $args && $args->getOption('skip-relation-check');
$found = $this->findTableReferencedBy($schema, $fieldName);
if (!$found) {
if ($found) {
$tmpModelName = Inflector::camelize($found);
} elseif (!$allowAliasRelations) {
continue;
}
$tmpModelName = Inflector::camelize($found);
}
$assoc = [
'alias' => $tmpModelName,
Expand Down Expand Up @@ -1319,6 +1323,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
])->addOption('no-fixture', [
'boolean' => true,
'help' => 'Do not generate a test fixture skeleton.',
])->addOption('skip-relation-check', [
'boolean' => true,
'help' => 'Generate relations for all "example_id" fields'
. ' without checking the database if a table "examples" exists.',
])->setEpilog(
'Omitting all arguments and options will list the table names you can generate models for.'
);
Expand Down
3 changes: 2 additions & 1 deletion src/Command/PluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Cake\Utility\Filesystem;
use Cake\Utility\Inflector;
use RuntimeException;
use function Cake\Core\env;

/**
* The Plugin Command handles creating an empty plugin, ready to be used
Expand Down Expand Up @@ -384,7 +385,7 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar
])->addOption('theme', [
'short' => 't',
'help' => 'The theme to use when baking code.',
'default' => Configure::read('Bake.theme') ?? '',
'default' => Configure::read('Bake.theme') ?: null,
'choices' => $this->_getBakeThemes(),
]);

Expand Down
1 change: 1 addition & 0 deletions src/Command/TemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Cake\View\Exception\MissingTemplateException;
use Exception;
use RuntimeException;
use function Cake\Core\namespaceSplit;

/**
* Task class for creating view template files.
Expand Down
2 changes: 2 additions & 0 deletions src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Cake\Utility\Inflector;
use ReflectionClass;
use UnexpectedValueException;
use function Cake\Core\namespaceSplit;
use function Cake\Core\pluginSplit;

/**
* Command class for generating test files.
Expand Down
1 change: 1 addition & 0 deletions src/Utility/Model/AssociationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use Exception;
use function Cake\Core\namespaceSplit;

/**
* Utility class to filter Model Table associations
Expand Down
1 change: 1 addition & 0 deletions src/View/BakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventInterface;
use Cake\TwigView\View\TwigView;
use function Cake\Core\pluginSplit;

class BakeView extends TwigView
{
Expand Down
5 changes: 3 additions & 2 deletions src/View/Helper/BakeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
use Cake\ORM\Table;
use Cake\Utility\Inflector;
use Cake\View\Helper;
use function pluginSplit;
use function Cake\Collection\collection;
use function Cake\Core\pluginSplit;

/**
* Bake helper
Expand Down Expand Up @@ -280,7 +281,7 @@ public function getViewFieldsData(array $fields, SchemaInterface $schema, array
$immediateAssociations = $associations['BelongsTo'];
$associationFields = collection($fields)
->map(function ($field) use ($immediateAssociations) {
foreach ($immediateAssociations as $alias => $details) {
foreach ($immediateAssociations as $details) {
if ($field === $details['foreignKey']) {
return [$field => $details];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/CodeGen/FileBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TestTable{}
);

$this->expectException(ParseException::class);
$builder = new FileBuilder($this->io, 'MyOtherApp\Model', $file);
new FileBuilder($this->io, 'MyOtherApp\Model', $file);
}

public function testUses(): void
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/Command/MailerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function testMainPlugin()
{
$this->_loadTestPlugin('TestBake');
$path = Plugin::path('TestBake');
$templatePath = Plugin::templatePath('TestBake');

$this->generatedFiles = [
$path . 'src/Mailer/ExampleMailer.php',
Expand Down
53 changes: 53 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,59 @@ public function testGetAssociationsAddAssociationIfTableExist()
$this->assertEquals($expected, $result);
}

/**
* Test that association generation adds `Anythings` association for `anything_id` field
* when using `--skip-relation-check` option, even if no db table exists
*
* @return void
*/
public function testGetAssociationsAddAssociationIfNoTableExistButAliasIsAllowed()
{
$items = $this->getTableLocator()->get('TodoItems');

$items->setSchema($items->getSchema()->addColumn('anything_id', ['type' => 'integer']));
$command = new ModelCommand();
$command->connection = 'test';

$args = new Arguments([], ['skip-relation-check' => true], []);
$io = $this->createMock(ConsoleIo::class);
$result = $command->getAssociations($items, $args, $io);
$expected = [
'belongsTo' => [
[
'alias' => 'Users',
'foreignKey' => 'user_id',
'joinType' => 'INNER',
],
[
'alias' => 'Anythings',
'foreignKey' => 'anything_id',
],
],
'hasMany' => [
[
'alias' => 'TodoTasks',
'foreignKey' => 'todo_item_id',
],
],
'belongsToMany' => [
[
'alias' => 'TodoLabels',
'foreignKey' => 'todo_item_id',
'joinTable' => 'todo_items_todo_labels',
'targetForeignKey' => 'todo_label_id',
],
],
'hasOne' => [
[
'alias' => 'TodoReminders',
'foreignKey' => 'todo_item_id',
],
],
];
$this->assertEquals($expected, $result);
}

/**
* Test that association generation ignores `_id` fields
*
Expand Down
23 changes: 17 additions & 6 deletions tests/TestCase/Command/PluginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class PluginCommandTest extends TestCase
{
protected $testAppFile = APP . 'Application.php';

protected $pluginsPath = TMP . 'plugin_task' . DS;

/**
* setUp method
*
Expand All @@ -46,12 +48,11 @@ public function setUp(): void
$this->setAppNamespace('Bake\Test\App');

// Output into a safe place.
$path = TMP . 'plugin_task' . DS;
Configure::write('App.paths.plugins', [$path]);
Configure::write('App.paths.plugins', [$this->pluginsPath]);

// Create the test output path
if (!file_exists($path)) {
mkdir($path, 0777, true);
if (!file_exists($this->pluginsPath)) {
mkdir($this->pluginsPath, 0777, true);
}

if (file_exists(APP . 'Application.php.bak')) {
Expand All @@ -69,7 +70,7 @@ public function setUp(): void
public function tearDown(): void
{
$fs = new Filesystem();
$fs->deleteDir(TMP . 'plugin_task');
$fs->deleteDir($this->pluginsPath);

if (file_exists(APP . 'Application.php.bak')) {
rename(APP . 'Application.php.bak', APP . 'Application.php');
Expand All @@ -90,6 +91,16 @@ public function testMainBakePluginContents()
$this->assertPluginContents('SimpleExample');
}

public function testBakingWithNonExistentPluginsDir()
{
$fs = new Filesystem();
$fs->deleteDir($this->pluginsPath);

$this->exec('bake plugin SimpleExample', ['y', 'n']);
$this->assertExitCode(CommandInterface::CODE_SUCCESS);
$this->assertPluginContents('SimpleExample');
}

/**
* test creating a plugin with a custom app namespace.
*
Expand Down Expand Up @@ -212,7 +223,7 @@ public function testFindPathNonExistent()
$result = $command->findPath($paths, $io);

$this->assertNull($result, 'no return');
$this->assertSame(TMP . 'plugin_task' . DS, $command->path);
$this->assertSame($this->pluginsPath, $command->path);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// phpcs:ignoreFile

use function Cake\Core\env;
use Bake\BakePlugin;
use Cake\Cache\Cache;
use Cake\Core\Configure;
Expand All @@ -38,7 +39,6 @@
unset($findRoot);
chdir($root);

require_once 'vendor/cakephp/cakephp/src/basics.php';
require_once 'vendor/autoload.php';

define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS);
Expand Down
2 changes: 1 addition & 1 deletion tests/comparisons/Template/testBakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</tr>
<tr>
<th><?= __('Profile') ?></th>
<td><?= $author->has('profile') ? $this->Html->link($author->profile->id, ['controller' => 'Profiles', 'action' => 'view', $author->profile->id]) : '' ?></td>
<td><?= $author->has('profile') ? $this->Html->link($author->profile->nick, ['controller' => 'Profiles', 'action' => 'view', $author->profile->id]) : '' ?></td>
</tr>
<tr>
<th><?= __('Id') ?></th>
Expand Down

0 comments on commit 2c90e60

Please sign in to comment.