ProviderTestCase.php 2.2 KB

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