summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/ApplicationTest.php29
-rw-r--r--tests/Unit/Config/ConfigTest.php115
-rw-r--r--tests/Unit/Container/ContainerTest.php104
-rw-r--r--tests/Unit/HelpersTest.php152
-rw-r--r--tests/Unit/Renderer/HtmlEngineTest.php67
-rw-r--r--tests/Unit/Renderer/RendererTest.php55
-rw-r--r--tests/Unit/Routing/UrlGeneratorTest.php51
7 files changed, 573 insertions, 0 deletions
diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php
new file mode 100644
index 00000000..77429f44
--- /dev/null
+++ b/tests/Unit/ApplicationTest.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Application;
+use Engelsystem\Container\Container;
+use PHPUnit\Framework\TestCase;
+use Psr\Container\ContainerInterface;
+
+class ApplicationTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Application::__construct
+ * @covers \Engelsystem\Application::registerBaseBindings
+ */
+ public function testConstructor()
+ {
+ $app = new Application();
+
+ $this->assertInstanceOf(Container::class, $app);
+ $this->assertInstanceOf(ContainerInterface::class, $app);
+ $this->assertSame($app, $app->get('app'));
+ $this->assertSame($app, $app->get('container'));
+ $this->assertSame($app, $app->get(Container::class));
+ $this->assertSame($app, $app->get(Application::class));
+ $this->assertSame($app, $app->get(ContainerInterface::class));
+ $this->assertSame($app, Container::getInstance());
+ }
+}
diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php
new file mode 100644
index 00000000..ce11ebd6
--- /dev/null
+++ b/tests/Unit/Config/ConfigTest.php
@@ -0,0 +1,115 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Config\Config;
+use PHPUnit\Framework\TestCase;
+
+class ConfigTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Config\Config::get
+ */
+ public function testGet()
+ {
+ $config = new Config();
+
+ $config->set('test', 'FooBar');
+ $this->assertEquals(['test' => 'FooBar'], $config->get(null));
+ $this->assertEquals('FooBar', $config->get('test'));
+
+ $this->assertEquals('defaultValue', $config->get('notExisting', 'defaultValue'));
+
+ $this->assertNull($config->get('notExisting'));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::set
+ */
+ public function testSet()
+ {
+ $config = new Config();
+
+ $config->set('test', 'FooBar');
+ $this->assertEquals('FooBar', $config->get('test'));
+
+ $config->set([
+ 'name' => 'Engelsystem',
+ 'mail' => ['user' => 'test'],
+ ]);
+ $this->assertEquals('Engelsystem', $config->get('name'));
+ $this->assertEquals(['user' => 'test'], $config->get('mail'));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::has
+ */
+ public function testHas()
+ {
+ $config = new Config();
+
+ $this->assertFalse($config->has('test'));
+
+ $config->set('test', 'FooBar');
+ $this->assertTrue($config->has('test'));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::remove
+ */
+ public function testRemove()
+ {
+ $config = new Config();
+ $config->set(['foo' => 'bar', 'test' => '123']);
+
+ $config->remove('foo');
+ $this->assertEquals(['test' => '123'], $config->get(null));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::__get
+ */
+ public function testMagicGet()
+ {
+ $config = new Config();
+
+ $config->set('test', 'FooBar');
+ $this->assertEquals('FooBar', $config->test);
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::__set
+ */
+ public function testMagicSet()
+ {
+ $config = new Config();
+
+ $config->test = 'FooBar';
+ $this->assertEquals('FooBar', $config->get('test'));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::__isset
+ */
+ public function testMagicIsset()
+ {
+ $config = new Config();
+
+ $this->assertFalse(isset($config->test));
+
+ $config->set('test', 'FooBar');
+ $this->assertTrue(isset($config->test));
+ }
+
+ /**
+ * @covers \Engelsystem\Config\Config::__unset
+ */
+ public function testMagicUnset()
+ {
+ $config = new Config();
+ $config->set(['foo' => 'bar', 'test' => '123']);
+
+ unset($config->foo);
+ $this->assertEquals(['test' => '123'], $config->get(null));
+ }
+}
diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php
new file mode 100644
index 00000000..89c34209
--- /dev/null
+++ b/tests/Unit/Container/ContainerTest.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Container\Container;
+use PHPUnit\Framework\TestCase;
+
+class ContainerTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Container\Container::get
+ */
+ public function testGet()
+ {
+ $container = new Container();
+ $class = new class
+ {
+ };
+
+ $container->instance('foo', $class);
+ $this->assertSame($class, $container->get('foo'));
+ }
+
+ /**
+ * @covers \Engelsystem\Container\Container::get
+ * @expectedException \Engelsystem\Container\NotFoundException
+ */
+ public function testGetException()
+ {
+ $container = new Container();
+
+ $container->get('not.registered.service');
+ }
+
+ /**
+ * @covers \Engelsystem\Container\Container::instance
+ * @covers \Engelsystem\Container\Container::resolve
+ */
+ public function testInstance()
+ {
+ $container = new Container();
+ $class = new class
+ {
+ };
+
+ $container->instance('foo', $class);
+ $this->assertSame($class, $container->get('foo'));
+ }
+
+ /**
+ * @covers \Engelsystem\Container\Container::has
+ */
+ public function testHas()
+ {
+ $container = new Container();
+
+ $this->assertFalse($container->has('test'));
+
+ $class = new class
+ {
+ };
+
+ $container->instance('test', $class);
+ $this->assertTrue($container->has('test'));
+ }
+
+ /**
+ * @covers \Engelsystem\Container\Container::singleton
+ */
+ public function testSingleton()
+ {
+ $container = new Container();
+ $class = new class
+ {
+ };
+
+ $container->singleton('foo', $class);
+ $this->assertSame($class, $container->get('foo'));
+ $this->assertSame($class, $container->get('foo'));
+ }
+
+ /**
+ * @covers \Engelsystem\Container\Container::setInstance
+ * @covers \Engelsystem\Container\Container::getInstance
+ */
+ public function testContainerSingleton()
+ {
+ // Ensure that no container has been initialized
+ $reflection = new \ReflectionProperty(Container::class, 'instance');
+ $reflection->setAccessible(true);
+ $reflection->setValue(null, null);
+ $reflection->setAccessible(false);
+
+ $container0 = new Container();
+ $container = Container::getInstance();
+
+ $this->assertNotSame($container0, $container);
+
+ $container1 = new Container;
+ Container::setInstance($container1);
+
+ $this->assertSame($container1, Container::getInstance());
+ }
+}
diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php
new file mode 100644
index 00000000..d9782888
--- /dev/null
+++ b/tests/Unit/HelpersTest.php
@@ -0,0 +1,152 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Application;
+use Engelsystem\Config\Config;
+use Engelsystem\Container\Container;
+use Engelsystem\Http\Request;
+use Engelsystem\Renderer\Renderer;
+use Engelsystem\Routing\UrlGenerator;
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class HelpersTest extends TestCase
+{
+ /**
+ * @covers \app
+ */
+ public function testApp()
+ {
+ $class = new class
+ {
+ };
+
+ $appMock = $this->getAppMock('some.name', $class);
+
+ $this->assertEquals($appMock, app());
+ $this->assertEquals($class, app('some.name'));
+ }
+
+ /**
+ * @covers \config
+ */
+ public function testConfig()
+ {
+ $configMock = $this->getMockBuilder(Config::class)
+ ->getMock();
+
+ $this->getAppMock('config', $configMock);
+ $this->assertEquals($configMock, config());
+
+ $configMock->expects($this->once())
+ ->method('set')
+ ->with(['foo' => 'bar']);
+
+ $this->assertTrue(config(['foo' => 'bar']));
+
+ $configMock->expects($this->once())
+ ->method('get')
+ ->with('mail')
+ ->willReturn(['user' => 'FooBar']);
+
+ $this->assertEquals(['user' => 'FooBar'], config('mail'));
+ }
+
+ /**
+ * @covers \request
+ */
+ public function testRequest()
+ {
+ $requestMock = $this->getMockBuilder(Request::class)
+ ->getMock();
+
+ $this->getAppMock('request', $requestMock);
+ $this->assertEquals($requestMock, request());
+
+ $requestMock->expects($this->once())
+ ->method('input')
+ ->with('requestKey')
+ ->willReturn('requestValue');
+
+ $this->assertEquals('requestValue', request('requestKey'));
+ }
+
+ /**
+ * @covers \session
+ */
+ public function testSession()
+ {
+ $sessionMock = $this->getMockBuilder(Session::class)
+ ->getMock();
+
+ $this->getAppMock('session', $sessionMock);
+ $this->assertEquals($sessionMock, session());
+
+ $sessionMock->expects($this->once())
+ ->method('get')
+ ->with('someKey')
+ ->willReturn('someValue');
+
+ $this->assertEquals('someValue', session('someKey'));
+ }
+
+ /**
+ * @covers \view
+ */
+ public function testView()
+ {
+ $rendererMock = $this->getMockBuilder(Renderer::class)
+ ->getMock();
+
+ $this->getAppMock('renderer', $rendererMock);
+ $this->assertEquals($rendererMock, view());
+
+ $rendererMock->expects($this->once())
+ ->method('render')
+ ->with('template.name', ['template' => 'data'])
+ ->willReturn('rendered template');
+
+ $this->assertEquals('rendered template', view('template.name', ['template' => 'data']));
+ }
+
+ /**
+ * @covers \url
+ */
+ public function testUrl()
+ {
+ $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class)
+ ->getMock();
+
+ $this->getAppMock('routing.urlGenerator', $urlGeneratorMock);
+ $this->assertEquals($urlGeneratorMock, url());
+
+ $urlGeneratorMock->expects($this->once())
+ ->method('to')
+ ->with('foo/bar', ['param' => 'value'])
+ ->willReturn('http://lorem.ipsum/foo/bar?param=value');
+
+ $this->assertEquals('http://lorem.ipsum/foo/bar?param=value', url('foo/bar', ['param' => 'value']));
+ }
+
+ /**
+ * @param string $alias
+ * @param object $object
+ * @return Application|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected function getAppMock($alias, $object)
+ {
+ $appMock = $this->getMockBuilder(Container::class)
+ ->getMock();
+
+ $appMock->expects($this->atLeastOnce())
+ ->method('get')
+ ->with($alias)
+ ->willReturn($object);
+
+ /** @var $appMock Application */
+ Application::setInstance($appMock);
+
+ return $appMock;
+ }
+}
diff --git a/tests/Unit/Renderer/HtmlEngineTest.php b/tests/Unit/Renderer/HtmlEngineTest.php
new file mode 100644
index 00000000..0b317b72
--- /dev/null
+++ b/tests/Unit/Renderer/HtmlEngineTest.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Renderer\HtmlEngine;
+use PHPUnit\Framework\TestCase;
+
+class HtmlEngineTest extends TestCase
+{
+ /** @var string[] */
+ protected $tmpFileNames = [];
+
+ /**
+ * @covers \Engelsystem\Renderer\HtmlEngine::get
+ */
+ public function testGet()
+ {
+ $engine = new HtmlEngine();
+
+ $file = $this->createTempFile('<div>%main_content%</div>');
+
+ $data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']);
+ $this->assertEquals('<div>Lorem ipsum dolor sit</div>', $data);
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\HtmlEngine::canRender
+ */
+ public function testCanRender()
+ {
+ $engine = new HtmlEngine();
+
+ $this->assertFalse($engine->canRender('/dev/null'));
+
+ $file = $this->createTempFile();
+ $this->assertTrue($engine->canRender($file));
+
+ $htmFile = $this->createTempFile('', '.htm');
+ $this->assertTrue($engine->canRender($htmFile));
+ }
+
+ /**
+ * @param string $content
+ * @param string $extension
+ * @return string
+ */
+ protected function createTempFile($content = '', $extension = '.html')
+ {
+ $tmpFileName = tempnam(sys_get_temp_dir(), 'EngelsystemUnitTest');
+
+ $fileName = $tmpFileName . $extension;
+ rename($tmpFileName, $fileName);
+
+ file_put_contents($fileName, $content);
+
+ $this->tmpFileNames[] = $fileName;
+
+ return $fileName;
+ }
+
+ public function tearDown()
+ {
+ foreach ($this->tmpFileNames as $fileName) {
+ unlink($fileName);
+ }
+ }
+}
diff --git a/tests/Unit/Renderer/RendererTest.php b/tests/Unit/Renderer/RendererTest.php
new file mode 100644
index 00000000..b0238078
--- /dev/null
+++ b/tests/Unit/Renderer/RendererTest.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Renderer\EngineInterface;
+use Engelsystem\Renderer\Renderer;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+
+class RendererTest extends TestCase
+{
+ public function testGet()
+ {
+ $renderer = new Renderer();
+
+ $nullRenderer = $this->getMockForAbstractClass(EngineInterface::class);
+
+ $nullRenderer->expects($this->atLeastOnce())
+ ->method('canRender')
+ ->willReturn(false);
+ $renderer->addRenderer($nullRenderer);
+
+ $mockRenderer = $this->getMockForAbstractClass(EngineInterface::class);
+
+ $mockRenderer->expects($this->atLeastOnce())
+ ->method('canRender')
+ ->with('foo.template')
+ ->willReturn(true);
+
+ $mockRenderer->expects($this->atLeastOnce())
+ ->method('get')
+ ->with('foo.template', ['lorem' => 'ipsum'])
+ ->willReturn('Rendered content');
+
+ $renderer->addRenderer($mockRenderer);
+ $data = $renderer->render('foo.template', ['lorem' => 'ipsum']);
+
+ $this->assertEquals('Rendered content', $data);
+ }
+
+ public function testError()
+ {
+ $renderer = new Renderer();
+
+ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
+ $loggerMock
+ ->expects($this->once())
+ ->method('error');
+
+ $renderer->setLogger($loggerMock);
+
+ $data = $renderer->render('testing.template');
+ $this->assertEquals('', $data);
+ }
+}
diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Routing/UrlGeneratorTest.php
new file mode 100644
index 00000000..fc23520a
--- /dev/null
+++ b/tests/Unit/Routing/UrlGeneratorTest.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Application;
+use Engelsystem\Container\Container;
+use Engelsystem\Http\Request;
+use Engelsystem\Routing\UrlGenerator;
+use PHPUnit\Framework\TestCase;
+
+class UrlGeneratorTest extends TestCase
+{
+ public function provideLinksTo()
+ {
+ return [
+ ['/foo/path', '/foo/path', 'http://foo.bar/foo/path', [], 'http://foo.bar/foo/path'],
+ ['foo', '/foo', 'https://foo.bar/foo', [], 'https://foo.bar/foo'],
+ ['foo', '/foo', 'http://f.b/foo', ['test' => 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'],
+ ];
+ }
+
+ /**
+ * @dataProvider provideLinksTo
+ * @covers \Engelsystem\Routing\UrlGenerator::to
+ *
+ * @param string $path
+ * @param string $willReturn
+ * @param string $urlToPath
+ * @param string[] $arguments
+ * @param string $expectedUrl
+ */
+ public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl)
+ {
+ $app = new Container();
+ $urlGenerator = new UrlGenerator();
+ Application::setInstance($app);
+
+ $request = $this->getMockBuilder(Request::class)
+ ->getMock();
+
+ $request->expects($this->once())
+ ->method('getUriForPath')
+ ->with($path)
+ ->willReturn($willReturn);
+
+ $app->instance('request', $request);
+
+ $url = $urlGenerator->to($urlToPath, $arguments);
+ $this->assertEquals($expectedUrl, $url);
+ }
+}