|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the zenstruck/foundry package. |
| 5 | + * |
| 6 | + * (c) Kevin Bond <kevinbond@gmail.com> |
| 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 Zenstruck\Foundry\Command; |
| 13 | + |
| 14 | +use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 17 | +use Symfony\Component\Console\Exception\LogicException; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 24 | +use Zenstruck\Foundry\Persistence\ResetDatabase\BeforeFirstTestResetter; |
| 25 | +use Zenstruck\Foundry\Story; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Nicolas PHILIPPE <nikophil@gmail.com> |
| 29 | + */ |
| 30 | +final class LoadStoryCommand extends Command |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + /** @var array<string, class-string<Story>> */ |
| 34 | + private readonly array $stories, |
| 35 | + /** @var array<string, array<string, class-string<Story>>> */ |
| 36 | + private readonly array $groupedStories, |
| 37 | + /** @var iterable<BeforeFirstTestResetter> */ |
| 38 | + private iterable $databaseResetters, |
| 39 | + private KernelInterface $kernel, |
| 40 | + ) { |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this |
| 47 | + ->addArgument('name', InputArgument::OPTIONAL, 'The name of the story to load.') |
| 48 | + ->addOption('append', 'a', InputOption::VALUE_NONE, 'Skip resetting database and append data to the existing database.') |
| 49 | + ; |
| 50 | + } |
| 51 | + |
| 52 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 53 | + { |
| 54 | + if (0 === \count($this->stories)) { |
| 55 | + throw new LogicException('No story as fixture available: add attribute #[AsFixture] to your story classes before running this command.'); |
| 56 | + } |
| 57 | + |
| 58 | + $io = new SymfonyStyle($input, $output); |
| 59 | + |
| 60 | + if (!$input->getOption('append')) { |
| 61 | + $this->resetDatabase(); |
| 62 | + } |
| 63 | + |
| 64 | + $stories = []; |
| 65 | + |
| 66 | + if (null === ($name = $input->getArgument('name'))) { |
| 67 | + if (1 === \count($this->stories)) { |
| 68 | + $name = \array_keys($this->stories)[0]; |
| 69 | + } else { |
| 70 | + $storyNames = \array_keys($this->stories); |
| 71 | + if (\count($this->groupedStories) > 0) { |
| 72 | + $storyNames[] = '(choose a group of stories...)'; |
| 73 | + } |
| 74 | + $name = $io->choice('Choose a story to load:', $storyNames); |
| 75 | + } |
| 76 | + |
| 77 | + if (!isset($this->stories[$name])) { |
| 78 | + $groupsNames = \array_keys($this->groupedStories); |
| 79 | + $name = $io->choice('Choose a group of stories:', $groupsNames); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (isset($this->stories[$name])) { |
| 84 | + $io->comment("Loading story with name \"{$name}\"..."); |
| 85 | + $stories = [$name => $this->stories[$name]]; |
| 86 | + } |
| 87 | + |
| 88 | + if (isset($this->groupedStories[$name])) { |
| 89 | + $io->comment("Loading stories group \"{$name}\"..."); |
| 90 | + $stories = $this->groupedStories[$name]; |
| 91 | + } |
| 92 | + |
| 93 | + if (!$stories) { |
| 94 | + throw new InvalidArgumentException("Story with name \"{$name}\" does not exist."); |
| 95 | + } |
| 96 | + |
| 97 | + foreach ($stories as $name => $storyClass) { |
| 98 | + $storyClass::load(); |
| 99 | + |
| 100 | + if ($io->isVerbose()) { |
| 101 | + $io->info("Story \"{$storyClass}\" loaded (name: {$name})."); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + $io->success('Stories successfully loaded!'); |
| 106 | + |
| 107 | + return self::SUCCESS; |
| 108 | + } |
| 109 | + |
| 110 | + private function resetDatabase(): void |
| 111 | + { |
| 112 | + // it is very not likely that we need dama when running this command |
| 113 | + if (\class_exists(StaticDriver::class) && StaticDriver::isKeepStaticConnections()) { |
| 114 | + StaticDriver::setKeepStaticConnections(false); |
| 115 | + } |
| 116 | + |
| 117 | + foreach ($this->databaseResetters as $databaseResetter) { |
| 118 | + $databaseResetter->resetBeforeFirstTest($this->kernel); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments