Skip to content

Commit

Permalink
Fix tests that were mocking final classes
Browse files Browse the repository at this point in the history
Signed-off-by: George Steel <george@net-glue.co.uk>
  • Loading branch information
gsteel committed Jul 17, 2024
1 parent 36aa3c3 commit fceea98
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 45 deletions.
22 changes: 22 additions & 0 deletions test/Asset/FixedResponseMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Router\Asset;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class FixedResponseMiddleware implements MiddlewareInterface
{
public function __construct(public readonly ResponseInterface $response)
{
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->response;
}
}
18 changes: 18 additions & 0 deletions test/Asset/NoOpMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Router\Asset;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class NoOpMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $handler->handle($request);
}
}
10 changes: 5 additions & 5 deletions test/Middleware/DispatchMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace MezzioTest\Router\Middleware;

use Mezzio\Router\Middleware\DispatchMiddleware;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use MezzioTest\Router\Asset\FixedResponseMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -60,11 +62,9 @@ public function testInvokesRouteResultWhenPresent(): void
->expects(self::never())
->method('handle');

$routeResult = $this->createMock(RouteResult::class);
$routeResult
->method('process')
->with($this->request, $this->handler)
->willReturn($this->response);
$routeResult = RouteResult::fromRoute(
new Route('/', new FixedResponseMiddleware($this->response)),
);

$this->request
->method('getAttribute')
Expand Down
6 changes: 4 additions & 2 deletions test/Middleware/ImplicitHeadMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use Mezzio\Router\RouterInterface;
use MezzioTest\Router\Asset\NoOpMiddleware;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -103,8 +104,9 @@ public function testReturnsResultOfHandlerWhenTheRouteResultMatchesARouteThatSup

public function testReturnsResultOfHandlerWhenRouteSupportsHeadExplicitly(): void
{
$route = $this->createMock(Route::class);
$result = RouteResult::fromRoute($route);
$result = RouteResult::fromRoute(
new Route('/', new NoOpMiddleware()),
);
$request = (new ServerRequest())
->withMethod(RequestMethod::METHOD_HEAD)
->withAttribute(RouteResult::class, $result);
Expand Down
7 changes: 4 additions & 3 deletions test/Middleware/ImplicitOptionsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use MezzioTest\Router\Asset\NoOpMiddleware;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -84,9 +85,9 @@ public function testMissingRouteResultInvokesHandler(): void

public function testReturnsResultOfHandlerWhenRouteSupportsOptionsExplicitly(): void
{
$route = $this->createMock(Route::class);

$result = RouteResult::fromRoute($route);
$result = RouteResult::fromRoute(
new Route('/', new NoOpMiddleware()),
);

$request = $this->createMock(ServerRequestInterface::class);
$request
Expand Down
47 changes: 12 additions & 35 deletions test/RouteResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use MezzioTest\Router\Asset\FixedResponseMiddleware;
use MezzioTest\Router\Asset\NoOpMiddleware;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function assert;

/**
* @see MockObject
*
Expand Down Expand Up @@ -45,7 +46,7 @@ public function testRouteFailureRetrieveHttpMethods(): void
public function testRouteMatchedParams(): void
{
$params = ['foo' => 'bar'];
$route = $this->createMock(Route::class);
$route = new Route('/foo', new NoOpMiddleware());
$result = RouteResult::fromRoute($route, $params);

self::assertSame($params, $result->getMatchedParams());
Expand All @@ -61,45 +62,27 @@ public function testRouteMethodFailure(): void
public function testRouteSuccessMethodFailure(): void
{
$params = ['foo' => 'bar'];
$route = $this->createMock(Route::class);
$route = new Route('/foo', new NoOpMiddleware());
$result = RouteResult::fromRoute($route, $params);

self::assertFalse($result->isMethodFailure());
}

/**
* @psalm-return array{route: Route&MockObject, result: RouteResult}
*/
public function testFromRouteShouldComposeRouteInResult(): array
public function testFromRouteShouldComposeRouteInResult(): RouteResult
{
$route = $this->createMock(Route::class);
$route = new Route('/foo', new NoOpMiddleware(), ['HEAD', 'OPTIONS', 'GET'], 'route');

$result = RouteResult::fromRoute($route, ['foo' => 'bar']);

self::assertTrue($result->isSuccess());
self::assertSame($route, $result->getMatchedRoute());

return ['route' => $route, 'result' => $result];
return $result;
}

/**
* @psalm-param array{result:RouteResult,route:Route&MockObject} $data
* @depends testFromRouteShouldComposeRouteInResult
*/
public function testAllAccessorsShouldReturnExpectedDataWhenResultCreatedViaFromRoute(array $data): void
#[Depends('testFromRouteShouldComposeRouteInResult')]
public function testAllAccessorsShouldReturnExpectedDataWhenResultCreatedViaFromRoute(RouteResult $result): void
{
$result = $data['result'];
$route = $data['route'];
assert($route instanceof MockObject);

$route
->method('getName')
->willReturn('route');

$route
->method('getAllowedMethods')
->willReturn(['HEAD', 'OPTIONS', 'GET']);

self::assertSame('route', $result->getMatchedRouteName());
self::assertSame(['HEAD', 'OPTIONS', 'GET'], $result->getAllowedMethods());
}
Expand All @@ -121,9 +104,7 @@ public function testFailureResultDoesNotIndicateAMethodFailureIfAllMethodsAreAll
return $result;
}

/**
* @depends testFailureResultDoesNotIndicateAMethodFailureIfAllMethodsAreAllowed
*/
#[Depends('testFailureResultDoesNotIndicateAMethodFailureIfAllMethodsAreAllowed')]
public function testAllowedMethodsIncludesASingleWildcardEntryWhenAllMethodsAllowedForFailureResult(
RouteResult $result
): void {
Expand Down Expand Up @@ -154,11 +135,7 @@ public function testSuccessfulResultProcessedAsMiddlewareDelegatesToRoute(): voi
->expects(self::never())
->method('handle');

$route = $this->createMock(Route::class);
$route
->method('process')
->with($request, $handler)
->willReturn($response);
$route = new Route('/', new FixedResponseMiddleware($response));

$result = RouteResult::fromRoute($route);

Expand Down

0 comments on commit fceea98

Please sign in to comment.