Refactored service provider tests

main
Igor Scheller 7 years ago
parent 915cbee3e0
commit 411ea5bb6d

@ -5,10 +5,10 @@ namespace Engelsystem\Test\Config;
use Engelsystem\Application; use Engelsystem\Application;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Config\ConfigServiceProvider; use Engelsystem\Config\ConfigServiceProvider;
use PHPUnit\Framework\TestCase; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
class ConfigServiceProviderTest extends TestCase class ConfigServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Config\ConfigServiceProvider::register() * @covers \Engelsystem\Config\ConfigServiceProvider::register()
@ -19,34 +19,15 @@ class ConfigServiceProviderTest extends TestCase
$config = $this->getMockBuilder(Config::class) $config = $this->getMockBuilder(Config::class)
->getMock(); ->getMock();
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getApp(['make', 'instance', 'get']);
$app = $this->getMockBuilder(Application::class)
->setMethods(['make', 'instance', 'get'])
->getMock();
Application::setInstance($app); Application::setInstance($app);
$app->expects($this->once()) $this->setExpects($app, 'make', [Config::class], $config);
->method('make') $this->setExpects($app, 'instance', ['config', $config]);
->with(Config::class) $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce());
->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()) $this->setExpects($config, 'set', null, null, $this->exactly(2));
->method('get') $this->setExpects($config, 'get', [null], []);
->with(null)
->willReturn([]);
$serviceProvider = new ConfigServiceProvider($app); $serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();

@ -2,14 +2,13 @@
namespace Engelsystem\Test\Database; namespace Engelsystem\Test\Database;
use Engelsystem\Application;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Database\DatabaseServiceProvider; use Engelsystem\Database\DatabaseServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception; use Exception;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
class DatabaseServiceProviderTest extends TestCase class DatabaseServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Database\DatabaseServiceProvider::register() * @covers \Engelsystem\Database\DatabaseServiceProvider::register()
@ -21,29 +20,18 @@ class DatabaseServiceProviderTest extends TestCase
$config = $this->getMockBuilder(Config::class) $config = $this->getMockBuilder(Config::class)
->getMock(); ->getMock();
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getApp(['get']);
$app = $this->getMockBuilder(Application::class)
->setMethods(['get'])
->getMock();
$app->expects($this->once())
->method('get')
->with('config')
->willReturn($config);
$config->expects($this->atLeastOnce()) $this->setExpects($app, 'get', ['config'], $config);
->method('get') $this->setExpects($config, 'get', ['database'], [
->with('database')
->willReturn([
'host' => 'localhost', 'host' => 'localhost',
'db' => 'database', 'db' => 'database',
'user' => 'user', 'user' => 'user',
'pw' => 'password', 'pw' => 'password',
]); ], $this->atLeastOnce());
$serviceProvider = new DatabaseServiceProvider($app);
$this->expectException(Exception::class); $this->expectException(Exception::class);
$serviceProvider = new DatabaseServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();
} }
} }

@ -2,13 +2,12 @@
namespace Engelsystem\Test\Exceptions; namespace Engelsystem\Test\Exceptions;
use Engelsystem\Application;
use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\ExceptionsServiceProvider;
use Engelsystem\Exceptions\Handler as ExceptionHandler; use Engelsystem\Exceptions\Handler as ExceptionHandler;
use PHPUnit\Framework\TestCase; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
class ExceptionsServiceProviderTest extends TestCase class ExceptionsServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register() * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register()
@ -19,19 +18,10 @@ class ExceptionsServiceProviderTest extends TestCase
$exceptionHandler = $this->getMockBuilder(ExceptionHandler::class) $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class)
->getMock(); ->getMock();
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getApp();
$app = $this->getMockBuilder(Application::class)
->setMethods(['make', 'instance'])
->getMock();
$app->expects($this->once())
->method('make')
->with(ExceptionHandler::class)
->willReturn($exceptionHandler);
$app->expects($this->once()) $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler);
->method('instance') $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]);
->with('error.handler', $exceptionHandler);
$serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider = new ExceptionsServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();

@ -2,14 +2,13 @@
namespace Engelsystem\Test\Logger; namespace Engelsystem\Test\Logger;
use Engelsystem\Application;
use Engelsystem\Logger\EngelsystemLogger; use Engelsystem\Logger\EngelsystemLogger;
use Engelsystem\Logger\LoggerServiceProvider; use Engelsystem\Logger\LoggerServiceProvider;
use PHPUnit\Framework\TestCase; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class LoggerServiceProviderTest extends TestCase class LoggerServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Logger\LoggerServiceProvider::register() * @covers \Engelsystem\Logger\LoggerServiceProvider::register()
@ -20,19 +19,10 @@ class LoggerServiceProviderTest extends TestCase
$logger = $this->getMockBuilder(EngelsystemLogger::class) $logger = $this->getMockBuilder(EngelsystemLogger::class)
->getMock(); ->getMock();
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getApp(['make', 'instance', 'bind']);
$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()) $this->setExpects($app, 'make', [EngelsystemLogger::class], $logger);
->method('instance') $this->setExpects($app, 'instance', ['logger', $logger]);
->with('logger', $logger);
$app->expects($this->atLeastOnce()) $app->expects($this->atLeastOnce())
->method('bind') ->method('bind')

@ -2,13 +2,12 @@
namespace Engelsystem\Test\Routing; namespace Engelsystem\Test\Routing;
use Engelsystem\Application;
use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\RoutingServiceProvider;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGenerator;
use PHPUnit\Framework\TestCase; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
class RoutingServiceProviderTest extends TestCase class RoutingServiceProviderTest extends ServiceProviderTest
{ {
/** /**
* @covers \Engelsystem\Routing\RoutingServiceProvider::register() * @covers \Engelsystem\Routing\RoutingServiceProvider::register()
@ -19,19 +18,10 @@ class RoutingServiceProviderTest extends TestCase
$urlGenerator = $this->getMockBuilder(UrlGenerator::class) $urlGenerator = $this->getMockBuilder(UrlGenerator::class)
->getMock(); ->getMock();
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ $app = $this->getApp();
$app = $this->getMockBuilder(Application::class)
->setMethods(['make', 'instance'])
->getMock();
$app->expects($this->once())
->method('make')
->with(UrlGenerator::class)
->willReturn($urlGenerator);
$app->expects($this->once()) $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator);
->method('instance') $this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]);
->with('routing.urlGenerator', $urlGenerator);
$serviceProvider = new RoutingServiceProvider($app); $serviceProvider = new RoutingServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();

@ -4,33 +4,44 @@ namespace Engelsystem\Test\Unit;
use Engelsystem\Application; use Engelsystem\Application;
use PHPUnit\Framework\TestCase; 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 abstract class ServiceProviderTest extends TestCase
{ {
/** /**
* @param array $methods * @param array $methods
* @return Application|PHPUnit_Framework_MockObject_MockObject * @return Application|MockObject
*/ */
protected function getApp($methods = ['make', 'instance']) protected function getApp($methods = ['make', 'instance'])
{ {
/** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ /** @var MockObject|Application $app */
return $this->getMockBuilder(Application::class) return $this->getMockBuilder(Application::class)
->setMethods($methods) ->setMethods($methods)
->getMock(); ->getMock();
} }
/** /**
* @param PHPUnit_Framework_MockObject_MockObject $object * @param MockObject $object
* @param string $method * @param string $method
* @param array $arguments * @param array $arguments
* @param mixed $return * @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); ->method($method);
if (is_null($arguments)) {
$invocation->withAnyParameters();
} else {
call_user_func_array([$invocation, 'with'], $arguments); call_user_func_array([$invocation, 'with'], $arguments);
}
if (!is_null($return)) { if (!is_null($return)) {
$invocation->willReturn($return); $invocation->willReturn($return);

Loading…
Cancel
Save