Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31547: Autowiring fails for CustomTag service when using 3rd party translator bundle #1335

Merged
merged 4 commits into from
Apr 17, 2020
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
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services/ui_config/mappers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
EzSystems\EzPlatformAdminUi\UI\Config\Mapper\FieldType\RichText\CustomTag:
autowire: true
arguments:
$translator: '@translator'
$translatorBag: '@translator'
$customTagsConfiguration: '%ezplatform.ezrichtext.custom_tags%'
$translationDomain: '%ezplatform.field_type.ezrichtext.custom_tags.translation_domain%'
$customTagAttributeMappers: !tagged ezplatform.admin_ui.richtext_custom_tag_attribute_mapper
Expand Down
35 changes: 23 additions & 12 deletions src/lib/Tests/UI/Config/Mapper/FieldType/RichText/CustomTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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\Component\Translation\TranslatorInterface;

/**
* UI Config Mapper test for RichText Custom Tags configuration.
Expand All @@ -31,7 +32,8 @@ public function testMapConfig(array $customTagsConfiguration, array $enabledCust
{
$mapper = new CustomTag(
$customTagsConfiguration,
$this->getTranslatorMock(),
$this->getTranslatorInterfaceMock(),
$this->getTranslatorBagInterfaceMock(),
'custom_tags',
$this->getPackagesMock(),
new ArrayObject([
Expand Down Expand Up @@ -163,7 +165,22 @@ public function providerForTestMapConfig(): array
/**
* @return \Symfony\Component\Translation\TranslatorInterface|\PHPUnit\Framework\MockObject\MockObject
*/
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 \Symfony\Component\Translation\TranslatorBagInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private function getTranslatorBagInterfaceMock(): MockObject
{
$catalogueMock = $this->createMock(MessageCatalogueInterface::class);
$catalogueMock
Expand All @@ -172,21 +189,15 @@ private function getTranslatorMock(): MockObject
->withAnyParameters()
->willReturn(false);

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

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

return $translatorMock;
return $translatorBagMock;
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/lib/UI/Config/Mapper/FieldType/RichText/CustomTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use EzSystems\EzPlatformAdminUi\UI\Config\Mapper\FieldType\RichText\CustomTag\AttributeMapper;
use RuntimeException;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Traversable;

/**
Expand All @@ -22,9 +23,12 @@ class CustomTag
/** @var array */
private $customTagsConfiguration;

/** @var Translator */
/** @var \Symfony\Component\Translation\TranslatorInterface */
private $translator;

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

/** @var Packages */
private $packages;

Expand All @@ -44,20 +48,23 @@ class CustomTag
* both TranslatorInterface and TranslatorBagInterface.
*
* @param array $customTagsConfiguration
* @param \Symfony\Component\Translation\Translator $translator
* @param \Symfony\Component\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,
Translator $translator,
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 @@ -169,7 +176,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