From e202f4db30562549ce0ab66e86e1a728faede754 Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Mon, 29 Jan 2024 17:06:52 +0100 Subject: [PATCH 1/5] Drop PHP 7.x-8.0, add PHP 8.2-8.3 support (#55) * Drop PHP 7.x-8.0, add PHP 8.2-8.3 support * Add /.phpunit.result.cache to gitignore --- .github/workflows/ci.yml | 20 +++++--------------- .gitignore | 1 + 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bac6b77..e254e5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,26 +10,16 @@ jobs: strategy: fail-fast: false matrix: - php: [ '8.0', '8.1' ] + php: [ '8.1', '8.2', '8.3' ] strategy: [ 'highest' ] sf_version: ['5.*', '6.*'] include: - - php: 7.2 - sf_version: '4.*' - - php: 7.3 - sf_version: '3.*' - - php: 7.3 - sf_version: '4.*' - - php: 7.3 - sf_version: '5.*' - - php: 7.4 - strategy: 'lowest' - - php: 7.4 - sf_version: '4.*' - - php: 7.4 - sf_version: '5.*' - php: 8.1 strategy: 'lowest' + - php: 8.2 + sf_version: '7.*' + - php: 8.3 + sf_version: '7.*' steps: - name: Checkout code uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 8c35a25..052719b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.php-cs-fixer.cache +/.phpunit.result.cache /behat.yml /build/ /composer.lock From b4e58eae6423de14abedf2da6195c310744d6b32 Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Mon, 29 Jan 2024 17:04:31 +0100 Subject: [PATCH 2/5] Allow Symfony 7 support (#52) * Allow Symfony 7 support * Symfony 7 support (#54) * :bug: Add prophecy-phpunit (v10) * :bug: Fix assert regex * :bug: Fix setMethods --------- Co-authored-by: Damien Lagae --- composer.json | 7 +- tests/Unit/Storage/ChainStorageTest.php | 114 ++++++++++++ tests/Unit/Storage/FileStorageTest.php | 233 ++++++++++++++++++++++++ tests/Unit/XliffConverterTest.php | 41 +++++ 4 files changed, 392 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Storage/ChainStorageTest.php create mode 100644 tests/Unit/Storage/FileStorageTest.php create mode 100644 tests/Unit/XliffConverterTest.php diff --git a/composer.json b/composer.json index 03c6aa4..b97d88b 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,12 @@ ], "require": { "php": ">=7.2", - "symfony/translation": " ^3.4 || ^4.3 || ^5.0 || ^6.0" + "symfony/translation": " ^3.4 || ^4.3 || ^5.0 || ^6.0 || ^7.0" }, "require-dev": { - "symfony/phpunit-bridge": "^4.3 || ^5.0 || ^6.0", - "phpunit/phpunit": ">=8.5.23" + "symfony/framework-bundle": " ^3.4 || ^4.3 || ^5.0 || ^6.0 || ^7.0", + "phpunit/phpunit": ">=8.5.23", + "phpspec/prophecy-phpunit": "^2.1" }, "autoload": { "psr-4": { diff --git a/tests/Unit/Storage/ChainStorageTest.php b/tests/Unit/Storage/ChainStorageTest.php new file mode 100644 index 0000000..0fc54fb --- /dev/null +++ b/tests/Unit/Storage/ChainStorageTest.php @@ -0,0 +1,114 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\common\tests\Unit\Storage; + +use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; +use Symfony\Component\Translation\MessageCatalogueInterface; +use Translation\Common\Model\Message; +use Translation\Common\Storage\ChainStorage; +use Translation\Common\Storage\StorageInterface; + +class ChainStorageTest extends TestCase +{ + use ProphecyTrait; + private $childStorage1; + private $childStorage2; + private $storage; + + protected function setUp(): void + { + $this->childStorage1 = $this->prophesize(StorageInterface::class); + $this->childStorage2 = $this->prophesize(StorageInterface::class); + + $this->storage = new ChainStorage(); + $this->storage->addStorage($this->childStorage1->reveal()); + $this->storage->addStorage($this->childStorage2->reveal()); + } + + public function testGetWithMessageInFirstStorage() + { + $expectedMessage = new Message('PHP Translation IS awesome!'); + + $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn($expectedMessage); + $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldNotBeCalled(); + + $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); + $this->assertSame($expectedMessage, $message); + } + + public function testGetWithMessageInSecondStorage() + { + $expectedMessage = new Message('PHP Translation IS awesome!'); + + $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); + $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn($expectedMessage); + + $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); + $this->assertSame($expectedMessage, $message); + } + + public function testGetWithMessageNotFound() + { + $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); + $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); + + $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); + $this->assertNull($message); + } + + public function testCreateCallAllStorages() + { + $message = new Message('PHP Translation IS awesome!'); + + $this->childStorage1->create($message)->shouldBeCalledtimes(1); + $this->childStorage2->create($message)->shouldBeCalledtimes(1); + + $this->storage->create($message); + } + + public function testUpdateCallAllStorages() + { + $message = new Message('PHP Translation IS awesome!'); + + $this->childStorage1->update($message)->shouldBeCalledtimes(1); + $this->childStorage2->update($message)->shouldBeCalledtimes(1); + + $this->storage->update($message); + } + + public function testDeleteCallAllStorages() + { + $this->childStorage1->delete('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1); + $this->childStorage2->delete('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1); + + $this->storage->delete('en', 'messages', 'php_translation_is_awesome'); + } + + public function testExportCallOnlyTransferrableStorage() + { + $messageCatalogue = $this->prophesize(MessageCatalogueInterface::class)->reveal(); + + $this->childStorage2->export($messageCatalogue, [])->shouldBeCalledtimes(1); + + $this->storage->export($messageCatalogue, []); + } + + public function testImportCallOnlyTransferrableStorage() + { + $messageCatalogue = $this->prophesize(MessageCatalogueInterface::class)->reveal(); + + $this->childStorage2->import($messageCatalogue, [])->shouldBeCalledtimes(1); + + $this->storage->import($messageCatalogue, []); + } +} diff --git a/tests/Unit/Storage/FileStorageTest.php b/tests/Unit/Storage/FileStorageTest.php new file mode 100644 index 0000000..7b0afb1 --- /dev/null +++ b/tests/Unit/Storage/FileStorageTest.php @@ -0,0 +1,233 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Common\Tests\Unit\Storage; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Translation\Loader\XliffFileLoader; +use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\MessageCatalogueInterface; +use Symfony\Component\Translation\Reader\TranslationReader; +use Symfony\Component\Translation\Writer\TranslationWriter; +use Translation\Common\Model\Message; +use Translation\Common\Storage\FileStorage; + +/** + * @author Tobias Nyholm + */ +class FileStorageTest extends TestCase +{ + public function testConstructor() + { + $storage = new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), ['foo']); + $this->assertInstanceOf(FileStorage::class, $storage); + } + + public function testConstructorEmptyArray() + { + $this->expectException(\LogicException::class); + + new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), []); + } + + public function testCreateNewCatalogue() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + $writer->expects($this->once()) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->isInstanceOf(MessageCatalogueInterface::class), + 'xlf', + ['path' => 'foo', 'xliff_version' => '2.0'] + ); + + $storage = new FileStorage($writer, $this->createTranslationLoader(), ['foo']); + $storage->create(new Message('key', 'domain', 'en', 'Message')); + + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + $writer->expects($this->once()) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->isInstanceOf(MessageCatalogueInterface::class), + 'format', + ['path' => 'bar', 'default_output_format' => 'format', 'xliff_version' => '2.0'] + ); + + $storage = new FileStorage($writer, $this->createTranslationLoader(), ['bar'], ['default_output_format' => 'format']); + $storage->create(new Message('key', 'domain', 'en', 'Message')); + } + + public function testCreateExistingCatalogue() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + $writer->expects($this->once()) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->isInstanceOf(MessageCatalogueInterface::class), + 'xlf', + ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] + ); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]); + + $storage->create(new Message('key', 'messages', 'en', 'Translation')); + } + + public function testGet() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->disableOriginalConstructor() + ->getMock(); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); + + $this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation()); + + // Missing locale + $this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation()); + + // Missing domain + $this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation()); + + // Missing key + $this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation()); + } + + public function testUpdate() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + $writer->expects($this->exactly(2)) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->isInstanceOf(MessageCatalogueInterface::class), + 'xlf', + ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] + ); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); + + $storage->update(new Message('key', 'messages', 'en', 'Translation')); + $storage->update(new Message('test_1', 'messages', 'en', 'Translation')); + } + + public function testDelete() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + + $writer->expects($this->once()) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->callback(function (MessageCatalogueInterface $catalogue) { + return !$catalogue->defines('test_0', 'messages'); + }), + 'xlf', + ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] + ); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); + + $storage->delete('en', 'messages', 'test_0'); + } + + public function testImport() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->onlyMethods([$this->getMethodNameToWriteTranslations()]) + ->disableOriginalConstructor() + ->getMock(); + + $writer->expects($this->once()) + ->method($this->getMethodNameToWriteTranslations()) + ->with( + $this->callback(function (MessageCatalogueInterface $catalogue) { + return $catalogue->defines('test_4711', 'messages'); + }), + 'xlf', + ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] + ); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); + $catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]); + + $storage->import($catalogue); + } + + public function testExport() + { + $writer = $this->getMockBuilder(TranslationWriter::class) + ->disableOriginalConstructor() + ->getMock(); + + $loader = $this->createTranslationLoader(); + $loader->addLoader('xlf', new XliffFileLoader()); + $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); + + $catalogue = new MessageCatalogue('en'); + $storage->export($catalogue); + $this->assertTrue($catalogue->defines('test_0', 'messages')); + $this->assertTrue($catalogue->defines('test_1', 'messages')); + + // Wrong locale + $catalogue = new MessageCatalogue('sv'); + $storage->export($catalogue); + $this->assertFalse($catalogue->defines('test_0', 'messages')); + } + + /** + * @return string + */ + private function getFixturePath() + { + return realpath(__DIR__.'/../../Fixtures/single-file'); + } + + /** + * @return TranslationReader + */ + private function createTranslationLoader() + { + return new TranslationReader(); + } + + private function getMethodNameToWriteTranslations() + { + if (method_exists(TranslationWriter::class, 'write')) { + return 'write'; + } + + return 'writeTranslations'; + } +} diff --git a/tests/Unit/XliffConverterTest.php b/tests/Unit/XliffConverterTest.php new file mode 100644 index 0000000..351c0c9 --- /dev/null +++ b/tests/Unit/XliffConverterTest.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Common\Tests\Unit; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Translation\MessageCatalogue; +use Translation\Common\XliffConverter; + +/** + * @author Tobias Nyholm + */ +class XliffConverterTest extends TestCase +{ + public function testContentToCatalogue() + { + $content = file_get_contents(__DIR__.'/../Fixtures/single-file/messages.en.xlf'); + $catalogue = XliffConverter::contentToCatalogue($content, 'en', 'messages'); + + $this->assertEquals('en', $catalogue->getLocale()); + $this->assertEquals(['messages'], $catalogue->getDomains()); + $this->assertCount(2, $catalogue->all('messages')); + } + + public function testCatalogueToContent() + { + $catalogue = new MessageCatalogue('en'); + $catalogue->add(['foobar' => 'bar']); + $content = XliffConverter::catalogueToContent($catalogue, 'messages'); + + $this->assertMatchesRegularExpression('/foobar/', $content); + } +} From b48862f79cdceb4de136ab9d0b9e33076c90ae01 Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Wed, 16 Feb 2022 12:47:07 +0200 Subject: [PATCH 3/5] Update link to the docs with HTTPS link (#51) --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index bea96b8..63c5708 100644 --- a/Readme.md +++ b/Readme.md @@ -15,4 +15,4 @@ $ composer require php-translation/common ## Documentation -Read the full documentation at [https://php-translation.readthedocs.io/](https://php-translation.readthedocs.io/en/latest/). +Read the full documentation at [https://php-translation.readthedocs.io](https://php-translation.readthedocs.io/en/latest/). From ea8d3498d05c3f39b45991dd79a812d3db54ec37 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Mon, 29 Jan 2024 17:22:39 +0100 Subject: [PATCH 4/5] Drop bad files --- tests/Unit/Storage/ChainStorageTest.php | 114 ------------ tests/Unit/Storage/FileStorageTest.php | 233 ------------------------ tests/Unit/XliffConverterTest.php | 41 ----- 3 files changed, 388 deletions(-) delete mode 100644 tests/Unit/Storage/ChainStorageTest.php delete mode 100644 tests/Unit/Storage/FileStorageTest.php delete mode 100644 tests/Unit/XliffConverterTest.php diff --git a/tests/Unit/Storage/ChainStorageTest.php b/tests/Unit/Storage/ChainStorageTest.php deleted file mode 100644 index 0fc54fb..0000000 --- a/tests/Unit/Storage/ChainStorageTest.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Translation\common\tests\Unit\Storage; - -use PHPUnit\Framework\TestCase; -use Prophecy\PhpUnit\ProphecyTrait; -use Symfony\Component\Translation\MessageCatalogueInterface; -use Translation\Common\Model\Message; -use Translation\Common\Storage\ChainStorage; -use Translation\Common\Storage\StorageInterface; - -class ChainStorageTest extends TestCase -{ - use ProphecyTrait; - private $childStorage1; - private $childStorage2; - private $storage; - - protected function setUp(): void - { - $this->childStorage1 = $this->prophesize(StorageInterface::class); - $this->childStorage2 = $this->prophesize(StorageInterface::class); - - $this->storage = new ChainStorage(); - $this->storage->addStorage($this->childStorage1->reveal()); - $this->storage->addStorage($this->childStorage2->reveal()); - } - - public function testGetWithMessageInFirstStorage() - { - $expectedMessage = new Message('PHP Translation IS awesome!'); - - $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn($expectedMessage); - $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldNotBeCalled(); - - $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); - $this->assertSame($expectedMessage, $message); - } - - public function testGetWithMessageInSecondStorage() - { - $expectedMessage = new Message('PHP Translation IS awesome!'); - - $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); - $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn($expectedMessage); - - $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); - $this->assertSame($expectedMessage, $message); - } - - public function testGetWithMessageNotFound() - { - $this->childStorage1->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); - $this->childStorage2->get('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1)->willReturn(null); - - $message = $this->storage->get('en', 'messages', 'php_translation_is_awesome'); - $this->assertNull($message); - } - - public function testCreateCallAllStorages() - { - $message = new Message('PHP Translation IS awesome!'); - - $this->childStorage1->create($message)->shouldBeCalledtimes(1); - $this->childStorage2->create($message)->shouldBeCalledtimes(1); - - $this->storage->create($message); - } - - public function testUpdateCallAllStorages() - { - $message = new Message('PHP Translation IS awesome!'); - - $this->childStorage1->update($message)->shouldBeCalledtimes(1); - $this->childStorage2->update($message)->shouldBeCalledtimes(1); - - $this->storage->update($message); - } - - public function testDeleteCallAllStorages() - { - $this->childStorage1->delete('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1); - $this->childStorage2->delete('en', 'messages', 'php_translation_is_awesome')->shouldBeCalledtimes(1); - - $this->storage->delete('en', 'messages', 'php_translation_is_awesome'); - } - - public function testExportCallOnlyTransferrableStorage() - { - $messageCatalogue = $this->prophesize(MessageCatalogueInterface::class)->reveal(); - - $this->childStorage2->export($messageCatalogue, [])->shouldBeCalledtimes(1); - - $this->storage->export($messageCatalogue, []); - } - - public function testImportCallOnlyTransferrableStorage() - { - $messageCatalogue = $this->prophesize(MessageCatalogueInterface::class)->reveal(); - - $this->childStorage2->import($messageCatalogue, [])->shouldBeCalledtimes(1); - - $this->storage->import($messageCatalogue, []); - } -} diff --git a/tests/Unit/Storage/FileStorageTest.php b/tests/Unit/Storage/FileStorageTest.php deleted file mode 100644 index 7b0afb1..0000000 --- a/tests/Unit/Storage/FileStorageTest.php +++ /dev/null @@ -1,233 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Translation\Common\Tests\Unit\Storage; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Translation\Loader\XliffFileLoader; -use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\MessageCatalogueInterface; -use Symfony\Component\Translation\Reader\TranslationReader; -use Symfony\Component\Translation\Writer\TranslationWriter; -use Translation\Common\Model\Message; -use Translation\Common\Storage\FileStorage; - -/** - * @author Tobias Nyholm - */ -class FileStorageTest extends TestCase -{ - public function testConstructor() - { - $storage = new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), ['foo']); - $this->assertInstanceOf(FileStorage::class, $storage); - } - - public function testConstructorEmptyArray() - { - $this->expectException(\LogicException::class); - - new FileStorage(new TranslationWriter(), $this->createTranslationLoader(), []); - } - - public function testCreateNewCatalogue() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - $writer->expects($this->once()) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->isInstanceOf(MessageCatalogueInterface::class), - 'xlf', - ['path' => 'foo', 'xliff_version' => '2.0'] - ); - - $storage = new FileStorage($writer, $this->createTranslationLoader(), ['foo']); - $storage->create(new Message('key', 'domain', 'en', 'Message')); - - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - $writer->expects($this->once()) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->isInstanceOf(MessageCatalogueInterface::class), - 'format', - ['path' => 'bar', 'default_output_format' => 'format', 'xliff_version' => '2.0'] - ); - - $storage = new FileStorage($writer, $this->createTranslationLoader(), ['bar'], ['default_output_format' => 'format']); - $storage->create(new Message('key', 'domain', 'en', 'Message')); - } - - public function testCreateExistingCatalogue() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - $writer->expects($this->once()) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->isInstanceOf(MessageCatalogueInterface::class), - 'xlf', - ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] - ); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, ['foo', $this->getFixturePath()]); - - $storage->create(new Message('key', 'messages', 'en', 'Translation')); - } - - public function testGet() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->disableOriginalConstructor() - ->getMock(); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); - - $this->assertEquals('Bazbar', $storage->get('en', 'messages', 'test_1')->getTranslation()); - - // Missing locale - $this->assertEquals('test_1', $storage->get('sv', 'messages', 'test_1')->getTranslation()); - - // Missing domain - $this->assertEquals('test_1', $storage->get('en', 'xx', 'test_1')->getTranslation()); - - // Missing key - $this->assertEquals('miss', $storage->get('en', 'messages', 'miss')->getTranslation()); - } - - public function testUpdate() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - $writer->expects($this->exactly(2)) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->isInstanceOf(MessageCatalogueInterface::class), - 'xlf', - ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] - ); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); - - $storage->update(new Message('key', 'messages', 'en', 'Translation')); - $storage->update(new Message('test_1', 'messages', 'en', 'Translation')); - } - - public function testDelete() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - - $writer->expects($this->once()) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->callback(function (MessageCatalogueInterface $catalogue) { - return !$catalogue->defines('test_0', 'messages'); - }), - 'xlf', - ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] - ); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); - - $storage->delete('en', 'messages', 'test_0'); - } - - public function testImport() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->onlyMethods([$this->getMethodNameToWriteTranslations()]) - ->disableOriginalConstructor() - ->getMock(); - - $writer->expects($this->once()) - ->method($this->getMethodNameToWriteTranslations()) - ->with( - $this->callback(function (MessageCatalogueInterface $catalogue) { - return $catalogue->defines('test_4711', 'messages'); - }), - 'xlf', - ['path' => $this->getFixturePath(), 'xliff_version' => '2.0'] - ); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); - $catalogue = new MessageCatalogue('en', ['messages' => ['test_4711' => 'foobar']]); - - $storage->import($catalogue); - } - - public function testExport() - { - $writer = $this->getMockBuilder(TranslationWriter::class) - ->disableOriginalConstructor() - ->getMock(); - - $loader = $this->createTranslationLoader(); - $loader->addLoader('xlf', new XliffFileLoader()); - $storage = new FileStorage($writer, $loader, [$this->getFixturePath()]); - - $catalogue = new MessageCatalogue('en'); - $storage->export($catalogue); - $this->assertTrue($catalogue->defines('test_0', 'messages')); - $this->assertTrue($catalogue->defines('test_1', 'messages')); - - // Wrong locale - $catalogue = new MessageCatalogue('sv'); - $storage->export($catalogue); - $this->assertFalse($catalogue->defines('test_0', 'messages')); - } - - /** - * @return string - */ - private function getFixturePath() - { - return realpath(__DIR__.'/../../Fixtures/single-file'); - } - - /** - * @return TranslationReader - */ - private function createTranslationLoader() - { - return new TranslationReader(); - } - - private function getMethodNameToWriteTranslations() - { - if (method_exists(TranslationWriter::class, 'write')) { - return 'write'; - } - - return 'writeTranslations'; - } -} diff --git a/tests/Unit/XliffConverterTest.php b/tests/Unit/XliffConverterTest.php deleted file mode 100644 index 351c0c9..0000000 --- a/tests/Unit/XliffConverterTest.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Translation\Common\Tests\Unit; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Translation\MessageCatalogue; -use Translation\Common\XliffConverter; - -/** - * @author Tobias Nyholm - */ -class XliffConverterTest extends TestCase -{ - public function testContentToCatalogue() - { - $content = file_get_contents(__DIR__.'/../Fixtures/single-file/messages.en.xlf'); - $catalogue = XliffConverter::contentToCatalogue($content, 'en', 'messages'); - - $this->assertEquals('en', $catalogue->getLocale()); - $this->assertEquals(['messages'], $catalogue->getDomains()); - $this->assertCount(2, $catalogue->all('messages')); - } - - public function testCatalogueToContent() - { - $catalogue = new MessageCatalogue('en'); - $catalogue->add(['foobar' => 'bar']); - $content = XliffConverter::catalogueToContent($catalogue, 'messages'); - - $this->assertMatchesRegularExpression('/foobar/', $content); - } -} From 0a457933387876b883abc0371daafb0c8ee0c3c2 Mon Sep 17 00:00:00 2001 From: Victor Bocharsky Date: Mon, 29 Jan 2024 17:28:27 +0100 Subject: [PATCH 5/5] Drop lowest support --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e254e5b..d4a4470 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,6 @@ jobs: strategy: [ 'highest' ] sf_version: ['5.*', '6.*'] include: - - php: 8.1 - strategy: 'lowest' - php: 8.2 sf_version: '7.*' - php: 8.3