From 411ea5bb6d0ecf32e6c989a99fac120502db9fe9 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 14:15:19 +0100 Subject: [PATCH] Refactored service provider tests --- .../Unit/Config/ConfigServiceProviderTest.php | 35 +++++-------------- .../Database/DatabaseServiceProviderTest.php | 34 ++++++------------ .../ExceptionsServiceProviderTest.php | 20 +++-------- .../Unit/Logger/LoggerServiceProviderTest.php | 20 +++-------- .../Routing/RoutingServiceProviderTest.php | 20 +++-------- tests/Unit/ServiceProviderTest.php | 31 ++++++++++------ 6 files changed, 55 insertions(+), 105 deletions(-) diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index 26128e79..96ac8704 100644 --- a/tests/Unit/Config/ConfigServiceProviderTest.php +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -5,10 +5,10 @@ namespace Engelsystem\Test\Config; use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Config\ConfigServiceProvider; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class ConfigServiceProviderTest extends TestCase +class ConfigServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Config\ConfigServiceProvider::register() @@ -19,34 +19,15 @@ class ConfigServiceProviderTest extends TestCase $config = $this->getMockBuilder(Config::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['make', 'instance', 'get']) - ->getMock(); + $app = $this->getApp(['make', 'instance', 'get']); 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(); + $this->setExpects($app, 'make', [Config::class], $config); + $this->setExpects($app, 'instance', ['config', $config]); + $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce()); - $config->expects($this->once()) - ->method('get') - ->with(null) - ->willReturn([]); + $this->setExpects($config, 'set', null, null, $this->exactly(2)); + $this->setExpects($config, 'get', [null], []); $serviceProvider = new ConfigServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php index d0e3e164..d61f5ae7 100644 --- a/tests/Unit/Database/DatabaseServiceProviderTest.php +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -2,14 +2,13 @@ namespace Engelsystem\Test\Database; -use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Database\DatabaseServiceProvider; +use Engelsystem\Test\Unit\ServiceProviderTest; use Exception; -use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject; -class DatabaseServiceProviderTest extends TestCase +class DatabaseServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Database\DatabaseServiceProvider::register() @@ -21,29 +20,18 @@ class DatabaseServiceProviderTest extends TestCase $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', - ]); + $app = $this->getApp(['get']); - $serviceProvider = new DatabaseServiceProvider($app); + $this->setExpects($app, 'get', ['config'], $config); + $this->setExpects($config, 'get', ['database'], [ + 'host' => 'localhost', + 'db' => 'database', + 'user' => 'user', + 'pw' => 'password', + ], $this->atLeastOnce()); $this->expectException(Exception::class); + $serviceProvider = new DatabaseServiceProvider($app); $serviceProvider->register(); } } diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php index 26eddb75..fc48d39e 100644 --- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -2,13 +2,12 @@ namespace Engelsystem\Test\Exceptions; -use Engelsystem\Application; use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\Handler as ExceptionHandler; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class ExceptionsServiceProviderTest extends TestCase +class ExceptionsServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register() @@ -19,19 +18,10 @@ class ExceptionsServiceProviderTest extends TestCase $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 = $this->getApp(); - $app->expects($this->once()) - ->method('instance') - ->with('error.handler', $exceptionHandler); + $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler); + $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]); $serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index 5143d236..66f63cf4 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -2,14 +2,13 @@ namespace Engelsystem\Test\Logger; -use Engelsystem\Application; use Engelsystem\Logger\EngelsystemLogger; use Engelsystem\Logger\LoggerServiceProvider; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; use Psr\Log\LoggerInterface; -class LoggerServiceProviderTest extends TestCase +class LoggerServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Logger\LoggerServiceProvider::register() @@ -20,19 +19,10 @@ class LoggerServiceProviderTest extends TestCase $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 = $this->getApp(['make', 'instance', 'bind']); - $app->expects($this->once()) - ->method('instance') - ->with('logger', $logger); + $this->setExpects($app, 'make', [EngelsystemLogger::class], $logger); + $this->setExpects($app, 'instance', ['logger', $logger]); $app->expects($this->atLeastOnce()) ->method('bind') diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index 4f1cd5fc..bb2a1d65 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -2,13 +2,12 @@ namespace Engelsystem\Test\Routing; -use Engelsystem\Application; use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\UrlGenerator; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class RoutingServiceProviderTest extends TestCase +class RoutingServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Routing\RoutingServiceProvider::register() @@ -19,19 +18,10 @@ class RoutingServiceProviderTest extends TestCase $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 = $this->getApp(); - $app->expects($this->once()) - ->method('instance') - ->with('routing.urlGenerator', $urlGenerator); + $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator); + $this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]); $serviceProvider = new RoutingServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php index be843742..dc58a65e 100644 --- a/tests/Unit/ServiceProviderTest.php +++ b/tests/Unit/ServiceProviderTest.php @@ -4,33 +4,44 @@ namespace Engelsystem\Test\Unit; use Engelsystem\Application; use PHPUnit\Framework\TestCase; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_MockObject_Matcher_InvokedRecorder as InvokedRecorder; +use PHPUnit_Framework_MockObject_MockObject as MockObject; abstract class ServiceProviderTest extends TestCase { /** * @param array $methods - * @return Application|PHPUnit_Framework_MockObject_MockObject + * @return Application|MockObject */ protected function getApp($methods = ['make', 'instance']) { - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + /** @var 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 + * @param MockObject $object + * @param string $method + * @param array $arguments + * @param mixed $return + * @param InvokedRecorder $times */ - protected function setExpects($object, $method, $arguments, $return = null) + protected function setExpects($object, $method, $arguments = null, $return = null, $times = null) { - $invocation = $object->expects($this->once()) + if (is_null($times)) { + $times = $this->once(); + } + + $invocation = $object->expects($times) ->method($method); - call_user_func_array([$invocation, 'with'], $arguments); + + if (is_null($arguments)) { + $invocation->withAnyParameters(); + } else { + call_user_func_array([$invocation, 'with'], $arguments); + } if (!is_null($return)) { $invocation->willReturn($return);