diff --git a/.github/workflows/on-pull-request.yml b/.github/workflows/on-pull-request.yml index 9aa361c..d26760e 100644 --- a/.github/workflows/on-pull-request.yml +++ b/.github/workflows/on-pull-request.yml @@ -13,11 +13,11 @@ jobs: max-parallel: 3 matrix: php: - - 7.2 - 7.3 - 7.4 - 8.0 - 8.1 + - 8.2 composer: - 2 name: Test - php:${{ matrix.php }}; composer:${{ matrix.composer }} @@ -36,6 +36,8 @@ jobs: run: composer validate --strict --no-check-lock - name: Install Composer dependencies (with dev) run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader + - name: PHPUnit + run: composer test - name: Install Composer dependencies (without dev) run: composer install --no-progress --no-dev --no-suggest --prefer-dist --optimize-autoloader diff --git a/.gitignore b/.gitignore index 94640f4..0b01c02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ /composer.lock -.idea/ \ No newline at end of file +.idea/ +/.phpunit.result.cache diff --git a/README.md b/README.md index be65adf..0eca489 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # Smarty Translate -This library adds new smarty block and var modifier `trans` for using with [Symfony Translation](symfony/translation) compatible library. +This library adds new smarty block and var modifier `trans` for using with [Symfony Translation](https://github.com/symfony/translation) compatible library. ## Installation diff --git a/composer.json b/composer.json index d40a7c6..ff14653 100644 --- a/composer.json +++ b/composer.json @@ -1,10 +1,10 @@ { "name": "imponeer/smarty-translate", - "description": "Library that provides trans modifer and block smarty extensions based on Symfony Translations contracts", + "description": "Library that provides trans modifier and block smarty extensions based on Symfony Translations contracts", "type": "library", "require": { - "imponeer/smarty-extensions-contracts": "^1.0 || ^2.0", - "php": ">=7.2.5", + "imponeer/smarty-extensions-contracts": "^3.0", + "php": "^7.3 || ^8.0", "symfony/translation-contracts": "^2.3" }, "autoload": { @@ -24,5 +24,12 @@ "translate", "symfony", "smarty-plugins" - ] + ], + "require-dev": { + "phpunit/phpunit": "^8.0", + "symfony/translation": "^5.1 || ^6.2" + }, + "scripts": { + "test": "phpunit --testdox" + } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..5dbe142 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,8 @@ + + + + + ./tests + + + \ No newline at end of file diff --git a/tests/TransBlockTest.php b/tests/TransBlockTest.php new file mode 100644 index 0000000..7111e1f --- /dev/null +++ b/tests/TransBlockTest.php @@ -0,0 +1,134 @@ +translator = new Translator('en'); + $this->translator->addLoader( + "array", + new ArrayLoader() + ); + $this->translator->addResource('array', [ + 'test' => 'TEST', + ], 'en', 'default'); + $this->translator->addResource('array', [ + 'test2' => 'TEST2', + ], 'en'); + $this->translator->addResource('array', [ + 'test3' => 'TEST3 {value}', + ], 'en'); + $this->translator->addResource('array', [ + 'test4' => 'TEST4 {value}', + ], 'en', 'default'); + $this->translator->addResource('array', [ + 'test4' => 'TEST4[X] {value}', + ], 'lt', 'default'); + + + $this->plugin = new TransBlock($this->translator); + + $this->smarty = new Smarty(); + $this->smarty->caching = Smarty::CACHING_OFF; + $this->smarty->registerPlugin( + 'block', + $this->plugin->getName(), + [$this->plugin, 'execute'] + ); + + parent::setUp(); + } + + public function testGetName() { + $this->assertSame('trans', $this->plugin->getName()); + } + + public function testInvokeWithCorrectDomain() { + $src = urlencode('{trans domain="default"}test{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test', [], 'default'), + $ret + ); + } + + public function testInvokeWithoutDomain() { + $src = urlencode('{trans}test2{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test2'), + $ret + ); + } + + public function testInvokeWithCorrectParameters() { + $src = urlencode('{trans parameters=["value" => "xx"]}test3{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test3', ['value' => 'xx']), + $ret + ); + } + + public function testInvokeWithDomainAndCorrectParameters() { + $src = urlencode('{trans domain="default" parameters=["value" => "xx"]}test4{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default'), + $ret + ); + } + + public function testInvokeWithDomainLocaleAndCorrectParameters() { + $src = urlencode('{trans locale="lt" domain="default" parameters=["value" => "xx"]}test4{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default', 'lt'), + $ret + ); + } + + public function testInvokeWithNotCorrectDomain() { + $src = urlencode('{trans domain="default2" parameters=["value" => "xx"]}test4{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame("test4", $ret); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default2'), + $ret + ); + } + + public function testInvokeWithNotCorrectParameters() { + $src = urlencode('{trans domain="default" parameters=["valueX" => "xx"]}test4{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['valueX' => 'xx'], 'default'), + $ret + ); + } + + public function testInvokeWithNotCorrectLocale() { + $src = urlencode('{trans domain="default" locale="be" parameters=["valueX" => "xx"]}test4{/trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame("test4", $ret); + } + +} \ No newline at end of file diff --git a/tests/TransVarModifierTest.php b/tests/TransVarModifierTest.php new file mode 100644 index 0000000..52fb1f0 --- /dev/null +++ b/tests/TransVarModifierTest.php @@ -0,0 +1,138 @@ +translator = new Translator('en'); + $this->translator->addLoader( + "array", + new ArrayLoader() + ); + $this->translator->addResource('array', [ + 'test' => 'TEST', + ], 'en', 'default'); + $this->translator->addResource('array', [ + 'test2' => 'TEST2', + ], 'en'); + $this->translator->addResource('array', [ + 'test3' => 'TEST3 {value}', + ], 'en'); + $this->translator->addResource('array', [ + 'test4' => 'TEST4 {value}', + ], 'en', 'default'); + $this->translator->addResource('array', [ + 'test4' => 'TEST4[X] {value}', + ], 'lt', 'default'); + + + $this->plugin = new TransVarModifier($this->translator); + + $this->smarty = new Smarty(); + $this->smarty->caching = Smarty::CACHING_OFF; + $this->smarty->registerPlugin( + 'modifier', + $this->plugin->getName(), + [$this->plugin, 'execute'] + ); + + parent::setUp(); + } + + public function testGetName() { + $this->assertSame('trans', $this->plugin->getName()); + } + + public function testInvokeWithCorrectDomain() { + $src = urlencode('{"test"|trans:[]:"default"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test', [], 'default'), + $ret + ); + } + + public function testInvokeWithoutDomain() { + $src = urlencode('{"test2"|trans}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test2'), + $ret + ); + } + + public function testInvokeWithCorrectParameters() { + $src = urlencode('{"test3"|trans:["value" => "xx"]}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test3', ['value' => 'xx']), + $ret + ); + } + + public function testInvokeWithDomainAndCorrectParameters() { + $src = urlencode('{"test4"|trans:["value" => "xx"]:"default"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default'), + $ret + ); + } + + public function testInvokeWithDomainLocaleAndCorrectParameters() { + $src = urlencode('{"test4"|trans:["value" => "xx"]:"default":"lt"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default', 'lt'), + $ret + ); + } + + public function testInvokeWithNotCorrectDomain() { + $src = urlencode('{"test4"|trans:["value" => "xx"]:"default2"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame("test4", $ret); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default2'), + $ret + ); + } + + public function testInvokeWithNotCorrectParameters() { + $src = urlencode('{"test4"|trans:["valueX" => "xx"]:"default"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['valueX' => 'xx'], 'default'), + $ret + ); + } + + public function testInvokeWithNotCorrectLocale() { + $src = urlencode('{"test4"|trans:["valueX" => "xx"]:"default":"be"}'); + $ret = $this->smarty->fetch('eval:urlencode:'.$src); + $this->assertSame( + $this->translator->trans('test4', ['value' => 'xx'], 'default', 'be'), + $ret + ); + } + +} \ No newline at end of file