-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #322 [SFBundle] Easier translation of order-field (sstok)
This PR was merged into the 2.0-dev branch. Discussion ---------- | Q | A | ------------- | --- | Bug fix? | no | New feature? |yes | BC breaks? | yes/no | Deprecations? | no | Tickets | | License | MIT When the field is an order field but the translated label doesn't begin with an-`@` prepend the `@`-sign. Commits ------- c4d054b [SFBundle] Easier translation of order-field
- Loading branch information
Showing
2 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
lib/Symfony/SearchBundle/Tests/TranslatorBasedAliasResolverTest.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,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the RollerworksSearch package. | ||
* | ||
* (c) Sebastiaan Stok <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Rollerworks\Bundle\SearchBundle\Tests; | ||
|
||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Rollerworks\Bundle\SearchBundle\TranslatorBasedAliasResolver; | ||
use Rollerworks\Bundle\SearchBundle\Type\TranslatableFieldTypeExtension; | ||
use Rollerworks\Bundle\SearchBundle\Type\TranslatableOrderFieldTypeExtension; | ||
use Rollerworks\Component\Search\Extension\Core\Type\TextType; | ||
use Rollerworks\Component\Search\Field\OrderFieldType; | ||
use Rollerworks\Component\Search\Test\SearchIntegrationTestCase; | ||
use Symfony\Component\Translation\TranslatableMessage; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TranslatorBasedAliasResolverTest extends SearchIntegrationTestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** @test */ | ||
public function it_uses_field_name_when_label_is_empty(): void | ||
{ | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
$translator->expects($this->never())->method('trans'); | ||
|
||
$resolver = new TranslatorBasedAliasResolver($translator); | ||
$searchFactory = $this->getFactory(); | ||
|
||
$this->assertEquals('id', $resolver($searchFactory->createField('id', TextType::class))); | ||
$this->assertEquals('name', $resolver($searchFactory->createField('name', TextType::class))); | ||
$this->assertEquals('@id', $resolver($searchFactory->createField('@id', OrderFieldType::class))); | ||
} | ||
|
||
/** @test */ | ||
public function it_keeps_label_as_is_when_not_translatable(): void | ||
{ | ||
$translator = $this->createMock(TranslatorInterface::class); | ||
$translator->expects($this->never())->method('trans'); | ||
|
||
$resolver = new TranslatorBasedAliasResolver($translator); | ||
$searchFactory = $this->getFactory(); | ||
|
||
$this->assertEquals('id2', $resolver($searchFactory->createField('id', TextType::class, ['label' => 'id2']))); | ||
$this->assertEquals('name2', $resolver($searchFactory->createField('name', TextType::class, ['label' => 'name2']))); | ||
$this->assertEquals('@id2', $resolver($searchFactory->createField('@id', OrderFieldType::class, ['label' => '@id2']))); | ||
$this->assertEquals('@id2', $resolver($searchFactory->createField('@id', OrderFieldType::class, ['label' => 'id2']))); | ||
} | ||
|
||
/** @test */ | ||
public function it_translates_label_when_translatable(): void | ||
{ | ||
$translator = $this->prophesize(TranslatorInterface::class); | ||
$translator->trans(Argument::cetera())->will(static fn(array $args): string => $args[0][0] === '@' ? $args[0] . 'T' : 'T' . $args[0]); | ||
|
||
$resolver = new TranslatorBasedAliasResolver($translator->reveal()); | ||
$searchFactory = $this->getFactory(); | ||
|
||
$this->assertEquals('Tid2', $resolver($searchFactory->createField('id', TextType::class, ['label' => new TranslatableMessage('id2')]))); | ||
$this->assertEquals('Tname2', $resolver($searchFactory->createField('name', TextType::class, ['label' => new TranslatableMessage('name2')]))); | ||
$this->assertEquals('@id2T', $resolver($searchFactory->createField('@id', OrderFieldType::class, ['label' => new TranslatableMessage('@id2')]))); | ||
$this->assertEquals('@Tid2', $resolver($searchFactory->createField('@id', OrderFieldType::class, ['label' => new TranslatableMessage('id2')]))); | ||
} | ||
|
||
protected function getTypeExtensions(): array | ||
{ | ||
return [ | ||
new TranslatableFieldTypeExtension(), | ||
new TranslatableOrderFieldTypeExtension(), | ||
]; | ||
} | ||
} |
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