-
-
Notifications
You must be signed in to change notification settings - Fork 93
feat: Introduce #[AsFixture]
attribute and foundry:load-fixture
command
#903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e14022c
refactor: reorganize ZenstruckFoundryBundle::loadExtension()
nikophil 1038d39
feat: add LoadStoryCommand
nikophil 34ce1b4
feat: actually load story form the command
nikophil 56b1202
feat: throw when a name collision occur with fixture names
nikophil 6c1306e
feat: load fixtures in group
nikophil d953f87
feat: ensure fixtures is only loaded once
nikophil 80c0a6f
feat: restore db before fixtures command
nikophil 23846e5
feat: add output and interactivity
nikophil fddd6d5
feat(story as fixture): if only one fixture, load it by default
nikophil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Attribute; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsFixture | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
/** @var list<string> */ | ||
public readonly array $groups = [], | ||
) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Command; | ||
|
||
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Exception\LogicException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Persistence\ResetDatabase\BeforeFirstTestResetter; | ||
use Zenstruck\Foundry\Story; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class LoadStoryCommand extends Command | ||
{ | ||
public function __construct( | ||
/** @var array<string, class-string<Story>> */ | ||
private readonly array $stories, | ||
/** @var array<string, array<string, class-string<Story>>> */ | ||
private readonly array $groupedStories, | ||
/** @var iterable<BeforeFirstTestResetter> */ | ||
private iterable $databaseResetters, | ||
private KernelInterface $kernel, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('name', InputArgument::OPTIONAL, 'The name of the story to load.') | ||
->addOption('append', 'a', InputOption::VALUE_NONE, 'Skip resetting database and append data to the existing database.') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
if (0 === \count($this->stories)) { | ||
throw new LogicException('No story as fixture available: add attribute #[AsFixture] to your story classes before running this command.'); | ||
} | ||
|
||
$io = new SymfonyStyle($input, $output); | ||
|
||
if (!$input->getOption('append')) { | ||
$this->resetDatabase(); | ||
} | ||
|
||
$stories = []; | ||
|
||
if (null === ($name = $input->getArgument('name'))) { | ||
if (1 === \count($this->stories)) { | ||
$name = \array_keys($this->stories)[0]; | ||
} else { | ||
$storyNames = \array_keys($this->stories); | ||
if (\count($this->groupedStories) > 0) { | ||
$storyNames[] = '(choose a group of stories...)'; | ||
} | ||
$name = $io->choice('Choose a story to load:', $storyNames); | ||
} | ||
|
||
if (!isset($this->stories[$name])) { | ||
$groupsNames = \array_keys($this->groupedStories); | ||
$name = $io->choice('Choose a group of stories:', $groupsNames); | ||
} | ||
} | ||
|
||
if (isset($this->stories[$name])) { | ||
$io->comment("Loading story with name \"{$name}\"..."); | ||
$stories = [$name => $this->stories[$name]]; | ||
} | ||
|
||
if (isset($this->groupedStories[$name])) { | ||
$io->comment("Loading stories group \"{$name}\"..."); | ||
$stories = $this->groupedStories[$name]; | ||
} | ||
|
||
if (!$stories) { | ||
throw new InvalidArgumentException("Story with name \"{$name}\" does not exist."); | ||
} | ||
|
||
foreach ($stories as $name => $storyClass) { | ||
$storyClass::load(); | ||
|
||
if ($io->isVerbose()) { | ||
$io->info("Story \"{$storyClass}\" loaded (name: {$name})."); | ||
} | ||
} | ||
|
||
$io->success('Stories successfully loaded!'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function resetDatabase(): void | ||
{ | ||
// it is very not likely that we need dama when running this command | ||
if (\class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections()) { | ||
StaticDriver::setKeepStaticConnections(false); | ||
} | ||
|
||
foreach ($this->databaseResetters as $databaseResetter) { | ||
$databaseResetter->resetBeforeFirstTest($this->kernel); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
final class AsFixtureStoryCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->has('.zenstruck_foundry.story.load_story-command')) { | ||
return; | ||
} | ||
|
||
/** @var array<string, Reference> $fixtureStories */ | ||
$fixtureStories = []; | ||
$groupedFixtureStories = []; | ||
foreach ($container->findTaggedServiceIds('foundry.story.fixture') as $id => $tags) { | ||
if (1 !== \count($tags)) { | ||
throw new LogicException('Tag "foundry.story.fixture" must be used only once per service.'); | ||
} | ||
|
||
$name = $tags[0]['name']; | ||
|
||
if (isset($fixtureStories[$name])) { | ||
throw new LogicException("Cannot use #[AsFixture] name \"{$name}\" for service \"{$id}\". This name is already used by service \"{$fixtureStories[$name]}\"."); | ||
} | ||
|
||
$storyClass = $container->findDefinition($id)->getClass(); | ||
|
||
$fixtureStories[$name] = $storyClass; | ||
|
||
$groups = $tags[0]['groups']; | ||
if (!$groups) { | ||
continue; | ||
} | ||
|
||
foreach ($groups as $group) { | ||
$groupedFixtureStories[$group] ??= []; | ||
$groupedFixtureStories[$group][$name] = $storyClass; | ||
} | ||
} | ||
|
||
if ($collisionNames = \array_intersect(\array_keys($fixtureStories), \array_keys($groupedFixtureStories))) { | ||
$collisionNames = \implode('", "', $collisionNames); | ||
throw new LogicException("Cannot use #[AsFixture] group(s) \"{$collisionNames}\", they collide with fixture names."); | ||
} | ||
|
||
$container->findDefinition('.zenstruck_foundry.story.load_story-command') | ||
->setArgument('$stories', $fixtureStories) | ||
->setArgument('$groupedStories', $groupedFixtureStories); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.