ProviderTestCase.php 2.6 KB

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