Skip to content

Commit

Permalink
Add deletion helper to reduce complexity (#64)
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

* 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](PHP-CS-Fixer/PHP-CS-Fixer@v3.57.2...v3.58.1)

---
updated-dependencies:
- dependency-name: friendsofphp/php-cs-fixer
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>
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] <[email protected]>
Co-authored-by: Robin Valley <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent 8bff2e9 commit faead55
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
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 FixtureHelper $helper;

abstract public function load(): void;

/** @return string[] */
Expand All @@ -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;
}
}
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;
}
}
7 changes: 5 additions & 2 deletions src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class FixtureLoader
/**
* @param \Traversable<Fixture> $fixtures
*/
public function __construct(\Traversable $fixtures)
{
public function __construct(
\Traversable $fixtures,
private readonly FixtureHelper $helper,
) {
$this->fixtures = iterator_to_array($fixtures);
}

Expand Down Expand Up @@ -177,6 +179,7 @@ private function runFixtures(FixtureOption $option, array $fixtures, ?SymfonySty
continue;
}

$fixture->setHelper($this->helper);
$fixture->load();
}
}
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(),
);
}
}

0 comments on commit faead55

Please sign in to comment.