summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/ApplicationTest.php171
-rw-r--r--tests/Unit/Config/ConfigTest.php115
-rw-r--r--tests/Unit/HelpersTest.php166
-rw-r--r--tests/Unit/Renderer/HtmlEngineTest.php67
-rw-r--r--tests/Unit/Renderer/RendererTest.php55
-rw-r--r--tests/Unit/Routing/UrlGeneratorTest.php51
6 files changed, 625 insertions, 0 deletions
diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php
new file mode 100644
index 00000000..78310134
--- /dev/null
+++ b/tests/Unit/ApplicationTest.php
@@ -0,0 +1,171 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Application;
+use Engelsystem\Config\Config;
+use Engelsystem\Container\Container;
+use Engelsystem\Container\ServiceProvider;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+use Psr\Container\ContainerInterface;
+use ReflectionClass;
+
+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, Application::getInstance());
+ $this->assertSame($app, Container::getInstance());
+ }
+
+ /**
+ * @covers \Engelsystem\Application::setAppPath
+ * @covers \Engelsystem\Application::registerPaths
+ * @covers \Engelsystem\Application::path
+ */
+ public function testAppPath()
+ {
+ $app = new Application();
+
+ $this->assertFalse($app->has('path'));
+
+ $app->setAppPath('.');
+ $this->assertTrue($app->has('path'));
+ $this->assertTrue($app->has('path.config'));
+ $this->assertTrue($app->has('path.lang'));
+
+ $this->assertEquals(realpath('.'), $app->path());
+ $this->assertEquals(realpath('.') . '/config', $app->get('path.config'));
+
+ $app->setAppPath('./../');
+ $this->assertEquals(realpath('../') . '/config', $app->get('path.config'));
+ }
+
+ /**
+ * @covers \Engelsystem\Application::register
+ */
+ public function testRegister()
+ {
+ $app = new Application();
+
+ $serviceProvider = $this->mockServiceProvider($app, ['register']);
+ $serviceProvider->expects($this->once())
+ ->method('register');
+
+ $app->register($serviceProvider);
+
+ $anotherServiceProvider = $this->mockServiceProvider($app, ['register', 'boot']);
+ $anotherServiceProvider->expects($this->once())
+ ->method('register');
+ $anotherServiceProvider->expects($this->once())
+ ->method('boot');
+
+ $app->bootstrap();
+ $app->register($anotherServiceProvider);
+ }
+
+ /**
+ * @covers \Engelsystem\Application::register
+ */
+ public function testRegisterBoot()
+ {
+ $app = new Application();
+ $app->bootstrap();
+
+ $serviceProvider = $this->mockServiceProvider($app, ['register', 'boot']);
+ $serviceProvider->expects($this->once())
+ ->method('register');
+ $serviceProvider->expects($this->once())
+ ->method('boot');
+
+ $app->register($serviceProvider);
+ }
+
+ /**
+ * @covers \Engelsystem\Application::register
+ */
+ public function testRegisterClassName()
+ {
+ $app = new Application();
+
+ $mockClassName = $this->getMockClass(ServiceProvider::class);
+ $serviceProvider = $this->getMockBuilder($mockClassName)
+ ->setConstructorArgs([$app])
+ ->setMethods(['register'])
+ ->getMock();
+
+ $serviceProvider->expects($this->once())
+ ->method('register');
+
+ $app->instance($mockClassName, $serviceProvider);
+ $app->register($mockClassName);
+ }
+
+ /**
+ * @covers \Engelsystem\Application::bootstrap
+ * @covers \Engelsystem\Application::isBooted
+ */
+ public function testBootstrap()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['register'])
+ ->getMock();
+
+ $serviceProvider = $this->mockServiceProvider($app, ['boot']);
+ $serviceProvider->expects($this->once())
+ ->method('boot');
+
+ $app->expects($this->once())
+ ->method('register')
+ ->with($serviceProvider);
+
+ $config = $this->getMockBuilder(Config::class)
+ ->getMock();
+
+ $config->expects($this->once())
+ ->method('get')
+ ->with('providers')
+ ->willReturn([$serviceProvider]);
+
+ $property = (new ReflectionClass($app))->getProperty('serviceProviders');
+ $property->setAccessible(true);
+ $property->setValue($app, [$serviceProvider]);
+
+ $app->bootstrap($config);
+
+ $this->assertTrue($app->isBooted());
+
+ // Run bootstrap another time to ensure that providers are registered only once
+ $app->bootstrap($config);
+ }
+
+ /**
+ * @param Application $app
+ * @param array $methods
+ * @return PHPUnit_Framework_MockObject_MockObject|ServiceProvider
+ */
+ protected function mockServiceProvider(Application $app, $methods = [])
+ {
+ $serviceProvider = $this->getMockBuilder(ServiceProvider::class)
+ ->setConstructorArgs([$app])
+ ->setMethods($methods)
+ ->getMockForAbstractClass();
+
+ return $serviceProvider;
+ }
+}
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/HelpersTest.php b/tests/Unit/HelpersTest.php
new file mode 100644
index 00000000..9ec824af
--- /dev/null
+++ b/tests/Unit/HelpersTest.php
@@ -0,0 +1,166 @@
+<?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 \env
+ */
+ public function testEnv()
+ {
+ putenv('envTestVar=someContent');
+
+ $env = env('envTestVar');
+ $this->assertEquals('someContent', $env);
+
+ $env = env('someRandomEnvVarThatShouldNeverExist', 'someDefaultValue');
+ $this->assertEquals('someDefaultValue', $env);
+ }
+
+ /**
+ * @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);
+ }
+}