You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.9 KiB

3 years ago
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Test;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpClient\MockHttpClient;
  14. use Symfony\Component\Translation\Dumper\XliffFileDumper;
  15. use Symfony\Component\Translation\Exception\IncompleteDsnException;
  16. use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
  17. use Symfony\Component\Translation\Loader\LoaderInterface;
  18. use Symfony\Component\Translation\Provider\Dsn;
  19. use Symfony\Component\Translation\Provider\ProviderFactoryInterface;
  20. use Symfony\Contracts\HttpClient\HttpClientInterface;
  21. /**
  22. * A test case to ease testing a translation provider factory.
  23. *
  24. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  25. *
  26. * @internal
  27. */
  28. abstract class ProviderFactoryTestCase extends TestCase
  29. {
  30. protected $client;
  31. protected $logger;
  32. protected $defaultLocale;
  33. protected $loader;
  34. protected $xliffFileDumper;
  35. abstract public function createFactory(): ProviderFactoryInterface;
  36. /**
  37. * @return iterable<array{0: bool, 1: string}>
  38. */
  39. abstract public function supportsProvider(): iterable;
  40. /**
  41. * @return iterable<array{0: string, 1: string, 2: TransportInterface}>
  42. */
  43. abstract public function createProvider(): iterable;
  44. /**
  45. * @return iterable<array{0: string, 1: string|null}>
  46. */
  47. public function unsupportedSchemeProvider(): iterable
  48. {
  49. return [];
  50. }
  51. /**
  52. * @return iterable<array{0: string, 1: string|null}>
  53. */
  54. public function incompleteDsnProvider(): iterable
  55. {
  56. return [];
  57. }
  58. /**
  59. * @dataProvider supportsProvider
  60. */
  61. public function testSupports(bool $expected, string $dsn)
  62. {
  63. $factory = $this->createFactory();
  64. $this->assertSame($expected, $factory->supports(new Dsn($dsn)));
  65. }
  66. /**
  67. * @dataProvider createProvider
  68. */
  69. public function testCreate(string $expected, string $dsn)
  70. {
  71. $factory = $this->createFactory();
  72. $provider = $factory->create(new Dsn($dsn));
  73. $this->assertSame($expected, (string) $provider);
  74. }
  75. /**
  76. * @dataProvider unsupportedSchemeProvider
  77. */
  78. public function testUnsupportedSchemeException(string $dsn, string $message = null)
  79. {
  80. $factory = $this->createFactory();
  81. $dsn = new Dsn($dsn);
  82. $this->expectException(UnsupportedSchemeException::class);
  83. if (null !== $message) {
  84. $this->expectExceptionMessage($message);
  85. }
  86. $factory->create($dsn);
  87. }
  88. /**
  89. * @dataProvider incompleteDsnProvider
  90. */
  91. public function testIncompleteDsnException(string $dsn, string $message = null)
  92. {
  93. $factory = $this->createFactory();
  94. $dsn = new Dsn($dsn);
  95. $this->expectException(IncompleteDsnException::class);
  96. if (null !== $message) {
  97. $this->expectExceptionMessage($message);
  98. }
  99. $factory->create($dsn);
  100. }
  101. protected function getClient(): HttpClientInterface
  102. {
  103. return $this->client ?? $this->client = new MockHttpClient();
  104. }
  105. protected function getLogger(): LoggerInterface
  106. {
  107. return $this->logger ?? $this->logger = $this->createMock(LoggerInterface::class);
  108. }
  109. protected function getDefaultLocale(): string
  110. {
  111. return $this->defaultLocale ?? $this->defaultLocale = 'en';
  112. }
  113. protected function getLoader(): LoaderInterface
  114. {
  115. return $this->loader ?? $this->loader = $this->createMock(LoaderInterface::class);
  116. }
  117. protected function getXliffFileDumper(): XliffFileDumper
  118. {
  119. return $this->xliffFileDumper ?? $this->xliffFileDumper = $this->createMock(XliffFileDumper::class);
  120. }
  121. }