engelsystem/tests/Unit/Exceptions/ExceptionsServiceProviderTe...

135 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2017-10-31 13:40:13 +01:00
<?php
namespace Engelsystem\Test\Unit\Exceptions;
2017-10-31 13:40:13 +01:00
use Engelsystem\Exceptions\ExceptionsServiceProvider;
2017-11-20 17:08:05 +01:00
use Engelsystem\Exceptions\Handler;
2017-11-24 15:08:43 +01:00
use Engelsystem\Exceptions\Handlers\HandlerInterface;
use Engelsystem\Exceptions\Handlers\Legacy;
use Engelsystem\Exceptions\Handlers\LegacyDevelopment;
use Engelsystem\Exceptions\Handlers\Whoops;
use Engelsystem\Http\Request;
2017-10-31 14:15:19 +01:00
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
2017-10-31 13:40:13 +01:00
2017-10-31 14:15:19 +01:00
class ExceptionsServiceProviderTest extends ServiceProviderTest
2017-10-31 13:40:13 +01:00
{
/**
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addDevelopmentHandler
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addProductionHandler
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register
2017-10-31 13:40:13 +01:00
*/
public function testRegister()
{
2017-11-24 15:08:43 +01:00
$app = $this->getApp(['make', 'instance', 'bind']);
/** @var Handler|MockObject $handler */
2017-11-24 15:08:43 +01:00
$handler = $this->createMock(Handler::class);
$this->setExpects($handler, 'register');
/** @var Legacy|MockObject $legacyHandler */
$legacyHandler = $this->createMock(Legacy::class);
/** @var LegacyDevelopment|MockObject $developmentHandler */
$developmentHandler = $this->createMock(LegacyDevelopment::class);
$whoopsHandler = $this->getMockBuilder(Whoops::class)
->setConstructorArgs([$app])
2017-10-31 13:40:13 +01:00
->getMock();
2017-11-24 15:08:43 +01:00
$app->expects($this->exactly(3))
->method('instance')
->withConsecutive(
['error.handler.production', $legacyHandler],
['error.handler.development', $whoopsHandler],
['error.handler', $handler]
);
2017-10-31 13:40:13 +01:00
2017-11-24 15:08:43 +01:00
$app->expects($this->exactly(4))
->method('make')
->withConsecutive(
[Handler::class],
[Legacy::class],
[LegacyDevelopment::class],
[Whoops::class]
)
->willReturnOnConsecutiveCalls(
$handler,
$legacyHandler,
$developmentHandler,
$whoopsHandler
);
$app->expects($this->exactly(2))
->method('bind')
->withConsecutive(
[HandlerInterface::class, 'error.handler.production'],
[Handler::class, 'error.handler']
);
$handler->expects($this->exactly(2))
->method('setHandler')
->withConsecutive(
[Handler::ENV_PRODUCTION, $legacyHandler],
[Handler::ENV_DEVELOPMENT, $whoopsHandler]
);
2017-10-31 13:40:13 +01:00
$serviceProvider = new ExceptionsServiceProvider($app);
$serviceProvider->register();
}
2017-11-24 15:08:43 +01:00
/**
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::boot
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addLogger
2017-11-24 15:08:43 +01:00
*/
public function testBoot()
{
/** @var HandlerInterface|MockObject $handlerImpl */
$handlerImpl = $this->getMockForAbstractClass(HandlerInterface::class);
/** @var Legacy|MockObject $loggingHandler */
$loggingHandler = $this->createMock(Legacy::class);
/** @var Handler|MockObject $handler */
2017-11-24 15:08:43 +01:00
$handler = $this->createMock(Handler::class);
/** @var Request|MockObject $request */
2017-11-24 15:08:43 +01:00
$request = $this->createMock(Request::class);
/** @var LoggerInterface|MockObject $log */
$log = $this->getMockForAbstractClass(LoggerInterface::class);
$handler->expects($this->exactly(2))
2017-11-24 15:08:43 +01:00
->method('setRequest')
->with($request);
$handler->expects($this->exactly(2))
->method('getHandler')
->willReturnOnConsecutiveCalls([$handlerImpl], [$loggingHandler]);
$loggingHandler->expects($this->once())
->method('setLogger')
->with($log);
2017-11-24 15:08:43 +01:00
$app = $this->getApp(['get']);
$app->expects($this->exactly(5))
2017-11-24 15:08:43 +01:00
->method('get')
->withConsecutive(
['error.handler'],
['request'],
['error.handler'],
['request'],
[LoggerInterface::class]
2017-11-24 15:08:43 +01:00
)
->willReturnOnConsecutiveCalls(
$handler,
$request,
$handler,
$request,
$log
2017-11-24 15:08:43 +01:00
);
$provider = new ExceptionsServiceProvider($app);
$provider->boot();
$provider->boot();
2017-11-24 15:08:43 +01:00
}
2017-10-31 13:40:13 +01:00
}