-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
135 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
tests/bundle/Core/Converter/LocationArgumentResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Tests\Bundle\Core\Converter; | ||
|
||
use Generator; | ||
use Ibexa\Bundle\Core\Converter\LocationArgumentResolver; | ||
use Ibexa\Contracts\Core\Exception\InvalidArgumentException; | ||
use Ibexa\Contracts\Core\Repository\LocationService; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Location; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** | ||
* @covers \Ibexa\Bundle\Core\Converter\LocationArgumentResolver | ||
*/ | ||
final class LocationArgumentResolverTest extends TestCase | ||
{ | ||
private const PARAMETER_LOCATION_ID = 'locationId'; | ||
|
||
private LocationArgumentResolver $locationArgumentResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
$locationService = $this->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<array{ | ||
* bool, | ||
* \Symfony\Component\HttpFoundation\Request, | ||
* \Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata | ||
* }> | ||
*/ | ||
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; | ||
} | ||
} |