Skip to content

Commit

Permalink
[Tests] Added test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Sep 1, 2023
1 parent 7bb5943 commit d0df5b2
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions tests/bundle/Core/Converter/LocationArgumentResolverTest.php
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;
}
}

0 comments on commit d0df5b2

Please sign in to comment.