ProviderTestCase.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. abstract class ProviderTestCase extends TestCase
  26. {
  27. protected HttpClientInterface $client;
  28. protected LoggerInterface|MockObject $logger;
  29. protected string $defaultLocale;
  30. protected LoaderInterface|MockObject $loader;
  31. protected XliffFileDumper|MockObject $xliffFileDumper;
  32. protected TranslatorBagInterface|MockObject $translatorBag;
  33. abstract public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface;
  34. /**
  35. * @return iterable<array{0: ProviderInterface, 1: string}>
  36. */
  37. abstract public static function toStringProvider(): iterable;
  38. /**
  39. * @dataProvider toStringProvider
  40. */
  41. public function testToString(ProviderInterface $provider, string $expected)
  42. {
  43. $this->assertSame($expected, (string) $provider);
  44. }
  45. protected function getClient(): MockHttpClient
  46. {
  47. return $this->client ??= new MockHttpClient();
  48. }
  49. protected function getLoader(): LoaderInterface
  50. {
  51. return $this->loader ??= $this->createMock(LoaderInterface::class);
  52. }
  53. protected function getLogger(): LoggerInterface
  54. {
  55. return $this->logger ??= $this->createMock(LoggerInterface::class);
  56. }
  57. protected function getDefaultLocale(): string
  58. {
  59. return $this->defaultLocale ??= 'en';
  60. }
  61. protected function getXliffFileDumper(): XliffFileDumper
  62. {
  63. return $this->xliffFileDumper ??= $this->createMock(XliffFileDumper::class);
  64. }
  65. protected function getTranslatorBag(): TranslatorBagInterface
  66. {
  67. return $this->translatorBag ??= $this->createMock(TranslatorBagInterface::class);
  68. }
  69. }