Skip to content

Commit

Permalink
Merge pull request #18 from imponeer/tests-and-some-updated-dependencies
Browse files Browse the repository at this point in the history
Added tests & imponeer/smarty-extensions-contracts updated to 3.0
  • Loading branch information
MekDrop authored Feb 1, 2023
2 parents e1f7dd9 + 991d045 commit d9187c7
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/composer.lock
.idea/
.idea/
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -24,5 +24,12 @@
"translate",
"symfony",
"smarty-plugins"
]
],
"require-dev": {
"phpunit/phpunit": "^8.0",
"symfony/translation": "^5.1 || ^6.2"
},
"scripts": {
"test": "phpunit --testdox"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="Main tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
134 changes: 134 additions & 0 deletions tests/TransBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

use Imponeer\Smarty\Extensions\Translate\TransBlock;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
class TransBlockTest extends TestCase
{

/**
* @var Translator
*/
private $translator;
/**
* @var TransBlock
*/
private $plugin;
/**
* @var Smarty
*/
private $smarty;

protected function setUp(): void
{
$this->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);
}

}
138 changes: 138 additions & 0 deletions tests/TransVarModifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

use Imponeer\Smarty\Extensions\Translate\TransVarModifier;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;

class TransVarModifierTest extends TestCase
{

/**
* @var Translator
*/
private $translator;
/**
* @var TransVarModifier
*/
private $plugin;
/**
* @var Smarty
*/
private $smarty;

protected function setUp(): void
{
$this->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
);
}

}

0 comments on commit d9187c7

Please sign in to comment.