Skip to content

Commit b949294

Browse files
committed
Add a test
1 parent a6da79b commit b949294

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/Graphql/Resolver/ResourceFieldResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function __invoke($source, $args, $context, ResolveInfo $info)
4040
}
4141

4242
if ('_id' === $info->fieldName && isset($source['id'])) {
43-
$property = 'id';
44-
} elseif (isset($source[$info->fieldName])) {
43+
$property = $source['id'];
44+
} elseif (\is_array($source) && isset($source[$info->fieldName])) {
4545
$property = $source[$info->fieldName];
4646
} elseif (isset($source->{$info->fieldName})) {
4747
$property = $source->{$info->fieldName};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Graphql\Resolver;
15+
16+
use ApiPlatform\Core\Api\IriConverterInterface;
17+
use ApiPlatform\Core\Graphql\Resolver\ResourceFieldResolver;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
19+
use GraphQL\Type\Definition\ResolveInfo;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class ResourceFieldResolverTest extends TestCase
23+
{
24+
public function testId()
25+
{
26+
$dummy = new Dummy();
27+
28+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
29+
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummies/1')->shouldBeCalled();
30+
31+
$resolveInfo = new ResolveInfo([]);
32+
$resolveInfo->fieldName = 'id';
33+
$resolveInfo->fieldNodes = [];
34+
35+
$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
36+
$this->assertEquals('/dummies/1', $resolver(['#item' => serialize($dummy)], [], [], $resolveInfo));
37+
}
38+
39+
public function testOriginalId()
40+
{
41+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
42+
43+
$resolveInfo = new ResolveInfo([]);
44+
$resolveInfo->fieldName = '_id';
45+
$resolveInfo->fieldNodes = [];
46+
47+
$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
48+
$this->assertEquals(1, $resolver(['id' => 1], [], [], $resolveInfo));
49+
}
50+
51+
public function testDirectAccess()
52+
{
53+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
54+
55+
$resolveInfo = new ResolveInfo([]);
56+
$resolveInfo->fieldName = 'foo';
57+
$resolveInfo->fieldNodes = [];
58+
59+
$resolver = new ResourceFieldResolver($iriConverterProphecy->reveal());
60+
$this->assertEquals('bar', $resolver(['foo' => 'bar'], [], [], $resolveInfo));
61+
$this->assertEquals('bar', $resolver((object) ['foo' => 'bar'], [], [], $resolveInfo));
62+
}
63+
}

0 commit comments

Comments
 (0)