ProviderFactoryTestCase.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. abstract class ProviderFactoryTestCase extends TestCase
  29. {
  30. protected HttpClientInterface $client;
  31. protected LoggerInterface|MockObject $logger;
  32. protected string $defaultLocale;
  33. protected LoaderInterface|MockObject $loader;
  34. protected XliffFileDumper|MockObject $xliffFileDumper;
  35. protected TranslatorBagInterface|MockObject $translatorBag;
  36. abstract public function createFactory(): ProviderFactoryInterface;
  37. /**
  38. * @return iterable<array{0: bool, 1: string}>
  39. */
  40. abstract public static function supportsProvider(): iterable;
  41. /**
  42. * @return iterable<array{0: string, 1: string}>
  43. */
  44. abstract public static function createProvider(): iterable;
  45. /**
  46. * @return iterable<array{0: string, 1: string|null}>
  47. */
  48. public static function unsupportedSchemeProvider(): iterable
  49. {
  50. return [];
  51. }
  52. /**
  53. * @return iterable<array{0: string, 1: string|null}>
  54. */
  55. public static function incompleteDsnProvider(): iterable
  56. {
  57. return [];
  58. }
  59. /**
  60. * @dataProvider supportsProvider
  61. */
  62. public function testSupports(bool $expected, string $dsn)
  63. {
  64. $factory = $this->createFactory();
  65. $this->assertSame($expected, $factory->supports(new Dsn($dsn)));
  66. }
  67. /**
  68. * @dataProvider createProvider
  69. */
  70. public function testCreate(string $expected, string $dsn)
  71. {
  72. $factory = $this->createFactory();
  73. $provider = $factory->create(new Dsn($dsn));
  74. $this->assertSame($expected, (string) $provider);
  75. }
  76. /**
  77. * @dataProvider unsupportedSchemeProvider
  78. */
  79. public function testUnsupportedSchemeException(string $dsn, ?string $message = null)
  80. {
  81. $factory = $this->createFactory();
  82. $dsn = new Dsn($dsn);
  83. $this->expectException(UnsupportedSchemeException::class);
  84. if (null !== $message) {
  85. $this->expectExceptionMessage($message);
  86. }
  87. $factory->create($dsn);
  88. }
  89. /**
  90. * @dataProvider incompleteDsnProvider
  91. */
  92. public function testIncompleteDsnException(string $dsn, ?string $message = null)
  93. {
  94. $factory = $this->createFactory();
  95. $dsn = new Dsn($dsn);
  96. $this->expectException(IncompleteDsnException::class);
  97. if (null !== $message) {
  98. $this->expectExceptionMessage($message);
  99. }
  100. $factory->create($dsn);
  101. }
  102. protected function getClient(): HttpClientInterface
  103. {
  104. return $this->client ??= new MockHttpClient();
  105. }
  106. protected function getLogger(): LoggerInterface
  107. {
  108. return $this->logger ??= $this->createMock(LoggerInterface::class);
  109. }
  110. protected function getDefaultLocale(): string
  111. {
  112. return $this->defaultLocale ??= 'en';
  113. }
  114. protected function getLoader(): LoaderInterface
  115. {
  116. return $this->loader ??= $this->createMock(LoaderInterface::class);
  117. }
  118. protected function getXliffFileDumper(): XliffFileDumper
  119. {
  120. return $this->xliffFileDumper ??= $this->createMock(XliffFileDumper::class);
  121. }
  122. protected function getTranslatorBag(): TranslatorBagInterface
  123. {
  124. return $this->translatorBag ??= $this->createMock(TranslatorBagInterface::class);
  125. }
  126. }