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