Skip to content

Commit

Permalink
Allow to run single fixtures with all their dependencies (recursively) (
Browse files Browse the repository at this point in the history
#16)

* Add "--with-dependencies" option to the single run command

* Test recursive logic

* Build fixture reference before fetching recursive dependencies

* Run fixtures with dependencies

* Build references before

* Use fixture reference

* Execute original fixture given aswell

* Update trait

* Update changelog, run linting / code styling tools
  • Loading branch information
jkniest authored Nov 8, 2022
1 parent 238bdaa commit 6fe0b6d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.0] - 2022-09-19
### Added
- Added the option `--with-dependencies` / `-w` to the `fixture:run:single` command
- This command recursively runs all fixtures that are required by the given fixture
- Added new method to the fixture trait: `runSingleFixtureWithDependencies()`

## [2.0.0] - 2022-08-04
> **Please see the UPGRADE.md guide for instructions**
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ shell:

## Install all dependencies
dependencies:
make docker COMMAND="composer install --no-interaction --optimize-autoloader --no-suggest"
make docker COMMAND="composer install --no-interaction --optimize-autoloader"
make docker COMMAND="npm ci"

## Install all dependencies and prepare everything
install:
make dependencies
make docker-base COMMAND="composer config minimum-stability dev"
make docker-base COMMAND="composer require basecom/sw6-fixtures-plugin:*"
make docker-base COMMAND="./bin/console plugin:refresh"
make docker-base COMMAND="./bin/console plugin:install --activate BasecomFixturePlugin"

###########
# Linting #
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}
},
"require": {
"php": "^7.4 || ^8.0 || ^8.1",
"shopware/core": "6.3.*|6.4.*",
"shopware/administration": "6.3.*|6.4.*",
"shopware/storefront": "6.3.*|6.4.*"
Expand Down
11 changes: 10 additions & 1 deletion src/Command/LoadSingleFixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Command\Command;
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;

Expand All @@ -27,6 +28,7 @@ protected function configure(): void
{
$this
->setHelp('This command allows you to run only one specific fixture')
->addOption('with-dependencies', 'w', InputOption::VALUE_NONE, 'Run fixture with dependencies')
->addArgument('fixtureName', InputArgument::REQUIRED, 'Name of Fixture to load');
}

Expand All @@ -44,7 +46,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$this->loader->runSingle($io, $groupNameInput);
$withDependencies = $input->getOption('with-dependencies');
if (!\is_bool($withDependencies)) {
$io->error('Please make sure that your argument is of type boolean');

return Command::FAILURE;
}

$this->loader->runSingle($io, $groupNameInput, $withDependencies);

$io->success('Done!');

Expand Down
24 changes: 21 additions & 3 deletions src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function runAll(SymfonyStyle $io): void
$this->runFixtures($io, $this->fixtures);
}

public function runSingle(SymfonyStyle $io, string $fixtureName): void
public function runSingle(SymfonyStyle $io, string $fixtureName, bool $withDependencies = false): void
{
foreach ($this->fixtures as $fixture) {
$className = \get_class($fixture) ?: '';
Expand All @@ -33,8 +33,19 @@ public function runSingle(SymfonyStyle $io, string $fixtureName): void
}

$io->note('Fixture '.$className.' found and will be loaded.');
$bag = new FixtureBag();
$fixture->load($bag);

if (!$withDependencies) {
$bag = new FixtureBag();
$fixture->load($bag);

return;
}

$this->fixtureReference = $this->buildFixtureReference($this->fixtures);
$this->runFixtures($io, array_merge(array_map(
fn (string $fixtureClass) => $this->fixtureReference[$fixtureClass],
$this->recursiveGetAllDependenciesOfFixture($fixture)
), [$fixture]));

return;
}
Expand Down Expand Up @@ -125,6 +136,13 @@ private function runFixtures(SymfonyStyle $io, array $fixtures): void
}
}

private function recursiveGetAllDependenciesOfFixture(Fixture $fixture): array
{
return array_unique(array_merge($fixture->dependsOn(), array_reduce($fixture->dependsOn(), function ($carry, $item) {
return array_merge($carry, $this->recursiveGetAllDependenciesOfFixture($this->fixtureReference[$item]));
}, [])));
}

private function buildFixtureReference(array $fixtures): array
{
$result = [];
Expand Down
19 changes: 19 additions & 0 deletions src/FixtureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ private function runFixtures(?array $fixtures = []): void
}
}
}

private function runSingleFixtureWithDependencies(string $fixture): void
{
$application = new Application(KernelLifecycleManager::getKernel());

$fixtureCommand = $application->find('fixture:load:single');

$returnCode = $fixtureCommand->run(
new ArrayInput(
['fixtureName' => $fixture, '--with-dependencies' => true],
$fixtureCommand->getDefinition()
),
new BufferedOutput()
);

if ($returnCode !== 0) {
throw new \RuntimeException('fixture:single');
}
}
}

0 comments on commit 6fe0b6d

Please sign in to comment.