diff --git a/composer.json b/composer.json index 8a82d00..fc9ec15 100644 --- a/composer.json +++ b/composer.json @@ -51,8 +51,6 @@ "laminas/laminas-coding-standard": "~2.4.0", "laminas/laminas-diactoros": "^2.11", "laminas/laminas-stratigility": "^3.4", - "phpspec/prophecy": "^1.9", - "phpspec/prophecy-phpunit": "^2.0", "phpunit/phpunit": "^9.5.11", "psalm/plugin-phpunit": "^0.17.0", "vimeo/psalm": "^4.17" diff --git a/composer.lock b/composer.lock index 3bec60d..a8a6a73 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2fabf092f23dedb7b22b3a76f0c86f7f", + "content-hash": "0ed98d9753e4f60e051fd3867351fba9", "packages": [ { "name": "fig/http-message-util", @@ -1922,125 +1922,6 @@ }, "time": "2022-03-15T21:29:03+00:00" }, - { - "name": "phpspec/prophecy", - "version": "v1.15.0", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy.git", - "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13", - "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13", - "shasum": "" - }, - "require": { - "doctrine/instantiator": "^1.2", - "php": "^7.2 || ~8.0, <8.2", - "phpdocumentor/reflection-docblock": "^5.2", - "sebastian/comparator": "^3.0 || ^4.0", - "sebastian/recursion-context": "^3.0 || ^4.0" - }, - "require-dev": { - "phpspec/phpspec": "^6.0 || ^7.0", - "phpunit/phpunit": "^8.0 || ^9.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.x-dev" - } - }, - "autoload": { - "psr-4": { - "Prophecy\\": "src/Prophecy" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Konstantin Kudryashov", - "email": "ever.zet@gmail.com", - "homepage": "http://everzet.com" - }, - { - "name": "Marcello Duarte", - "email": "marcello.duarte@gmail.com" - } - ], - "description": "Highly opinionated mocking framework for PHP 5.3+", - "homepage": "https://github.com/phpspec/prophecy", - "keywords": [ - "Double", - "Dummy", - "fake", - "mock", - "spy", - "stub" - ], - "support": { - "issues": "https://github.com/phpspec/prophecy/issues", - "source": "https://github.com/phpspec/prophecy/tree/v1.15.0" - }, - "time": "2021-12-08T12:19:24+00:00" - }, - { - "name": "phpspec/prophecy-phpunit", - "version": "v2.0.1", - "source": { - "type": "git", - "url": "https://github.com/phpspec/prophecy-phpunit.git", - "reference": "2d7a9df55f257d2cba9b1d0c0963a54960657177" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy-phpunit/zipball/2d7a9df55f257d2cba9b1d0c0963a54960657177", - "reference": "2d7a9df55f257d2cba9b1d0c0963a54960657177", - "shasum": "" - }, - "require": { - "php": "^7.3 || ^8", - "phpspec/prophecy": "^1.3", - "phpunit/phpunit": "^9.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0-dev" - } - }, - "autoload": { - "psr-4": { - "Prophecy\\PhpUnit\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Christophe Coevoet", - "email": "stof@notk.org" - } - ], - "description": "Integrating the Prophecy mocking library in PHPUnit test cases", - "homepage": "http://phpspec.net", - "keywords": [ - "phpunit", - "prophecy" - ], - "support": { - "issues": "https://github.com/phpspec/prophecy-phpunit/issues", - "source": "https://github.com/phpspec/prophecy-phpunit/tree/v2.0.1" - }, - "time": "2020-07-09T08:33:42+00:00" - }, { "name": "phpstan/phpdoc-parser", "version": "1.5.1", diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 31f077c..c140170 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + @@ -162,30 +162,20 @@ - - testExplicitRequest - testWithoutImplicitMiddleware - $value - - testExplicitRequest - testWithoutImplicitMiddleware - - - $factories - $factories - $factories - $factories - $factories - $factories - - - $factories - + + + \Zend\Expressive\Router\Middleware\DispatchMiddleware + \Zend\Expressive\Router\Middleware\ImplicitHeadMiddleware + \Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware + \Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware + \Zend\Expressive\Router\Middleware\RouteMiddleware + \Zend\Expressive\Router\RouteCollector + @@ -218,7 +208,9 @@ - + + new CallableResponseFactoryDecorator(fn (): ResponseInterface => $this->response) + diff --git a/src/Test/AbstractImplicitMethodsIntegrationTest.php b/src/Test/AbstractImplicitMethodsIntegrationTest.php index c924029..901a342 100644 --- a/src/Test/AbstractImplicitMethodsIntegrationTest.php +++ b/src/Test/AbstractImplicitMethodsIntegrationTest.php @@ -176,7 +176,7 @@ static function (ServerRequestInterface $request) use ($method, $implicitRoute): $response = $pipeline->process($request, $finalHandler); - self::assertEquals(StatusCode::STATUS_OK, $response->getStatusCode()); + self::assertSame(StatusCode::STATUS_OK, $response->getStatusCode()); self::assertSame('FOO BAR BODY', (string) $response->getBody()); self::assertTrue($response->hasHeader('foo-bar')); self::assertSame('baz', $response->getHeaderLine('foo-bar')); @@ -355,7 +355,7 @@ static function (ServerRequestInterface $request) use ($route1, $expectedParams) $response = $pipeline->process($request, $finalHandler); - self::assertEquals(StatusCode::STATUS_OK, $response->getStatusCode()); + self::assertSame(StatusCode::STATUS_OK, $response->getStatusCode()); self::assertEmpty((string) $response->getBody()); self::assertTrue($response->hasHeader('foo-bar')); self::assertSame('baz', $response->getHeaderLine('foo-bar')); @@ -454,7 +454,7 @@ public function testImplicitOptionsRequestRouteNotFound(): void $response = $pipeline->process($request, $finalHandler); - self::assertEquals(StatusCode::STATUS_IM_A_TEAPOT, $response->getStatusCode()); + self::assertSame(StatusCode::STATUS_IM_A_TEAPOT, $response->getStatusCode()); self::assertSame('FOO BAR BODY', (string) $response->getBody()); self::assertTrue($response->hasHeader('foo-bar')); self::assertSame('baz', $response->getHeaderLine('foo-bar')); diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index ed356e0..aa925fd 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -7,22 +7,37 @@ use Mezzio\Router\ConfigProvider; use Mezzio\Router\Middleware; use Mezzio\Router\RouteCollector; +use Mezzio\Router\RouteCollectorFactory; use PHPUnit\Framework\TestCase; -class ConfigProviderTest extends TestCase +/** @covers \Mezzio\Router\ConfigProvider */ +final class ConfigProviderTest extends TestCase { public function testProviderProvidesFactoriesForAllMiddleware(): void { $provider = new ConfigProvider(); - $config = $provider(); - $this->assertTrue(isset($config['dependencies']['factories'])); - $factories = $config['dependencies']['factories']; - $this->assertArrayHasKey(Middleware\DispatchMiddleware::class, $factories); - $this->assertArrayHasKey(Middleware\ImplicitHeadMiddleware::class, $factories); - $this->assertArrayHasKey(Middleware\ImplicitOptionsMiddleware::class, $factories); - $this->assertArrayHasKey(Middleware\MethodNotAllowedMiddleware::class, $factories); - $this->assertArrayHasKey(Middleware\RouteMiddleware::class, $factories); - $this->assertArrayHasKey(RouteCollector::class, $factories); + self::assertSame([ + 'dependencies' => [ + 'aliases' => [ + // @codingStandardsIgnoreStart + \Zend\Expressive\Router\Middleware\DispatchMiddleware::class => Middleware\DispatchMiddleware::class, + \Zend\Expressive\Router\Middleware\ImplicitHeadMiddleware::class => Middleware\ImplicitHeadMiddleware::class, + \Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware::class => Middleware\ImplicitOptionsMiddleware::class, + \Zend\Expressive\Router\Middleware\MethodNotAllowedMiddleware::class => Middleware\MethodNotAllowedMiddleware::class, + \Zend\Expressive\Router\Middleware\RouteMiddleware::class => Middleware\RouteMiddleware::class, + \Zend\Expressive\Router\RouteCollector::class => RouteCollector::class, + // @codingStandardsIgnoreEnd + ], + 'factories' => [ + Middleware\DispatchMiddleware::class => Middleware\DispatchMiddlewareFactory::class, + Middleware\ImplicitHeadMiddleware::class => Middleware\ImplicitHeadMiddlewareFactory::class, + Middleware\ImplicitOptionsMiddleware::class => Middleware\ImplicitOptionsMiddlewareFactory::class, + Middleware\MethodNotAllowedMiddleware::class => Middleware\MethodNotAllowedMiddlewareFactory::class, + Middleware\RouteMiddleware::class => Middleware\RouteMiddlewareFactory::class, + RouteCollector::class => RouteCollectorFactory::class, + ], + ], + ], $provider()); } } diff --git a/test/InMemoryContainer.php b/test/InMemoryContainer.php index 2969dc0..3583092 100644 --- a/test/InMemoryContainer.php +++ b/test/InMemoryContainer.php @@ -15,10 +15,7 @@ final class InMemoryContainer implements ContainerInterface /** @var array */ private $services = []; - /** - * @param string $id - * @return mixed - */ + /** {@inheritDoc} */ public function get($id) { if (! $this->has($id)) { @@ -29,9 +26,7 @@ public function get($id) return $this->services[$id]; } - /** - * @param string $id - */ + /** {@inheritDoc} */ public function has($id): bool { return array_key_exists($id, $this->services); diff --git a/test/Middleware/DispatchMiddlewareFactoryTest.php b/test/Middleware/DispatchMiddlewareFactoryTest.php index 25c275f..186492e 100644 --- a/test/Middleware/DispatchMiddlewareFactoryTest.php +++ b/test/Middleware/DispatchMiddlewareFactoryTest.php @@ -8,7 +8,8 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; -class DispatchMiddlewareFactoryTest extends TestCase +/** @covers \Mezzio\Router\Middleware\DispatchMiddlewareFactory */ +final class DispatchMiddlewareFactoryTest extends TestCase { public function testFactoryProducesDispatchMiddleware(): void { diff --git a/test/Middleware/DispatchMiddlewareTest.php b/test/Middleware/DispatchMiddlewareTest.php index b591c5b..2d6ec74 100644 --- a/test/Middleware/DispatchMiddlewareTest.php +++ b/test/Middleware/DispatchMiddlewareTest.php @@ -12,25 +12,28 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; -class DispatchMiddlewareTest extends TestCase +/** @covers \Mezzio\Router\Middleware\DispatchMiddleware */ +final class DispatchMiddlewareTest extends TestCase { /** @var RequestHandlerInterface&MockObject */ - private $handler; - - /** @var DispatchMiddleware */ - private $middleware; + private RequestHandlerInterface $handler; /** @var ServerRequestInterface&MockObject */ - private $request; + private ServerRequestInterface $request; /** @var ResponseInterface&MockObject */ - private $response; + private ResponseInterface $response; + + private DispatchMiddleware $middleware; protected function setUp(): void { - $this->response = $this->createMock(ResponseInterface::class); - $this->request = $this->createMock(ServerRequestInterface::class); - $this->handler = $this->createMock(RequestHandlerInterface::class); + parent::setUp(); + + $this->response = $this->createMock(ResponseInterface::class); + $this->request = $this->createMock(ServerRequestInterface::class); + $this->handler = $this->createMock(RequestHandlerInterface::class); + $this->middleware = new DispatchMiddleware(); } diff --git a/test/Middleware/ImplicitHeadMiddlewareFactoryTest.php b/test/Middleware/ImplicitHeadMiddlewareFactoryTest.php index 82113cc..78d8053 100644 --- a/test/Middleware/ImplicitHeadMiddlewareFactoryTest.php +++ b/test/Middleware/ImplicitHeadMiddlewareFactoryTest.php @@ -13,16 +13,18 @@ use Psr\Http\Message\StreamInterface; use Zend\Expressive\Router\RouterInterface as ZendExpressiveRouterInterface; -class ImplicitHeadMiddlewareFactoryTest extends TestCase +/** @covers \Mezzio\Router\Middleware\ImplicitHeadMiddlewareFactory */ +final class ImplicitHeadMiddlewareFactoryTest extends TestCase { /** @var ContainerInterface&MockObject */ - private $container; + private ContainerInterface $container; - /** @var ImplicitHeadMiddlewareFactory */ - private $factory; + private ImplicitHeadMiddlewareFactory $factory; protected function setUp(): void { + parent::setUp(); + $this->container = $this->createMock(ContainerInterface::class); $this->factory = new ImplicitHeadMiddlewareFactory(); } @@ -35,6 +37,7 @@ public function testFactoryRaisesExceptionIfRouterInterfaceServiceIsMissing(): v ->willReturn(false); $this->expectException(MissingDependencyException::class); + ($this->factory)($this->container); } @@ -46,13 +49,14 @@ public function testFactoryRaisesExceptionIfStreamFactoryServiceIsMissing(): voi ->willReturnOnConsecutiveCalls(true, false); $this->expectException(MissingDependencyException::class); + ($this->factory)($this->container); } public function testFactoryProducesImplicitHeadMiddlewareWhenAllDependenciesPresent(): void { $router = $this->createMock(RouterInterface::class); - $streamFactory = function (): void { + $streamFactory = static function (): void { }; $this->container @@ -65,7 +69,7 @@ public function testFactoryProducesImplicitHeadMiddlewareWhenAllDependenciesPres ->expects(self::exactly(2)) ->method('get') ->withConsecutive([RouterInterface::class], [StreamInterface::class]) - ->willReturnOnConsecutiveCalls($router, $streamFactory); + ->willReturn($router, $streamFactory); ($this->factory)($this->container); } diff --git a/test/Middleware/ImplicitHeadMiddlewareTest.php b/test/Middleware/ImplicitHeadMiddlewareTest.php index 79eb78f..18e6f29 100644 --- a/test/Middleware/ImplicitHeadMiddlewareTest.php +++ b/test/Middleware/ImplicitHeadMiddlewareTest.php @@ -17,28 +17,28 @@ use Psr\Http\Server\RequestHandlerInterface; use Zend\Expressive\Router\RouteResult as ZendExpressiveRouteResult; -class ImplicitHeadMiddlewareTest extends TestCase +/** @covers \Mezzio\Router\Middleware\ImplicitHeadMiddleware */ +final class ImplicitHeadMiddlewareTest extends TestCase { - /** @var ImplicitHeadMiddleware */ - private $middleware; - /** @var ResponseInterface&MockObject */ - private $response; + private ResponseInterface $response; /** @var RouterInterface&MockObject */ - private $router; + private RouterInterface $router; /** @var StreamInterface&MockObject */ - private $stream; + private StreamInterface $stream; + + private ImplicitHeadMiddleware $middleware; protected function setUp(): void { + parent::setUp(); + $this->router = $this->createMock(RouterInterface::class); $this->stream = $this->createMock(StreamInterface::class); - $streamFactory = function (): StreamInterface { - return $this->stream; - }; + $streamFactory = fn (): StreamInterface => $this->stream; $this->middleware = new ImplicitHeadMiddleware($this->router, $streamFactory); $this->response = $this->createMock(ResponseInterface::class); @@ -124,6 +124,7 @@ public function testReturnsResultOfHandlerWhenRouteDoesNotExplicitlySupportHeadA ->method('getAttribute') ->with(RouteResult::class) ->willReturn($result); + $request ->expects(self::once()) ->method('withMethod') diff --git a/test/Middleware/ImplicitOptionsMiddlewareFactoryTest.php b/test/Middleware/ImplicitOptionsMiddlewareFactoryTest.php index 2aabdf5..ab39248 100644 --- a/test/Middleware/ImplicitOptionsMiddlewareFactoryTest.php +++ b/test/Middleware/ImplicitOptionsMiddlewareFactoryTest.php @@ -12,16 +12,18 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; -class ImplicitOptionsMiddlewareFactoryTest extends TestCase +/** @covers \Mezzio\Router\Middleware\ImplicitOptionsMiddlewareFactory */ +final class ImplicitOptionsMiddlewareFactoryTest extends TestCase { /** @var ContainerInterface&MockObject */ - private $container; + private ContainerInterface $container; - /** @var ImplicitOptionsMiddlewareFactory */ - private $factory; + private ImplicitOptionsMiddlewareFactory $factory; protected function setUp(): void { + parent::setUp(); + $this->container = $this->createMock(ContainerInterface::class); $this->factory = new ImplicitOptionsMiddlewareFactory(); } @@ -29,6 +31,7 @@ protected function setUp(): void public function testFactoryRaisesExceptionIfResponseFactoryServiceIsMissing(): void { $this->expectException(MissingDependencyException::class); + ($this->factory)($this->container); } diff --git a/test/Middleware/ImplicitOptionsMiddlewareTest.php b/test/Middleware/ImplicitOptionsMiddlewareTest.php index c058fd5..7ec06c3 100644 --- a/test/Middleware/ImplicitOptionsMiddlewareTest.php +++ b/test/Middleware/ImplicitOptionsMiddlewareTest.php @@ -16,20 +16,20 @@ use function implode; -class ImplicitOptionsMiddlewareTest extends TestCase +/** @covers \Mezzio\Router\Middleware\ImplicitOptionsMiddleware */ +final class ImplicitOptionsMiddlewareTest extends TestCase { - /** @var ImplicitOptionsMiddleware */ - private $middleware; - /** @var ResponseInterface&MockObject */ - private $response; + private ResponseInterface $response; + + private ImplicitOptionsMiddleware $middleware; protected function setUp(): void { + parent::setUp(); + $this->response = $this->createMock(ResponseInterface::class); - $responseFactory = function (): ResponseInterface { - return $this->response; - }; + $responseFactory = fn (): ResponseInterface => $this->response; $this->middleware = new ImplicitOptionsMiddleware($responseFactory); } @@ -40,6 +40,7 @@ public function testNonOptionsRequestInvokesHandler(): void $request ->method('getMethod') ->willReturn(RequestMethod::METHOD_GET); + $request ->expects(self::never()) ->method('getAttribute'); @@ -52,7 +53,8 @@ public function testNonOptionsRequestInvokesHandler(): void ->willReturn($response); $result = $this->middleware->process($request, $handler); - $this->assertSame($response, $result); + + self::assertSame($response, $result); } public function testMissingRouteResultInvokesHandler(): void @@ -76,7 +78,8 @@ public function testMissingRouteResultInvokesHandler(): void ->willReturn($response); $result = $this->middleware->process($request, $handler); - $this->assertSame($response, $result); + + self::assertSame($response, $result); } public function testReturnsResultOfHandlerWhenRouteSupportsOptionsExplicitly(): void @@ -104,7 +107,8 @@ public function testReturnsResultOfHandlerWhenRouteSupportsOptionsExplicitly(): ->willReturn($response); $result = $this->middleware->process($request, $handler); - $this->assertSame($response, $result); + + self::assertSame($response, $result); } public function testInjectsAllowHeaderInResponseProvidedToConstructorDuringOptionsRequest(): void @@ -139,7 +143,8 @@ public function testInjectsAllowHeaderInResponseProvidedToConstructorDuringOptio ->willReturnSelf(); $result = $this->middleware->process($request, $handler); - $this->assertSame($this->response, $result); + + self::assertSame($this->response, $result); } public function testReturnsResultOfHandlerWhenRouteNotFound(): void @@ -165,6 +170,7 @@ public function testReturnsResultOfHandlerWhenRouteNotFound(): void ->willReturn($response); $result = $this->middleware->process($request, $handler); - $this->assertSame($response, $result); + + self::assertSame($response, $result); } } diff --git a/test/Middleware/MethodNotAllowedMiddlewareFactoryTest.php b/test/Middleware/MethodNotAllowedMiddlewareFactoryTest.php index fdd920d..7794e67 100644 --- a/test/Middleware/MethodNotAllowedMiddlewareFactoryTest.php +++ b/test/Middleware/MethodNotAllowedMiddlewareFactoryTest.php @@ -16,16 +16,18 @@ use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; -class MethodNotAllowedMiddlewareFactoryTest extends TestCase +/** @covers \Mezzio\Router\Middleware\MethodNotAllowedMiddlewareFactory */ +final class MethodNotAllowedMiddlewareFactoryTest extends TestCase { /** @var ContainerInterface&MockObject */ - private $container; + private ContainerInterface $container; - /** @var MethodNotAllowedMiddlewareFactory */ - private $factory; + private MethodNotAllowedMiddlewareFactory $factory; protected function setUp(): void { + parent::setUp(); + $this->container = $this->createMock(ContainerInterface::class); $this->factory = new MethodNotAllowedMiddlewareFactory(); } @@ -62,9 +64,7 @@ public function configurationsWithOverriddenResponseInterfaceFactory(): Generato 'dependencies' => [ 'delegators' => [ ResponseInterface::class => [ - function (): ResponseInterface { - return $this->createMock(ResponseInterface::class); - }, + fn (): ResponseInterface => $this->createMock(ResponseInterface::class), ], ], ], @@ -78,20 +78,22 @@ public function testFactoryRaisesExceptionIfResponseFactoryServiceIsMissing(): v ->method('has') ->withConsecutive([ResponseFactoryInterface::class], [ResponseInterface::class]) ->willReturn(false); + $this->expectException(MissingDependencyException::class); + ($this->factory)($this->container); } public function testFactoryProducesMethodNotAllowedMiddlewareWhenAllDependenciesPresent(): void { - $factory = function (): void { + $factory = static function (): void { }; $this->container ->expects(self::exactly(2)) ->method('has') ->withConsecutive([ResponseFactoryInterface::class], [ResponseInterface::class]) - ->willReturnOnConsecutiveCalls(false, true); + ->willReturn(false, true); $this->container ->expects(self::once()) @@ -115,6 +117,7 @@ public function testWillUseResponseFactoryInterfaceFromContainerWhenApplicationF ]); $container->set(ResponseFactoryInterface::class, $responseFactory); $middleware = ($this->factory)($container); + self::assertSame($responseFactory, $middleware->getResponseFactory()); } @@ -130,14 +133,13 @@ public function testWontUseResponseFactoryInterfaceFromContainerWhenApplicationF $container->set('config', $config); $container->set(ResponseFactoryInterface::class, $responseFactory); $response = $this->createMock(ResponseInterface::class); - $container->set(ResponseInterface::class, function () use ($response): ResponseInterface { - return $response; - }); + $container->set(ResponseInterface::class, static fn (): ResponseInterface => $response); $middleware = ($this->factory)($container); $responseFactoryFromGenerator = $middleware->getResponseFactory(); + self::assertNotSame($responseFactory, $responseFactoryFromGenerator); self::assertInstanceOf(CallableResponseFactoryDecorator::class, $responseFactoryFromGenerator); - self::assertEquals($response, $responseFactoryFromGenerator->getResponseFromCallable()); + self::assertSame($response, $responseFactoryFromGenerator->getResponseFromCallable()); } } diff --git a/test/Middleware/MethodNotAllowedMiddlewareTest.php b/test/Middleware/MethodNotAllowedMiddlewareTest.php index f23a2e5..e420c1c 100644 --- a/test/Middleware/MethodNotAllowedMiddlewareTest.php +++ b/test/Middleware/MethodNotAllowedMiddlewareTest.php @@ -14,28 +14,28 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; -class MethodNotAllowedMiddlewareTest extends TestCase +/** @covers \Mezzio\Router\Middleware\MethodNotAllowedMiddleware */ +final class MethodNotAllowedMiddlewareTest extends TestCase { /** @var RequestHandlerInterface&MockObject */ - private $handler; - - /** @var MethodNotAllowedMiddleware */ - private $middleware; + private RequestHandlerInterface $handler; /** @var ServerRequestInterface&MockObject */ - private $request; + private ServerRequestInterface $request; /** @var ResponseInterface&MockObject */ - private $response; + private ResponseInterface $response; + + private MethodNotAllowedMiddleware $middleware; protected function setUp(): void { + parent::setUp(); + $this->handler = $this->createMock(RequestHandlerInterface::class); $this->request = $this->createMock(ServerRequestInterface::class); $this->response = $this->createMock(ResponseInterface::class); - $responseFactory = function (): ResponseInterface { - return $this->response; - }; + $responseFactory = fn (): ResponseInterface => $this->response; $this->middleware = new MethodNotAllowedMiddleware($responseFactory); } @@ -60,7 +60,7 @@ public function testDelegatesToHandlerIfNoRouteResultPresentInRequest(): void ->expects(self::never()) ->method('withHeader'); - $this->assertSame( + self::assertSame( $this->response, $this->middleware->process($this->request, $this->handler) ); @@ -88,7 +88,7 @@ public function testDelegatesToHandlerIfRouteResultNotAMethodFailure(): void ->expects(self::never()) ->method('withHeader'); - $this->assertSame( + self::assertSame( $this->response, $this->middleware->process($this->request, $this->handler) ); @@ -119,7 +119,7 @@ public function testReturns405ResponseWithAllowHeaderIfResultDueToMethodFailure( ->with('Allow', 'GET,POST') ->willReturnSelf(); - $this->assertSame( + self::assertSame( $this->response, $this->middleware->process($this->request, $this->handler) ); diff --git a/test/Middleware/RouteMiddlewareFactoryTest.php b/test/Middleware/RouteMiddlewareFactoryTest.php index d94b125..b558be2 100644 --- a/test/Middleware/RouteMiddlewareFactoryTest.php +++ b/test/Middleware/RouteMiddlewareFactoryTest.php @@ -11,16 +11,18 @@ use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; -class RouteMiddlewareFactoryTest extends TestCase +/** @covers \Mezzio\Router\Middleware\RouteMiddlewareFactory */ +final class RouteMiddlewareFactoryTest extends TestCase { /** @var ContainerInterface&MockObject */ - private $container; + private ContainerInterface $container; - /** @var RouteMiddlewareFactory */ - private $factory; + private RouteMiddlewareFactory $factory; protected function setUp(): void { + parent::setUp(); + $this->container = $this->createMock(ContainerInterface::class); $this->factory = new RouteMiddlewareFactory(); } @@ -34,12 +36,14 @@ public function testFactoryRaisesExceptionIfRouterServiceIsMissing(): void ->willReturn(false); $this->expectException(MissingDependencyException::class); + ($this->factory)($this->container); } public function testFactoryProducesRouteMiddlewareWhenAllDependenciesPresent(): void { $router = $this->createMock(RouterInterface::class); + $this->container ->expects(self::once()) ->method('has') @@ -58,11 +62,13 @@ public function testFactoryProducesRouteMiddlewareWhenAllDependenciesPresent(): public function testFactoryAllowsSpecifyingRouterServiceViaConstructor(): void { $router = $this->createMock(RouterInterface::class); + $this->container ->expects(self::once()) ->method('has') ->with(Router::class) ->willReturn(true); + $this->container ->expects(self::once()) ->method('get') @@ -72,6 +78,7 @@ public function testFactoryAllowsSpecifyingRouterServiceViaConstructor(): void $factory = new RouteMiddlewareFactory(Router::class); $middleware = $factory($this->container); + self::assertSame($router, $middleware->getRouter()); } diff --git a/test/Middleware/RouteMiddlewareTest.php b/test/Middleware/RouteMiddlewareTest.php index 1a6973b..5b183ac 100644 --- a/test/Middleware/RouteMiddlewareTest.php +++ b/test/Middleware/RouteMiddlewareTest.php @@ -16,25 +16,27 @@ use Psr\Http\Server\RequestHandlerInterface; use Zend\Expressive\Router\RouteResult as ZendExpressiveRouteResult; -class RouteMiddlewareTest extends TestCase +/** @covers \Mezzio\Router\Middleware\RouteMiddleware */ +final class RouteMiddlewareTest extends TestCase { /** @var RouterInterface&MockObject */ - private $router; + private RouterInterface $router; /** @var ResponseInterface&MockObject */ - private $response; - - /** @var RouteMiddleware */ - private $middleware; + private ResponseInterface $response; /** @var ServerRequestInterface&MockObject */ - private $request; + private ServerRequestInterface $request; /** @var RequestHandlerInterface&MockObject */ - private $handler; + private RequestHandlerInterface $handler; + + private RouteMiddleware $middleware; protected function setUp(): void { + parent::setUp(); + $this->router = $this->createMock(RouterInterface::class); $this->request = $this->createMock(ServerRequestInterface::class); $this->response = $this->createMock(ResponseInterface::class); @@ -72,6 +74,7 @@ public function testRoutingFailureDueToHttpMethodCallsHandlerWithRequestComposin )->willReturnSelf(); $response = $this->middleware->process($this->request, $this->handler); + self::assertSame($this->response, $response); } @@ -81,8 +84,9 @@ public function testGeneralRoutingFailureInvokesHandlerWithRequestComposingRoute $this->router ->method('match') - ->with($this->request) + ->with($this->request) ->willReturn($result); + $this->handler ->method('handle') ->with($this->request) @@ -103,6 +107,7 @@ public function testGeneralRoutingFailureInvokesHandlerWithRequestComposingRoute )->willReturnSelf(); $response = $this->middleware->process($this->request, $this->handler); + self::assertSame($this->response, $response); } @@ -148,6 +153,7 @@ public function testRoutingSuccessInvokesHandlerWithRequestComposingRouteResultA ->willReturn($this->response); $response = $this->middleware->process($this->request, $this->handler); + self::assertSame($this->response, $response); } } diff --git a/test/Response/CallableResponseFactoryDecoratorTest.php b/test/Response/CallableResponseFactoryDecoratorTest.php index 2243788..7c1319a 100644 --- a/test/Response/CallableResponseFactoryDecoratorTest.php +++ b/test/Response/CallableResponseFactoryDecoratorTest.php @@ -9,21 +9,20 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; +/** @covers \Mezzio\Router\Response\CallableResponseFactoryDecorator */ final class CallableResponseFactoryDecoratorTest extends TestCase { - /** @var MockObject&ResponseInterface */ - private $response; + /** @var ResponseInterface&MockObject */ + private ResponseInterface $response; - /** @var CallableResponseFactoryDecorator */ - private $factory; + private CallableResponseFactoryDecorator $factory; protected function setUp(): void { parent::setUp(); + $this->response = $this->createMock(ResponseInterface::class); - $this->factory = new CallableResponseFactoryDecorator(function (): ResponseInterface { - return $this->response; - }); + $this->factory = new CallableResponseFactoryDecorator(fn (): ResponseInterface => $this->response); } public function testWillPassStatusCodeAndPhraseToCallable(): void @@ -44,6 +43,6 @@ public function testWillReturnSameResponseInstance(): void ->method('withStatus') ->willReturnSelf(); - self::assertEquals($this->response, $this->factory->createResponse()); + self::assertSame($this->response, $this->factory->createResponse()); } } diff --git a/test/RouteCollectorFactoryTest.php b/test/RouteCollectorFactoryTest.php index 94e1bb9..a5225d4 100644 --- a/test/RouteCollectorFactoryTest.php +++ b/test/RouteCollectorFactoryTest.php @@ -18,16 +18,18 @@ use function sprintf; -class RouteCollectorFactoryTest extends TestCase +/** @covers \Mezzio\Router\RouteCollectorFactory */ +final class RouteCollectorFactoryTest extends TestCase { /** @var ContainerInterface&MockObject */ - private $container; + private ContainerInterface $container; - /** @var RouteCollectorFactory */ - private $factory; + private RouteCollectorFactory $factory; protected function setUp(): void { + parent::setUp(); + $this->container = $this->createMock(ContainerInterface::class); $this->factory = new RouteCollectorFactory(); } @@ -42,12 +44,14 @@ public function testFactoryRaisesExceptionIfRouterServiceIsMissing(): void $this->expectException(MissingDependencyException::class); $this->expectExceptionMessage(RouteCollector::class); + ($this->factory)($this->container); } public function testFactoryProducesRouteCollectorWhenAllDependenciesPresent(): void { $router = $this->createMock(RouterInterface::class); + $this->container ->expects(self::exactly(2)) ->method('has') @@ -107,6 +111,7 @@ public function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromAn private function testFactoryProducesRouteCollectorUsingDetectDuplicatesFlagFromConfig($config): void { $router = $this->createMock(RouterInterface::class); + $this->container ->expects(self::exactly(2)) ->method('has') diff --git a/test/RouteCollectorTest.php b/test/RouteCollectorTest.php index bd3071d..c97dcf7 100644 --- a/test/RouteCollectorTest.php +++ b/test/RouteCollectorTest.php @@ -22,22 +22,23 @@ use function range; use function sprintf; -class RouteCollectorTest extends TestCase +/** @covers \Mezzio\Router\RouteCollector */ +final class RouteCollectorTest extends TestCase { /** @var RouterInterface&MockObject */ - private $router; + private RouterInterface $router; /** @var ResponseInterface&MockObject */ - private $response; + private ResponseInterface $response; - /** @var RouteCollector */ - private $collector; + private RouteCollector $collector; - /** @var MiddlewareInterface */ - private $noopMiddleware; + private MiddlewareInterface $noopMiddleware; protected function setUp(): void { + parent::setUp(); + $this->router = $this->createMock(RouterInterface::class); $this->response = $this->createMock(ResponseInterface::class); $this->collector = new RouteCollector($this->router); @@ -47,8 +48,7 @@ protected function setUp(): void public function createNoopMiddleware(): MiddlewareInterface { return new class ($this->response) implements MiddlewareInterface { - /** @var ResponseInterface */ - private $response; + private ResponseInterface $response; public function __construct(ResponseInterface $response) { @@ -87,7 +87,8 @@ public function testRouteMethodReturnsRouteInstance(): void ->willReturnArgument(0); $route = $this->collector->route('/foo', $this->noopMiddleware); - self::assertEquals('/foo', $route->getPath()); + + self::assertSame('/foo', $route->getPath()); self::assertSame($this->noopMiddleware, $route->getMiddleware()); } @@ -99,7 +100,8 @@ public function testAnyRouteMethod(): void ->willReturnArgument(0); $route = $this->collector->any('/foo', $this->noopMiddleware); - self::assertEquals('/foo', $route->getPath()); + + self::assertSame('/foo', $route->getPath()); self::assertSame($this->noopMiddleware, $route->getMiddleware()); self::assertSame(Route::HTTP_METHOD_ANY, $route->getAllowedMethods()); } @@ -116,7 +118,8 @@ public function testCanCallRouteWithHttpMethods($method): void ->willReturnArgument(0); $route = $this->collector->route('/foo', $this->noopMiddleware, [$method]); - self::assertEquals('/foo', $route->getPath()); + + self::assertSame('/foo', $route->getPath()); self::assertSame($this->noopMiddleware, $route->getMiddleware()); self::assertTrue($route->allowsMethod($method)); self::assertSame([$method], $route->getAllowedMethods()); @@ -131,7 +134,7 @@ public function testCanCallRouteWithMultipleHttpMethods(): void $methods = array_keys($this->commonHttpMethods()); $route = $this->collector->route('/foo', $this->noopMiddleware, $methods); - self::assertEquals('/foo', $route->getPath()); + self::assertSame('/foo', $route->getPath()); self::assertSame($this->noopMiddleware, $route->getMiddleware()); self::assertSame($methods, $route->getAllowedMethods()); } @@ -145,7 +148,9 @@ public function testCallingRouteWithExistingPathAndOmittingMethodsArgumentRaises $this->collector->route('/foo', $this->noopMiddleware); $this->collector->route('/bar', $this->noopMiddleware); + $this->expectException(Exception\DuplicateRouteException::class); + $this->collector->route('/foo', $this->createNoopMiddleware()); } @@ -174,6 +179,7 @@ public function invalidPathTypes(): array public function testCallingRouteWithAnInvalidPathTypeRaisesAnException($path): void { $this->expectException(TypeError::class); + $this->collector->route($path, $this->createNoopMiddleware()); } @@ -184,10 +190,11 @@ public function testCallingRouteWithAnInvalidPathTypeRaisesAnException($path): v public function testCommonHttpMethodsAreExposedAsClassMethodsAndReturnRoutes($method): void { $route = $this->collector->{$method}('/foo', $this->noopMiddleware); + self::assertInstanceOf(Route::class, $route); - self::assertEquals('/foo', $route->getPath()); + self::assertSame('/foo', $route->getPath()); self::assertSame($this->noopMiddleware, $route->getMiddleware()); - self::assertEquals([$method], $route->getAllowedMethods()); + self::assertSame([$method], $route->getAllowedMethods()); } public function testCreatingHttpRouteMethodWithExistingPathButDifferentMethodCreatesNewRouteInstance(): void @@ -201,6 +208,7 @@ public function testCreatingHttpRouteMethodWithExistingPathButDifferentMethodCre $middleware = $this->createNoopMiddleware(); $test = $this->collector->get('/foo', $middleware); + self::assertNotSame($route, $test); self::assertSame($route->getPath(), $test->getPath()); self::assertSame(['GET'], $test->getAllowedMethods()); @@ -236,6 +244,7 @@ public function testCreatingHttpRouteWithExistingPathShouldBeLinear(): void foreach (range(1, 10) as $item) { $this->collector->get("/bar$item", $this->noopMiddleware); } + $baseDuration = microtime(true) - $start; $this->collector = new RouteCollector($this->router); diff --git a/test/RouteResultTest.php b/test/RouteResultTest.php index 42467e9..a2152e5 100644 --- a/test/RouteResultTest.php +++ b/test/RouteResultTest.php @@ -19,23 +19,26 @@ * * @covers \Mezzio\Router\RouteResult */ -class RouteResultTest extends TestCase +final class RouteResultTest extends TestCase { public function testRouteNameIsNotRetrievable(): void { $result = RouteResult::fromRouteFailure([]); + self::assertFalse($result->getMatchedRouteName()); } public function testRouteFailureRetrieveAllHttpMethods(): void { $result = RouteResult::fromRouteFailure(Route::HTTP_METHOD_ANY); + self::assertSame(Route::HTTP_METHOD_ANY, $result->getAllowedMethods()); } public function testRouteFailureRetrieveHttpMethods(): void { $result = RouteResult::fromRouteFailure([]); + self::assertSame([], $result->getAllowedMethods()); } @@ -51,6 +54,7 @@ public function testRouteMatchedParams(): void public function testRouteMethodFailure(): void { $result = RouteResult::fromRouteFailure(['GET']); + self::assertTrue($result->isMethodFailure()); } @@ -71,6 +75,7 @@ public function testFromRouteShouldComposeRouteInResult(): array $route = $this->createMock(Route::class); $result = RouteResult::fromRoute($route, ['foo' => 'bar']); + self::assertTrue($result->isSuccess()); self::assertSame($route, $result->getMatchedRoute()); @@ -95,21 +100,24 @@ public function testAllAccessorsShouldReturnExpectedDataWhenResultCreatedViaFrom ->method('getAllowedMethods') ->willReturn(['HEAD', 'OPTIONS', 'GET']); - self::assertEquals('route', $result->getMatchedRouteName()); - self::assertEquals(['HEAD', 'OPTIONS', 'GET'], $result->getAllowedMethods()); + self::assertSame('route', $result->getMatchedRouteName()); + self::assertSame(['HEAD', 'OPTIONS', 'GET'], $result->getAllowedMethods()); } public function testRouteFailureWithNoAllowedHttpMethodsShouldReportTrueForIsMethodFailure(): void { $result = RouteResult::fromRouteFailure([]); + self::assertTrue($result->isMethodFailure()); } public function testFailureResultDoesNotIndicateAMethodFailureIfAllMethodsAreAllowed(): RouteResult { $result = RouteResult::fromRouteFailure(Route::HTTP_METHOD_ANY); + self::assertTrue($result->isFailure()); self::assertFalse($result->isMethodFailure()); + return $result; } diff --git a/test/RouteTest.php b/test/RouteTest.php index 2a545ac..985fe74 100644 --- a/test/RouteTest.php +++ b/test/RouteTest.php @@ -17,64 +17,71 @@ use function sprintf; -/** - * @covers \Mezzio\Router\Route - */ -class RouteTest extends TestCase +/** @covers \Mezzio\Router\Route */ +final class RouteTest extends TestCase { /** @var MiddlewareInterface&MockObject */ - private $noopMiddleware; + private MiddlewareInterface $noopMiddleware; protected function setUp(): void { + parent::setUp(); + $this->noopMiddleware = $this->createMock(MiddlewareInterface::class); } public function testRoutePathIsRetrievable(): void { $route = new Route('/foo', $this->noopMiddleware); - $this->assertEquals('/foo', $route->getPath()); + + self::assertSame('/foo', $route->getPath()); } public function testRouteMiddlewareIsRetrievable(): void { $route = new Route('/foo', $this->noopMiddleware); - $this->assertSame($this->noopMiddleware, $route->getMiddleware()); + + self::assertSame($this->noopMiddleware, $route->getMiddleware()); } public function testRouteInstanceAcceptsAllHttpMethodsByDefault(): void { $route = new Route('/foo', $this->noopMiddleware); - $this->assertSame(Route::HTTP_METHOD_ANY, $route->getAllowedMethods()); + + self::assertSame(Route::HTTP_METHOD_ANY, $route->getAllowedMethods()); } public function testRouteAllowsSpecifyingHttpMethods(): void { $methods = [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST]; $route = new Route('/foo', $this->noopMiddleware, $methods); - $this->assertSame($methods, $route->getAllowedMethods()); + + self::assertSame($methods, $route->getAllowedMethods()); } public function testRouteCanMatchMethod(): void { $methods = [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST]; $route = new Route('/foo', $this->noopMiddleware, $methods); - $this->assertTrue($route->allowsMethod(RequestMethod::METHOD_GET)); - $this->assertTrue($route->allowsMethod(RequestMethod::METHOD_POST)); - $this->assertFalse($route->allowsMethod(RequestMethod::METHOD_PATCH)); - $this->assertFalse($route->allowsMethod(RequestMethod::METHOD_DELETE)); + + self::assertTrue($route->allowsMethod(RequestMethod::METHOD_GET)); + self::assertTrue($route->allowsMethod(RequestMethod::METHOD_POST)); + self::assertFalse($route->allowsMethod(RequestMethod::METHOD_PATCH)); + self::assertFalse($route->allowsMethod(RequestMethod::METHOD_DELETE)); } public function testRouteHeadMethodIsNotAllowedByDefault(): void { $route = new Route('/foo', $this->noopMiddleware, [RequestMethod::METHOD_GET]); - $this->assertFalse($route->allowsMethod(RequestMethod::METHOD_HEAD)); + + self::assertFalse($route->allowsMethod(RequestMethod::METHOD_HEAD)); } public function testRouteOptionsMethodIsNotAllowedByDefault(): void { $route = new Route('/foo', $this->noopMiddleware, [RequestMethod::METHOD_GET]); - $this->assertFalse($route->allowsMethod(RequestMethod::METHOD_OPTIONS)); + + self::assertFalse($route->allowsMethod(RequestMethod::METHOD_OPTIONS)); } public function testRouteAllowsSpecifyingOptions(): void @@ -82,37 +89,43 @@ public function testRouteAllowsSpecifyingOptions(): void $options = ['foo' => 'bar']; $route = new Route('/foo', $this->noopMiddleware); $route->setOptions($options); - $this->assertSame($options, $route->getOptions()); + + self::assertSame($options, $route->getOptions()); } public function testRouteOptionsAreEmptyByDefault(): void { $route = new Route('/foo', $this->noopMiddleware); - $this->assertSame([], $route->getOptions()); + + self::assertSame([], $route->getOptions()); } public function testRouteNameForRouteAcceptingAnyMethodMatchesPathByDefault(): void { $route = new Route('/test', $this->noopMiddleware); - $this->assertSame('/test', $route->getName()); + + self::assertSame('/test', $route->getName()); } public function testRouteNameWithConstructor(): void { $route = new Route('/test', $this->noopMiddleware, [RequestMethod::METHOD_GET], 'test'); - $this->assertSame('test', $route->getName()); + + self::assertSame('test', $route->getName()); } public function testRouteNameWithGET(): void { $route = new Route('/test', $this->noopMiddleware, [RequestMethod::METHOD_GET]); - $this->assertSame('/test^GET', $route->getName()); + + self::assertSame('/test^GET', $route->getName()); } public function testRouteNameWithGetAndPost(): void { $route = new Route('/test', $this->noopMiddleware, [RequestMethod::METHOD_GET, RequestMethod::METHOD_POST]); - $this->assertSame('/test^GET' . Route::HTTP_METHOD_SEPARATOR . RequestMethod::METHOD_POST, $route->getName()); + + self::assertSame('/test^GET' . Route::HTTP_METHOD_SEPARATOR . RequestMethod::METHOD_POST, $route->getName()); } /** @@ -168,7 +181,7 @@ public function testRouteNameIsMutable(): void $route = new Route('/foo', $this->noopMiddleware, [RequestMethod::METHOD_GET], 'foo'); $route->setName('bar'); - $this->assertSame('bar', $route->getName()); + self::assertSame('bar', $route->getName()); } /** @@ -199,7 +212,8 @@ public function testAllowsHttpInteropMiddleware(): void { $middleware = $this->createMock(MiddlewareInterface::class); $route = new Route('/test', $middleware, Route::HTTP_METHOD_ANY); - $this->assertSame($middleware, $route->getMiddleware()); + + self::assertSame($middleware, $route->getMiddleware()); } /** @@ -252,7 +266,8 @@ public function testRouteIsMiddlewareAndProxiesToComposedMiddleware(): void ->willReturn($response); $route = new Route('/foo', $middleware); - $this->assertSame($response, $route->process($request, $handler)); + + self::assertSame($response, $route->process($request, $handler)); } public function testConstructorShouldRaiseExceptionIfMethodsArgumentIsAnEmptyArray(): void @@ -261,6 +276,7 @@ public function testConstructorShouldRaiseExceptionIfMethodsArgumentIsAnEmptyArr $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('empty'); + new Route('/foo', $middleware, []); } }