Skip to content

Commit

Permalink
Update test trait & harden psalm linting (#63)
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

* Disable error warnings if generics arent provided, since they were added in SW 6.5.something
  • Loading branch information
jkniest authored Jun 14, 2024
1 parent 887c02b commit 8bff2e9
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 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

### 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
6 changes: 6 additions & 0 deletions src/FixtureLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

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

/**
* @param \Traversable<Fixture> $fixtures
*/
public function __construct(\Traversable $fixtures)
{
$this->fixtures = iterator_to_array($fixtures);
Expand Down Expand Up @@ -127,6 +131,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
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
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
16 changes: 16 additions & 0 deletions src/Utils/SalesChannelUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@
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\Country\CountryCollection;
use Shopware\Core\System\Country\CountryEntity;
use Shopware\Core\System\Currency\CurrencyCollection;
use Shopware\Core\System\Currency\CurrencyEntity;
use Shopware\Core\System\Language\LanguageCollection;
use Shopware\Core\System\Language\LanguageEntity;
use Shopware\Core\System\Locale\LocaleCollection;
use Shopware\Core\System\Locale\LocaleEntity;
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetCollection;
use Shopware\Core\System\Snippet\Aggregate\SnippetSet\SnippetSetEntity;
use Shopware\Core\System\Tax\TaxCollection;
use Shopware\Core\System\Tax\TaxEntity;

readonly class SalesChannelUtils
{
/**
* @param EntityRepository<SalesChannelCollection> $salesChannelRepository
* @param EntityRepository<SnippetSetCollection> $snippetSetRepository
* @param EntityRepository<TaxCollection> $taxRepository
* @param EntityRepository<CountryCollection> $countryRepository
* @param EntityRepository<LanguageCollection> $languageRepository
* @param EntityRepository<CurrencyCollection> $currencyRepository
* @param EntityRepository<LocaleCollection> $localeRepository
*/
public function __construct(
private EntityRepository $salesChannelRepository,
private EntityRepository $snippetSetRepository,
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/ShippingMethodUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
Expand All @@ -12,6 +13,9 @@

readonly class ShippingMethodUtils
{
/**
* @param EntityRepository<ShippingMethodCollection> $shippingMethodRepository
*/
public function __construct(
private EntityRepository $shippingMethodRepository,
) {
Expand Down

0 comments on commit 8bff2e9

Please sign in to comment.