Refactored service provider tests
This commit is contained in:
parent
915cbee3e0
commit
411ea5bb6d
|
@ -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);
|
||||
$this->setExpects($app, 'make', [Config::class], $config);
|
||||
$this->setExpects($app, 'instance', ['config', $config]);
|
||||
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce());
|
||||
|
||||
$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([]);
|
||||
$this->setExpects($config, 'set', null, null, $this->exactly(2));
|
||||
$this->setExpects($config, 'get', [null], []);
|
||||
|
||||
$serviceProvider = new ConfigServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
|
|
|
@ -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 = $this->getApp(['get']);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('get')
|
||||
->with('config')
|
||||
->willReturn($config);
|
||||
|
||||
$config->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with('database')
|
||||
->willReturn([
|
||||
$this->setExpects($app, 'get', ['config'], $config);
|
||||
$this->setExpects($config, 'get', ['database'], [
|
||||
'host' => 'localhost',
|
||||
'db' => 'database',
|
||||
'user' => 'user',
|
||||
'pw' => 'password',
|
||||
]);
|
||||
|
||||
$serviceProvider = new DatabaseServiceProvider($app);
|
||||
], $this->atLeastOnce());
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
$serviceProvider = new DatabaseServiceProvider($app);
|
||||
$serviceProvider->register();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 = $this->getApp();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(ExceptionHandler::class)
|
||||
->willReturn($exceptionHandler);
|
||||
|
||||
$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();
|
||||
|
|
|
@ -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 = $this->getApp(['make', 'instance', 'bind']);
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(EngelsystemLogger::class)
|
||||
->willReturn($logger);
|
||||
|
||||
$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')
|
||||
|
|
|
@ -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 = $this->getApp();
|
||||
|
||||
$app->expects($this->once())
|
||||
->method('make')
|
||||
->with(UrlGenerator::class)
|
||||
->willReturn($urlGenerator);
|
||||
|
||||
$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();
|
||||
|
|
|
@ -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 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);
|
||||
|
||||
if (is_null($arguments)) {
|
||||
$invocation->withAnyParameters();
|
||||
} else {
|
||||
call_user_func_array([$invocation, 'with'], $arguments);
|
||||
}
|
||||
|
||||
if (!is_null($return)) {
|
||||
$invocation->willReturn($return);
|
||||
|
|
Loading…
Reference in New Issue