Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Apr 24, 2019
1 parent 9222ded commit 5fc02d9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Routing/RouteNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getRouteName(string $resourceClass, $operationType /*, array $co
$operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType));
$methods = $route->getMethods();

if ($resourceClass === $currentResourceClass && null !== $operation && (empty($methods) || \in_array('GET', $methods, true))) {
if (null !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, true) && null !== $operation && (empty($methods) || \in_array('GET', $methods, true))) {
if (OperationType::SUBRESOURCE === $operationType && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) {
continue;
}
Expand Down
68 changes: 46 additions & 22 deletions tests/Bridge/Symfony/Routing/RouteNameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
use ApiPlatform\Core\Api\OperationType;
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolver;
use ApiPlatform\Core\Bridge\Symfony\Routing\RouteNameResolverInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
use FOS\UserBundle\Model\User as BaseUser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

/**
* @author Teoh Han Hui <teohhanhui@gmail.com>
* @author Vincent Chalamon <vincentchalamon@gmail.com>
*/
class RouteNameResolverTest extends TestCase
{
Expand All @@ -38,19 +41,19 @@ public function testConstruct()
public function testGetRouteNameForItemRouteWithNoMatchingRoute()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('No item route associated with the type "AppBundle\\Entity\\User".');
$this->expectExceptionMessage('No item route associated with the type "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\User".');

$routeCollection = new RouteCollection();
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$routeNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM);
$routeNameResolver->getRouteName(User::class, OperationType::ITEM);
}

/**
Expand All @@ -61,19 +64,19 @@ public function testGetRouteNameForItemRouteLegacy()
{
$routeCollection = new RouteCollection();
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_item_operation_name' => 'some_item_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName('AppBundle\Entity\User', false);
$actual = $routeNameResolver->getRouteName(User::class, false);

$this->assertSame('some_item_route', $actual);
}
Expand All @@ -82,39 +85,60 @@ public function testGetRouteNameForItemRoute()
{
$routeCollection = new RouteCollection();
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_item_operation_name' => 'some_item_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM);
$actual = $routeNameResolver->getRouteName(User::class, OperationType::ITEM);

$this->assertSame('some_item_route', $actual);
}

public function testGetRouteNameForItemRouteFromAbstract()
{
$routeCollection = new RouteCollection();
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => BaseUser::class,
'_api_collection_operation_name' => 'some_collection_op',
]));
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => BaseUser::class,
'_api_item_operation_name' => 'some_item_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName(User::class, OperationType::ITEM);

$this->assertSame('some_item_route', $actual);
}

public function testGetRouteNameForCollectionRouteWithNoMatchingRoute()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('No collection route associated with the type "AppBundle\\Entity\\User".');
$this->expectExceptionMessage('No collection route associated with the type "ApiPlatform\\Core\\Tests\\Fixtures\\TestBundle\\Entity\\User".');

$routeCollection = new RouteCollection();
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_item_operation_name' => 'some_item_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$routeNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION);
$routeNameResolver->getRouteName(User::class, OperationType::COLLECTION);
}

/**
Expand All @@ -125,19 +149,19 @@ public function testGetRouteNameForCollectionRouteLegacy()
{
$routeCollection = new RouteCollection();
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_item_operation_name' => 'some_item_op',
]));
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName('AppBundle\Entity\User', true);
$actual = $routeNameResolver->getRouteName(User::class, true);

$this->assertSame('some_collection_route', $actual);
}
Expand All @@ -146,19 +170,19 @@ public function testGetRouteNameForCollectionRoute()
{
$routeCollection = new RouteCollection();
$routeCollection->add('some_item_route', new Route('/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_item_operation_name' => 'some_item_op',
]));
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION);
$actual = $routeNameResolver->getRouteName(User::class, OperationType::COLLECTION);

$this->assertSame('some_collection_route', $actual);
}
Expand All @@ -167,25 +191,25 @@ public function testGetRouteNameForSubresourceRoute()
{
$routeCollection = new RouteCollection();
$routeCollection->add('a_some_subresource_route', new Route('/a/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_subresource_operation_name' => 'some_other_item_op',
'_api_subresource_context' => ['identifiers' => [[1, 'bar']]],
]));
$routeCollection->add('b_some_subresource_route', new Route('/b/some/item/path/{id}', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_subresource_operation_name' => 'some_item_op',
'_api_subresource_context' => ['identifiers' => [[1, 'foo']]],
]));
$routeCollection->add('some_collection_route', new Route('/some/collection/path', [
'_api_resource_class' => 'AppBundle\Entity\User',
'_api_resource_class' => User::class,
'_api_collection_operation_name' => 'some_collection_op',
]));

$routerProphecy = $this->prophesize(RouterInterface::class);
$routerProphecy->getRouteCollection()->willReturn($routeCollection);

$routeNameResolver = new RouteNameResolver($routerProphecy->reveal());
$actual = $routeNameResolver->getRouteName('AppBundle\Entity\User', OperationType::SUBRESOURCE, ['subresource_resources' => ['foo' => 1]]);
$actual = $routeNameResolver->getRouteName(User::class, OperationType::SUBRESOURCE, ['subresource_resources' => ['foo' => 1]]);

$this->assertSame('b_some_subresource_route', $actual);
}
Expand Down

0 comments on commit 5fc02d9

Please sign in to comment.