2018-08-25 21:16:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
2018-10-28 12:59:49 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2018-08-25 21:16:20 +02:00
|
|
|
use Engelsystem\Middleware\LegacyMiddleware;
|
|
|
|
use Engelsystem\Middleware\RouteDispatcher;
|
|
|
|
use Engelsystem\Middleware\RouteDispatcherServiceProvider;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
|
|
|
use FastRoute\Dispatcher as FastRouteDispatcher;
|
|
|
|
use Illuminate\Contracts\Container\ContextualBindingBuilder;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
|
|
|
|
class RouteDispatcherServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\RouteDispatcherServiceProvider::register()
|
|
|
|
*/
|
|
|
|
public function testRegister(): void
|
|
|
|
{
|
2018-10-28 12:59:49 +01:00
|
|
|
/** @var ContextualBindingBuilder|MockObject $bindingBuilder */
|
2018-08-25 21:16:20 +02:00
|
|
|
$bindingBuilder = $this->createMock(ContextualBindingBuilder::class);
|
2018-10-28 12:59:49 +01:00
|
|
|
/** @var FastRouteDispatcher|MockObject $routeDispatcher */
|
2018-08-25 21:16:20 +02:00
|
|
|
$routeDispatcher = $this->getMockForAbstractClass(FastRouteDispatcher::class);
|
2018-10-28 12:59:49 +01:00
|
|
|
$config = new Config(['environment' => 'development']);
|
2018-08-25 21:16:20 +02:00
|
|
|
|
2018-10-28 12:59:49 +01:00
|
|
|
$app = $this->getApp(['alias', 'when', 'get']);
|
|
|
|
|
|
|
|
$app->expects($this->exactly(2))
|
|
|
|
->method('get')
|
|
|
|
->withConsecutive(['config'], ['path.cache.routes'])
|
|
|
|
->willReturn($config, '/foo/routes.cache');
|
2018-08-25 21:16:20 +02:00
|
|
|
|
|
|
|
$app->expects($this->once())
|
|
|
|
->method('alias')
|
|
|
|
->with(RouteDispatcher::class, 'route.dispatcher');
|
|
|
|
|
|
|
|
$app->expects($this->exactly(2))
|
|
|
|
->method('when')
|
|
|
|
->with(RouteDispatcher::class)
|
|
|
|
->willReturn($bindingBuilder);
|
|
|
|
|
|
|
|
$bindingBuilder->expects($this->exactly(2))
|
|
|
|
->method('needs')
|
|
|
|
->withConsecutive(
|
|
|
|
[FastRouteDispatcher::class],
|
|
|
|
[MiddlewareInterface::class]
|
|
|
|
)
|
|
|
|
->willReturn($bindingBuilder);
|
|
|
|
|
|
|
|
$bindingBuilder->expects($this->exactly(2))
|
|
|
|
->method('give')
|
|
|
|
->with($this->callback(function ($subject) {
|
|
|
|
if (is_callable($subject)) {
|
|
|
|
$subject();
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_callable($subject) || $subject == LegacyMiddleware::class;
|
|
|
|
}));
|
|
|
|
|
|
|
|
/** @var RouteDispatcherServiceProvider|MockObject $serviceProvider */
|
|
|
|
$serviceProvider = $this->getMockBuilder(RouteDispatcherServiceProvider::class)
|
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['generateRouting'])
|
2018-08-25 21:16:20 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$serviceProvider->expects($this->once())
|
|
|
|
->method('generateRouting')
|
|
|
|
->willReturn($routeDispatcher);
|
|
|
|
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
}
|