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
15 changes: 15 additions & 0 deletions docs/en/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ For non-conventional relations, you can use references in constraints or foreign
->addForeignKey('shipping_country_id', 'countries', 'cid')
```

### Fixture Factories

When a Fixture Factories plugin such as
[vierge-noire/cakephp-fixture-factories](https://github.com/vierge-noire/cakephp-fixture-factories)
or [dereuromark/cakephp-fixture-factories](https://github.com/dereuromark/cakephp-fixture-factories)
is installed, `bake model` does not generate a fixture class, and baked test
cases have no `$fixtures` property. Bake the factory with that plugin's own command:

```bash
bin/cake bake fixture_factory Articles
```

`bake fixture` still generates a fixture class when you ask for one, and
`bake test` honors an explicit `--fixtures` list.

## Bake Enums

You can use Bake to generate [backed enums](https://www.php.net/manual/en/language.enumerations.backed.php) for use in your models.
Expand Down
11 changes: 11 additions & 0 deletions src/Command/BakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ public function createTemplateRenderer(): TemplateRenderer
return $renderer;
}

/**
* Check if a Fixture Factories plugin is installed.
*
* @return bool
*/
protected function hasFixtureFactories(): bool
{
return class_exists('CakephpFixtureFactories\Plugin')
|| class_exists('CakephpFixtureFactories\CakephpFixtureFactoriesPlugin');
}

/**
* Delete empty file in a given path
*
Expand Down
6 changes: 6 additions & 0 deletions src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,12 @@ public function bakeFixture(
if ($args->getOption('no-fixture')) {
return;
}
if ($this->hasFixtureFactories()) {
$io->info('Fixture Factories plugin detected - skipping fixture generation.');
$io->info(sprintf('Bake a factory instead: `bin/cake bake fixture_factory %s`', $className));

return;
}
$fixture = new FixtureCommand();
$fixtureArgs = new Arguments(
[$className],
Expand Down
17 changes: 3 additions & 14 deletions src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ public function bake(string $type, string $className, Arguments $args, ConsoleIo
$hasFixtureFactories = $this->hasFixtureFactories();

if (!$args->getOption('no-fixture')) {
if ($hasFixtureFactories) {
$io->info('Fixture Factories plugin detected - skipping fixture property generation.');
} elseif ($args->getOption('fixtures')) {
if ($args->getOption('fixtures')) {
$fixtures = array_map('trim', explode(',', (string)$args->getOption('fixtures')));
$this->_fixtures = array_filter($fixtures);
} elseif ($hasFixtureFactories) {
$io->info('Fixture Factories plugin detected - skipping fixture property generation.');
} elseif ($this->typeCanDetectFixtures($type) && class_exists($fullClassName)) {
$io->out('Bake is detecting possible fixtures...');
$testSubject = $this->buildTestSubject($type, $fullClassName);
Expand Down Expand Up @@ -373,17 +373,6 @@ public function bake(string $type, string $className, Arguments $args, ConsoleIo
return false;
}

/**
* Check if the CakePHP Fixture Factories plugin is available
*
* @return bool
*/
protected function hasFixtureFactories(): bool
{
return class_exists('CakephpFixtureFactories\Plugin')
|| class_exists('CakephpFixtureFactories\CakephpFixtureFactoriesPlugin');
}

/**
* Checks whether the chosen type can find its own fixtures.
* Currently only model, and controller are supported
Expand Down
2 changes: 1 addition & 1 deletion templates/bake/tests/test_case.twig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class {{ className }}Test extends TestCase
{%~ endfor %}
{% endif %}

{%- if fixtures and not (hasFixtureFactories|default(false)) %}
{%- if fixtures %}
/**
* Fixtures
*
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,32 @@ public function testBakeFixtureDisabled(): void
$this->assertFileDoesNotExist(ROOT . 'tests/TestCase/Model/Table/TodoItemsTableTest.php');
}

/**
* No fixture is baked when a Fixture Factories plugin is installed.
*
* @return void
*/
public function testBakeFixtureWithFixtureFactories(): void
{
$this->generatedFiles = [
ROOT . 'tests/Fixture/TodoItemsFixture.php',
];
$command = new class extends ModelCommand {
protected function hasFixtureFactories(): bool
{
return true;
}
};
$out = new StubConsoleOutput();
$io = new ConsoleIo($out, new StubConsoleOutput(), new StubConsoleInput([]));

$command->bakeFixture('TodoItems', 'todo_items', new Arguments([], [], []), $io);

$this->assertFileDoesNotExist($this->generatedFiles[0]);
$this->assertStringContainsString('skipping fixture generation', $out->output());
$this->assertStringContainsString('bin/cake bake fixture_factory TodoItems', $out->output());
}

/**
* Ensure that the test object is correctly called.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/TestCase/Command/TestCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use Bake\Test\App\Model\Table\CategoryThreadsTable;
use Bake\Test\TestCase\TestCase;
use Cake\Console\CommandInterface;
use Cake\Console\ConsoleIo;
use Cake\Console\TestSuite\StubConsoleInput;
use Cake\Console\TestSuite\StubConsoleOutput;
use Cake\Core\Plugin;
use Cake\Http\ServerRequest as Request;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -386,6 +389,62 @@ public function testBakeNoFixtureParam(): void
$this->assertSameAsFile(__FUNCTION__ . '.php', file_get_contents($this->generatedFiles[0]));
}

/**
* Fixtures are left out when a Fixture Factories plugin is installed.
*
* @return void
*/
public function testBakeWithFixtureFactories(): void
{
$this->generatedFiles = [
ROOT . 'tests/TestCase/Model/Table/AuthorsTableTest.php',
];
$out = new StubConsoleOutput();
$io = new ConsoleIo($out, new StubConsoleOutput(), new StubConsoleInput([]));

$result = $this->fixtureFactoriesCommand()->run(['table', 'Authors'], $io);

$this->assertSame(CommandInterface::CODE_SUCCESS, $result);
$this->assertStringContainsString('Fixture Factories plugin detected', $out->output());
$this->assertStringNotContainsString('$fixtures', (string)file_get_contents($this->generatedFiles[0]));
}

/**
* An explicit fixture list is used even if a Fixture Factories plugin is installed.
*
* @return void
*/
public function testBakeFixturesParamWithFixtureFactories(): void
{
$this->generatedFiles = [
ROOT . 'tests/TestCase/Model/Table/AuthorsTableTest.php',
];
$out = new StubConsoleOutput();
$io = new ConsoleIo($out, new StubConsoleOutput(), new StubConsoleInput([]));

$result = $this->fixtureFactoriesCommand()->run(
['table', 'Authors', '--fixtures', 'app.Posts,app.Comments,app.Users'],
$io,
);

$this->assertSame(CommandInterface::CODE_SUCCESS, $result);
$this->assertStringNotContainsString('Fixture Factories plugin detected', $out->output());
$this->assertSameAsFile('testBakeFixturesParam.php', file_get_contents($this->generatedFiles[0]));
}

/**
* @return \Bake\Command\TestCommand
*/
protected function fixtureFactoriesCommand(): TestCommand
{
return new class extends TestCommand {
protected function hasFixtureFactories(): bool
{
return true;
}
};
}

/**
* Test baking a test for a cell.
*
Expand Down
Loading