2018-08-25 21:16:20 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-25 21:16:20 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Application;
|
|
|
|
use Engelsystem\Middleware\CallableHandler;
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\HasStaticMethod;
|
|
|
|
use Engelsystem\Test\Unit\Middleware\Stub\ResolvesMiddlewareTraitImplementation;
|
|
|
|
use InvalidArgumentException;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
|
|
|
|
class ResolvesMiddlewareTraitTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::isMiddleware
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
|
2018-08-25 21:16:20 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testResolveMiddleware(): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
/** @var Application|MockObject $container */
|
|
|
|
$container = $this->createMock(Application::class);
|
|
|
|
$middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
|
|
|
|
$callable = [HasStaticMethod::class, 'foo'];
|
|
|
|
|
|
|
|
$container->expects($this->exactly(3))
|
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
['FooBarClass'],
|
|
|
|
[CallableHandler::class, ['callable' => $callable]],
|
|
|
|
['UnresolvableClass']
|
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$middlewareInterface,
|
|
|
|
$middlewareInterface,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
$middleware = new ResolvesMiddlewareTraitImplementation($container);
|
|
|
|
|
|
|
|
$return = $middleware->callResolveMiddleware('FooBarClass');
|
|
|
|
$this->assertEquals($middlewareInterface, $return);
|
|
|
|
|
|
|
|
$return = $middleware->callResolveMiddleware($callable);
|
|
|
|
$this->assertEquals($middlewareInterface, $return);
|
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$middleware->callResolveMiddleware('UnresolvableClass');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Middleware\ResolvesMiddlewareTrait::resolveMiddleware
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testResolveMiddlewareNoContainer(): void
|
2018-08-25 21:16:20 +02:00
|
|
|
{
|
|
|
|
$middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class);
|
|
|
|
|
|
|
|
$middleware = new ResolvesMiddlewareTraitImplementation();
|
|
|
|
$return = $middleware->callResolveMiddleware($middlewareInterface);
|
|
|
|
|
|
|
|
$this->assertEquals($middlewareInterface, $return);
|
|
|
|
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$middleware->callResolveMiddleware('FooBarClass');
|
|
|
|
}
|
|
|
|
}
|