From faead552e26d7c69f10669271cae267e56909264 Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Fri, 14 Jun 2024 09:32:02 +0200 Subject: [PATCH] Add deletion helper to reduce complexity (#64) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Dont run vendor fixtures by default, add flag to run vendor fixtures * Trailing slashes! * Change docs formats * Use first class callable syntax * Refactor FixtureTrait * Enable phpstan rule: checkMissingIterableValueType * Enable rule checkGenericClassInNonGenericObjectType in psalm * Add database utils and first method to delete entities * Automatically assign the fixtureHelper to each fixture * Disable error warnings if generics arent provided, since they were added in SW 6.5.something * Update src/FixtureLoader.php It canĀ“t be readonly, because you use a setter and you will set the helper on other classes * Update friendsofphp/php-cs-fixer requirement from 3.57.2 to 3.58.1 (#65) Updates the requirements on [friendsofphp/php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) to permit the latest version. - [Release notes](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/releases) - [Changelog](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/CHANGELOG.md) - [Commits](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/compare/v3.57.2...v3.58.1) --- updated-dependencies: - dependency-name: friendsofphp/php-cs-fixer dependency-type: direct:development ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update test trait & harden psalm linting (#63) * Dont run vendor fixtures by default, add flag to run vendor fixtures * Trailing slashes! * Change docs formats * Use first class callable syntax * Refactor FixtureTrait * Enable phpstan rule: checkMissingIterableValueType * Enable rule checkGenericClassInNonGenericObjectType in psalm * Disable error warnings if generics arent provided, since they were added in SW 6.5.something * Remove readonly param --------- Signed-off-by: dependabot[bot] Co-authored-by: Robin Valley <75166000+rvalley98@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ src/Fixture.php | 10 ++++++++++ src/FixtureHelper.php | 11 +++++++++++ src/FixtureLoader.php | 7 +++++-- src/Utils/DatabaseUtils.php | 31 +++++++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/Utils/DatabaseUtils.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fd50aef..bbc3246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ 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` @@ -19,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `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 diff --git a/src/Fixture.php b/src/Fixture.php index da7e1a3..5b78b3f 100644 --- a/src/Fixture.php +++ b/src/Fixture.php @@ -6,6 +6,8 @@ abstract class Fixture { + protected FixtureHelper $helper; + abstract public function load(): void; /** @return string[] */ @@ -24,4 +26,12 @@ public function groups(): array { return []; } + + /** + * @internal this method should only be called from the FixtureLoader + */ + final public function setHelper(FixtureHelper $helper): void + { + $this->helper = $helper; + } } diff --git a/src/FixtureHelper.php b/src/FixtureHelper.php index d78658d..b1a35e9 100644 --- a/src/FixtureHelper.php +++ b/src/FixtureHelper.php @@ -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; @@ -22,6 +23,7 @@ public function __construct( private PaymentMethodUtils $paymentMethodUtils, private ShippingMethodUtils $shippingMethodUtils, private CustomerUtils $customerUtils, + private DatabaseUtils $databaseUtils, ) { } @@ -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; + } } diff --git a/src/FixtureLoader.php b/src/FixtureLoader.php index 1b3cf08..eaf72e2 100644 --- a/src/FixtureLoader.php +++ b/src/FixtureLoader.php @@ -14,8 +14,10 @@ class FixtureLoader /** * @param \Traversable $fixtures */ - public function __construct(\Traversable $fixtures) - { + public function __construct( + \Traversable $fixtures, + private readonly FixtureHelper $helper, + ) { $this->fixtures = iterator_to_array($fixtures); } @@ -177,6 +179,7 @@ private function runFixtures(FixtureOption $option, array $fixtures, ?SymfonySty continue; } + $fixture->setHelper($this->helper); $fixture->load(); } } diff --git a/src/Utils/DatabaseUtils.php b/src/Utils/DatabaseUtils.php new file mode 100644 index 0000000..e1a5bb7 --- /dev/null +++ b/src/Utils/DatabaseUtils.php @@ -0,0 +1,31 @@ +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(), + ); + } +}