| 148 |
lars |
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\Test;
|
|
|
13 |
|
|
|
14 |
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
15 |
use PHPUnit\Framework\TestCase;
|
|
|
16 |
use Psr\Log\LoggerInterface;
|
|
|
17 |
use Symfony\Component\HttpClient\MockHttpClient;
|
|
|
18 |
use Symfony\Component\Translation\Dumper\XliffFileDumper;
|
|
|
19 |
use Symfony\Component\Translation\Exception\IncompleteDsnException;
|
|
|
20 |
use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
|
|
|
21 |
use Symfony\Component\Translation\Loader\LoaderInterface;
|
|
|
22 |
use Symfony\Component\Translation\Provider\Dsn;
|
|
|
23 |
use Symfony\Component\Translation\Provider\ProviderFactoryInterface;
|
|
|
24 |
use Symfony\Component\Translation\TranslatorBagInterface;
|
|
|
25 |
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* A test case to ease testing a translation provider factory.
|
|
|
29 |
*
|
|
|
30 |
* @author Mathieu Santostefano <msantostefano@protonmail.com>
|
|
|
31 |
*
|
|
|
32 |
* @internal
|
|
|
33 |
*/
|
|
|
34 |
abstract class ProviderFactoryTestCase extends TestCase
|
|
|
35 |
{
|
|
|
36 |
protected HttpClientInterface $client;
|
|
|
37 |
protected LoggerInterface|MockObject $logger;
|
|
|
38 |
protected string $defaultLocale;
|
|
|
39 |
protected LoaderInterface|MockObject $loader;
|
|
|
40 |
protected XliffFileDumper|MockObject $xliffFileDumper;
|
|
|
41 |
protected TranslatorBagInterface|MockObject $translatorBag;
|
|
|
42 |
|
|
|
43 |
abstract public function createFactory(): ProviderFactoryInterface;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @return iterable<array{0: bool, 1: string}>
|
|
|
47 |
*/
|
|
|
48 |
abstract public function supportsProvider(): iterable;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return iterable<array{0: string, 1: string, 2: TransportInterface}>
|
|
|
52 |
*/
|
|
|
53 |
abstract public function createProvider(): iterable;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @return iterable<array{0: string, 1: string|null}>
|
|
|
57 |
*/
|
|
|
58 |
public function unsupportedSchemeProvider(): iterable
|
|
|
59 |
{
|
|
|
60 |
return [];
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* @return iterable<array{0: string, 1: string|null}>
|
|
|
65 |
*/
|
|
|
66 |
public function incompleteDsnProvider(): iterable
|
|
|
67 |
{
|
|
|
68 |
return [];
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @dataProvider supportsProvider
|
|
|
73 |
*/
|
|
|
74 |
public function testSupports(bool $expected, string $dsn)
|
|
|
75 |
{
|
|
|
76 |
$factory = $this->createFactory();
|
|
|
77 |
|
|
|
78 |
$this->assertSame($expected, $factory->supports(new Dsn($dsn)));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @dataProvider createProvider
|
|
|
83 |
*/
|
|
|
84 |
public function testCreate(string $expected, string $dsn)
|
|
|
85 |
{
|
|
|
86 |
$factory = $this->createFactory();
|
|
|
87 |
$provider = $factory->create(new Dsn($dsn));
|
|
|
88 |
|
|
|
89 |
$this->assertSame($expected, (string) $provider);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* @dataProvider unsupportedSchemeProvider
|
|
|
94 |
*/
|
|
|
95 |
public function testUnsupportedSchemeException(string $dsn, string $message = null)
|
|
|
96 |
{
|
|
|
97 |
$factory = $this->createFactory();
|
|
|
98 |
|
|
|
99 |
$dsn = new Dsn($dsn);
|
|
|
100 |
|
|
|
101 |
$this->expectException(UnsupportedSchemeException::class);
|
|
|
102 |
if (null !== $message) {
|
|
|
103 |
$this->expectExceptionMessage($message);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$factory->create($dsn);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* @dataProvider incompleteDsnProvider
|
|
|
111 |
*/
|
|
|
112 |
public function testIncompleteDsnException(string $dsn, string $message = null)
|
|
|
113 |
{
|
|
|
114 |
$factory = $this->createFactory();
|
|
|
115 |
|
|
|
116 |
$dsn = new Dsn($dsn);
|
|
|
117 |
|
|
|
118 |
$this->expectException(IncompleteDsnException::class);
|
|
|
119 |
if (null !== $message) {
|
|
|
120 |
$this->expectExceptionMessage($message);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
$factory->create($dsn);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
protected function getClient(): HttpClientInterface
|
|
|
127 |
{
|
|
|
128 |
return $this->client ??= new MockHttpClient();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
protected function getLogger(): LoggerInterface
|
|
|
132 |
{
|
|
|
133 |
return $this->logger ??= $this->createMock(LoggerInterface::class);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
protected function getDefaultLocale(): string
|
|
|
137 |
{
|
|
|
138 |
return $this->defaultLocale ??= 'en';
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
protected function getLoader(): LoaderInterface
|
|
|
142 |
{
|
|
|
143 |
return $this->loader ??= $this->createMock(LoaderInterface::class);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
protected function getXliffFileDumper(): XliffFileDumper
|
|
|
147 |
{
|
|
|
148 |
return $this->xliffFileDumper ??= $this->createMock(XliffFileDumper::class);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
protected function getTranslatorBag(): TranslatorBagInterface
|
|
|
152 |
{
|
|
|
153 |
return $this->translatorBag ??= $this->createMock(TranslatorBagInterface::class);
|
|
|
154 |
}
|
|
|
155 |
}
|