Migrated NotFoundTrackerMiddlewareTest to use PHPUnit mocks

This commit is contained in:
Alejandro Celaya 2022-10-21 19:29:02 +02:00
parent d842025835
commit ff543b151c

View File

@ -4,12 +4,9 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\ErrorHandler; namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory; use Laminas\Diactoros\ServerRequestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType; use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
@ -18,35 +15,31 @@ use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
class NotFoundTrackerMiddlewareTest extends TestCase class NotFoundTrackerMiddlewareTest extends TestCase
{ {
use ProphecyTrait;
private NotFoundTrackerMiddleware $middleware; private NotFoundTrackerMiddleware $middleware;
private ServerRequestInterface $request; private ServerRequestInterface $request;
private ObjectProphecy $requestTracker; private MockObject $handler;
private ObjectProphecy $notFoundType; private MockObject $requestTracker;
private ObjectProphecy $handler;
protected function setUp(): void protected function setUp(): void
{ {
$this->notFoundType = $this->prophesize(NotFoundType::class); $this->handler = $this->createMock(RequestHandlerInterface::class);
$this->handler = $this->prophesize(RequestHandlerInterface::class); $this->requestTracker = $this->createMock(RequestTrackerInterface::class);
$this->handler->handle(Argument::cetera())->willReturn(new Response()); $this->middleware = new NotFoundTrackerMiddleware($this->requestTracker);
$this->requestTracker = $this->prophesize(RequestTrackerInterface::class);
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker->reveal());
$this->request = ServerRequestFactory::fromGlobals()->withAttribute( $this->request = ServerRequestFactory::fromGlobals()->withAttribute(
NotFoundType::class, NotFoundType::class,
$this->notFoundType->reveal(), $this->createMock(NotFoundType::class),
); );
} }
/** @test */ /** @test */
public function delegatesIntoRequestTracker(): void public function delegatesIntoRequestTracker(): void
{ {
$this->middleware->process($this->request, $this->handler->reveal()); $this->handler->expects($this->once())->method('handle')->with($this->equalTo($this->request));
$this->requestTracker->expects($this->once())->method('trackNotFoundIfApplicable')->with(
$this->equalTo($this->request),
);
$this->requestTracker->trackNotFoundIfApplicable($this->request)->shouldHaveBeenCalledOnce(); $this->middleware->process($this->request, $this->handler);
$this->handler->handle($this->request)->shouldHaveBeenCalledOnce();
} }
} }