diff --git a/tests/bundle/Core/Converter/LocationArgumentResolverTest.php b/tests/bundle/Core/Converter/LocationArgumentResolverTest.php new file mode 100644 index 0000000000..50d120b662 --- /dev/null +++ b/tests/bundle/Core/Converter/LocationArgumentResolverTest.php @@ -0,0 +1,135 @@ +createMock(LocationService::class); + $this->locationArgumentResolver = new LocationArgumentResolver($locationService); + } + + /** + * @dataProvider provideDataForTestSupports + */ + public function testSupports( + bool $expected, + Request $request, + ArgumentMetadata $argumentMetadata + ): void { + self::assertSame( + $expected, + $this->locationArgumentResolver->supports( + $request, + $argumentMetadata + ) + ); + } + + public function testResolveThrowsInvalidArgumentException(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Argument \'locationId\' is invalid: Expected numeric type, string given.'); + + $generator = $this->locationArgumentResolver->resolve( + new Request( + [ + 'locationId' => 'foo', + ] + ), + $this->createMock(ArgumentMetadata::class) + ); + + self::assertInstanceOf(Generator::class, $generator); + + $generator->getReturn(); + } + + public function testResolve(): void + { + $resolvedArgumentsGenerator = $this->locationArgumentResolver->resolve( + $this->createRequest(true, false, 1), + $this->createMock(ArgumentMetadata::class) + ); + + self::assertInstanceOf(Generator::class, $resolvedArgumentsGenerator); + $resolvedArguments = iterator_to_array($resolvedArgumentsGenerator); + + self::assertCount(1, $resolvedArguments); + + $value = current($resolvedArguments); + self::assertInstanceOf( + Location::class, + $value + ); + } + + /** + * @return iterable + */ + public function provideDataForTestSupports(): iterable + { + $argumentMetadata = $this->createMock(ArgumentMetadata::class); + + yield 'Supported - locationId passed to request query' => [ + true, + $this->createRequest(true, false, 1), + $argumentMetadata, + ]; + + yield 'Not supported - locationId passed to request attributes' => [ + false, + $this->createRequest(false, true, 1), + $argumentMetadata, + ]; + + yield 'Not supported - locationId passed to request attributes and query' => [ + false, + $this->createRequest(true, true, 1), + $argumentMetadata, + ]; + } + + private function createRequest( + bool $addToQuery, + bool $addToAttributes, + ?int $locationId = null + ): Request { + $request = Request::create('/'); + + if ($addToQuery) { + $request->query->set(self::PARAMETER_LOCATION_ID, $locationId); + } + + if ($addToAttributes) { + $request->attributes->set(self::PARAMETER_LOCATION_ID, $locationId); + } + + return $request; + } +}