Skip to content
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

Add deletion helper to reduce complexity #64

Merged
merged 20 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2543d52
Dont run vendor fixtures by default, add flag to run vendor fixtures
jkniest Apr 24, 2024
6d9a441
Merge remote-tracking branch 'origin/main' into feature/never-vendor-…
jkniest Apr 24, 2024
a13c731
Trailing slashes!
jkniest Apr 24, 2024
1a744a2
Change docs formats
jkniest May 2, 2024
dbe23cd
Merge branch 'main' into feature/never-vendor-again
jkniest May 7, 2024
d477439
Merge branch 'main' into feature/never-vendor-again
jkniest Jun 3, 2024
0d6634f
Use first class callable syntax
jkniest Jun 3, 2024
53437db
Refactor FixtureTrait
jkniest Jun 3, 2024
4741742
Enable phpstan rule: checkMissingIterableValueType
jkniest Jun 3, 2024
48c9399
Enable rule checkGenericClassInNonGenericObjectType in psalm
jkniest Jun 3, 2024
1983de3
Merge remote-tracking branch 'origin/main' into feature/update-test-t…
jkniest Jun 3, 2024
e2213e2
Add database utils and first method to delete entities
jkniest Jun 3, 2024
61f8976
Automatically assign the fixtureHelper to each fixture
jkniest Jun 3, 2024
777751c
Disable error warnings if generics arent provided, since they were ad…
jkniest Jun 3, 2024
2bb5a36
Merge branch 'feature/update-test-trait' into feature/delete-helper
jkniest Jun 3, 2024
6f8bd23
Update src/FixtureLoader.php
rvalley98 Jun 13, 2024
5e97c23
Update friendsofphp/php-cs-fixer requirement from 3.57.2 to 3.58.1 (#65)
dependabot[bot] Jun 4, 2024
fa66ff2
Update test trait & harden psalm linting (#63)
jkniest Jun 14, 2024
c9695a1
Remove readonly param
jkniest Jun 14, 2024
4df1ab0
Merge remote-tracking branch 'origin/main' into feature/delete-helper
jkniest Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for Shopware 6.6
- Added `--dry` option to all fixture load commands
- This option will prevent the fixtures from being executed but still prints all fixtures it would execute
- Added new DatabaseUtils with a few helpful methods:
- `deleteEntities` takes an entity name and criteria and deletes all entities which match the criteria

### 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
- Refactored `FixtureTrait` to not use command anymore but direct Fixture Loader
- `FixtureTrait::loadFixtures` now takes in a FixtureOption parameter
- `FixtureTrait::runSpecificFixtures` is an alias to run specific fixtures with optionally dependencies
- `FixtureTrait::runSingleFixture` (before `FixtureTrait::runSingleFixtureWithDependencies`) with dependencies can now be configured as the second parameter
- `FixtureTrait::runFixtureGroup` is a new function to execute whole fixture groups with optionally dependencies
- Each fixture now has direct access to the FixtureHelper using `$this->helper`
- **Breaking** If you have the helper (or any other helper) previously assigned to `$this->helper` it will either fail or override the FixturePlugin helper

### Removed
- Dropped support for PHP 8.1
Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ parameters:
# The level 9 is the highest level
level: 9

checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
# This is ignored until we drop support for Shopware 6.5
- message: '#Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityRepository is not generic\.#'
reportUnmatched: false
4 changes: 3 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
<!-- This is ignored until we drop support for Shopware 6.5 -->
<TooManyTemplateParams errorLevel="suppress" />

<LessSpecificImplementedReturnType errorLevel="suppress" />

<LessSpecificReturnType errorLevel="info" />
<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
Expand Down
10 changes: 10 additions & 0 deletions src/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

abstract class Fixture
{
protected readonly FixtureHelper $helper;

Check failure on line 9 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Class Basecom\FixturePlugin\Fixture has an uninitialized readonly property $helper. Assign it in the constructor.

Check failure on line 9 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Class Basecom\FixturePlugin\Fixture has an uninitialized readonly property $helper. Assign it in the constructor.
jkniest marked this conversation as resolved.
Show resolved Hide resolved

abstract public function load(): void;

/** @return string[] */
Expand All @@ -24,4 +26,12 @@
{
return [];
}

/**
* @internal This method should only be called from the FixtureLoader.
*/
public final function setHelper(FixtureHelper $helper): void
{
$this->helper = $helper;

Check failure on line 35 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Readonly property Basecom\FixturePlugin\Fixture::$helper is assigned outside of the constructor.

Check failure on line 35 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2, lowest)

InaccessibleProperty

src/Fixture.php:35:9: InaccessibleProperty: Basecom\FixturePlugin\Fixture::$helper is marked readonly (see https://psalm.dev/054)

Check failure on line 35 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / Psalm (8.2, stable)

InaccessibleProperty

src/Fixture.php:35:9: InaccessibleProperty: Basecom\FixturePlugin\Fixture::$helper is marked readonly (see https://psalm.dev/054)

Check failure on line 35 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Readonly property Basecom\FixturePlugin\Fixture::$helper is assigned outside of the constructor.

Check failure on line 35 in src/Fixture.php

View workflow job for this annotation

GitHub Actions / Psalm (8.3, lowest)

InaccessibleProperty

src/Fixture.php:35:9: InaccessibleProperty: Basecom\FixturePlugin\Fixture::$helper is marked readonly (see https://psalm.dev/054)
}
}
11 changes: 11 additions & 0 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Basecom\FixturePlugin\Utils\CategoryUtils;
use Basecom\FixturePlugin\Utils\CmsUtils;
use Basecom\FixturePlugin\Utils\CustomerUtils;
use Basecom\FixturePlugin\Utils\DatabaseUtils;
use Basecom\FixturePlugin\Utils\MediaUtils;
use Basecom\FixturePlugin\Utils\PaymentMethodUtils;
use Basecom\FixturePlugin\Utils\SalesChannelUtils;
Expand All @@ -22,6 +23,7 @@ public function __construct(
private PaymentMethodUtils $paymentMethodUtils,
private ShippingMethodUtils $shippingMethodUtils,
private CustomerUtils $customerUtils,
private DatabaseUtils $databaseUtils,
) {
}

Expand Down Expand Up @@ -87,4 +89,13 @@ public function ShippingMethod(): ShippingMethodUtils
{
return $this->shippingMethodUtils;
}

/**
* Use this to access the general database helper functions
* of the fixture helper class.
*/
public function Database(): DatabaseUtils
{
return $this->databaseUtils;
}
}
12 changes: 11 additions & 1 deletion src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@

class FixtureLoader
{
/** @var array<Fixture> */
private readonly array $fixtures;

public function __construct(\Traversable $fixtures)
/**
* @param \Traversable<Fixture> $fixtures
*/
public function __construct(
\Traversable $fixtures,
private FixtureHelper $helper,
)
{
$this->fixtures = iterator_to_array($fixtures);
}
Expand Down Expand Up @@ -127,6 +134,8 @@ private function checkThatAllDependenciesAreInGroup(

/**
* Check if dependencies of fixture are also in the same group. If not, show error and stop process.
*
* @param array<string, Fixture> $references
*/
private function checkDependenciesAreInSameGroup(
Fixture $fixture,
Expand Down Expand Up @@ -171,6 +180,7 @@ private function runFixtures(FixtureOption $option, array $fixtures, ?SymfonySty
continue;
}

$fixture->setHelper($this->helper);
$fixture->load();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/FixtureOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

readonly class FixtureOption
{
/**
* @param array<string> $fixtureNames
*/
public function __construct(
public bool $dryMode = false,
public ?string $groupName = null,
Expand Down
81 changes: 33 additions & 48 deletions src/FixtureTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,54 @@

namespace Basecom\FixturePlugin;

use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Shopware\Core\Framework\DependencyInjection\DependencyInjectionException;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;

trait FixtureTrait
{
private function runFixtures(?array $fixtures = []): void
{
if (empty($fixtures)) {
$application = new Application(KernelLifecycleManager::getKernel());
$fixtureCommand = $application->find('fixture:load');

$returnCode = $fixtureCommand->run(
new ArrayInput(
$fixtures,
$fixtureCommand->getDefinition(),
),
new BufferedOutput(), // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
);
use IntegrationTestBehaviour;

if ($returnCode !== 0) {
throw new \RuntimeException('fixture:load');
}
private function runFixtures(FixtureOption $options): void
{
$fixtureLoader = $this->getContainer()->get(FixtureLoader::class);

return;
if (!$fixtureLoader instanceof FixtureLoader) {
throw new DependencyInjectionException(404, 'FIXTURE_LOADER_NOT_FOUND', 'Fixture Loader not found in container');
}

foreach ($fixtures as $fixture) {
$application = new Application(KernelLifecycleManager::getKernel());
$fixtureLoader->run($options);
}

$fixtureCommand = $application->find('fixture:load:single');
/**
* @param array<string> $fixtures
*/
private function runSpecificFixtures(array $fixtures = [], bool $withDependencies = false): void
{
$options = new FixtureOption(
fixtureNames: $fixtures,
withDependencies: $withDependencies,
);

$returnCode = $fixtureCommand->run(
new ArrayInput(
['fixtureName' => $fixture],
$fixtureCommand->getDefinition(),
),
new BufferedOutput(), // use new ConsoleOutput() if you don't want to hide output, new BufferedOutput()
);
if ($returnCode !== 0) {
throw new \RuntimeException('fixture:single');
}
}
$this->runFixtures($options);
}

private function runSingleFixtureWithDependencies(string $fixture): void
private function runSingleFixture(string $fixture, bool $withDependencies = false): void
{
$application = new Application(KernelLifecycleManager::getKernel());
$options = new FixtureOption(
fixtureNames: [$fixture],
withDependencies: $withDependencies,
);

$fixtureCommand = $application->find('fixture:load:single');
$this->runFixtures($options);
}

$returnCode = $fixtureCommand->run(
new ArrayInput(
['fixtureName' => $fixture, '--with-dependencies' => true],
$fixtureCommand->getDefinition(),
),
new BufferedOutput(),
private function runFixtureGroup(string $group, bool $withDependencies = false): void
{
$options = new FixtureOption(
groupName: $group,
withDependencies: $withDependencies,
);

if ($returnCode !== 0) {
throw new \RuntimeException('fixture:single');
}
$this->runFixtures($options);
}
}
4 changes: 4 additions & 0 deletions src/Utils/CategoryUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Content\Category\CategoryCollection;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand All @@ -14,6 +15,9 @@

readonly class CategoryUtils
{
/**
* @param EntityRepository<CategoryCollection> $categoryRepository
*/
public function __construct(
private EntityRepository $categoryRepository,
) {
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/CmsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Content\Cms\CmsPageCollection;
use Shopware\Core\Content\Cms\CmsPageEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand All @@ -13,6 +14,9 @@

readonly class CmsUtils
{
/**
* @param EntityRepository<CmsPageCollection> $cmsPageRepository
*/
public function __construct(
private EntityRepository $cmsPageRepository,
) {
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/CustomerUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Salutation\SalutationCollection;
use Shopware\Core\System\Salutation\SalutationEntity;

readonly class CustomerUtils
{
/**
* @param EntityRepository<SalutationCollection> $salutationRepository
*/
public function __construct(
private EntityRepository $salutationRepository,
) {
Expand Down
31 changes: 31 additions & 0 deletions src/Utils/DatabaseUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

readonly class DatabaseUtils
{
public function __construct(
private DefinitionInstanceRegistry $definitionInstanceRegistry,
) {
}

public function deleteEntities(string $entity, Criteria $criteria): void
{
$repository = $this->definitionInstanceRegistry->getRepository($entity);

// First load all the ids of the entities
$ids = $repository->searchIds($criteria, Context::createDefaultContext())->getData();

// Delete all entities with the IDs
$repository->delete(
array_values($ids),
Context::createDefaultContext(),
);
}
}
6 changes: 6 additions & 0 deletions src/Utils/MediaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderCollection;
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
use Shopware\Core\Content\Media\File\FileFetcher;
use Shopware\Core\Content\Media\File\FileSaver;
use Shopware\Core\Content\Media\MediaCollection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

readonly class MediaUtils
{
/**
* @param EntityRepository<MediaCollection> $mediaRepository
* @param EntityRepository<MediaFolderCollection> $mediaFolderRepository
*/
public function __construct(
private EntityRepository $mediaRepository,
private EntityRepository $mediaFolderRepository,
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/PaymentMethodUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\InvoicePayment;
use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand All @@ -13,6 +14,9 @@

readonly class PaymentMethodUtils
{
/**
* @param EntityRepository<PaymentMethodCollection> $paymentMethodRepository
*/
public function __construct(
private EntityRepository $paymentMethodRepository,
) {
Expand Down
Loading
Loading