Skip to content

Commit

Permalink
Merge branch 'main' into feature/took-time
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest authored Jun 14, 2024
2 parents f10760f + 6f2c0a2 commit fb25df1
Show file tree
Hide file tree
Showing 16 changed files with 509 additions and 238 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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
- Added a small cache for all utilities. It prevents loading data twice within the same request / command execution
- Added small helper function: `$fixtureHelper->ensureNotEmpty` which throws an exception if something is empty (using the PHP empty function)

### Changed
- Changed argument type on `SalesChannelUtils::getTax()` from `int` to `float`
Expand All @@ -23,11 +25,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `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
- **Breaking** Moved `SalesChannelUtils::getLanguage()` to `LanguageAndLocaleUtils::getLanguage()`
- **Breaking** Moved `SalesChannelUtils::getLocale()` to `LanguageAndLocaleUtils::getLocale()`
- **Breaking** Moved `SalesChannelUtils::getCountry()` to `LanguageAndLocaleUtils::getCountry()`
- **Breaking** Moved `SalesChannelUtils::getSnippetSet()` to `LanguageAndLocaleUtils::getSnippetSet()`
- **Breaking** Moved `SalesChannelUtils::getCurrencyEuro()` to `CurrencyUtils::getCurrencyEuro()`
- **Breaking** Moved `SalesChannelUtils::getTax19()` to `TaxUtils::getTax19()`
- **Breaking** Moved `SalesChannelUtils::getTax()` to `TaxUtils::getTax()`

### Removed
- Dropped support for PHP 8.1
- Dropped support for Shopware 6.3 & 6.4
- Removed FixtureBag
- **Breaking** Removed FixtureBag
- **Breaking** CategoryUtils
- Removed method `getFirst` on CategoryUtils
- Removed method `getByName` on CategoryUtils
- **Breaking** Renamed `CategoryUtils` to `SalutationUtils`

## [2.4.0] - 2023-11-15
### Added
Expand Down
4 changes: 2 additions & 2 deletions _examples/CustomerFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function load(FixtureBag $bag): void
'defaultPaymentMethodId' => $this->helper->PaymentMethod()->getInvoicePaymentMethod()->getId(),
'defaultBillingAddress' => [
'id' => self::ADDRESS_ID,
'salutationId' => $this->helper->Customer()->getNotSpecifiedSalutation()->getId(),
'salutationId' => $this->helper->Salutation()->getNotSpecifiedSalutation()->getId(),
'firstName' => 'John',
'lastName' => 'Doe',
'zipcode' => '1234',
Expand All @@ -41,7 +41,7 @@ public function load(FixtureBag $bag): void
'countryId' => $this->helper->SalesChannel()->getCountry('DE')->getId(),
],
'defaultShippingAddressId' => self::ADDRESS_ID,
'salutationId' => $this->helper->Customer()->getNotSpecifiedSalutation()->getId(),
'salutationId' => $this->helper->Salutation()->getNotSpecifiedSalutation()->getId(),
'customerNumber' => '1122',
'firstName' => 'John',
'lastName' => 'Doe',
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"php": "^8.2 || ^8.3",
"shopware/core": "6.5.*|6.6.*",
"shopware/administration": "6.5.*|6.6.*",
"shopware/storefront": "6.5.*|6.6.*"
"shopware/storefront": "6.5.*|6.6.*",
"spatie/once": "^3.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.58.1",
Expand Down
53 changes: 48 additions & 5 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

use Basecom\FixturePlugin\Utils\CategoryUtils;
use Basecom\FixturePlugin\Utils\CmsUtils;
use Basecom\FixturePlugin\Utils\CustomerUtils;
use Basecom\FixturePlugin\Utils\CurrencyUtils;
use Basecom\FixturePlugin\Utils\DatabaseUtils;
use Basecom\FixturePlugin\Utils\LanguageAndLocaleUtils;
use Basecom\FixturePlugin\Utils\MediaUtils;
use Basecom\FixturePlugin\Utils\PaymentMethodUtils;
use Basecom\FixturePlugin\Utils\SalesChannelUtils;
use Basecom\FixturePlugin\Utils\SalutationUtils;
use Basecom\FixturePlugin\Utils\ShippingMethodUtils;
use Basecom\FixturePlugin\Utils\TaxUtils;

readonly class FixtureHelper
{
Expand All @@ -22,11 +25,24 @@ public function __construct(
private CmsUtils $cmsUtils,
private PaymentMethodUtils $paymentMethodUtils,
private ShippingMethodUtils $shippingMethodUtils,
private CustomerUtils $customerUtils,
private SalutationUtils $salutationUtils,
private DatabaseUtils $databaseUtils,
private LanguageAndLocaleUtils $languageAndLocaleUtils,
private CurrencyUtils $currencyUtils,
private TaxUtils $taxUtils,
) {
}

/**
* @phpstan-assert !empty $something
*/
public function ensureNotEmpty(mixed $something): void
{
if (empty($something)) {
throw new \LogicException('Expected parameter not to be empty, but it was.');
}
}

/**
* Use this to access the media related features
* of the fixture helper class.
Expand Down Expand Up @@ -55,12 +71,12 @@ public function SalesChannel(): SalesChannelUtils
}

/**
* Use this to access the customer related features
* Use this to access the salutation related features
* of the fixture helper class.
*/
public function Customer(): CustomerUtils
public function Salutation(): SalutationUtils
{
return $this->customerUtils;
return $this->salutationUtils;
}

/**
Expand Down Expand Up @@ -90,6 +106,33 @@ public function ShippingMethod(): ShippingMethodUtils
return $this->shippingMethodUtils;
}

/**
* Use this to access the language & locale related features
* of the fixture helper class.
*/
public function LanguageAndLocale(): LanguageAndLocaleUtils
{
return $this->languageAndLocaleUtils;
}

/**
* Use this to access the currency related features
* of the fixture helper class.
*/
public function Currency(): CurrencyUtils
{
return $this->currencyUtils;
}

/**
* Use this to access the tax related features
* of the fixture helper class.
*/
public function Tax(): TaxUtils
{
return $this->taxUtils;
}

/**
* Use this to access the general database helper functions
* of the fixture helper class.
Expand Down
61 changes: 23 additions & 38 deletions src/Utils/CategoryUtils.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection ALL */

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;
Expand All @@ -13,6 +11,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with categories. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Category()->……();
* ```
*/
readonly class CategoryUtils
{
/**
Expand All @@ -23,46 +30,24 @@ public function __construct(
) {
}

public function getRootCategory(): ?CategoryEntity
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('autoIncrement', 1))
->addFilter(new EqualsFilter('level', 1))
->setLimit(1);

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
}

public function getFirst(): ?CategoryEntity
{
$criteria = (new Criteria())->addFilter(
new EqualsFilter('level', '1'),
)->setLimit(1);

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
}

/**
* Gets the first found category with the provided name.
* Gets the root category of the shop or.
*/
public function getByName(string $name): ?CategoryEntity
public function getRootCategory(): ?CategoryEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
$criteria->setLimit(1);
return once(function (): ?CategoryEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('autoIncrement', 1))
->addFilter(new EqualsFilter('level', 1))
->setLimit(1);

$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();
$category = $this->categoryRepository
->search($criteria, Context::createDefaultContext())
->first();

return $category instanceof CategoryEntity ? $category : null;
return $category instanceof CategoryEntity ? $category : null;
});
}
}
29 changes: 21 additions & 8 deletions src/Utils/CmsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with the shopware cms. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Cms()->……();
* ```
*/
readonly class CmsUtils
{
/**
Expand All @@ -24,15 +33,19 @@ public function __construct(

public function getDefaultCategoryLayout(): ?CmsPageEntity
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('locked', '1'))
->addFilter(new EqualsAnyFilter('translations.name', ['Default category layout', 'Default listing layout']))
->setLimit(1);
return once(function (): ?CmsPageEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('locked', '1'))
->addFilter(new EqualsAnyFilter('translations.name', ['Default category layout', 'Default listing layout']))
->setLimit(1);

$cmsPage = $this->cmsPageRepository
->search($criteria, Context::createDefaultContext())
->first();
$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

return $cmsPage instanceof CmsPageEntity ? $cmsPage : null;
$cmsPage = $this->cmsPageRepository
->search($criteria, Context::createDefaultContext())
->first();

return $cmsPage instanceof CmsPageEntity ? $cmsPage : null;
});
}
}
49 changes: 49 additions & 0 deletions src/Utils/CurrencyUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;

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;
use Shopware\Core\System\Currency\CurrencyCollection;
use Shopware\Core\System\Currency\CurrencyEntity;

/**
* This class provides utility methods to work with currencies. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Currency()->……();
* ```
*/
class CurrencyUtils
{
/**
* @param EntityRepository<CurrencyCollection> $currencyRepository
*/
public function __construct(
private EntityRepository $currencyRepository,
) {
}

public function getCurrencyEuro(): ?CurrencyEntity
{
return once(function (): ?CurrencyEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('isoCode', 'EUR'))
->setLimit(1);

$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

$currency = $this->currencyRepository
->search($criteria, Context::createDefaultContext())
->first();

return $currency instanceof CurrencyEntity ? $currency : null;
});
}
}
36 changes: 0 additions & 36 deletions src/Utils/CustomerUtils.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Utils/DatabaseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

/**
* This class provides utility methods to directly interact with the database. It has build in
* caching to prevent multiple database queries for the same data within one command
* execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Database()->……();
* ```
*/
readonly class DatabaseUtils
{
public function __construct(
Expand Down
Loading

0 comments on commit fb25df1

Please sign in to comment.