2017-10-31 13:40:13 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Exceptions;
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2023-01-24 19:23:57 +01:00
|
|
|
use Engelsystem\Environment;
|
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;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-04-19 20:41:38 +02:00
|
|
|
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
|
|
|
{
|
|
|
|
/**
|
2020-04-19 20:41:38 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addDevelopmentHandler
|
|
|
|
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addProductionHandler
|
|
|
|
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register
|
2017-10-31 13:40:13 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
2017-11-24 15:08:43 +01:00
|
|
|
$app = $this->getApp(['make', 'instance', 'bind']);
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @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(
|
2023-01-24 19:23:57 +01:00
|
|
|
[Environment::PRODUCTION, $legacyHandler],
|
|
|
|
[Environment::DEVELOPMENT, $whoopsHandler]
|
2017-11-24 15:08:43 +01:00
|
|
|
);
|
2017-10-31 13:40:13 +01:00
|
|
|
|
|
|
|
$serviceProvider = new ExceptionsServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2017-11-24 15:08:43 +01:00
|
|
|
|
|
|
|
/**
|
2020-04-19 20:41:38 +02:00
|
|
|
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::boot
|
|
|
|
* @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addLogger
|
2017-11-24 15:08:43 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testBoot(): void
|
2017-11-24 15:08:43 +01:00
|
|
|
{
|
2020-04-19 20:41:38 +02:00
|
|
|
/** @var HandlerInterface|MockObject $handlerImpl */
|
|
|
|
$handlerImpl = $this->getMockForAbstractClass(HandlerInterface::class);
|
|
|
|
|
|
|
|
/** @var Legacy|MockObject $loggingHandler */
|
|
|
|
$loggingHandler = $this->createMock(Legacy::class);
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Handler|MockObject $handler */
|
2017-11-24 15:08:43 +01:00
|
|
|
$handler = $this->createMock(Handler::class);
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Request|MockObject $request */
|
2017-11-24 15:08:43 +01:00
|
|
|
$request = $this->createMock(Request::class);
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
/** @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);
|
2020-04-19 20:41:38 +02:00
|
|
|
$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']);
|
2020-04-19 20:41:38 +02:00
|
|
|
$app->expects($this->exactly(5))
|
2017-11-24 15:08:43 +01:00
|
|
|
->method('get')
|
|
|
|
->withConsecutive(
|
|
|
|
['error.handler'],
|
2020-04-19 20:41:38 +02:00
|
|
|
['request'],
|
|
|
|
['error.handler'],
|
|
|
|
['request'],
|
|
|
|
[LoggerInterface::class]
|
2017-11-24 15:08:43 +01:00
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$handler,
|
2020-04-19 20:41:38 +02:00
|
|
|
$request,
|
|
|
|
$handler,
|
|
|
|
$request,
|
|
|
|
$log
|
2017-11-24 15:08:43 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$provider = new ExceptionsServiceProvider($app);
|
|
|
|
$provider->boot();
|
2020-04-19 20:41:38 +02:00
|
|
|
$provider->boot();
|
2017-11-24 15:08:43 +01:00
|
|
|
}
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|