summaryrefslogtreecommitdiff
path: root/tests/Unit
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2017-10-31 13:40:13 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2017-10-31 13:40:13 +0100
commit60fd72cd1a1e4e53b9af87e00a8c27687c6b5385 (patch)
treebcf250d05a49a848644af44f09fb897467e3ecb0 /tests/Unit
parentfb05a38a963784716c105e7f214615ad13d2f272 (diff)
Added service providers
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/Config/ConfigServiceProviderTest.php54
-rw-r--r--tests/Unit/Database/DatabaseServiceProviderTest.php49
-rw-r--r--tests/Unit/Exceptions/ExceptionsServiceProviderTest.php39
-rw-r--r--tests/Unit/Logger/LoggerServiceProviderTest.php47
-rw-r--r--tests/Unit/Renderer/RendererServiceProviderTest.php81
-rw-r--r--tests/Unit/Routing/RoutingServiceProviderTest.php39
-rw-r--r--tests/Unit/ServiceProviderTest.php39
7 files changed, 348 insertions, 0 deletions
diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php
new file mode 100644
index 00000000..26128e79
--- /dev/null
+++ b/tests/Unit/Config/ConfigServiceProviderTest.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace Engelsystem\Test\Config;
+
+use Engelsystem\Application;
+use Engelsystem\Config\Config;
+use Engelsystem\Config\ConfigServiceProvider;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class ConfigServiceProviderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Config\ConfigServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
+ $config = $this->getMockBuilder(Config::class)
+ ->getMock();
+
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['make', 'instance', 'get'])
+ ->getMock();
+ Application::setInstance($app);
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(Config::class)
+ ->willReturn($config);
+
+ $app->expects($this->once())
+ ->method('instance')
+ ->with('config', $config);
+
+ $app->expects($this->atLeastOnce())
+ ->method('get')
+ ->with('path.config')
+ ->willReturn(__DIR__ . '/../../../config');
+
+ $config->expects($this->exactly(2))
+ ->method('set')
+ ->withAnyParameters();
+
+ $config->expects($this->once())
+ ->method('get')
+ ->with(null)
+ ->willReturn([]);
+
+ $serviceProvider = new ConfigServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php
new file mode 100644
index 00000000..d0e3e164
--- /dev/null
+++ b/tests/Unit/Database/DatabaseServiceProviderTest.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Engelsystem\Test\Database;
+
+use Engelsystem\Application;
+use Engelsystem\Config\Config;
+use Engelsystem\Database\DatabaseServiceProvider;
+use Exception;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class DatabaseServiceProviderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Database\DatabaseServiceProvider::register()
+ * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
+ $config = $this->getMockBuilder(Config::class)
+ ->getMock();
+
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['get'])
+ ->getMock();
+
+ $app->expects($this->once())
+ ->method('get')
+ ->with('config')
+ ->willReturn($config);
+
+ $config->expects($this->atLeastOnce())
+ ->method('get')
+ ->with('database')
+ ->willReturn([
+ 'host' => 'localhost',
+ 'db' => 'database',
+ 'user' => 'user',
+ 'pw' => 'password',
+ ]);
+
+ $serviceProvider = new DatabaseServiceProvider($app);
+ $this->expectException(Exception::class);
+
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
new file mode 100644
index 00000000..26eddb75
--- /dev/null
+++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Engelsystem\Test\Exceptions;
+
+use Engelsystem\Application;
+use Engelsystem\Exceptions\ExceptionsServiceProvider;
+use Engelsystem\Exceptions\Handler as ExceptionHandler;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class ExceptionsServiceProviderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|ExceptionHandler $exceptionHandler */
+ $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class)
+ ->getMock();
+
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['make', 'instance'])
+ ->getMock();
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(ExceptionHandler::class)
+ ->willReturn($exceptionHandler);
+
+ $app->expects($this->once())
+ ->method('instance')
+ ->with('error.handler', $exceptionHandler);
+
+ $serviceProvider = new ExceptionsServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php
new file mode 100644
index 00000000..5143d236
--- /dev/null
+++ b/tests/Unit/Logger/LoggerServiceProviderTest.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Engelsystem\Test\Logger;
+
+use Engelsystem\Application;
+use Engelsystem\Logger\EngelsystemLogger;
+use Engelsystem\Logger\LoggerServiceProvider;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+use Psr\Log\LoggerInterface;
+
+class LoggerServiceProviderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Logger\LoggerServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|EngelsystemLogger $logger */
+ $logger = $this->getMockBuilder(EngelsystemLogger::class)
+ ->getMock();
+
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['make', 'instance', 'bind'])
+ ->getMock();
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(EngelsystemLogger::class)
+ ->willReturn($logger);
+
+ $app->expects($this->once())
+ ->method('instance')
+ ->with('logger', $logger);
+
+ $app->expects($this->atLeastOnce())
+ ->method('bind')
+ ->withConsecutive(
+ [LoggerInterface::class, 'logger'],
+ [EngelsystemLogger::class, 'logger']
+ );
+
+ $serviceProvider = new LoggerServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/Renderer/RendererServiceProviderTest.php b/tests/Unit/Renderer/RendererServiceProviderTest.php
new file mode 100644
index 00000000..f9044d8b
--- /dev/null
+++ b/tests/Unit/Renderer/RendererServiceProviderTest.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Engelsystem\Test\Logger;
+
+use Engelsystem\Renderer\EngineInterface;
+use Engelsystem\Renderer\HtmlEngine;
+use Engelsystem\Renderer\Renderer;
+use Engelsystem\Renderer\RendererServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class RendererServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\RendererServiceProvider::register()
+ * @covers \Engelsystem\Renderer\RendererServiceProvider::registerRenderer()
+ * @covers \Engelsystem\Renderer\RendererServiceProvider::registerHtmlEngine()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */
+ $renderer = $this->getMockBuilder(Renderer::class)
+ ->getMock();
+ /** @var PHPUnit_Framework_MockObject_MockObject|HtmlEngine $htmlEngine */
+ $htmlEngine = $this->getMockBuilder(HtmlEngine::class)
+ ->getMock();
+
+ $app = $this->getApp(['make', 'instance', 'tag']);
+
+ $app->expects($this->exactly(2))
+ ->method('make')
+ ->withConsecutive(
+ [Renderer::class],
+ [HtmlEngine::class]
+ )->willReturnOnConsecutiveCalls(
+ $renderer,
+ $htmlEngine
+ );
+
+ $app->expects($this->exactly(2))
+ ->method('instance')
+ ->withConsecutive(
+ ['renderer', $renderer],
+ ['renderer.htmlEngine', $htmlEngine]
+ );
+
+ $this->setExpects($app, 'tag', ['renderer.htmlEngine', ['renderer.engine']]);
+
+ $serviceProvider = new RendererServiceProvider($app);
+ $serviceProvider->register();
+ }
+
+ /**
+ * @covers \Engelsystem\Renderer\RendererServiceProvider::boot()
+ */
+ public function testBoot()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */
+ $renderer = $this->getMockBuilder(Renderer::class)
+ ->getMock();
+ /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine1 */
+ $engine1 = $this->getMockForAbstractClass(EngineInterface::class);
+ /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine2 */
+ $engine2 = $this->getMockForAbstractClass(EngineInterface::class);
+
+ $app = $this->getApp(['get', 'tagged']);
+
+ $engines = [$engine1, $engine2];
+
+ $this->setExpects($app, 'get', ['renderer'], $renderer);
+ $this->setExpects($app, 'tagged', ['renderer.engine'], $engines);
+
+ $invocation = $renderer
+ ->expects($this->exactly(count($engines)))
+ ->method('addRenderer');
+ call_user_func_array([$invocation, 'withConsecutive'], $engines);
+
+ $serviceProvider = new RendererServiceProvider($app);
+ $serviceProvider->boot();
+ }
+}
diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php
new file mode 100644
index 00000000..4f1cd5fc
--- /dev/null
+++ b/tests/Unit/Routing/RoutingServiceProviderTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Engelsystem\Test\Routing;
+
+use Engelsystem\Application;
+use Engelsystem\Routing\RoutingServiceProvider;
+use Engelsystem\Routing\UrlGenerator;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+
+class RoutingServiceProviderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Routing\RoutingServiceProvider::register()
+ */
+ public function testRegister()
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */
+ $urlGenerator = $this->getMockBuilder(UrlGenerator::class)
+ ->getMock();
+
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ $app = $this->getMockBuilder(Application::class)
+ ->setMethods(['make', 'instance'])
+ ->getMock();
+
+ $app->expects($this->once())
+ ->method('make')
+ ->with(UrlGenerator::class)
+ ->willReturn($urlGenerator);
+
+ $app->expects($this->once())
+ ->method('instance')
+ ->with('routing.urlGenerator', $urlGenerator);
+
+ $serviceProvider = new RoutingServiceProvider($app);
+ $serviceProvider->register();
+ }
+}
diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php
new file mode 100644
index 00000000..be843742
--- /dev/null
+++ b/tests/Unit/ServiceProviderTest.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Engelsystem\Test\Unit;
+
+use Engelsystem\Application;
+use PHPUnit\Framework\TestCase;
+use PHPUnit_Framework_MockObject_MockObject;
+
+abstract class ServiceProviderTest extends TestCase
+{
+ /**
+ * @param array $methods
+ * @return Application|PHPUnit_Framework_MockObject_MockObject
+ */
+ protected function getApp($methods = ['make', 'instance'])
+ {
+ /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */
+ return $this->getMockBuilder(Application::class)
+ ->setMethods($methods)
+ ->getMock();
+ }
+
+ /**
+ * @param PHPUnit_Framework_MockObject_MockObject $object
+ * @param string $method
+ * @param array $arguments
+ * @param mixed $return
+ */
+ protected function setExpects($object, $method, $arguments, $return = null)
+ {
+ $invocation = $object->expects($this->once())
+ ->method($method);
+ call_user_func_array([$invocation, 'with'], $arguments);
+
+ if (!is_null($return)) {
+ $invocation->willReturn($return);
+ }
+ }
+}