Skip to content

Commit

Permalink
NEXT-35521 - Added + updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jozsefdamokos committed Apr 26, 2024
1 parent 07d6a1a commit 65c5c54
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 5 deletions.
Expand Up @@ -5,9 +5,6 @@ author: Jasper Peeters
author_email: jasper.peeters@meteor.be
author_github: JasperP98
---

# Core

* Add `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events

* Add language id property to databag for mail events
* Added `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events
* Added language id property to databag for mail events
Expand Up @@ -25,6 +25,7 @@
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Event\MailAware;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Log\Package;
Expand Down Expand Up @@ -265,6 +266,7 @@ public function testActionExecuted(?string $replyTo, array $exptectedReplyTo = [
'subject' => null,
'mediaIds' => [],
'senderName' => null,
'languageId' => null,
'attachmentsConfig' => new MailAttachmentsConfig(
Context::createDefaultContext(),
$this->mailTemplate,
Expand Down Expand Up @@ -423,6 +425,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'recipient' => ['type' => 'customer'],
'documentTypeIds' => null,
]);
$languageId = Uuid::randomHex();

$expected = [
'data' => [
Expand All @@ -437,6 +440,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'subject' => null,
'mediaIds' => [],
'senderName' => null,
'languageId' => $languageId,
'attachmentsConfig' => new MailAttachmentsConfig(
Context::createDefaultContext(),
$this->mailTemplate,
Expand Down Expand Up @@ -467,6 +471,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
$flow->setData(MailAware::MAIL_STRUCT, $templateData);
$flow->setData(MailAware::SALES_CHANNEL_ID, TestDefaults::SALES_CHANNEL);
$flow->setData(OrderAware::ORDER_ID, $orderId);
$flow->setData(LanguageAware::LANGUAGE_ID, $languageId);
$flow->setData(FlowMailVariables::CONTACT_FORM_DATA, [
'email' => 'customer@example.com',
'firstName' => 'Max',
Expand Down Expand Up @@ -506,6 +511,7 @@ public function testActionExecutedWithRecipientFromStoreData(): void
'firstName' => 'Max',
'lastName' => 'Mustermann',
],
'languageId' => $languageId,
]
);

Expand Down
122 changes: 122 additions & 0 deletions tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php
@@ -0,0 +1,122 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Content\Flow\Dispatching\Storer\LanguageStorer;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\LanguageAware;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub\LanguageAwareEvent;
use Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub\NonLanguageAwareEvent;

/**
* @internal
*/
#[Package('services-settings')]
#[CoversClass(LanguageStorer::class)]
class LanguageStorerTest extends TestCase
{
/**
* @param array<string, mixed> $stored
* @param array<string, mixed> $expected
*/
#[DataProvider('storeDataProvider')]
public function testStore(FlowEventAware $event, array $stored, array $expected): void
{
$storer = new LanguageStorer();
$stored = $storer->store($event, $stored);

static::assertSame($expected, $stored);
}

public static function storeDataProvider(): \Generator
{
$languageId = Uuid::randomHex();

yield 'store null' => [
'event' => new LanguageAwareEvent(null),
'stored' => [],
'expected' => [LanguageAware::LANGUAGE_ID => null],
];

yield 'store id' => [
'event' => new LanguageAwareEvent($languageId),
'stored' => [],
'expected' => [LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'store existing' => [
'event' => new LanguageAwareEvent($languageId),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'store null with existing' => [
'event' => new LanguageAwareEvent(null),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => null],
];

yield 'store non language aware' => [
'event' => new NonLanguageAwareEvent(),
'stored' => [],
'expected' => [],
];

yield 'store non language aware with existing' => [
'event' => new NonLanguageAwareEvent(),
'stored' => ['message' => 'hi'],
'expected' => ['message' => 'hi'],
];

$languageId2 = Uuid::randomHex();

yield 'store overwrite' => [
'event' => new LanguageAwareEvent($languageId2),
'stored' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId],
'expected' => ['message' => 'hi', LanguageAware::LANGUAGE_ID => $languageId2],
];
}

/**
* @param array<string, mixed> $expected
*/
#[DataProvider('restoreDataProvider')]
public function testRestore(StorableFlow $flow, array $expected): void
{
$storer = new LanguageStorer();
$storer->restore($flow);

static::assertSame($expected, $flow->data());
}

public static function restoreDataProvider(): \Generator
{
$languageId = Uuid::randomHex();

yield 'restore empty' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), []),
'expected' => [],
];

yield 'restore id' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), [
LanguageAware::LANGUAGE_ID => $languageId,
]),
'expected' => [LanguageAware::LANGUAGE_ID => $languageId],
];

yield 'restore null' => [
'flow' => new StorableFlow('foo', Context::createDefaultContext(), [
LanguageAware::LANGUAGE_ID => null,
]),
'expected' => [LanguageAware::LANGUAGE_ID => null],
];
}
}
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\EventData\ScalarValueType;
use Shopware\Core\Framework\Event\FlowEventAware;
use Shopware\Core\Framework\Event\LanguageAware;

/**
* @internal
*/
class LanguageAwareEvent implements FlowEventAware, LanguageAware
{
public function __construct(private readonly ?string $languageId)
{
}

public function getName(): string
{
return 'test';
}

public function getContext(): Context
{
return Context::createDefaultContext();
}

public static function getAvailableData(): EventDataCollection
{
return (new EventDataCollection())
->add('languageId', new ScalarValueType(ScalarValueType::TYPE_STRING));
}

public function getLanguageId(): ?string
{
return $this->languageId;
}
}
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Flow\Dispatching\Storer\Stub;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\FlowEventAware;

/**
* @internal
*/
class NonLanguageAwareEvent implements FlowEventAware
{
public function getName(): string
{
return 'test';
}

public function getContext(): Context
{
return Context::createDefaultContext();
}

public static function getAvailableData(): EventDataCollection
{
return new EventDataCollection();
}
}

0 comments on commit 65c5c54

Please sign in to comment.