Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 1 deletion src/DataProvider/RuleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#[Package('fundamentals@after-sales')]
class RuleProvider extends DemoDataProvider
{
public const CART_AMOUNT_RULE_ID = 'e1378db7808a478f919e0d740d5d6c1a';

private Connection $connection;

public function __construct(Connection $connection)
Expand Down Expand Up @@ -134,7 +136,7 @@ public function getPayload(): array
],
],
[
'id' => 'e1378db7808a478f919e0d740d5d6c1a',
'id' => self::CART_AMOUNT_RULE_ID,
'name' => 'Warenkorbwert größer/gleich 0',
'priority' => 100,
'conditions' => [
Expand Down
17 changes: 1 addition & 16 deletions src/DataProvider/ShippingMethodProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ public function getEntity(): string

public function getPayload(): array
{
$ruleId = $this->getRuleId();
$payload = [];
foreach ($this->getShippingMethodIds() as $shippingMethodId) {
$payload[] = [
'id' => $shippingMethodId,
'availabilityRuleId' => $ruleId,
'availabilityRuleId' => RuleProvider::CART_AMOUNT_RULE_ID,
];
}

Expand All @@ -56,18 +55,4 @@ private function getShippingMethodIds(): array
FROM `shipping_method`;
');
}

private function getRuleId(): string
{
$result = $this->connection->fetchOne('
SELECT LOWER(HEX(`id`))
FROM `rule`
');

if (!$result) {
throw new \RuntimeException('No rule found, please make sure that basic data is available by running the migrations.');
}

return (string) $result;
}
}
23 changes: 23 additions & 0 deletions tests/DemoDataServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Category\CategoryCollection;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use Swag\PlatformDemoData\DataProvider\RuleProvider;
use Swag\PlatformDemoData\DemoDataService;

#[Package('fundamentals@after-sales')]
Expand Down Expand Up @@ -55,6 +58,26 @@ public function testGenerate(): void
$this->assertEntityCountGreaterThanOrEqual(16, 'product.repository');
}

public function testGenerateAssignsCartAmountRuleToAllShippingMethods(): void
{
$connection = $this->getContainer()->get(Connection::class);

// A rule sorting before all others must not become the availability rule of the shipping methods.
$connection->insert('rule', [
'id' => Uuid::fromHexToBytes('00000000000000000000000000000001'),
'name' => 'Never matching',
'priority' => 1,
'created_at' => (new \DateTimeImmutable())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]);

$demoDataService = $this->getContainer()->get(DemoDataService::class);
$demoDataService->generate(new Context(new SystemSource()));

$availabilityRuleIds = $connection->fetchFirstColumn('SELECT DISTINCT LOWER(HEX(availability_rule_id)) FROM shipping_method');

static::assertSame([RuleProvider::CART_AMOUNT_RULE_ID], $availabilityRuleIds);
}

private function assertEntityCountGreaterThanOrEqual(int $expectedCount, string $repositoryName): void
{
/** @var EntityRepository<CategoryCollection> $repository */
Expand Down
Loading