Passed
Push — master ( d2e487...b38026 )
by Tobias
05:31
created

FallbackTranslatorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 42
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testTranslateWithSubstitutedParameters() 0 25 1
A testWithNotLocaleAwareTranslator() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
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 Translation\Bundle\Tests\Unit\Translator;
13
14
use Nyholm\NSA;
15
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
17
use Translation\Bundle\Translator\FallbackTranslator;
18
use Translation\Bundle\Translator\TranslatorInterface;
19
use Translation\Translator\Translator;
20
use Translation\Translator\TranslatorService;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class FallbackTranslatorTest extends TestCase
26
{
27
    public function testWithNotLocaleAwareTranslator()
28
    {
29
        if (!\interface_exists(NewTranslatorInterface::class)) {
30
            $this->markTestSkipped('Relevant only when NewTranslatorInterface is available.');
31
        }
32
33
        $symfonyTranslator = $this->getMockBuilder(NewTranslatorInterface::class)->getMock();
34
        $translator = new Translator();
35
36
        $this->expectException(\InvalidArgumentException::class);
37
        $this->expectExceptionMessage('The given translator must implements LocaleAwareInterface.');
38
39
        new FallbackTranslator('en', $symfonyTranslator, $translator);
40
    }
41
42
    public function testTranslateWithSubstitutedParameters(): void
43
    {
44
        $symfonyTranslator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
45
46
        $translatorService = $this->getMockBuilder(TranslatorService::class)
47
            ->setMethods(['translate'])
48
            ->getMock();
49
        $translatorService->method('translate')->willReturnArgument(0);
50
51
        $translator = new Translator();
52
        $translator->addTranslatorService($translatorService);
53
54
        $service = new FallbackTranslator('en', $symfonyTranslator, $translator);
55
56
        // One parameter test
57
        $result = NSA::invokeMethod($service, 'translateWithSubstitutedParameters', 'abc bar abc', 'en', ['%foo%' => 'bar']);
58
        $this->assertEquals('abc bar abc', $result);
59
60
        // Two parameters test
61
        $result = NSA::invokeMethod($service, 'translateWithSubstitutedParameters', 'abc bar abc baz', 'en', ['%foo%' => 'bar', '%biz%' => 'baz']);
62
        $this->assertEquals('abc bar abc baz', $result);
63
64
        // Test with object
65
        $result = NSA::invokeMethod($service, 'translateWithSubstitutedParameters', 'abc object abc', 'en', ['%foo%' => new Minor('object')]);
66
        $this->assertEquals('abc object abc', $result);
67
    }
68
}
69
70
class Minor
71
{
72
    private $name;
73
74
    public function __construct($name)
75
    {
76
        $this->name = $name;
77
    }
78
79
    public function __toString()
80
    {
81
        return $this->name;
82
    }
83
}
84