From 54b38e7873c0a3ff74c72ece677f2dec8019849d Mon Sep 17 00:00:00 2001 From: Mathieu Santostefano Date: Mon, 21 Aug 2023 09:37:33 +0200 Subject: [PATCH] [Translation] [Phrase] Refacto ReadConfig and WriteConfig into arrays --- .../Bridge/Phrase/Config/ReadConfig.php | 86 --------- .../Bridge/Phrase/PhraseProvider.php | 26 +-- .../Bridge/Phrase/PhraseProviderFactory.php | 52 +++++- .../Phrase/Tests/Config/ReadConfigTest.php | 168 ------------------ .../Phrase/Tests/Config/WriteConfigTest.php | 108 ----------- .../Phrase/Tests/PhraseProviderTest.php | 123 +++---------- 6 files changed, 87 insertions(+), 476 deletions(-) delete mode 100644 src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php delete mode 100644 src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/ReadConfigTest.php delete mode 100644 src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/WriteConfigTest.php diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php b/src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php deleted file mode 100644 index b818116dbd920..0000000000000 --- a/src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php +++ /dev/null @@ -1,86 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Bridge\Phrase\Config; - -use Symfony\Component\Translation\Provider\Dsn; - -/** - * @author wicliff - */ -class ReadConfig -{ - private const DEFAULTS = [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ]; - - private function __construct( - private array $options, - private readonly bool $fallbackEnabled - ) { - } - - /** - * @return $this - */ - public function setTag(string $tag): static - { - $this->options['tags'] = $tag; - - return $this; - } - - public function isFallbackLocaleEnabled(): bool - { - return $this->fallbackEnabled; - } - - /** - * @return $this - */ - public function setFallbackLocale(string $locale): static - { - $this->options['fallback_locale_id'] = $locale; - - return $this; - } - - public function getOptions(): array - { - return $this->options; - } - - /** - * @return $this - */ - public static function fromDsn(Dsn $dsn): static - { - $options = $dsn->getOptions()['read'] ?? []; - - // enforce empty translations when fallback locale is enabled - if (true === $fallbackLocale = filter_var($options['fallback_locale_enabled'] ?? false, \FILTER_VALIDATE_BOOL)) { - $options['include_empty_translations'] = '1'; - } - - unset($options['file_format'], $options['tags'], $options['tag'], $options['fallback_locale_id'], $options['fallback_locale_enabled']); - - $options['format_options'] = array_merge(self::DEFAULTS['format_options'], $options['format_options'] ?? []); - - $configOptions = array_merge(self::DEFAULTS, $options); - - return new self($configOptions, $fallbackLocale); - } -} diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProvider.php b/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProvider.php index dfc9c31d06a5a..a649d4ba59909 100644 --- a/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProvider.php +++ b/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProvider.php @@ -11,13 +11,10 @@ namespace Symfony\Component\Translation\Bridge\Phrase; -use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\Multipart\FormDataPart; -use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig; -use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig; use Symfony\Component\Translation\Dumper\XliffFileDumper; use Symfony\Component\Translation\Exception\ProviderException; use Symfony\Component\Translation\Loader\LoaderInterface; @@ -42,8 +39,9 @@ public function __construct( private readonly CacheItemPoolInterface $cache, private readonly string $defaultLocale, private readonly string $endpoint, - private readonly ReadConfig $readConfig, - private readonly WriteConfig $writeConfig, + private array $readConfig, + private array $writeConfig, + private readonly bool $isFallbackLocaleEnabled = false, ) { } @@ -58,7 +56,7 @@ public function write(TranslatorBagInterface $translatorBag): void foreach ($translatorBag->getCatalogues() as $catalogue) { foreach ($catalogue->getDomains() as $domain) { - if (0 === \count($catalogue->all($domain))) { + if (!\count($catalogue->all($domain))) { continue; } @@ -67,7 +65,9 @@ public function write(TranslatorBagInterface $translatorBag): void $content = $this->xliffFileDumper->formatCatalogue($catalogue, $domain, ['default_locale' => $this->defaultLocale]); $filename = sprintf('%d-%s-%s.xlf', date('YmdHis'), $domain, $catalogue->getLocale()); - $fields = array_merge($this->writeConfig->setTag($domain)->setLocale($phraseLocale)->getOptions(), ['file' => new DataPart($content, $filename, 'application/xml')]); + $this->writeConfig['tags'] = $domain; + $this->writeConfig['locale_id'] = $phraseLocale; + $fields = array_merge($this->writeConfig, ['file' => new DataPart($content, $filename, 'application/xml')]); $formData = new FormDataPart($fields); @@ -93,13 +93,13 @@ public function read(array $domains, array $locales): TranslatorBag $phraseLocale = $this->getLocale($locale); foreach ($domains as $domain) { - $this->readConfig->setTag($domain); + $this->readConfig['tags'] = $domain; - if ($this->readConfig->isFallbackLocaleEnabled() && null !== $fallbackLocale = $this->getFallbackLocale($locale)) { - $this->readConfig->setFallbackLocale($fallbackLocale); + if ($this->isFallbackLocaleEnabled && null !== $fallbackLocale = $this->getFallbackLocale($locale)) { + $this->readConfig['fallback_locale_id'] = $fallbackLocale; } - $cacheKey = $this->generateCacheKey($locale, $domain, $this->readConfig->getOptions()); + $cacheKey = $this->generateCacheKey($locale, $domain, $this->readConfig); $cacheItem = $this->cache->getItem($cacheKey); $headers = []; @@ -110,7 +110,7 @@ public function read(array $domains, array $locales): TranslatorBag } $response = $this->httpClient->request('GET', 'locales/'.$phraseLocale.'/download', [ - 'query' => $this->readConfig->getOptions(), + 'query' => $this->readConfig, 'headers' => $headers, ]); @@ -124,7 +124,7 @@ public function read(array $domains, array $locales): TranslatorBag $translatorBag->addCatalogue($this->loader->load($content, $locale, $domain)); // using weak etags, responses for requests with fallback locale enabled can not be reliably cached... - if (false === $this->readConfig->isFallbackLocaleEnabled()) { + if (!$this->isFallbackLocaleEnabled) { $headers = $response->getHeaders(false); $cacheItem->set(['etag' => $headers['etag'][0], 'modified' => $headers['last-modified'][0], 'content' => $content]); $this->cache->save($cacheItem); diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProviderFactory.php b/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProviderFactory.php index 75981e7e6fefe..a56b2c26536a5 100644 --- a/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProviderFactory.php +++ b/src/Symfony/Component/Translation/Bridge/Phrase/PhraseProviderFactory.php @@ -13,8 +13,6 @@ use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; -use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig; -use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig; use Symfony\Component\Translation\Dumper\XliffFileDumper; use Symfony\Component\Translation\Exception\UnsupportedSchemeException; use Symfony\Component\Translation\Loader\LoaderInterface; @@ -29,6 +27,18 @@ class PhraseProviderFactory extends AbstractProviderFactory { private const HOST = 'api.phrase.com'; + private const READ_CONFIG_DEFAULT = [ + 'file_format' => 'symfony_xliff', + 'include_empty_translations' => '1', + 'tags' => [], + 'format_options' => [ + 'enclose_in_cdata' => '1', + ], + ]; + private const WRITE_CONFIG_DEFAULT = [ + 'file_format' => 'symfony_xliff', + 'update_translations' => '1', + ]; public function __construct( private readonly HttpClientInterface $httpClient, @@ -60,14 +70,46 @@ public function create(Dsn $dsn): ProviderInterface ], ]); - $readConfig = ReadConfig::fromDsn($dsn); - $writeConfig = WriteConfig::fromDsn($dsn); + $readConfig = $this->readConfigFromDsn($dsn); + $writeConfig = $this->writeConfigFromDsn($dsn); - return new PhraseProvider($client, $this->logger, $this->loader, $this->xliffFileDumper, $this->cache, $this->defaultLocale, $endpoint, $readConfig, $writeConfig); + return new PhraseProvider($client, $this->logger, $this->loader, $this->xliffFileDumper, $this->cache, $this->defaultLocale, $endpoint, $readConfig, $writeConfig, $this->isFallbackLocaleEnabled($dsn)); } protected function getSupportedSchemes(): array { return ['phrase']; } + + private function isFallbackLocaleEnabled(Dsn $dsn): bool + { + $options = $dsn->getOptions()['read'] ?? []; + + return filter_var($options['fallback_locale_enabled'] ?? false, \FILTER_VALIDATE_BOOL); + } + + private function readConfigFromDsn(Dsn $dsn): array + { + $options = $dsn->getOptions()['read'] ?? []; + + // enforce empty translations when fallback locale is enabled + if ($this->isFallbackLocaleEnabled($dsn)) { + $options['include_empty_translations'] = '1'; + } + + unset($options['file_format'], $options['tags'], $options['tag'], $options['fallback_locale_id'], $options['fallback_locale_enabled']); + + $options['format_options'] = array_merge(self::READ_CONFIG_DEFAULT['format_options'], $options['format_options'] ?? []); + + return array_merge(self::READ_CONFIG_DEFAULT, $options); + } + + private function writeConfigFromDsn(Dsn $dsn): array + { + $options = $dsn->getOptions()['write'] ?? []; + + unset($options['file_format'], $options['tags'], $options['locale_id'], $options['file']); + + return array_merge(self::WRITE_CONFIG_DEFAULT, $options); + } } diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/ReadConfigTest.php b/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/ReadConfigTest.php deleted file mode 100644 index 90f53a2581999..0000000000000 --- a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/ReadConfigTest.php +++ /dev/null @@ -1,168 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Bridge\Phrase\Tests\Config; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig; -use Symfony\Component\Translation\Provider\Dsn; - -/** - * @author wicliff - */ -class ReadConfigTest extends TestCase -{ - /** - * @dataProvider dsnOptionsProvider - */ - public function testCreateFromDsn(string $dsn, array $expectedOptions) - { - $config = ReadConfig::fromDsn(new Dsn($dsn)); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public function testWithTag() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject'; - - $expectedOptions = [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => 'messages', - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ]; - - $config = ReadConfig::fromDsn(new Dsn($dsn)); - $config->setTag('messages'); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public function testWithTagAndFallbackLocale() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject'; - - $expectedOptions = [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => 'messages', - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - 'fallback_locale_id' => 'en', - ]; - - $config = ReadConfig::fromDsn(new Dsn($dsn)); - $config->setTag('messages')->setFallbackLocale('en'); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public function testFallbackLocaleEnabled() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&read[fallback_locale_enabled]=1'; - $config = ReadConfig::fromDsn(new Dsn($dsn)); - $this->assertTrue($config->isFallbackLocaleEnabled()); - } - - public function testFallbackLocaleDisabled() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject'; - $config = ReadConfig::fromDsn(new Dsn($dsn)); - $this->assertFalse($config->isFallbackLocaleEnabled()); - } - - public static function dsnOptionsProvider(): \Generator - { - yield 'default options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ], - ]; - - yield 'overwrite non protected options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&&read[format_options][enclose_in_cdata]=0&read[include_empty_translations]=0', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '0', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '0', - ], - ], - ]; - - yield 'every single option' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?read%5Binclude_empty_translations%5D=0&read%5Bformat_options%5D%5Binclude_translation_state%5D=1&read%5Bbranch%5D=foo&read%5Bexclude_empty_zero_forms%5D=1&read%5Binclude_translated_keys%5D=1&read%5Bkeep_notranslate_tags%5D=0&read%5Bencoding%5D=UTF-8&read%5Binclude_unverified_translations%5D=1&read%5Buse_last_reviewed_version%5D=1', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '0', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - 'include_translation_state' => '1', - ], - 'branch' => 'foo', - 'exclude_empty_zero_forms' => '1', - 'include_translated_keys' => '1', - 'keep_notranslate_tags' => '0', - 'encoding' => 'UTF-8', - 'include_unverified_translations' => '1', - 'use_last_reviewed_version' => '1', - ], - ]; - - yield 'overwrite protected options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&&read[file_format]=yaml&read[tags][]=foo&read[tags][]=bar', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ], - ]; - - yield 'fallback enabled empty translations disabled' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&read[include_empty_translations]=0&read[fallback_locale_enabled]=1', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ], - ]; - - yield 'fallback disabled empty translations disabled' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&read[include_empty_translations]=0&read[fallback_locale_enabled]=0', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '0', - 'tags' => [], - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ], - ]; - } -} diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/WriteConfigTest.php b/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/WriteConfigTest.php deleted file mode 100644 index 640818cf18221..0000000000000 --- a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/Config/WriteConfigTest.php +++ /dev/null @@ -1,108 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Bridge\Phrase\Tests\Config; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig; -use Symfony\Component\Translation\Provider\Dsn; - -/** - * @author wicliff - */ -class WriteConfigTest extends TestCase -{ - /** - * @dataProvider dsnOptionsProvider - */ - public function testCreateFromDsn(string $dsn, array $expectedOptions) - { - $config = WriteConfig::fromDsn(new Dsn($dsn)); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public function testWithTag() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject'; - - $expectedOptions = [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - 'tags' => 'messages', - ]; - - $config = WriteConfig::fromDsn(new Dsn($dsn)); - $config->setTag('messages'); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public function testWithTagAndLocale() - { - $dsn = 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject'; - - $expectedOptions = [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - 'tags' => 'messages', - 'locale_id' => 'foo', - ]; - - $config = WriteConfig::fromDsn(new Dsn($dsn)); - $config->setTag('messages')->setLocale('foo'); - - $this->assertSame($expectedOptions, $config->getOptions()); - } - - public static function dsnOptionsProvider(): \Generator - { - yield 'default options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - ], - ]; - - yield 'overwrite non protected options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&write[update_translations]=0', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '0', - ], - ]; - - yield 'every single option' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?write%5Bupdate_translations%5D=1&write%5Bupdate_descriptions%5D=0&write%5Bskip_upload_tags%5D=1&write%5Bskip_unverification%5D=0&write%5Bfile_encoding%5D=UTF-8&write%5Blocale_mapping%5D%5Ben%5D=2&write%5Bformat_options%5D%5Bfoo%5D=bar&write%5Bautotranslate%5D=1&write%5Bmark_reviewed%5D=1', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - 'update_descriptions' => '0', - 'skip_upload_tags' => '1', - 'skip_unverification' => '0', - 'file_encoding' => 'UTF-8', - 'locale_mapping' => ['en' => '2'], - 'format_options' => ['foo' => 'bar'], - 'autotranslate' => '1', - 'mark_reviewed' => '1', - ], - ]; - - yield 'overwrite protected options' => [ - 'dsn' => 'phrase://PROJECT_ID:API_TOKEN@default?userAgent=myProject&write[file_format]=yaml', - 'expected_options' => [ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - ], - ]; - } -} diff --git a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/PhraseProviderTest.php b/src/Symfony/Component/Translation/Bridge/Phrase/Tests/PhraseProviderTest.php index d13aca1f1aa7a..2ab0b861a5cd7 100644 --- a/src/Symfony/Component/Translation/Bridge/Phrase/Tests/PhraseProviderTest.php +++ b/src/Symfony/Component/Translation/Bridge/Phrase/Tests/PhraseProviderTest.php @@ -19,13 +19,10 @@ use Symfony\Component\HttpClient\HttpClientTrait; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\Response\MockResponse; -use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig; -use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig; use Symfony\Component\Translation\Bridge\Phrase\PhraseProvider; use Symfony\Component\Translation\Dumper\XliffFileDumper; use Symfony\Component\Translation\Exception\ProviderExceptionInterface; use Symfony\Component\Translation\Loader\LoaderInterface; -use Symfony\Component\Translation\LoggingTranslator; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Provider\ProviderInterface; use Symfony\Component\Translation\TranslatorBag; @@ -47,8 +44,8 @@ class PhraseProviderTest extends TestCase private MockObject&CacheItemPoolInterface $cache; private string $defaultLocale; private string $endpoint; - private MockObject&ReadConfig $readConfig; - private MockObject&WriteConfig $writeConfig; + private array $readConfig; + private array $writeConfig; /** * @dataProvider toStringProvider @@ -87,8 +84,6 @@ public function testRead(string $locale, string $localeId, string $domain, strin })) ->willReturn($item); - $this->readConfigWithDefaultValues($domain); - $responses = [ 'init locales' => $this->getInitLocaleResponseMock(), 'download locale' => $this->getDownloadLocaleResponseMock($domain, $localeId, $responseContent), @@ -149,8 +144,6 @@ public function testReadCached(string $locale, string $localeId, string $domain, ->method('save') ->with($item); - $this->readConfigWithDefaultValues($domain); - $responses = [ 'init locales' => $this->getInitLocaleResponseMock(), 'download locale' => function (string $method, string $url, array $options = []): ResponseInterface { @@ -185,7 +178,6 @@ public function testReadCached(string $locale, string $localeId, string $domain, public function testReadFallbackLocale() { $locale = 'en_GB'; - $localeId = '13604ec993beefcdaba732812cdb828c'; $domain = 'messages'; $bag = new TranslatorBag(); @@ -226,27 +218,12 @@ public function testReadFallbackLocale() ->willReturn($item); $this->getCache()->expects(self::never())->method('save'); - - $this->getReadConfig() - ->method('getOptions') - ->willReturn([ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => $domain, - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - 'fallback_locale_id' => 'de', - ]); - - $this->getReadConfig()->expects(self::once())->method('setTag')->with($domain)->willReturnSelf(); - $this->getReadConfig()->expects(self::once())->method('setFallbackLocale')->with('de')->willReturnSelf(); - $this->getReadConfig()->expects(self::exactly(2))->method('isFallbackLocaleEnabled')->willReturn(true); $this->getLoader()->expects($this->once())->method('load')->willReturn($bag->getCatalogue($locale)); $responses = [ 'init locales' => $this->getInitLocaleResponseMock(), - 'download locale' => function (string $method, string $url, array $options) use ($localeId): ResponseInterface { + 'download locale' => function (string $method, string $url, array $options): ResponseInterface { + $localeId = '13604ec993beefcdaba732812cdb828c'; $query = [ 'file_format' => 'symfony_xliff', 'include_empty_translations' => '1', @@ -275,7 +252,7 @@ public function testReadFallbackLocale() 'Authorization' => 'token API_TOKEN', 'User-Agent' => 'myProject', ], - ]), endpoint: 'api.phrase.com/api/v2'); + ]), endpoint: 'api.phrase.com/api/v2', isFallbackLocaleEnabled: true); $provider->read([$domain], [$locale]); } @@ -286,13 +263,6 @@ public function testReadFallbackLocale() public function testCacheKeyOptionsSort(array $options, string $expectedKey) { $this->getCache()->expects(self::once())->method('getItem')->with($expectedKey); - $this->getReadConfig()->method('getOptions')->willReturn($options); - - $this->getReadConfig()->expects(self::once()) - ->method('setTag') - ->with('messages') - ->willReturnSelf(); - $this->getLoader()->method('load')->willReturn(new MessageCatalogue('en')); $responses = [ @@ -376,16 +346,6 @@ public function cacheItemProvider(): \Generator ]; } - public function testTranslatorBagAssert() - { - $this->expectExceptionMessage('assert($translatorBag instanceof TranslatorBag)'); - - $trans = $this->createMock(LoggingTranslator::class); - $provider = $this->createProvider(); - - $provider->write($trans); - } - public function cacheKeyProvider(): \Generator { yield 'sortorder one' => [ @@ -397,7 +357,7 @@ public function cacheKeyProvider(): \Generator 'enclose_in_cdata' => '1', ], ], - 'expected_key' => 'en_GB.messages.d8c311727922efc26536fc843bfee3e464850205', + 'expected_key' => 'en_GB.messages.099584009f94b788bd46580c17f49c0b22c55e16', ]; yield 'sortorder two' => [ @@ -409,7 +369,7 @@ public function cacheKeyProvider(): \Generator ], 'tags' => [], ], - 'expected_key' => 'en_GB.messages.d8c311727922efc26536fc843bfee3e464850205', + 'expected_key' => 'en_GB.messages.099584009f94b788bd46580c17f49c0b22c55e16', ]; } @@ -486,8 +446,6 @@ public function testInitLocalesExceptions(int $statusCode, string $expectedExcep public function testInitLocalesPaginated() { - $this->readConfigWithDefaultValues('messages'); - $this->getLoader()->method('load')->willReturn(new MessageCatalogue('en')); $responses = [ @@ -543,8 +501,6 @@ public function testInitLocalesPaginated() public function testCreateUnknownLocale() { - $this->readConfigWithDefaultValues('messages'); - $this->getLoader()->method('load')->willReturn(new MessageCatalogue('en')); $responses = [ @@ -712,7 +668,7 @@ public function testDeleteProviderExceptions(int $statusCode, string $expectedEx */ public function testWrite(string $locale, string $localeId, string $domain, string $content, TranslatorBag $bag) { - $this->writeConfigWithDefaultValues($domain, $localeId); + $this->getWriteConfig($domain, $localeId); $responses = [ 'init locales' => $this->getInitLocaleResponseMock(), @@ -1162,7 +1118,7 @@ private function getInitLocaleResponseMock(): \Closure }; } - private function createProvider(MockHttpClient $httpClient = null, string $endpoint = null, XliffFileDumper $dumper = null): ProviderInterface + private function createProvider(MockHttpClient $httpClient = null, string $endpoint = null, XliffFileDumper $dumper = null, bool $isFallbackLocaleEnabled = false): ProviderInterface { return new PhraseProvider( $httpClient ?? $this->getHttpClient(), @@ -1173,7 +1129,8 @@ private function createProvider(MockHttpClient $httpClient = null, string $endpo $this->getDefaultLocale(), $endpoint ?? $this->getEndpoint(), $this->getReadConfig(), - $this->getWriteConfig() + $this->getWriteConfig(), + $isFallbackLocaleEnabled, ); } @@ -1212,51 +1169,25 @@ private function getEndpoint(): string return $this->endpoint ??= 'api.phrase.com'; } - private function getReadConfig(): ReadConfig&MockObject + private function getReadConfig(): array { - return $this->readConfig ??= $this->createMock(ReadConfig::class); - } - - private function getWriteConfig(): WriteConfig&MockObject - { - return $this->writeConfig ??= $this->createMock(WriteConfig::class); - } - - private function readConfigWithDefaultValues(string $domain): void - { - $this->getReadConfig() - ->method('getOptions') - ->willReturn([ - 'file_format' => 'symfony_xliff', - 'include_empty_translations' => '1', - 'tags' => $domain, - 'format_options' => [ - 'enclose_in_cdata' => '1', - ], - ]); + return $this->readConfig ??= [ + 'file_format' => 'symfony_xliff', + 'include_empty_translations' => '1', + 'tags' => [], + 'format_options' => [ + 'enclose_in_cdata' => '1', + ], + ]; } - private function writeConfigWithDefaultValues(string $domain, string $phraseLocale): void + private function getWriteConfig(string $domain = 'messages', string $phraseLocale = 'en_GB'): array { - $this->getWriteConfig() - ->method('getOptions') - ->willReturn([ - 'file_format' => 'symfony_xliff', - 'update_translations' => '1', - 'tags' => $domain, - 'locale_id' => $phraseLocale, - ]); - - $this->getWriteConfig() - ->expects(self::once()) - ->method('setTag') - ->with($domain) - ->willReturnSelf(); - - $this->getWriteConfig() - ->expects(self::once()) - ->method('setLocale') - ->with($phraseLocale) - ->willReturnSelf(); + return $this->writeConfig ??= [ + 'file_format' => 'symfony_xliff', + 'update_translations' => '1', + 'tags' => $domain, + 'locale_id' => $phraseLocale, + ]; } }