Skip to content

Commit

Permalink
Don't run vendor fixtures by default (#58)
Browse files Browse the repository at this point in the history
* Dont run vendor fixtures by default, add flag to run vendor fixtures

* Trailing slashes!

* Change docs formats

* Use first class callable syntax
  • Loading branch information
jkniest authored Jun 3, 2024
1 parent 53718c4 commit 61f426a
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
'object_operator_without_whitespace' => false,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
]
],
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'parameters', 'match']],
]);
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Changed argument type on `SalesChannelUtils::getTax()` from `int` to `float`
- **Breaking** By default no fixtures in the vendor directory are loaded. Added option `--vendor` to load them

### Removed
- Dropped support for PHP 8.1
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"

services:
shopware:
image: 'dockware/dev:6.6.0.1'
image: 'dockware/dev:6.6.1.1'
ports:
- "80:80" # Webserver / Apache
- "3306:3306" # MySQL database
Expand Down
13 changes: 10 additions & 3 deletions src/Command/LoadFixtureGroupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
class LoadFixtureGroupCommand extends Command
{
public function __construct(
private readonly FixtureLoader $loader
private readonly FixtureLoader $loader,
) {
parent::__construct();
}

protected function configure(): void
{
$this->addArgument('groupName', InputArgument::REQUIRED, 'Name of fixture group')
->addOption('dry', description: 'Only list fixtures that would run without executing them');
->addOption('dry', description: 'Only list fixtures that would run without executing them')
->addOption('vendor', description: 'Include fixtures from vendor packages');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -35,6 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var string $groupNameInput */
$groupNameInput = $input->getArgument('groupName');
$dry = (bool) ($input->getOption('dry') ?? false);
$vendor = (bool) ($input->getOption('vendor') ?? false);

if (!\is_string($groupNameInput)) {
$io->error('Please make sure that your argument is of type string');
Expand All @@ -48,9 +50,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->note('[INFO] Dry run mode enabled. No fixtures will be executed.');
}

if ($vendor) {
$io->note('[INFO] Including fixtures from vendor packages.');
}

$options = new FixtureOption(
dryMode: $dry,
groupName: $groupNameInput
groupName: $groupNameInput,
withVendor: $vendor,
);

if (!$this->loader->run($options, $io)) {
Expand Down
15 changes: 11 additions & 4 deletions src/Command/LoadFixturesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@
class LoadFixturesCommand extends Command
{
public function __construct(
private readonly FixtureLoader $loader
private readonly FixtureLoader $loader,
) {
parent::__construct();
}

protected function configure(): void
{
$this->addOption('dry', description: 'Only list fixtures that would run without executing them');
$this->addOption('dry', description: 'Only list fixtures that would run without executing them')
->addOption('vendor', description: 'Include fixtures from vendor packages');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$dry = (bool) ($input->getOption('dry') ?? false);
$dry = (bool) ($input->getOption('dry') ?? false);
$vendor = (bool) ($input->getOption('vendor') ?? false);

$io->title('Running all fixtures');

if ($dry) {
$io->note('[INFO] Dry run mode enabled. No fixtures will be executed.');
}

if ($vendor) {
$io->note('[INFO] Including fixtures from vendor packages.');
}

$option = new FixtureOption(
dryMode: $dry
dryMode: $dry,
withVendor: $vendor,
);

if (!$this->loader->run($option, $io)) {
Expand Down
11 changes: 9 additions & 2 deletions src/Command/LoadSingleFixtureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class LoadSingleFixtureCommand extends Command
{
public function __construct(
private readonly FixtureLoader $loader
private readonly FixtureLoader $loader,
) {
parent::__construct();
}
Expand All @@ -27,6 +27,7 @@ protected function configure(): void
{
$this->addOption('with-dependencies', 'w', InputOption::VALUE_NONE, 'Run fixture with dependencies')
->addOption('dry', description: 'Only list fixtures that would run without executing them')
->addOption('vendor', description: 'Include fixtures from vendor packages')
->addArgument('fixtureName', InputArgument::REQUIRED, 'Name of Fixture to load');
}

Expand All @@ -38,6 +39,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$fixtureName = $input->getArgument('fixtureName');
$dry = (bool) ($input->getOption('dry') ?? false);
$withDependencies = (bool) ($input->getOption('with-dependencies') ?? false);
$vendor = (bool) ($input->getOption('vendor') ?? false);

if (!\is_string($fixtureName)) {
$io->error('Please make sure that your argument is of type string');
Expand All @@ -51,10 +53,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->note('[INFO] Dry run mode enabled. No fixtures will be executed.');
}

if ($vendor) {
$io->note('[INFO] Including fixtures from vendor packages.');
}

$options = new FixtureOption(
dryMode: $dry,
fixtureNames: [$fixtureName],
withDependencies: $withDependencies
withDependencies: $withDependencies,
withVendor: $vendor,
);

if (!$this->loader->run($options, $io)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Basecom\FixturePlugin;

abstract readonly class Fixture
abstract class Fixture
{
abstract public function load(): void;

Expand Down
2 changes: 1 addition & 1 deletion src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
private CmsUtils $cmsUtils,
private PaymentMethodUtils $paymentMethodUtils,
private ShippingMethodUtils $shippingMethodUtils,
private CustomerUtils $customerUtils
private CustomerUtils $customerUtils,
) {
}

Expand Down
39 changes: 24 additions & 15 deletions src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public function __construct(\Traversable $fixtures)
* can be configured using the FixtureOption object.
*
* Generally speaking the following options are available:
* - $dryMode: If set to true, the fixtures will not be executed (only printed)
* - $groupName: If set, only fixtures with the given group name will be executed
* - $fixtureNames: If set, only fixtures with the given class name will be executed
* - $withDependencies: If set to true, all dependencies of the fixtures will be executed as well
* - `$dryMode`: If set to true, the fixtures will not be executed (only printed)
* - `$groupName`: If set, only fixtures with the given group name will be executed
* - `$fixtureNames`: If set, only fixtures with the given class name will be executed
* - `$withDependencies`: If set to true, all dependencies of the fixtures will be executed as well
* - `$withVendor`: If set to true, all fixtures found in vendor directory will be executed as well
*/
public function run(FixtureOption $option, ?SymfonyStyle $io = null): bool
{
Expand Down Expand Up @@ -66,7 +67,7 @@ private function prefilterFixtures(FixtureOption $option): array
if (!empty($group)) {
$fixtures = array_filter(
$fixtures,
static fn (Fixture $fixture) => \in_array(strtolower($group), array_map('strtolower', $fixture->groups()), true)
static fn (Fixture $fixture) => \in_array(strtolower($group), array_map('strtolower', $fixture->groups()), true),
);
}

Expand All @@ -78,7 +79,18 @@ static function (Fixture $fixture) use ($option) {
$className = substr(strrchr($fqcn, '\\') ?: '', 1);

return \in_array($className, $option->fixtureNames, true);
}
},
);
}

if (!$option->withVendor) {
$fixtures = array_filter(
$fixtures,
static function (Fixture $fixture) {
$reflectionClass = new \ReflectionClass($fixture::class);

return !str_contains($reflectionClass->getFileName() ?: '', '/vendor/');
},
);
}

Expand All @@ -98,7 +110,7 @@ static function (Fixture $fixture) use ($option) {
private function checkThatAllDependenciesAreInGroup(
array $fixtureReferences,
string $groupName,
?SymfonyStyle $io = null
?SymfonyStyle $io = null,
): bool {
foreach ($fixtureReferences as $fixture) {
if (\count($fixture->dependsOn()) <= 0) {
Expand All @@ -120,7 +132,7 @@ private function checkDependenciesAreInSameGroup(
Fixture $fixture,
array $references,
string $groupName,
?SymfonyStyle $io = null
?SymfonyStyle $io = null,
): bool {
$dependencies = $fixture->dependsOn();
$inGroup = array_map('strtolower', array_keys($references));
Expand Down Expand Up @@ -189,8 +201,8 @@ private function recursiveGetAllDependenciesOfFixtures(array $fixtures): array
$fixtures,
array_map(
static fn (string $key) => $allFixtures[$key],
$keys
)
$keys,
),
);
}

Expand Down Expand Up @@ -239,7 +251,7 @@ private function sortAllByPriority(array $fixtures): array
{
uasort(
$fixtures,
static fn (Fixture $fixture1, Fixture $fixture2): int => $fixture2->priority() <=> $fixture1->priority()
static fn (Fixture $fixture1, Fixture $fixture2): int => $fixture2->priority() <=> $fixture1->priority(),
);

return $fixtures;
Expand All @@ -255,10 +267,7 @@ private function sortAllByPriority(array $fixtures): array
*/
private function buildDependencyTree(array $fixtures): array
{
uasort(
$fixtures,
fn (Fixture $a, Fixture $b) => $this->compareDependencies($a, $b)
);
uasort($fixtures, $this->compareDependencies(...));

return $fixtures;
}
Expand Down
1 change: 1 addition & 0 deletions src/FixtureOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public ?string $groupName = null,
public array $fixtureNames = [],
public bool $withDependencies = false,
public bool $withVendor = false,
) {
}
}
12 changes: 6 additions & 6 deletions src/FixtureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ private function runFixtures(?array $fixtures = []): void
$returnCode = $fixtureCommand->run(
new ArrayInput(
$fixtures,
$fixtureCommand->getDefinition()
$fixtureCommand->getDefinition(),
),
new BufferedOutput() // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
new BufferedOutput(), // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
);

if ($returnCode !== 0) {
Expand All @@ -41,9 +41,9 @@ private function runFixtures(?array $fixtures = []): void
$returnCode = $fixtureCommand->run(
new ArrayInput(
['fixtureName' => $fixture],
$fixtureCommand->getDefinition()
$fixtureCommand->getDefinition(),
),
new BufferedOutput() // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
new BufferedOutput(), // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
);
if ($returnCode !== 0) {
throw new \RuntimeException('fixture:single');
Expand All @@ -60,9 +60,9 @@ private function runSingleFixtureWithDependencies(string $fixture): void
$returnCode = $fixtureCommand->run(
new ArrayInput(
['fixtureName' => $fixture, '--with-dependencies' => true],
$fixtureCommand->getDefinition()
$fixtureCommand->getDefinition(),
),
new BufferedOutput()
new BufferedOutput(),
);

if ($returnCode !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/CategoryUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
readonly class CategoryUtils
{
public function __construct(
private EntityRepository $categoryRepository
private EntityRepository $categoryRepository,
) {
}

Expand All @@ -36,7 +36,7 @@ public function getRootCategory(): ?CategoryEntity
public function getFirst(): ?CategoryEntity
{
$criteria = (new Criteria())->addFilter(
new EqualsFilter('level', '1')
new EqualsFilter('level', '1'),
)->setLimit(1);

$category = $this->categoryRepository
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/CmsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
readonly class CmsUtils
{
public function __construct(
private EntityRepository $cmsPageRepository
private EntityRepository $cmsPageRepository,
) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/Utils/CustomerUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
readonly class CustomerUtils
{
public function __construct(
private EntityRepository $salutationRepository
private EntityRepository $salutationRepository,
) {
}

public function getNotSpecifiedSalutation(): ?SalutationEntity
{
$criteria = (new Criteria())->addFilter(
new EqualsFilter('salutationKey', 'not_specified')
new EqualsFilter('salutationKey', 'not_specified'),
)->setLimit(1);

$salutation = $this->salutationRepository
Expand Down
8 changes: 4 additions & 4 deletions src/Utils/MediaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
private EntityRepository $mediaRepository,
private EntityRepository $mediaFolderRepository,
private FileSaver $fileSaver,
private FileFetcher $fileFetcher
private FileFetcher $fileFetcher,
) {
}

Expand Down Expand Up @@ -49,20 +49,20 @@ public function upload(string $mediaId, string $folderId, string $filename, stri
'mediaFolderId' => $folderId,
],
],
$ctx
$ctx,
);

$uploadedFile = $this->fileFetcher->fetchBlob(
(string) file_get_contents($filename),
$extension,
$contentType
$contentType,
);

$this->fileSaver->persistFileToMedia(
$uploadedFile,
basename($filename, '.'.$extension),
$mediaId,
$ctx
$ctx,
);
}
}
Loading

0 comments on commit 61f426a

Please sign in to comment.