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\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\Loader\LoaderInterface;
  17. use Symfony\Component\Translation\Provider\ProviderInterface;
  18. use Symfony\Component\Translation\TranslatorBagInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. /**
  21. * A test case to ease testing a translation provider.
  22. *
  23. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  24. *
  25. * @internal
  26. */
  27. abstract class ProviderTestCase extends TestCase
  28. {
  29. protected HttpClientInterface $client;
  30. protected LoggerInterface|MockObject $logger;
  31. protected string $defaultLocale;
  32. protected LoaderInterface|MockObject $loader;
  33. protected XliffFileDumper|MockObject $xliffFileDumper;
  34. protected TranslatorBagInterface|MockObject $translatorBag;
  35. abstract public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface;
  36. /**
  37. * @return iterable<array{0: ProviderInterface, 1: string}>
  38. */
  39. abstract public static function toStringProvider(): iterable;
  40. /**
  41. * @dataProvider toStringProvider
  42. */
  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. }