summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-29 15:29:48 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:32 +0200
commit8257864829ffdfb410f05e0dd0a9c781f48b741a (patch)
treea0334208645bdb04625545e6775a602939c0a859 /tests/Unit
parenta2c47304d8230f1937c2a4f019aba89fa74203bc (diff)
Added translation support for twig templates
See https://twig-extensions.readthedocs.io/en/latest/i18n.html for documentation
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php36
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/TranslationTest.php60
2 files changed, 96 insertions, 0 deletions
diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
index 10f2e69a..e1c5a378 100644
--- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
+++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
@@ -8,6 +8,27 @@ use Twig_Function as TwigFunction;
abstract class ExtensionTest extends TestCase
{
/**
+ * Assert that a twig filter was registered
+ *
+ * @param string $name
+ * @param callable $callback
+ * @param TwigFunction[] $functions
+ */
+ protected function assertFilterExists($name, $callback, $functions)
+ {
+ foreach ($functions as $function) {
+ if ($function->getName() != $name) {
+ continue;
+ }
+
+ $this->assertEquals($callback, $function->getCallable());
+ return;
+ }
+
+ $this->fail(sprintf('Filter %s not found', $name));
+ }
+
+ /**
* Assert that a twig function was registered
*
* @param string $name
@@ -45,4 +66,19 @@ abstract class ExtensionTest extends TestCase
$this->fail(sprintf('Global %s not found', $name));
}
+
+ /**
+ * Assert that a token parser was set
+ *
+ * @param $tokenParser
+ * @param $tokenParsers
+ */
+ protected function assertTokenParserExists($tokenParser, $tokenParsers)
+ {
+ $this->assertArraySubset(
+ [$tokenParser],
+ $tokenParsers,
+ sprintf('Token parser %s not found', get_class($tokenParser))
+ );
+ }
}
diff --git a/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php b/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php
new file mode 100644
index 00000000..f1548604
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Renderer\Twig\Extensions\Translation;
+use PHPUnit\Framework\MockObject\MockObject;
+use Twig_Extensions_TokenParser_Trans as TranslationTokenParser;
+
+class TranslationTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::__construct
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getFilters
+ */
+ public function testGeFilters()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var TranslationTokenParser|MockObject $parser */
+ $parser = $this->createMock(TranslationTokenParser::class);
+
+ $extension = new Translation($translator, $parser);
+ $filters = $extension->getFilters();
+
+ $this->assertExtensionExists('trans', [$translator, 'translate'], $filters);
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getFunctions
+ */
+ public function testGetFunctions()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var TranslationTokenParser|MockObject $parser */
+ $parser = $this->createMock(TranslationTokenParser::class);
+
+ $extension = new Translation($translator, $parser);
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('__', [$translator, 'translate'], $functions);
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getTokenParsers
+ */
+ public function testGetTokenParsers()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var TranslationTokenParser|MockObject $parser */
+ $parser = $this->createMock(TranslationTokenParser::class);
+
+ $extension = new Translation($translator, $parser);
+ $tokenParsers = $extension->getTokenParsers();
+
+ $this->assertTokenParserExists($parser, $tokenParsers);
+ }
+}