Skip to content

Commit 87ab223

Browse files
committed
feature #105 Added a maker to generate empty fixture classes (javiereguiluz, weaverryan)
This PR was merged into the 1.0-dev branch. Discussion ---------- Added a maker to generate empty fixture classes This fixes #84 following the new best practices recommended for Doctrine fixtures (https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html). Commits ------- 32cbedb no space e4efd5b tweak 6ecad09 fixtures -> fixtures 359c44d Minor tweaks to make:fixtures 35d79e4 Adding missing use b9b731c Added an argument to set the fixtures class name ec5dffc Fixed issues cbe6573 Added the missing command help 66fb43d Added a maker to generate empty fixture classes
2 parents 4162f12 + 32cbedb commit 87ab223

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/Maker/MakeFixtures.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Maker;
13+
14+
use Doctrine\Bundle\FixturesBundle\Fixture;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
17+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
18+
use Symfony\Bundle\MakerBundle\InputConfiguration;
19+
use Symfony\Bundle\MakerBundle\Str;
20+
use Symfony\Bundle\MakerBundle\Validator;
21+
use Symfony\Component\Console\Command\Command;
22+
use Symfony\Component\Console\Input\InputArgument;
23+
use Symfony\Component\Console\Input\InputInterface;
24+
25+
/**
26+
* @author Javier Eguiluz <[email protected]>
27+
* @author Ryan Weaver <[email protected]>
28+
*/
29+
final class MakeFixtures extends AbstractMaker
30+
{
31+
public static function getCommandName(): string
32+
{
33+
return 'make:fixtures';
34+
}
35+
36+
public function configureCommand(Command $command, InputConfiguration $inputConf)
37+
{
38+
$command
39+
->setDescription('Creates a new class to load Doctrine fixtures')
40+
->addArgument('fixtures-class', InputArgument::OPTIONAL, 'The class name of the fixtures to create (e.g. <fg=yellow>AppFixtures</>)')
41+
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeFixture.txt'))
42+
;
43+
}
44+
45+
public function getParameters(InputInterface $input): array
46+
{
47+
$fixturesClassName = Str::asClassName($input->getArgument('fixtures-class'));
48+
Validator::validateClassName($fixturesClassName);
49+
50+
return [
51+
'fixtures_class_name' => $fixturesClassName,
52+
];
53+
}
54+
55+
public function getFiles(array $params): array
56+
{
57+
return [
58+
__DIR__.'/../Resources/skeleton/doctrine/Fixtures.tpl.php' => 'src/DataFixtures/'.$params['fixtures_class_name'].'.php',
59+
];
60+
}
61+
62+
public function writeSuccessMessage(array $params, ConsoleStyle $io)
63+
{
64+
parent::writeSuccessMessage($params, $io);
65+
66+
$io->text([
67+
'Next: Open your new fixtures class and start customizing it.',
68+
sprintf('Load your fixtures by running: <comment>php %s doctrine:fixtures:load</comment>', $_SERVER['PHP_SELF']),
69+
'Docs: <fg=yellow>https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html</>',
70+
]);
71+
}
72+
73+
public function configureDependencies(DependencyBuilder $dependencies)
74+
{
75+
$dependencies->addClassDependency(
76+
Column::class,
77+
'doctrine'
78+
);
79+
$dependencies->addClassDependency(
80+
Fixture::class,
81+
'orm-fixtures'
82+
);
83+
}
84+
}

src/Resources/config/makers.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<tag name="maker.command" />
2626
</service>
2727

28+
<service id="maker.maker.make_fixtures" class="Symfony\Bundle\MakerBundle\Maker\MakeFixtures">
29+
<tag name="maker.command" />
30+
</service>
31+
2832
<service id="maker.maker.make_form" class="Symfony\Bundle\MakerBundle\Maker\MakeForm">
2933
<tag name="maker.command" />
3034
</service>

src/Resources/help/MakeFixture.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The <info>%command.name%</info> command generates a new Doctrine fixtures class.
2+
3+
<info>php %command.full_name% AppFixtures</info>
4+
5+
If the argument is missing, the command will ask for a class interactively.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace App\DataFixtures;
4+
5+
use Doctrine\Bundle\FixturesBundle\Fixture;
6+
use Doctrine\Common\Persistence\ObjectManager;
7+
8+
class <?= $fixtures_class_name ?> extends Fixture
9+
{
10+
public function load(ObjectManager $manager)
11+
{
12+
// $product = new Product();
13+
// $manager->persist($product);
14+
15+
$manager->flush();
16+
}
17+
}

tests/Maker/FunctionalTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Bundle\MakerBundle\Maker\MakeCommand;
1313
use Symfony\Bundle\MakerBundle\Maker\MakeController;
1414
use Symfony\Bundle\MakerBundle\Maker\MakeEntity;
15+
use Symfony\Bundle\MakerBundle\Maker\MakeFixtures;
1516
use Symfony\Bundle\MakerBundle\Maker\MakeForm;
1617
use Symfony\Bundle\MakerBundle\Maker\MakeFunctionalTest;
1718
use Symfony\Bundle\MakerBundle\Maker\MakeMigration;
@@ -111,6 +112,16 @@ public function getCommandTests()
111112
->addPostMakeCommand('./bin/console doctrine:schema:create --env=test')
112113
];
113114

115+
yield 'fixtures' => [MakerTestDetails::createTest(
116+
$this->getMakerInstance(MakeFixtures::class),
117+
[
118+
'AppFixtures'
119+
])
120+
->assert(function(string $output, string $directory) {
121+
$this->assertContains('created: src/DataFixtures/AppFixtures.php', $output);
122+
})
123+
];
124+
114125
yield 'form' => [MakerTestDetails::createTest(
115126
$this->getMakerInstance(MakeForm::class),
116127
[

0 commit comments

Comments
 (0)