You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB

3 years ago
  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\Contracts\HttpClient\HttpClientInterface;
  19. /**
  20. * A test case to ease testing a translation provider.
  21. *
  22. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  23. *
  24. * @internal
  25. */
  26. abstract class ProviderTestCase extends TestCase
  27. {
  28. protected $client;
  29. protected $logger;
  30. protected $defaultLocale;
  31. protected $loader;
  32. protected $xliffFileDumper;
  33. abstract public function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint): ProviderInterface;
  34. /**
  35. * @return iterable<array{0: string, 1: ProviderInterface}>
  36. */
  37. abstract public 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 ?? $this->client = new MockHttpClient();
  48. }
  49. /**
  50. * @return LoaderInterface&MockObject
  51. */
  52. protected function getLoader(): LoaderInterface
  53. {
  54. return $this->loader ?? $this->loader = $this->createMock(LoaderInterface::class);
  55. }
  56. /**
  57. * @return LoaderInterface&MockObject
  58. */
  59. protected function getLogger(): LoggerInterface
  60. {
  61. return $this->logger ?? $this->logger = $this->createMock(LoggerInterface::class);
  62. }
  63. protected function getDefaultLocale(): string
  64. {
  65. return $this->defaultLocale ?? $this->defaultLocale = 'en';
  66. }
  67. /**
  68. * @return LoaderInterface&MockObject
  69. */
  70. protected function getXliffFileDumper(): XliffFileDumper
  71. {
  72. return $this->xliffFileDumper ?? $this->xliffFileDumper = $this->createMock(XliffFileDumper::class);
  73. }
  74. }