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\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\Loader\LoaderInterface; |
| 20 | use Symfony\Component\Translation\Provider\ProviderInterface; |
| 21 | use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 | |
| 23 | /** |
| 24 | * A test case to ease testing a translation provider. |
| 25 | * |
| 26 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 27 | * |
| 28 | * @internal |
| 29 | */ |
| 30 | abstract class ProviderTestCase extends TestCase |
| 31 | { |
| 32 | protected $client; |
| 33 | protected $logger; |
| 34 | protected $defaultLocale; |
| 35 | protected $loader; |
| 36 | protected $xliffFileDumper; |
| 37 | |
| 38 | abstract public function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface; |
| 39 | |
| 40 | /** |
| 41 | * @return iterable<array{0: string, 1: ProviderInterface}> |
| 42 | */ |
| 43 | abstract public function toStringProvider(): iterable; |
| 44 | |
| 45 | /** |
| 46 | * @dataProvider toStringProvider |
| 47 | */ |
| 48 | public function testToString(ProviderInterface $provider, string $expected) |
| 49 | { |
| 50 | $this->assertSame($expected, (string) $provider); |
| 51 | } |
| 52 | |
| 53 | protected function getClient(): MockHttpClient |
| 54 | { |
| 55 | return $this->client ?? $this->client = new MockHttpClient(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return LoaderInterface&MockObject |
| 60 | */ |
| 61 | protected function getLoader(): LoaderInterface |
| 62 | { |
| 63 | return $this->loader ?? $this->loader = $this->createMock(LoaderInterface::class); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @return LoaderInterface&MockObject |
| 68 | */ |
| 69 | protected function getLogger(): LoggerInterface |
| 70 | { |
| 71 | return $this->logger ?? $this->logger = $this->createMock(LoggerInterface::class); |
| 72 | } |
| 73 | |
| 74 | protected function getDefaultLocale(): string |
| 75 | { |
| 76 | return $this->defaultLocale ?? $this->defaultLocale = 'en'; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return LoaderInterface&MockObject |
| 81 | */ |
| 82 | protected function getXliffFileDumper(): XliffFileDumper |
| 83 | { |
| 84 | return $this->xliffFileDumper ?? $this->xliffFileDumper = $this->createMock(XliffFileDumper::class); |
| 85 | } |
| 86 | } |