Failed Conditions
Pull Request — master (#13)
by Adrien
14:06
created

TranslatorTest::getDefaultFelixContainer()

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\I18n;
6
7
use Ecodev\Felix\ConfigProvider;
8
use Ecodev\Felix\I18n\Translator;
9
use Laminas\ConfigAggregator\ArrayProvider;
10
use Laminas\ConfigAggregator\ConfigAggregator;
11
use Laminas\I18n\Translator\Loader\PhpArray;
12
use Laminas\ServiceManager\ServiceManager;
13
use PHPUnit\Framework\TestCase;
14
use Psr\Container\ContainerInterface;
15
16
class TranslatorTest extends TestCase
17
{
18
    protected function tearDown(): void
19
    {
20
        global $container;
21
        $container = null;
22
    }
23
24
    public function testTrWithLaminasI18n(): void
25
    {
26
        $aggregator = new ConfigAggregator([
27
            ConfigProvider::class,
28
            \Laminas\I18n\ConfigProvider::class,
29
            new ArrayProvider([
30
                'translator' => [
31
                    'translation_files' => [
32
                        [
33
                            'type' => PhpArray::class,
34
                            'filename' => 'tests/I18n/fr.php',
35
                        ],
36
                    ],
37
                ],
38
                'dependencies' => [
39
                    'factories' => [
40
                        Translator::class => \Laminas\I18n\Translator\TranslatorServiceFactory::class,
41
                    ],
42
                ],
43
            ]),
44
        ]);
45
46
        global $container;
47
        $container = $this->createContainer($aggregator);
48
49
        self::assertSame('translated value translated value', _tr('foo %param% param %param%', ['param' => 'value']));
50
    }
51
52
    public function testTrWithCustomTranslator(): void
53
    {
54
        $aggregator = new ConfigAggregator([
55
            new ArrayProvider([
56
                'dependencies' => [
57
                    'factories' => [
58
                        Translator::class => fn () => new class() implements Translator {
59
                            public function translate(string $message): string
60
                            {
61
                                return 'translated %param% translated %param%';
62
                            }
63
                        },
64
                    ],
65
                ],
66
            ]),
67
        ]);
68
69
        global $container;
70
        $container = $this->createContainer($aggregator);
71
72
        self::assertSame('translated value translated value', _tr('foo %param% param %param%', ['param' => 'value']));
73
    }
74
75
    /**
76
     * @dataProvider providerTr
77
     */
78
    public function testTr(string $message, array $replacements, string $expected): void
79
    {
80
        global $container;
81
        $container = $this->createContainer($this->getDefaultFelixContainer());
82
83
        self::assertSame($expected, _tr($message, $replacements));
84
    }
85
86
    public function providerTr(): array
87
    {
88
        return [
89
            [
90
                'foo',
91
                [],
92
                'foo',
93
            ],
94
            [
95
                'foo %param% param',
96
                [],
97
                'foo %param% param',
98
            ],
99
            [
100
                'foo %param% param %param%',
101
                ['param' => 'value'],
102
                'foo value param value',
103
            ],
104
            [
105
                '%not-recursive% %string% %int% %float% %null%',
106
                [
107
                    'not-recursive' => '%string%',
108
                    'string' => 'value',
109
                    'int' => 123,
110
                    'float' => 0.75,
111
                    'null' => null,
112
                ],
113
                '%string% value 123 0.75 ',
114
            ],
115
        ];
116
    }
117
118
    private function getDefaultFelixContainer(): ConfigAggregator
119
    {
120
        return new ConfigAggregator([
121
            ConfigProvider::class,
122
        ]);
123
    }
124
125
    private function createContainer(ConfigAggregator $aggregator): ContainerInterface
126
    {
127
        $config = $aggregator->getMergedConfig();
128
        $dependencies = $config['dependencies'];
129
        $dependencies['services']['config'] = $config;
130
131
        $container = new ServiceManager($dependencies);
132
133
        return $container;
134
    }
135
}
136