Fix tests coverage

This commit is contained in:
Igor Scheller 2022-12-21 15:11:37 +01:00
parent f24d31b928
commit d1b7b6886e
4 changed files with 25 additions and 10 deletions

View File

@ -14,7 +14,7 @@ class Dispatcher implements MiddlewareInterface, RequestHandlerInterface
{ {
use ResolvesMiddlewareTrait; use ResolvesMiddlewareTrait;
protected RequestHandlerInterface $next; protected ?RequestHandlerInterface $next = null;
/** /**
* @param MiddlewareInterface[]|string[] $stack * @param MiddlewareInterface[]|string[] $stack

View File

@ -6,7 +6,6 @@ use Engelsystem\Application;
use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\BaseController;
use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
@ -28,6 +27,7 @@ class RequestHandler implements MiddlewareInterface
{ {
$requestHandler = $request->getAttribute('route-request-handler'); $requestHandler = $request->getAttribute('route-request-handler');
/** @var CallableHandler|MiddlewareInterface|RequestHandlerInterface $requestHandler */
$requestHandler = $this->resolveRequestHandler($requestHandler); $requestHandler = $this->resolveRequestHandler($requestHandler);
if ($requestHandler instanceof CallableHandler) { if ($requestHandler instanceof CallableHandler) {
@ -42,11 +42,11 @@ class RequestHandler implements MiddlewareInterface
return $requestHandler->process($request, $handler); return $requestHandler->process($request, $handler);
} }
if ($requestHandler instanceof RequestHandlerInterface) { /**
return $requestHandler->handle($request); * Is RequestHandlerInterface
} * @see RequestHandlerInterface
*/
throw new InvalidArgumentException('Unable to process request handler of type ' . gettype($requestHandler)); return $requestHandler->handle($request);
} }
/** /**

View File

@ -42,7 +42,7 @@ trait ResolvesMiddlewareTrait
} }
/** /**
* Checks if the given object is a middleware or middleware or request handler * Checks if the given object is a middleware or request handler
*/ */
protected function isMiddleware(mixed $middleware): bool protected function isMiddleware(mixed $middleware): bool
{ {

View File

@ -3,8 +3,12 @@
namespace Engelsystem\Test\Unit\Middleware; namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Application; use Engelsystem\Application;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Engelsystem\Middleware\Dispatcher; use Engelsystem\Middleware\Dispatcher;
use Error; use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
use InvalidArgumentException;
use LogicException;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -122,7 +126,7 @@ class DispatcherTest extends TestCase
/** @var ServerRequestInterface|MockObject $request */ /** @var ServerRequestInterface|MockObject $request */
$request = $this->createMock(ServerRequestInterface::class); $request = $this->createMock(ServerRequestInterface::class);
$this->expectException(Error::class); $this->expectException(LogicException::class);
$dispatcher = new Dispatcher(); $dispatcher = new Dispatcher();
$dispatcher->handle($request); $dispatcher->handle($request);
@ -163,6 +167,17 @@ class DispatcherTest extends TestCase
$dispatcher->handle($request); $dispatcher->handle($request);
} }
/**
* @covers \Engelsystem\Middleware\Dispatcher::handle
*/
public function testHandleCallResolveInvalidTypeResolved(): void
{
$instance = new Dispatcher([new ReturnResponseMiddlewareHandler(new Response())]);
$this->expectException(InvalidArgumentException::class);
$instance->handle(new Request());
}
/** /**
* @covers \Engelsystem\Middleware\Dispatcher::setContainer * @covers \Engelsystem\Middleware\Dispatcher::setContainer
*/ */