Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next-35521/auto-imported-from-github' into 'trunk'
NEXT-35521 - Add language to flow events See merge request shopware/6/product/platform!13672
- Loading branch information
Showing
9 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
changelog/_unreleased/2024-04-15-add-language-to-send-mail-event.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
issue: NEXT-35521 | ||
title: add-language-to-send-mail-event | ||
author: Jasper Peeters | ||
author_email: jasper.peeters@meteor.be | ||
author_github: JasperP98 | ||
--- | ||
# Core | ||
* Added `LanguageAware` and a `LanguageStorer` class to allow a specific language for flow events | ||
* Added language id property to databag for mail events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Core/Content/Flow/Dispatching/Storer/LanguageStorer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Content\Flow\Dispatching\Storer; | ||
|
||
use Shopware\Core\Content\Flow\Dispatching\StorableFlow; | ||
use Shopware\Core\Framework\Event\FlowEventAware; | ||
use Shopware\Core\Framework\Event\LanguageAware; | ||
use Shopware\Core\Framework\Log\Package; | ||
|
||
#[Package('services-settings')] | ||
class LanguageStorer extends FlowStorer | ||
{ | ||
/** | ||
* @param array<string, mixed> $stored | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function store(FlowEventAware $event, array $stored): array | ||
{ | ||
if (!$event instanceof LanguageAware) { | ||
return $stored; | ||
} | ||
|
||
$stored[LanguageAware::LANGUAGE_ID] = $event->getLanguageId(); | ||
|
||
return $stored; | ||
} | ||
|
||
public function restore(StorableFlow $storable): void | ||
{ | ||
if (!$storable->hasStore(LanguageAware::LANGUAGE_ID)) { | ||
return; | ||
} | ||
|
||
$storable->setData(LanguageAware::LANGUAGE_ID, $storable->getStore(LanguageAware::LANGUAGE_ID)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Shopware\Core\Framework\Event; | ||
|
||
use Shopware\Core\Framework\Log\Package; | ||
|
||
#[Package('services-settings')] | ||
#[IsFlowEventAware] | ||
interface LanguageAware | ||
{ | ||
public const LANGUAGE_ID = 'languageId'; | ||
|
||
public function getLanguageId(): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
tests/unit/Core/Content/Flow/Dispatching/Storer/LanguageStorerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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], | ||
]; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/LanguageAwareEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/unit/Core/Content/Flow/Dispatching/Storer/Stub/NonLanguageAwareEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |