diff --git a/tests/bundle/EventListener/RequestListenerTest.php b/tests/bundle/EventListener/RequestListenerTest.php index 1a0fb7cb..da1b5a2c 100644 --- a/tests/bundle/EventListener/RequestListenerTest.php +++ b/tests/bundle/EventListener/RequestListenerTest.php @@ -7,18 +7,22 @@ namespace Ibexa\Tests\Bundle\Rest\EventListener; use Ibexa\Bundle\Rest\EventListener\RequestListener; -use Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher; +use Ibexa\Bundle\Rest\UriParser\UriParser; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\Routing\Matcher\UrlMatcherInterface; -class RequestListenerTest extends EventListenerTest +final class RequestListenerTest extends EventListenerTest { public const REST_ROUTE = '/api/ibexa/v2/rest-route'; public const NON_REST_ROUTE = '/non-rest-route'; - public function provideExpectedSubscribedEventTypes() + /** + * @return array + */ + public function provideExpectedSubscribedEventTypes(): array { return [ [ @@ -27,97 +31,69 @@ public function provideExpectedSubscribedEventTypes() ]; } - public static function restRequestUrisProvider() + /** + * @retirm array + */ + public static function getDataForTestOnKernelRequest(): array { return [ - ['/api/ibexa/v2/true'], - ['/api/bundle-name/v2/true'], - ['/api/MyBundle12/v2/true'], - ['/api/ThisIs_Bundle123/v2/true'], - ['/api/my-bundle/v1/true'], - ['/api/my-bundle/v2/true'], - ['/api/my-bundle/v2.7/true'], - ['/api/my-bundle/v122.73/true'], + // REST requests + [self::REST_ROUTE, true], + ['/api/ibexa/v2/true', true], + ['/api/bundle-name/v2/true', true], + ['/api/MyBundle12/v2/true', true], + ['/api/ThisIs_Bundle123/v2/true', true], + ['/api/my-bundle/v1/true', true], + ['/api/my-bundle/v2/true', true], + ['/api/my-bundle/v2.7/true', true], + ['/api/my-bundle/v122.73/true', true], + // non-REST requests + [self::NON_REST_ROUTE, false], + ['/ap/ezp/v2/false', false], + ['/api/bundle name/v2/false', false], + ['/api/My/Bundle/v2/false', false], + ['/api//v2/false', false], + ['/api/my-bundle/v/false', false], + ['/api/my-bundle/v2-2/false', false], + ['/api/my-bundle/v2 7/false', false], + ['/api/my-bundle/v/7/false', false], ]; } - public static function nonRestRequestsUrisProvider() + /** + * @retirm array + */ + public static function nonRestRequestsUrisProvider(): array { return [ - ['/ap/ezp/v2/false'], - ['/api/bundle name/v2/false'], - ['/api/My/Bundle/v2/false'], - ['/api//v2/false'], - ['/api/my-bundle/v/false'], - ['/api/my-bundle/v2-2/false'], - ['/api/my-bundle/v2 7/false'], - ['/api/my-bundle/v/7/false'], ]; } - public function testOnKernelRequestNotMasterRequest() + public function testOnKernelRequestNotMasterRequest(): void { $request = $this->performFakeRequest(self::REST_ROUTE, HttpKernelInterface::SUB_REQUEST); self::assertTrue($request->attributes->get('is_rest_request')); } - public function testOnKernelRequestNotRestRequest() - { - $request = $this->performFakeRequest(self::NON_REST_ROUTE); - - self::assertFalse($request->attributes->get('is_rest_request')); - } - - public function testOnKernelRequestRestRequest() - { - $request = $this->performFakeRequest(self::REST_ROUTE); - - self::assertTrue($request->attributes->get('is_rest_request')); - } - - /** - * @dataProvider restRequestUrisProvider - */ - public function testRestRequestVariations($uri) - { - $request = $this->performFakeRequest($uri); - - self::assertTrue($request->attributes->get('is_rest_request')); - } - /** - * @dataProvider nonRestRequestsUrisProvider + * @dataProvider getDataForTestOnKernelRequest */ - public function testNonRestRequestVariations($uri) + public function testOnKernelRequest(string $uri, bool $isExpectedRestRequest): void { $request = $this->performFakeRequest($uri); - self::assertFalse($request->attributes->get('is_rest_request')); + self::assertSame($isExpectedRestRequest, $request->attributes->get('is_rest_request')); } - /** - * @return \Ibexa\Bundle\Rest\EventListener\RequestListener - */ - protected function getEventListener() + protected function getEventListener(): RequestListener { return new RequestListener( - $this->getVisitorDispatcherMock() + new UriParser($this->createMock(UrlMatcherInterface::class)) ); } - /** - * @return \Ibexa\Rest\Server\View\AcceptHeaderVisitorDispatcher|\PHPUnit\Framework\MockObject\MockObject - */ - public function getVisitorDispatcherMock() - { - return $this->createMock(AcceptHeaderVisitorDispatcher::class); - } - - /** - * @return \Symfony\Component\HttpFoundation\Request - */ - protected function performFakeRequest($uri, $type = HttpKernelInterface::MASTER_REQUEST) + protected function performFakeRequest(string $uri, int $type = HttpKernelInterface::MAIN_REQUEST): Request { $event = new RequestEvent( $this->createMock(HttpKernelInterface::class), diff --git a/tests/bundle/RequestParser/RouterTest.php b/tests/bundle/RequestParser/RouterTest.php index a234dfe7..fa92802f 100644 --- a/tests/bundle/RequestParser/RouterTest.php +++ b/tests/bundle/RequestParser/RouterTest.php @@ -4,39 +4,39 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ +declare(strict_types=1); + namespace Ibexa\Tests\Bundle\Rest\RequestParser; use Ibexa\Bundle\Rest\RequestParser\Router as RouterRequestParser; +use Ibexa\Bundle\Rest\UriParser\UriParser; use Ibexa\Contracts\Rest\Exceptions\InvalidArgumentException; +use Ibexa\Rest\RequestParser; +use PHPUnit\Framework\MockObject\Builder\InvocationMocker; +use PHPUnit\Framework\MockObject\Rule\InvokedCount as InvokedCountMatcher; use PHPUnit\Framework\TestCase; -use Symfony\Cmf\Component\Routing\ChainRouter; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\RouterInterface; -class RouterTest extends TestCase +final class RouterTest extends TestCase { - /** - * @var \Symfony\Cmf\Component\Routing\ChainRouter - */ - private $router; + private RouterInterface $router; - protected static $routePrefix = '/api/test/v1'; + private static $routePrefix = '/api/test/v1'; - public function testParse() + public function testParse(): void { $uri = self::$routePrefix . '/'; - $request = Request::create($uri, 'GET'); $expectedMatchResult = [ '_route' => 'ibexa.rest.test_route', '_controller' => '', ]; - $this->getRouterMock() - ->expects($this->once()) - ->method('matchRequest') - ->willReturn($expectedMatchResult); + $this->getRouterInvocationMockerForMatchingUri($uri) + ->willReturn($expectedMatchResult) + ; self::assertEquals( $expectedMatchResult, @@ -44,37 +44,36 @@ public function testParse() ); } - public function testParseNoMatch() + public function testParseNoMatch(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('No route matched \'/api/test/v1/nomatch\''); + $exceptionMessage = 'No route matched \'/api/test/v1/nomatch\''; + $this->expectExceptionMessage($exceptionMessage); $uri = self::$routePrefix . '/nomatch'; - $this->getRouterMock() - ->expects($this->once()) - ->method('matchRequest') - ->will($this->throwException(new ResourceNotFoundException())); + $this->getRouterInvocationMockerForMatchingUri($uri) + ->willThrowException(new ResourceNotFoundException($exceptionMessage)) + ; $this->getRequestParser()->parse($uri); } - public function testParseNoPrefix() + public function testParseNoPrefix(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('No route matched \'/no/prefix\''); + $exceptionMessage = 'No route matched \'/no/prefix\''; + $this->expectExceptionMessage($exceptionMessage); $uri = '/no/prefix'; - $this->getRouterMock() - ->expects($this->once()) - ->method('matchRequest') - ->will($this->throwException(new ResourceNotFoundException())); + // invalid prefix should cause internal url matcher not to be called + $this->getRouterInvocationMockerForMatchingUri($uri, self::never()); $this->getRequestParser()->parse($uri); } - public function testParseHref() + public function testParseHref(): void { $href = '/api/test/v1/content/objects/1'; @@ -83,18 +82,19 @@ public function testParseHref() 'contentId' => 1, ]; - $this->getRouterMock() - ->expects($this->once()) - ->method('matchRequest') - ->willReturn($expectedMatchResult); + $this->getRouterInvocationMockerForMatchingUri($href) + ->willReturn($expectedMatchResult) + ; self::assertEquals(1, $this->getRequestParser()->parseHref($href, 'contentId')); } - public function testParseHrefAttributeNotFound() + public function testParseHrefAttributeNotFound(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('No attribute \'badAttribute\' in route matched from /api/test/v1/content/no-attribute'); + $this->expectExceptionMessage( + 'No attribute \'badAttribute\' in route matched from /api/test/v1/content/no-attribute' + ); $href = '/api/test/v1/content/no-attribute'; @@ -102,25 +102,25 @@ public function testParseHrefAttributeNotFound() '_route' => 'ibexa.rest.test_parse_href_attribute_not_found', ]; - $this->getRouterMock() - ->expects($this->once()) - ->method('matchRequest') - ->willReturn($matchResult); + $this->getRouterInvocationMockerForMatchingUri($href) + ->willReturn($matchResult) + ; self::assertEquals(1, $this->getRequestParser()->parseHref($href, 'badAttribute')); } - public function testGenerate() + public function testGenerate(): void { $routeName = 'ibexa.rest.test_generate'; $arguments = ['arg1' => 1]; $expectedResult = self::$routePrefix . '/generate/' . $arguments['arg1']; $this->getRouterMock() - ->expects($this->once()) - ->method('generate') - ->with($routeName, $arguments) - ->willReturn($expectedResult); + ->expects($this->once()) + ->method('generate') + ->with($routeName, $arguments) + ->willReturn($expectedResult) + ; self::assertEquals( $expectedResult, @@ -128,32 +128,43 @@ public function testGenerate() ); } - /** - * @return \Ibexa\Bundle\Rest\RequestParser\Router - */ - private function getRequestParser() + private function getRequestParser(): RequestParser { + $routerMock = $this->getRouterMock(); + return new RouterRequestParser( - $this->getRouterMock() + $routerMock, + new UriParser($routerMock) ); } /** - * @return \Symfony\Cmf\Component\Routing\ChainRouter|\PHPUnit\Framework\MockObject\MockObject + * @return \Symfony\Component\Routing\RouterInterface&\PHPUnit\Framework\MockObject\MockObject */ - private function getRouterMock() + private function getRouterMock(): RouterInterface { if (!isset($this->router)) { - $this->router = $this->createMock(ChainRouter::class); + $this->router = $this->createMock(RouterInterface::class); $this->router - ->expects($this->any()) ->method('getContext') - ->willReturn(new RequestContext()); + ->willReturn(new RequestContext()) + ; } return $this->router; } + + private function getRouterInvocationMockerForMatchingUri( + string $uri, + ?InvokedCountMatcher $invokedCount = null + ): InvocationMocker { + return $this->getRouterMock() + ->expects($invokedCount ?? self::once()) + ->method('match') + ->with($uri) + ; + } } class_alias(RouterTest::class, 'EzSystems\EzPlatformRestBundle\Tests\RequestParser\RouterTest');