Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 14 | use PHPUnit\Framework\TestCase; |
| 15 | use Psr\Log\LoggerInterface; |
| 16 | use Symfony\Component\HttpClient\MockHttpClient; |
| 17 | use Symfony\Component\Translation\Dumper\XliffFileDumper; |
| 18 | use Symfony\Component\Translation\Loader\LoaderInterface; |
| 19 | use Symfony\Component\Translation\Provider\ProviderInterface; |
| 20 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 | |
| 22 | /** |
| 23 | * A test case to ease testing a translation provider. |
| 24 | * |
| 25 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 26 | * |
| 27 | * @internal |
| 28 | */ |
| 29 | abstract class ProviderTestCase extends TestCase |
| 30 | { |
| 31 | protected $client; |
| 32 | protected $logger; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 33 | protected string $defaultLocale; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 34 | protected $loader; |
| 35 | protected $xliffFileDumper; |
| 36 | |
| 37 | abstract public function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface; |
| 38 | |
| 39 | /** |
| 40 | * @return iterable<array{0: string, 1: ProviderInterface}> |
| 41 | */ |
| 42 | abstract public function toStringProvider(): iterable; |
| 43 | |
| 44 | /** |
| 45 | * @dataProvider toStringProvider |
| 46 | */ |
| 47 | public function testToString(ProviderInterface $provider, string $expected) |
| 48 | { |
| 49 | $this->assertSame($expected, (string) $provider); |
| 50 | } |
| 51 | |
| 52 | protected function getClient(): MockHttpClient |
| 53 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 54 | return $this->client ??= new MockHttpClient(); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 57 | protected function getLoader(): LoaderInterface |
| 58 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 59 | return $this->loader ??= $this->createMock(LoaderInterface::class); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 62 | protected function getLogger(): LoggerInterface |
| 63 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 64 | return $this->logger ??= $this->createMock(LoggerInterface::class); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | protected function getDefaultLocale(): string |
| 68 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 69 | return $this->defaultLocale ??= 'en'; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 72 | protected function getXliffFileDumper(): XliffFileDumper |
| 73 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 74 | return $this->xliffFileDumper ??= $this->createMock(XliffFileDumper::class); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 75 | } |
| 76 | } |