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 all 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
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,
rvalley98 marked this conversation as resolved.
Show resolved Hide resolved
) {
$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(),
);
}
}