Skip to content

Commit f49dcf5

Browse files
[Translation] Improve tests coverage
1 parent a4a843e commit f49dcf5

File tree

6 files changed

+136
-9
lines changed

6 files changed

+136
-9
lines changed

src/Symfony/Component/Translation/Formatter/MessageFormatter.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ public function __construct(TranslatorInterface $translator = null, IntlFormatte
3636

3737
public function format(string $message, string $locale, array $parameters = []): string
3838
{
39-
if ($this->translator instanceof TranslatorInterface) {
40-
return $this->translator->trans($message, $parameters, null, $locale);
41-
}
42-
43-
return strtr($message, $parameters);
39+
return $this->translator->trans($message, $parameters, null, $locale);
4440
}
4541

4642
public function formatIntl(string $message, string $locale, array $parameters = []): string

src/Symfony/Component/Translation/Tests/Catalogue/AbstractOperationTestCase.php

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Translation\Tests\Catalogue;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\Exception\LogicException;
1516
use Symfony\Component\Translation\MessageCatalogue;
1617
use Symfony\Component\Translation\MessageCatalogueInterface;
1718

@@ -70,5 +71,15 @@ public function testGetEmptyResult()
7071
);
7172
}
7273

74+
public function testSourceAndTargetWithDifferentLocales()
75+
{
76+
$this->expectException(LogicException::class);
77+
$this->expectExceptionMessage('Operated catalogues must belong to the same locale.');
78+
$this->createOperation(
79+
new MessageCatalogue('en'),
80+
new MessageCatalogue('fr')
81+
);
82+
}
83+
7384
abstract protected function createOperation(MessageCatalogueInterface $source, MessageCatalogueInterface $target);
7485
}

src/Symfony/Component/Translation/Tests/DataCollector/TranslationDataCollectorTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Translation\Tests\DataCollector;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
1517
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
1618
use Symfony\Component\Translation\DataCollectorTranslator;
1719

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

135+
public function testCollectAndReset()
136+
{
137+
$translator = $this->getTranslator();
138+
$translator->method('getLocale')->willReturn('fr');
139+
$translator->method('getFallbackLocales')->willReturn(['en']);
140+
141+
$dataCollector = new TranslationDataCollector($translator);
142+
$dataCollector->collect($this->createMock(Request::class), $this->createMock(Response::class));
143+
144+
$this->assertSame('fr', $dataCollector->getLocale());
145+
$this->assertSame(['en'], $dataCollector->getFallbackLocales());
146+
147+
$dataCollector->reset();
148+
149+
$this->assertNull($dataCollector->getLocale());
150+
$this->assertEmpty($dataCollector->getFallbackLocales());
151+
}
152+
133153
private function getTranslator()
134154
{
135155
$translator = $this

src/Symfony/Component/Translation/Tests/Exception/ProviderExceptionTest.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public function testExceptionWithDebugMessage()
2323
$mock->method('getInfo')->willReturn('debug');
2424

2525
$exception = new ProviderException('Exception message', $mock, 503);
26-
27-
self::assertInstanceOf(ProviderException::class, $exception);
26+
$this->assertSame('debug', $exception->getDebug());
2827
}
2928

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

3534
$exception = new ProviderException('Exception message', $mock, 503);
36-
37-
self::assertInstanceOf(ProviderException::class, $exception);
35+
$this->assertSame('', $exception->getDebug());
3836
}
3937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests\Provider;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\Provider\ProviderInterface;
16+
use Symfony\Component\Translation\Provider\TranslationProviderCollection;
17+
18+
class TranslationProviderCollectionTest extends TestCase
19+
{
20+
public function testKeys()
21+
{
22+
$this->assertSame(['foo', 'baz'], $this->createProviderCollection()->keys());
23+
}
24+
25+
public function testKeysWithGenerator()
26+
{
27+
$this->assertSame(['foo', 'baz'], (new TranslationProviderCollection(
28+
(function () {
29+
yield 'foo' => $this->createMock(ProviderInterface::class);
30+
31+
yield 'baz' => $this->createMock(ProviderInterface::class);
32+
})()
33+
))->keys());
34+
}
35+
36+
public function testToString()
37+
{
38+
$this->assertSame('[foo,baz]', (string) $this->createProviderCollection());
39+
}
40+
41+
public function testHas()
42+
{
43+
$this->assertTrue($this->createProviderCollection()->has('foo'));
44+
}
45+
46+
public function testGet()
47+
{
48+
$provider = $this->createMock(ProviderInterface::class);
49+
50+
$this->assertSame($provider, (new TranslationProviderCollection([
51+
'foo' => $provider,
52+
'baz' => $this->createMock(ProviderInterface::class),
53+
]))->get('foo'));
54+
}
55+
56+
public function testGetThrowsException()
57+
{
58+
$this->expectException(\InvalidArgumentException::class);
59+
$this->expectExceptionMessage('Provider "invalid" not found. Available: "[foo,baz]".');
60+
61+
$this->createProviderCollection()->get('invalid');
62+
}
63+
64+
private function createProviderCollection(): TranslationProviderCollection
65+
{
66+
return new TranslationProviderCollection([
67+
'foo' => $this->createMock(ProviderInterface::class),
68+
'baz' => $this->createMock(ProviderInterface::class),
69+
]);
70+
}
71+
}

src/Symfony/Component/Translation/Tests/Writer/TranslationWriterTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Translation\Dumper\DumperInterface;
16+
use Symfony\Component\Translation\Exception\InvalidArgumentException;
17+
use Symfony\Component\Translation\Exception\RuntimeException;
1618
use Symfony\Component\Translation\MessageCatalogue;
1719
use Symfony\Component\Translation\Writer\TranslationWriter;
1820

@@ -29,4 +31,33 @@ public function testWrite()
2931
$writer->addDumper('test', $dumper);
3032
$writer->write(new MessageCatalogue('en'), 'test');
3133
}
34+
35+
public function testGetFormats()
36+
{
37+
$writer = new TranslationWriter();
38+
$writer->addDumper('foo', $this->createMock(DumperInterface::class));
39+
$writer->addDumper('bar', $this->createMock(DumperInterface::class));
40+
41+
$this->assertEquals(['foo', 'bar'], $writer->getFormats());
42+
}
43+
44+
public function testFormatIsNotSupported()
45+
{
46+
$this->expectException(InvalidArgumentException::class);
47+
$this->expectExceptionMessage('There is no dumper associated with format "foo".');
48+
$writer = new TranslationWriter();
49+
50+
$writer->write(new MessageCatalogue('en'), 'foo');
51+
}
52+
53+
public function testUnwritableDirectory()
54+
{
55+
$writer = new TranslationWriter();
56+
$writer->addDumper('foo', $this->createMock(DumperInterface::class));
57+
58+
$this->expectException(RuntimeException::class);
59+
$this->expectExceptionMessage('Translation Writer was not able to create directory "/foo/bar/baz".');
60+
61+
$writer->write(new MessageCatalogue('en'), 'foo', ['path' => '/foo/bar/baz']);
62+
}
3263
}

0 commit comments

Comments
 (0)