Skip to content

Commit

Permalink
Add fixes for tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez authored and arti0090 committed Apr 29, 2021
1 parent e037a75 commit 6bb6936
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\DataProvider\OperationDataProviderTrait;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Exception\InvalidIdentifierException;
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
use ApiPlatform\Core\Util\AttributesExtractor;
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
use Symfony\Component\Routing\RouterInterface;
Expand All @@ -28,9 +29,10 @@ final class ItemIriToIdentifierConverter implements ItemIriToIdentifierConverter
/** @var RouterInterface */
private $router;

public function __construct(RouterInterface $router)
public function __construct(RouterInterface $router, IdentifierConverterInterface $identifierConverter)
{
$this->router = $router;
$this->identifierConverter = $identifierConverter;
}

public function getIdentifier(string $iri): string {
Expand All @@ -56,10 +58,10 @@ public function getIdentifier(string $iri): string {
throw new InvalidArgumentException($e->getMessage(), (int) $e->getCode(), $e);
}

if (is_array($identifiers)) {
if (count($identifiers) > 1) {
throw new InvalidArgumentException(sprintf('%s does not support subresources', self::class));
}

return (string) $identifiers;
return (string) array_values($identifiers)[0];
}
}
1 change: 1 addition & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@

<service id="Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter">
<argument type="service" id="api_platform.router" />
<argument type="service" id="api_platform.identifier.converter" on-invalid="ignore" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Bundle\ApiBundle\Tests\Converter;

use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Identifier\IdentifierConverterInterface;
use PHPUnit\Framework\TestCase;
use Sylius\Bundle\ApiBundle\Command\AddProductReview;
use Sylius\Bundle\ApiBundle\Converter\ItemIriToIdentifierConverter;
Expand All @@ -31,9 +32,11 @@ public function it_throws_invalid_argument_exception_if_no_route_matches(): void
$this->expectExceptionMessage('No route matches "/users/3".');

$router = $this->prophesize(RouterInterface::class);
$identifierConverter = $this->prophesize(IdentifierConverterInterface::class);

$router->match('/users/3')->willThrow(new RouteNotFoundException())->shouldBeCalledTimes(1);

$converter = new ItemIriToIdentifierConverter($router->reveal());
$converter = new ItemIriToIdentifierConverter($router->reveal(), $identifierConverter->reveal());
$converter->getIdentifier('/users/3');
}

Expand All @@ -46,29 +49,37 @@ public function it_throws_invalid_argument_exception_if_parameter_api_resource_c
$this->expectExceptionMessage('No resource associated to "/users/3".');

$router = $this->prophesize(RouterInterface::class);
$identifierConverter = $this->prophesize(IdentifierConverterInterface::class);

$router->match('/users/3')->willReturn([])->shouldBeCalledTimes(1);

$converter = new ItemIriToIdentifierConverter($router->reveal());
$converter = new ItemIriToIdentifierConverter($router->reveal(), $identifierConverter->reveal());
$converter->getIdentifier('/users/3');
}

/**
* @test
*/
public function it_throws_invalid_argument_exception_if_parameter_id_does_not_exist(): void
public function it_throws_invalid_argument_exception_if_converter_returns_more_than_one_identifier(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Parameter "id" not found');
$this->expectExceptionMessage('ItemIriToIdentifierConverter does not support subresources');

$router = $this->prophesize(RouterInterface::class);
$router->match('/users')->willReturn([
$identifierConverter = $this->prophesize(IdentifierConverterInterface::class);

$router->match('/users/3/nexts/5')->willReturn([
'_api_resource_class' => AddProductReview::class,
'_api_item_operation_name' => 'get',
'_api_identifiers' => [],
'_api_identifiers' => ['id', 'nextId'],
'id' => 3,
'nextId' => 5
])->shouldBeCalledTimes(1);

$converter = new ItemIriToIdentifierConverter($router->reveal());
$converter->getIdentifier('/users');
$identifierConverter->convert(['id' => 3, 'nextId' => 5], AddProductReview::class)->willReturn(['3', '5']);
$converter = new ItemIriToIdentifierConverter($router->reveal(), $identifierConverter->reveal());

$this->assertSame('3', $converter->getIdentifier('/users/3/nexts/5'));
}

/**
Expand All @@ -77,14 +88,17 @@ public function it_throws_invalid_argument_exception_if_parameter_id_does_not_ex
public function it_get_identifier(): void
{
$router = $this->prophesize(RouterInterface::class);
$identifierConverter = $this->prophesize(IdentifierConverterInterface::class);

$router->match('/users/3')->willReturn([
'_api_resource_class' => AddProductReview::class,
'_api_item_operation_name' => 'get',
'_api_identifiers' => ['id'],
'id' => 3,
])->shouldBeCalledTimes(1);

$converter = new ItemIriToIdentifierConverter($router->reveal());
$identifierConverter->convert(['id' => 3], AddProductReview::class)->willReturn(['3']);
$converter = new ItemIriToIdentifierConverter($router->reveal(), $identifierConverter->reveal());

$this->assertSame('3', $converter->getIdentifier('/users/3'));
}
Expand Down

0 comments on commit 6bb6936

Please sign in to comment.