summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-08 12:48:08 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-09 12:11:12 +0200
commit222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch)
tree62b1bfa9233000985331b9dea703d51dc24515a2 /tests
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/ApplicationTest.php1
-rw-r--r--tests/Unit/Config/ConfigServiceProviderTest.php14
-rw-r--r--tests/Unit/Helpers/TranslationServiceProviderTest.php86
-rw-r--r--tests/Unit/Helpers/TranslatorTest.php69
-rw-r--r--tests/Unit/HelpersTest.php25
-rw-r--r--tests/Unit/Http/ResponseTest.php37
-rw-r--r--tests/Unit/Http/SessionServiceProviderTest.php4
-rw-r--r--tests/Unit/Http/UrlGeneratorServiceProviderTest.php7
-rw-r--r--tests/Unit/Middleware/ErrorHandlerTest.php88
-rw-r--r--tests/Unit/Middleware/SetLocaleTest.php71
-rw-r--r--tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php10
-rw-r--r--tests/Unit/Renderer/RendererServiceProviderTest.php4
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/ConfigTest.php25
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php84
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php25
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/SessionTest.php25
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/TranslationTest.php60
-rw-r--r--tests/Unit/Renderer/Twig/Extensions/UrlTest.php64
-rw-r--r--tests/Unit/Renderer/TwigEngineTest.php60
-rw-r--r--tests/Unit/Renderer/TwigLoaderTest.php31
-rw-r--r--tests/Unit/Renderer/TwigServiceProviderTest.php155
21 files changed, 938 insertions, 7 deletions
diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php
index 866eb957..012226b2 100644
--- a/tests/Unit/ApplicationTest.php
+++ b/tests/Unit/ApplicationTest.php
@@ -48,6 +48,7 @@ class ApplicationTest extends TestCase
$this->assertTrue($app->has('path'));
$this->assertTrue($app->has('path.config'));
$this->assertTrue($app->has('path.lang'));
+ $this->assertTrue($app->has('path.views'));
$this->assertEquals(realpath('.'), $app->path());
$this->assertEquals(realpath('.') . '/config', $app->get('path.config'));
diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php
index 998c0ba1..925854be 100644
--- a/tests/Unit/Config/ConfigServiceProviderTest.php
+++ b/tests/Unit/Config/ConfigServiceProviderTest.php
@@ -24,8 +24,13 @@ class ConfigServiceProviderTest extends ServiceProviderTest
Application::setInstance($app);
$this->setExpects($app, 'make', [Config::class], $config);
- $this->setExpects($app, 'instance', ['config', $config]);
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce());
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [Config::class, $config],
+ ['config', $config]
+ );
$this->setExpects($config, 'set', null, null, $this->exactly(2));
$config->expects($this->exactly(3))
@@ -60,7 +65,12 @@ class ConfigServiceProviderTest extends ServiceProviderTest
Application::setInstance($app);
$this->setExpects($app, 'make', [Config::class], $config);
- $this->setExpects($app, 'instance', ['config', $config]);
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [Config::class, $config],
+ ['config', $config]
+ );
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce());
$this->setExpects($config, 'set', null, null, $this->never());
diff --git a/tests/Unit/Helpers/TranslationServiceProviderTest.php b/tests/Unit/Helpers/TranslationServiceProviderTest.php
new file mode 100644
index 00000000..c5ba7386
--- /dev/null
+++ b/tests/Unit/Helpers/TranslationServiceProviderTest.php
@@ -0,0 +1,86 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Config\Config;
+use Engelsystem\Helpers\TranslationServiceProvider;
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class TranslationServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\TranslationServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ $app = $this->getApp(['make', 'instance', 'get']);
+ /** @var Config|MockObject $config */
+ $config = $this->createMock(Config::class);
+ /** @var Session|MockObject $session */
+ $session = $this->createMock(Session::class);
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+
+ /** @var TranslationServiceProvider|MockObject $serviceProvider */
+ $serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class)
+ ->setConstructorArgs([$app])
+ ->setMethods(['initGettext', 'setLocale'])
+ ->getMock();
+
+ $serviceProvider->expects($this->once())
+ ->method('initGettext');
+
+ $app->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive(['config'], ['session'])
+ ->willReturnOnConsecutiveCalls($config, $session);
+
+ $defaultLocale = 'fo_OO';
+ $locale = 'te_ST.WTF-9';
+ $locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST.WTF-9' => 'WTF\'s Testing?'];
+ $config->expects($this->exactly(2))
+ ->method('get')
+ ->withConsecutive(
+ ['locales'],
+ ['default_locale']
+ )
+ ->willReturnOnConsecutiveCalls(
+ $locales,
+ $defaultLocale
+ );
+
+ $session->expects($this->once())
+ ->method('get')
+ ->with('locale', $defaultLocale)
+ ->willReturn($locale);
+ $session->expects($this->once())
+ ->method('set')
+ ->with('locale', $locale);
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(
+ Translator::class,
+ [
+ 'locale' => $locale,
+ 'locales' => $locales,
+ 'localeChangeCallback' => [$serviceProvider, 'setLocale']
+ ]
+ )
+ ->willReturn($translator);
+
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [Translator::class, $translator],
+ ['translator', $translator]
+ );
+
+ $serviceProvider->register();
+ }
+}
+
+
diff --git a/tests/Unit/Helpers/TranslatorTest.php b/tests/Unit/Helpers/TranslatorTest.php
new file mode 100644
index 00000000..396d2b65
--- /dev/null
+++ b/tests/Unit/Helpers/TranslatorTest.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Helpers;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use stdClass;
+
+class TranslatorTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Helpers\Translator::__construct()
+ * @covers \Engelsystem\Helpers\Translator::setLocale()
+ * @covers \Engelsystem\Helpers\Translator::setLocales()
+ * @covers \Engelsystem\Helpers\Translator::getLocale()
+ * @covers \Engelsystem\Helpers\Translator::getLocales()
+ * @covers \Engelsystem\Helpers\Translator::hasLocale()
+ */
+ public function testInit()
+ {
+ $locales = ['te_ST.ER-01' => 'Tests', 'fo_OO' => 'SomeFOO'];
+ $locale = 'te_ST.ER-01';
+
+ /** @var callable|MockObject $callable */
+ $callable = $this->getMockBuilder(stdClass::class)
+ ->setMethods(['__invoke'])
+ ->getMock();
+ $callable->expects($this->exactly(2))
+ ->method('__invoke')
+ ->withConsecutive(['te_ST.ER-01'], ['fo_OO']);
+
+ $translator = new Translator($locale, $locales, $callable);
+
+ $this->assertEquals($locales, $translator->getLocales());
+ $this->assertEquals($locale, $translator->getLocale());
+
+ $translator->setLocale('fo_OO');
+ $this->assertEquals('fo_OO', $translator->getLocale());
+
+ $newLocales = ['lo_RM' => 'Lorem', 'ip_SU-M' => 'Ipsum'];
+ $translator->setLocales($newLocales);
+ $this->assertEquals($newLocales, $translator->getLocales());
+
+ $this->assertTrue($translator->hasLocale('ip_SU-M'));
+ $this->assertFalse($translator->hasLocale('te_ST.ER-01'));
+ }
+
+ /**
+ * @covers \Engelsystem\Helpers\Translator::translate()
+ */
+ public function testTranslate()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->getMockBuilder(Translator::class)
+ ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']])
+ ->setMethods(['translateGettext'])
+ ->getMock();
+ $translator->expects($this->once())
+ ->method('translateGettext')
+ ->with('My favourite number is %u!')
+ ->willReturn('Meine Lieblingszahl ist die %u!');
+
+ $return = $translator->translate('My favourite number is %u!', [3]);
+ $this->assertEquals('Meine Lieblingszahl ist die 3!', $return);
+ }
+}
+
+
diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php
index fb9e6f00..b9cedd30 100644
--- a/tests/Unit/HelpersTest.php
+++ b/tests/Unit/HelpersTest.php
@@ -5,10 +5,10 @@ namespace Engelsystem\Test\Unit;
use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
+use Engelsystem\Helpers\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Renderer\Renderer;
-use Engelsystem\Http\UrlGenerator;
use Engelsystem\Http\UrlGeneratorInterface;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
@@ -196,6 +196,29 @@ class HelpersTest extends TestCase
}
/**
+ * @covers \__
+ * @covers \trans
+ */
+ public function testTrans()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->getMockBuilder(Translator::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->getAppMock('translator', $translator);
+
+ $translator->expects($this->exactly(2))
+ ->method('translate')
+ ->with('Lorem %s Ipsum', ['foo'])
+ ->willReturn('Lorem foo Ipsum');
+
+ $this->assertEquals($translator, trans());
+ $this->assertEquals('Lorem foo Ipsum', trans('Lorem %s Ipsum', ['foo']));
+ $this->assertEquals('Lorem foo Ipsum', __('Lorem %s Ipsum', ['foo']));
+ }
+
+ /**
* @covers \url
*/
public function testUrl()
diff --git a/tests/Unit/Http/ResponseTest.php b/tests/Unit/Http/ResponseTest.php
index f6c24767..d7dc37c0 100644
--- a/tests/Unit/Http/ResponseTest.php
+++ b/tests/Unit/Http/ResponseTest.php
@@ -3,6 +3,8 @@
namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\Response;
+use Engelsystem\Renderer\Renderer;
+use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
@@ -46,4 +48,37 @@ class ResponseTest extends TestCase
$this->assertNotEquals($response, $newResponse);
$this->assertEquals('Lorem Ipsum?', $newResponse->getContent());
}
-}
+
+ /**
+ * @covers \Engelsystem\Http\Response::withView
+ */
+ public function testWithView()
+ {
+ /** @var REnderer|MockObject $renderer */
+ $renderer = $this->createMock(Renderer::class);
+
+ $renderer->expects($this->once())
+ ->method('render')
+ ->with('foo', ['lorem' => 'ipsum'])
+ ->willReturn('Foo ipsum!');
+
+ $response = new Response('', 200, [], $renderer);
+ $newResponse = $response->withView('foo', ['lorem' => 'ipsum'], 505, ['test' => 'er']);
+
+ $this->assertNotEquals($response, $newResponse);
+ $this->assertEquals('Foo ipsum!', $newResponse->getContent());
+ $this->assertEquals(505, $newResponse->getStatusCode());
+ $this->assertArraySubset(['test' => ['er']], $newResponse->getHeaders());
+ }
+
+ /**
+ * @covers \Engelsystem\Http\Response::withView
+ */
+ public function testWithViewNoRenderer()
+ {
+ $this->expectException(\InvalidArgumentException::class);
+
+ $response = new Response();
+ $response->withView('foo');
+ }
+} \ No newline at end of file
diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php
index a78b4f72..d0125bc2 100644
--- a/tests/Unit/Http/SessionServiceProviderTest.php
+++ b/tests/Unit/Http/SessionServiceProviderTest.php
@@ -54,6 +54,7 @@ class SessionServiceProviderTest extends ServiceProviderTest
->method('instance')
->withConsecutive(
['session.storage', $sessionStorage],
+ [Session::class, $session],
['session', $session]
);
@@ -88,10 +89,11 @@ class SessionServiceProviderTest extends ServiceProviderTest
$sessionStorage,
$session
);
- $app->expects($this->exactly(2))
+ $app->expects($this->exactly(3))
->method('instance')
->withConsecutive(
['session.storage', $sessionStorage],
+ [Session::class, $session],
['session', $session]
);
diff --git a/tests/Unit/Http/UrlGeneratorServiceProviderTest.php b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php
index 874268b0..720af631 100644
--- a/tests/Unit/Http/UrlGeneratorServiceProviderTest.php
+++ b/tests/Unit/Http/UrlGeneratorServiceProviderTest.php
@@ -21,7 +21,12 @@ class UrlGeneratorServiceProviderTest extends ServiceProviderTest
$app = $this->getApp();
$this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator);
- $this->setExpects($app, 'instance', ['http.urlGenerator', $urlGenerator]);
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [UrlGenerator::class, $urlGenerator],
+ ['http.urlGenerator', $urlGenerator]
+ );
$serviceProvider = new UrlGeneratorServiceProvider($app);
$serviceProvider->register();
diff --git a/tests/Unit/Middleware/ErrorHandlerTest.php b/tests/Unit/Middleware/ErrorHandlerTest.php
new file mode 100644
index 00000000..abf9c52f
--- /dev/null
+++ b/tests/Unit/Middleware/ErrorHandlerTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Http\Response;
+use Engelsystem\Middleware\ErrorHandler;
+use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Twig_LoaderInterface as TwigLoader;
+
+class ErrorHandlerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\ErrorHandler::__construct
+ * @covers \Engelsystem\Middleware\ErrorHandler::process
+ * @covers \Engelsystem\Middleware\ErrorHandler::selectView
+ */
+ public function testProcess()
+ {
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->createMock(ServerRequestInterface::class);
+ /** @var ResponseInterface|MockObject $psrResponse */
+ $psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
+ $returnResponseHandler = new ReturnResponseMiddlewareHandler($psrResponse);
+
+ $psrResponse->expects($this->once())
+ ->method('getStatusCode')
+ ->willReturn(505);
+
+ $errorHandler = new ErrorHandler($twigLoader);
+
+ $return = $errorHandler->process($request, $returnResponseHandler);
+ $this->assertEquals($psrResponse, $return, 'Plain PSR-7 Response should be passed directly');
+
+ /** @var Response|MockObject $response */
+ $response = $this->createMock(Response::class);
+
+ $response->expects($this->exactly(3))
+ ->method('getStatusCode')
+ ->willReturnOnConsecutiveCalls(
+ 200,
+ 418,
+ 505
+ );
+
+ $returnResponseHandler->setResponse($response);
+ $return = $errorHandler->process($request, $returnResponseHandler);
+ $this->assertEquals($response, $return, 'Only Responses >= 400 should be processed');
+
+ $twigLoader->expects($this->exactly(4))
+ ->method('exists')
+ ->withConsecutive(
+ ['errors/418'],
+ ['errors/4'],
+ ['errors/400'],
+ ['errors/505']
+ )
+ ->willReturnOnConsecutiveCalls(
+ false,
+ false,
+ false,
+ true
+ );
+
+ $response->expects($this->exactly(2))
+ ->method('getContent')
+ ->willReturnOnConsecutiveCalls(
+ 'Teapot',
+ 'Internal Error!'
+ );
+
+ $response->expects($this->exactly(2))
+ ->method('withView')
+ ->withConsecutive(
+ ['errors/default', ['status' => 418, 'content' => 'Teapot'], 418],
+ ['errors/505', ['status' => 505, 'content' => 'Internal Error!'], 505]
+ )
+ ->willReturn($response);
+
+ $errorHandler->process($request, $returnResponseHandler);
+ $errorHandler->process($request, $returnResponseHandler);
+ }
+}
diff --git a/tests/Unit/Middleware/SetLocaleTest.php b/tests/Unit/Middleware/SetLocaleTest.php
new file mode 100644
index 00000000..c4e9d2a4
--- /dev/null
+++ b/tests/Unit/Middleware/SetLocaleTest.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Middleware;
+
+use Engelsystem\Helpers\Translator;
+use Engelsystem\Middleware\SetLocale;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class SetLocaleTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Middleware\SetLocale::__construct
+ * @covers \Engelsystem\Middleware\SetLocale::process
+ */
+ public function testRegister()
+ {
+ /** @var Translator|MockObject $translator */
+ $translator = $this->createMock(Translator::class);
+ /** @var Session|MockObject $session */
+ $session = $this->createMock(Session::class);
+ /** @var ServerRequestInterface|MockObject $request */
+ $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
+ /** @var RequestHandlerInterface|MockObject $handler */
+ $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
+ /** @var ResponseInterface|MockObject $response */
+ $response = $this->getMockForAbstractClass(ResponseInterface::class);
+
+ $locale = 'te_ST.UTF8';
+
+ $request->expects($this->exactly(3))
+ ->method('getQueryParams')
+ ->willReturnOnConsecutiveCalls(
+ [],
+ ['set-locale' => 'en_US.UTF8'],
+ ['set-locale' => $locale]
+ );
+
+ $translator->expects($this->exactly(2))
+ ->method('hasLocale')
+ ->withConsecutive(
+ ['en_US.UTF8'],
+ [$locale]
+ )
+ ->willReturnOnConsecutiveCalls(
+ false,
+ true
+ );
+ $translator->expects($this->once())
+ ->method('setLocale')
+ ->with($locale);
+
+ $session->expects($this->once())
+ ->method('set')
+ ->with('locale', $locale);
+
+ $handler->expects($this->exactly(3))
+ ->method('handle')
+ ->with($request)
+ ->willReturn($response);
+
+ $middleware = new SetLocale($translator, $session);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ $middleware->process($request, $handler);
+ }
+}
diff --git a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
index 323e07b4..370187dd 100644
--- a/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
+++ b/tests/Unit/Middleware/Stub/ReturnResponseMiddlewareHandler.php
@@ -27,4 +27,14 @@ class ReturnResponseMiddlewareHandler implements RequestHandlerInterface
{
return $this->response;
}
+
+ /**
+ * Set the response
+ *
+ * @param ResponseInterface $response
+ */
+ public function setResponse(ResponseInterface $response)
+ {
+ $this->response = $response;
+ }
}
diff --git a/tests/Unit/Renderer/RendererServiceProviderTest.php b/tests/Unit/Renderer/RendererServiceProviderTest.php
index 3826da7e..6cdf4363 100644
--- a/tests/Unit/Renderer/RendererServiceProviderTest.php
+++ b/tests/Unit/Renderer/RendererServiceProviderTest.php
@@ -37,10 +37,12 @@ class RendererServiceProviderTest extends ServiceProviderTest
$htmlEngine
);
- $app->expects($this->exactly(2))
+ $app->expects($this->exactly(4))
->method('instance')
->withConsecutive(
+ [Renderer::class, $renderer],
['renderer', $renderer],
+ [HtmlEngine::class, $htmlEngine],
['renderer.htmlEngine', $htmlEngine]
);
diff --git a/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php b/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php
new file mode 100644
index 00000000..6a9cb78a
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Config\Config as EngelsystemConfig;
+use Engelsystem\Renderer\Twig\Extensions\Config;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class ConfigTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Config::__construct
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Config::getFunctions
+ */
+ public function testGetFunctions()
+ {
+ /** @var EngelsystemConfig|MockObject $config */
+ $config = $this->createMock(EngelsystemConfig::class);
+
+ $extension = new Config($config);
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('config', [$config, 'get'], $functions);
+ }
+}
diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
new file mode 100644
index 00000000..e1c5a378
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use PHPUnit\Framework\TestCase;
+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
+ * @param callable $callback
+ * @param TwigFunction[] $functions
+ */
+ protected function assertExtensionExists($name, $callback, $functions)
+ {
+ foreach ($functions as $function) {
+ if ($function->getName() != $name) {
+ continue;
+ }
+
+ $this->assertEquals($callback, $function->getCallable());
+ return;
+ }
+
+ $this->fail(sprintf('Function %s not found', $name));
+ }
+
+ /**
+ * Assert that a global exists
+ *
+ * @param string $name
+ * @param mixed $value
+ * @param mixed[] $globals
+ */
+ protected function assertGlobalsExists($name, $value, $globals)
+ {
+ if (isset($globals[$name])) {
+ $this->assertArraySubset([$name => $value], $globals);
+
+ return;
+ }
+
+ $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/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
new file mode 100644
index 00000000..6cc3a4da
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Renderer\Twig\Extensions\Globals;
+
+class GlobalsTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
+ */
+ public function testGetGlobals()
+ {
+ $extension = new Globals();
+ $globals = $extension->getGlobals();
+
+ $this->assertGlobalsExists('user', [], $globals);
+
+ global $user;
+ $user['foo'] = 'bar';
+
+ $globals = $extension->getGlobals();
+ $this->assertGlobalsExists('user', ['foo' => 'bar'], $globals);
+ }
+}
diff --git a/tests/Unit/Renderer/Twig/Extensions/SessionTest.php b/tests/Unit/Renderer/Twig/Extensions/SessionTest.php
new file mode 100644
index 00000000..7ce4dc3a
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/SessionTest.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Renderer\Twig\Extensions\Session;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
+
+class SessionTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Session::__construct
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Session::getFunctions
+ */
+ public function testGetGlobals()
+ {
+ /** @var SymfonySession|MockObject $session */
+ $session = $this->createMock(SymfonySession::class);
+
+ $extension = new Session($session);
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('session_get', [$session, 'get'], $functions);
+ }
+}
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);
+ }
+}
diff --git a/tests/Unit/Renderer/Twig/Extensions/UrlTest.php b/tests/Unit/Renderer/Twig/Extensions/UrlTest.php
new file mode 100644
index 00000000..c7e40bea
--- /dev/null
+++ b/tests/Unit/Renderer/Twig/Extensions/UrlTest.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\UrlGenerator;
+use Engelsystem\Renderer\Twig\Extensions\Url;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class UrlTest extends ExtensionTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Url::__construct
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Url::getFunctions
+ */
+ public function testGetGlobals()
+ {
+ /** @var UrlGenerator|MockObject $urlGenerator */
+ $urlGenerator = $this->createMock(UrlGenerator::class);
+
+ $extension = new Url($urlGenerator);
+ $functions = $extension->getFunctions();
+
+ $this->assertExtensionExists('url', [$extension, 'getUrl'], $functions);
+ }
+
+ /**
+ * @return string[][]
+ */
+ public function getUrls()
+ {
+ return [
+ ['/', '/', 'http://foo.bar/'],
+ ['/foo', '/foo', 'http://foo.bar/foo'],
+ ['foo_bar', 'foo-bar', 'http://foo.bar/foo-bar'],
+ ['dolor', 'dolor', 'http://foo.bar/dolor?lorem_ipsum=dolor', ['lorem_ipsum' => 'dolor']],
+ ];
+ }
+
+ /**
+ * @dataProvider getUrls
+ *
+ * @param string $url
+ * @param string $return
+ * @param string $urlTo
+ * @param array $parameters
+ *
+ * @covers \Engelsystem\Renderer\Twig\Extensions\Url::getUrl
+ */
+ public function testGetUrl($url, $urlTo, $return, $parameters = [])
+ {
+ /** @var UrlGenerator|MockObject $urlGenerator */
+ $urlGenerator = $this->createMock(UrlGenerator::class);
+
+ $urlGenerator->expects($this->once())
+ ->method('to')
+ ->with($urlTo, $parameters)
+ ->willReturn($return);
+
+ $extension = new Url($urlGenerator);
+ $generatedUrl = $extension->getUrl($url, $parameters);
+
+ $this->assertEquals($return, $generatedUrl);
+ }
+}
diff --git a/tests/Unit/Renderer/TwigEngineTest.php b/tests/Unit/Renderer/TwigEngineTest.php
new file mode 100644
index 00000000..9d0618f1
--- /dev/null
+++ b/tests/Unit/Renderer/TwigEngineTest.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigEngine;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Twig_Environment as Twig;
+use Twig_LoaderInterface as LoaderInterface;
+
+class TwigEngineTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigEngine::__construct
+ * @covers \Engelsystem\Renderer\TwigEngine::get
+ */
+ public function testGet()
+ {
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+
+ $path = 'foo.twig';
+ $data = ['lorem' => 'ipsum'];
+
+ $twig->expects($this->once())
+ ->method('render')
+ ->with($path, $data)
+ ->willReturn('LoremIpsum!');
+
+ $engine = new TwigEngine($twig);
+ $return = $engine->get($path, $data);
+ $this->assertEquals('LoremIpsum!', $return);
+ }
+
+
+ /**
+ * @covers \Engelsystem\Renderer\TwigEngine::canRender
+ */
+ public function testCanRender()
+ {
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+ /** @var LoaderInterface|MockObject $loader */
+ $loader = $this->getMockForAbstractClass(LoaderInterface::class);
+
+ $path = 'foo.twig';
+
+ $twig->expects($this->once())
+ ->method('getLoader')
+ ->willReturn($loader);
+ $loader->expects($this->once())
+ ->method('exists')
+ ->with($path)
+ ->willReturn(true);
+
+ $engine = new TwigEngine($twig);
+ $return = $engine->canRender($path);
+ $this->assertTrue($return);
+ }
+}
diff --git a/tests/Unit/Renderer/TwigLoaderTest.php b/tests/Unit/Renderer/TwigLoaderTest.php
new file mode 100644
index 00000000..e6867643
--- /dev/null
+++ b/tests/Unit/Renderer/TwigLoaderTest.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigLoader;
+use PHPUnit\Framework\TestCase;
+use ReflectionClass as Reflection;
+
+class TwigLoaderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigLoader::findTemplate
+ */
+ public function testFindTemplate()
+ {
+ $loader = new TwigLoader();
+
+ $reflection = new Reflection(get_class($loader));
+ $property = $reflection->getProperty('cache');
+ $property->setAccessible(true);
+
+ $realPath = __DIR__ . '/Stub/foo.twig';
+ $property->setValue($loader, ['Stub/foo.twig' => $realPath]);
+
+ $return = $loader->findTemplate('Stub/foo.twig');
+ $this->assertEquals($realPath, $return);
+
+ $return = $loader->findTemplate('Stub/foo');
+ $this->assertEquals($realPath, $return);
+ }
+}
diff --git a/tests/Unit/Renderer/TwigServiceProviderTest.php b/tests/Unit/Renderer/TwigServiceProviderTest.php
new file mode 100644
index 00000000..3cd0da4d
--- /dev/null
+++ b/tests/Unit/Renderer/TwigServiceProviderTest.php
@@ -0,0 +1,155 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigEngine;
+use Engelsystem\Renderer\TwigLoader;
+use Engelsystem\Renderer\TwigServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use ReflectionClass as Reflection;
+use stdClass;
+use Twig_Environment as Twig;
+use Twig_ExtensionInterface as ExtensionInterface;
+use Twig_LoaderInterface as TwigLoaderInterface;
+
+class TwigServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::register
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigExtensions
+ */
+ public function testRegister()
+ {
+ $app = $this->getApp(['make', 'instance', 'tag']);
+ $class = $this->createMock(stdClass::class);
+
+ $className = 'Foo\Bar\Class';
+ $classAlias = 'twig.extension.foo';
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with('Foo\Bar\Class')
+ ->willReturn($class);
+
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ [$className, $class],
+ [$classAlias, $class]
+ );
+
+ $app->expects($this->once())
+ ->method('tag')
+ ->with($classAlias, ['twig.extension']);
+
+ /** @var TwigServiceProvider|MockObject $serviceProvider */
+ $serviceProvider = $this->getMockBuilder(TwigServiceProvider::class)
+ ->setConstructorArgs([$app])
+ ->setMethods(['registerTwigEngine'])
+ ->getMock();
+ $serviceProvider->expects($this->once())
+ ->method('registerTwigEngine');
+ $this->setExtensionsTo($serviceProvider, ['foo' => 'Foo\Bar\Class']);
+
+ $serviceProvider->register();
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::boot
+ */
+ public function testBoot()
+ {
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+ /** @var ExtensionInterface|MockObject $firsExtension */
+ $firsExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
+ /** @var ExtensionInterface|MockObject $secondExtension */
+ $secondExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
+
+ $app = $this->getApp(['get', 'tagged']);
+
+ $app->expects($this->once())
+ ->method('get')
+ ->with('twig.environment')
+ ->willReturn($twig);
+ $app->expects($this->once())
+ ->method('tagged')
+ ->with('twig.extension')
+ ->willReturn([$firsExtension, $secondExtension]);
+
+ $twig->expects($this->exactly(2))
+ ->method('addExtension')
+ ->withConsecutive($firsExtension, $secondExtension);
+
+ $serviceProvider = new TwigServiceProvider($app);
+ $serviceProvider->boot();
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigEngine
+ */
+ public function testRegisterTWigEngine()
+ {
+ /** @var TwigEngine|MockObject $htmlEngine */
+ $twigEngine = $this->createMock(TwigEngine::class);
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+
+ $app = $this->getApp(['make', 'instance', 'tag', 'get']);
+
+ $viewsPath = __DIR__ . '/Stub';
+
+ $app->expects($this->exactly(3))
+ ->method('make')
+ ->withConsecutive(
+ [TwigLoader::class, ['paths' => $viewsPath]],
+ [Twig::class],
+ [TwigEngine::class]
+ )->willReturnOnConsecutiveCalls(
+ $twigLoader,
+ $twig,
+ $twigEngine
+ );
+
+ $app->expects($this->exactly(6))
+ ->method('instance')
+ ->withConsecutive(
+ [TwigLoader::class, $twigLoader],
+ [TwigLoaderInterface::class, $twigLoader],
+ ['twig.loader', $twigLoader],
+ [Twig::class, $twig],
+ ['twig.environment', $twig],
+ ['renderer.twigEngine', $twigEngine]
+ );
+
+ $app->expects($this->once())
+ ->method('get')
+ ->with('path.views')
+ ->willReturn($viewsPath);
+
+ $this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]);
+
+ $serviceProvider = new TwigServiceProvider($app);
+ $this->setExtensionsTo($serviceProvider, []);
+
+ $serviceProvider->register();
+ }
+
+ /**
+ * @param TwigServiceProvider $serviceProvider
+ * @param array $extensions
+ * @throws \ReflectionException
+ */
+ protected function setExtensionsTo($serviceProvider, $extensions)
+ {
+ $reflection = new Reflection(get_class($serviceProvider));
+
+ $property = $reflection->getProperty('extensions');
+ $property->setAccessible(true);
+
+ $property->setValue($serviceProvider, $extensions);
+ }
+}