ProviderFactoryTestCase.php 4.2 KB

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