ProviderTestCase.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Attributes\DataProvider;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use PHPUnit\Framework\TestCase;
  14. use Psr\Log\LoggerInterface;
  15. use Psr\Log\NullLogger;
  16. use Symfony\Component\HttpClient\MockHttpClient;
  17. use Symfony\Component\Translation\Dumper\XliffFileDumper;
  18. use Symfony\Component\Translation\Loader\ArrayLoader;
  19. use Symfony\Component\Translation\Loader\LoaderInterface;
  20. use Symfony\Component\Translation\Provider\ProviderInterface;
  21. use Symfony\Component\Translation\TranslatorBag;
  22. use Symfony\Component\Translation\TranslatorBagInterface;
  23. use Symfony\Contracts\HttpClient\HttpClientInterface;
  24. /**
  25. * A test case to ease testing a translation provider.
  26. *
  27. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  28. */
  29. abstract class ProviderTestCase extends TestCase
  30. {
  31. protected HttpClientInterface $client;
  32. protected LoggerInterface|MockObject $logger;
  33. protected string $defaultLocale;
  34. protected LoaderInterface|MockObject $loader;
  35. protected XliffFileDumper|MockObject $xliffFileDumper;
  36. protected TranslatorBagInterface|MockObject $translatorBag;
  37. abstract public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface;
  38. /**
  39. * @return iterable<array{0: ProviderInterface, 1: string}>
  40. */
  41. abstract public static function toStringProvider(): iterable;
  42. /**
  43. * @dataProvider toStringProvider
  44. */
  45. #[DataProvider('toStringProvider')]
  46. public function testToString(ProviderInterface $provider, string $expected)
  47. {
  48. $this->assertSame($expected, (string) $provider);
  49. }
  50. protected function getClient(): MockHttpClient
  51. {
  52. return $this->client ??= new MockHttpClient();
  53. }
  54. protected function getLoader(): LoaderInterface
  55. {
  56. return $this->loader ??= new ArrayLoader();
  57. }
  58. protected function getLogger(): LoggerInterface
  59. {
  60. return $this->logger ??= new NullLogger();
  61. }
  62. protected function getDefaultLocale(): string
  63. {
  64. return $this->defaultLocale ??= 'en';
  65. }
  66. protected function getXliffFileDumper(): XliffFileDumper
  67. {
  68. return $this->xliffFileDumper ??= new XliffFileDumper();
  69. }
  70. protected function getTranslatorBag(): TranslatorBagInterface
  71. {
  72. return $this->translatorBag ??= new TranslatorBag();
  73. }
  74. }