|
| 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