Skip to content

[Translation] Improve tests coverage #52746

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

Merged
merged 1 commit into from
Nov 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function __construct(TranslatorInterface $translator = null, IntlFormatte

public function format(string $message, string $locale, array $parameters = []): string
{
if ($this->translator instanceof TranslatorInterface) {
return $this->translator->trans($message, $parameters, null, $locale);
}

return strtr($message, $parameters);
return $this->translator->trans($message, $parameters, null, $locale);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->translator cannot be anything else than a TranslatorInterface, as typed in the constructor.

}

public function formatIntl(string $message, string $locale, array $parameters = []): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Tests\Catalogue;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Exception\LogicException;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageCatalogueInterface;

Expand Down Expand Up @@ -70,5 +71,15 @@ public function testGetEmptyResult()
);
}

public function testSourceAndTargetWithDifferentLocales()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Operated catalogues must belong to the same locale.');
$this->createOperation(
new MessageCatalogue('en'),
new MessageCatalogue('fr')
);
}

abstract protected function createOperation(MessageCatalogueInterface $source, MessageCatalogueInterface $target);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Translation\Tests\DataCollector;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;

Expand Down Expand Up @@ -130,6 +132,24 @@ public function testCollect()
$this->assertEquals($expectedMessages, array_values($dataCollector->getMessages()->getValue(true)));
}

public function testCollectAndReset()
{
$translator = $this->getTranslator();
$translator->method('getLocale')->willReturn('fr');
$translator->method('getFallbackLocales')->willReturn(['en']);

$dataCollector = new TranslationDataCollector($translator);
$dataCollector->collect($this->createMock(Request::class), $this->createMock(Response::class));

$this->assertSame('fr', $dataCollector->getLocale());
$this->assertSame(['en'], $dataCollector->getFallbackLocales());

$dataCollector->reset();

$this->assertNull($dataCollector->getLocale());
$this->assertEmpty($dataCollector->getFallbackLocales());
}

private function getTranslator()
{
$translator = $this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function testExceptionWithDebugMessage()
$mock->method('getInfo')->willReturn('debug');

$exception = new ProviderException('Exception message', $mock, 503);

self::assertInstanceOf(ProviderException::class, $exception);
$this->assertSame('debug', $exception->getDebug());
}

public function testExceptionWithNullAsDebugMessage()
Expand All @@ -33,7 +32,6 @@ public function testExceptionWithNullAsDebugMessage()
$mock->method('getInfo')->willReturn(null);

$exception = new ProviderException('Exception message', $mock, 503);

self::assertInstanceOf(ProviderException::class, $exception);
$this->assertSame('', $exception->getDebug());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Tests\Provider;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\Provider\TranslationProviderCollection;

class TranslationProviderCollectionTest extends TestCase
{
public function testKeys()
{
$this->assertSame(['foo', 'baz'], $this->createProviderCollection()->keys());
}

public function testKeysWithGenerator()
{
$this->assertSame(['foo', 'baz'], (new TranslationProviderCollection(
(function () {
yield 'foo' => $this->createMock(ProviderInterface::class);

yield 'baz' => $this->createMock(ProviderInterface::class);
})()
))->keys());
}

public function testToString()
{
$this->assertSame('[foo,baz]', (string) $this->createProviderCollection());
}

public function testHas()
{
$this->assertTrue($this->createProviderCollection()->has('foo'));
}

public function testGet()
{
$provider = $this->createMock(ProviderInterface::class);

$this->assertSame($provider, (new TranslationProviderCollection([
'foo' => $provider,
'baz' => $this->createMock(ProviderInterface::class),
]))->get('foo'));
}

public function testGetThrowsException()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Provider "invalid" not found. Available: "[foo,baz]".');

$this->createProviderCollection()->get('invalid');
}

private function createProviderCollection(): TranslationProviderCollection
{
return new TranslationProviderCollection([
'foo' => $this->createMock(ProviderInterface::class),
'baz' => $this->createMock(ProviderInterface::class),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Dumper\DumperInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Exception\RuntimeException;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;

Expand All @@ -29,4 +31,33 @@ public function testWrite()
$writer->addDumper('test', $dumper);
$writer->write(new MessageCatalogue('en'), 'test');
}

public function testGetFormats()
{
$writer = new TranslationWriter();
$writer->addDumper('foo', $this->createMock(DumperInterface::class));
$writer->addDumper('bar', $this->createMock(DumperInterface::class));

$this->assertEquals(['foo', 'bar'], $writer->getFormats());
}

public function testFormatIsNotSupported()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('There is no dumper associated with format "foo".');
$writer = new TranslationWriter();

$writer->write(new MessageCatalogue('en'), 'foo');
}

public function testUnwritableDirectory()
{
$writer = new TranslationWriter();
$writer->addDumper('foo', $this->createMock(DumperInterface::class));

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Translation Writer was not able to create directory "/foo/bar/baz".');

$writer->write(new MessageCatalogue('en'), 'foo', ['path' => '/foo/bar/baz']);
}
}