blob: 6d5f4b7bf7dca004929f054c572143df70c1f268 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Test;
13
14use PHPUnit\Framework\TestCase;
15use Psr\Log\LoggerInterface;
16use Symfony\Component\HttpClient\MockHttpClient;
17use Symfony\Component\Translation\Dumper\XliffFileDumper;
18use Symfony\Component\Translation\Exception\IncompleteDsnException;
19use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
20use Symfony\Component\Translation\Loader\LoaderInterface;
21use Symfony\Component\Translation\Provider\Dsn;
22use Symfony\Component\Translation\Provider\ProviderFactoryInterface;
23use 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 */
32abstract class ProviderFactoryTestCase extends TestCase
33{
34 protected $client;
35 protected $logger;
36 protected $defaultLocale;
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 {
125 return $this->client ?? $this->client = new MockHttpClient();
126 }
127
128 protected function getLogger(): LoggerInterface
129 {
130 return $this->logger ?? $this->logger = $this->createMock(LoggerInterface::class);
131 }
132
133 protected function getDefaultLocale(): string
134 {
135 return $this->defaultLocale ?? $this->defaultLocale = 'en';
136 }
137
138 protected function getLoader(): LoaderInterface
139 {
140 return $this->loader ?? $this->loader = $this->createMock(LoaderInterface::class);
141 }
142
143 protected function getXliffFileDumper(): XliffFileDumper
144 {
145 return $this->xliffFileDumper ?? $this->xliffFileDumper = $this->createMock(XliffFileDumper::class);
146 }
147}