Skip to content

Commit

Permalink
EZP-31547: Autowiring fails for CustomTag service when using 3rd part…
Browse files Browse the repository at this point in the history
…y translator bundle (#142)
  • Loading branch information
mateuszbieniek authored Apr 24, 2020
1 parent 10b1f6e commit 3b52bde
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/bundle/Resources/config/ui/mappers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ services:
EzSystems\EzPlatformRichText\Configuration\UI\Mapper\CustomTag:
arguments:
$customTagsConfiguration: '%ezplatform.ezrichtext.custom_tags%'
$translatorBag: '@translator'
$translationDomain: '%ezrichtext.custom_tags.translation_domain%'
$customTagAttributeMappers: !tagged ezrichtext.configuration.custom_tag.mapper

Expand Down
9 changes: 8 additions & 1 deletion src/lib/Configuration/UI/Mapper/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use EzSystems\EzPlatformRichText\Configuration\UI\Mapper\CustomTag\AttributeMapper;
use RuntimeException;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Traversable;

Expand All @@ -27,6 +28,9 @@ final class CustomTag implements CustomTemplateConfigMapper
/** @var \Symfony\Contracts\Translation\TranslatorInterface */
private $translator;

/** @var \Symfony\Component\Translation\TranslatorBagInterface */
private $translatorBag;

/** @var \Symfony\Component\Asset\Packages */
private $packages;

Expand All @@ -47,19 +51,22 @@ final class CustomTag implements CustomTemplateConfigMapper
*
* @param array $customTagsConfiguration
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
* @param \Symfony\Component\Translation\TranslatorBagInterface $translatorBag
* @param string $translationDomain
* @param \Symfony\Component\Asset\Packages $packages
* @param \Traversable $customTagAttributeMappers
*/
public function __construct(
array $customTagsConfiguration,
TranslatorInterface $translator,
TranslatorBagInterface $translatorBag,
string $translationDomain,
Packages $packages,
Traversable $customTagAttributeMappers
) {
$this->customTagsConfiguration = $customTagsConfiguration;
$this->translator = $translator;
$this->translatorBag = $translatorBag;
$this->translationDomain = $translationDomain;
$this->packages = $packages;
$this->customTagAttributeMappers = $customTagAttributeMappers;
Expand Down Expand Up @@ -171,7 +178,7 @@ private function translateLabels(array $config): array
continue;
}

$transCatalogue = $this->translator->getCatalogue();
$transCatalogue = $this->translatorBag->getCatalogue();
foreach ($tagConfig['attributes'] as $attributeName => $attributeConfig) {
$config[$tagName]['attributes'][$attributeName]['label'] = $this->translator->trans(
/** @Ignore */
Expand Down
35 changes: 23 additions & 12 deletions tests/lib/Configuration/UI/Mapper/CustomTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* UI Config Mapper test for RichText Custom Tags configuration.
Expand All @@ -39,7 +40,8 @@ public function testMapConfig(
) {
$mapper = new CustomTag(
$customTagsConfiguration,
$this->getTranslatorMock(),
$this->getTranslatorInterfaceMock(),
$this->getTranslatorBagInterfaceMock(),
'custom_tags',
$this->getPackagesMock(),
new ArrayObject(
Expand Down Expand Up @@ -173,7 +175,22 @@ public function providerForTestMapConfig(): array
/**
* @return \PHPUnit\Framework\MockObject\MockObject|\Symfony\Contracts\Translation\TranslatorInterface
*/
private function getTranslatorMock(): MockObject
private function getTranslatorInterfaceMock(): MockObject
{
$translatorInterfaceMock = $this->createMock(TranslatorInterface::class);
$translatorInterfaceMock
->expects($this->any())
->method('trans')
->withAnyParameters()
->willReturnArgument(0);

return $translatorInterfaceMock;
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\Translation\TranslatorBagInterface
*/
private function getTranslatorBagInterfaceMock(): MockObject
{
$catalogueMock = $this->createMock(MessageCatalogueInterface::class);
$catalogueMock
Expand All @@ -182,21 +199,15 @@ private function getTranslatorMock(): MockObject
->withAnyParameters()
->willReturn(false);

$translatorMock = $this->createMock(Translator::class);
$translatorMock
$translatorBagInterfaceMock = $this->createMock(TranslatorBagInterface::class);
$translatorBagInterfaceMock
->expects($this->any())
->method('getCatalogue')
->willReturn(
$catalogueMock
);

$translatorMock
->expects($this->any())
->method('trans')
->withAnyParameters()
->willReturnArgument(0);

return $translatorMock;
return $translatorBagInterfaceMock;
}

/**
Expand Down

0 comments on commit 3b52bde

Please sign in to comment.