testDefaultFieldResolver()
last analyzed

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 6
c 3
b 0
f 0
dl 0
loc 8
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine;
6
7
use ArrayObject;
8
use GraphQL\Doctrine\DefaultFieldResolver;
9
use GraphQL\Doctrine\Definition\EntityID;
10
use GraphQL\Language\AST\OperationDefinitionNode;
11
use GraphQL\Type\Definition\FieldDefinition;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Definition\ResolveInfo;
14
use GraphQL\Type\Definition\Type;
15
use GraphQL\Type\Schema;
16
use GraphQLTests\Doctrine\Blog\Model\Special\DefaultValue;
17
use GraphQLTests\Doctrine\Blog\Model\Special\IgnoredGetter;
18
use GraphQLTests\Doctrine\Blog\Model\User;
19
use PHPUnit\Framework\Attributes\DataProvider;
20
use PHPUnit\Framework\TestCase;
21
22
final class DefaultFieldResolverTest extends TestCase
23
{
24
    public static function providerDefaultFieldResolver(): array
25
    {
26
        $fakeEntity = new User();
27
        $entityID = new class($fakeEntity) extends EntityID {
28
            public function __construct(private readonly User $fakeEntity)
29
            {
30
            }
31
32
            public function getEntity(): User
33
            {
34
                return $this->fakeEntity;
35
            }
36
        };
37
38
        return [
39
            [null, new IgnoredGetter(), 'privateProperty'],
40
            [null, new IgnoredGetter(), 'protectedProperty'],
41
            ['publicProperty', new IgnoredGetter(), 'publicProperty'],
42
            [null, new IgnoredGetter(), 'private'],
43
            [null, new IgnoredGetter(), 'protected'],
44
            ['getPublic', new IgnoredGetter(), 'public'],
45
            [[$fakeEntity, 2, ['foo']], new IgnoredGetter(), 'publicWithArgs', ['arg2' => 2, 'arg1' => $entityID]],
46
            [null, new IgnoredGetter(), 'nonExisting'],
47
            [null, new IgnoredGetter(), '__call'],
48
            [true, new IgnoredGetter(), 'isValid'],
49
            [true, new IgnoredGetter(), 'hasMoney'],
50
            ['john', new DefaultValue(), 'nameWithDefaultValueOnArgument'],
51
            ['jane', new DefaultValue(), 'nameWithDefaultValueOnArgument', ['name' => 'jane']],
52
            ['bar', ['foo' => 'bar'], 'foo'],
53
        ];
54
    }
55
56
    #[DataProvider('providerDefaultFieldResolver')]
57
    public function testDefaultFieldResolver(mixed $expected, array|object $source, string $fieldName, array $args = []): void
58
    {
59
        $resolver = new DefaultFieldResolver();
60
        $fieldDefinition = new FieldDefinition(['name' => $fieldName, 'type' => Type::boolean()]);
61
        $info = new ResolveInfo($fieldDefinition, new ArrayObject(), new ObjectType(['name' => 'foo', 'fields' => []]), [], new Schema([]), [], null, new OperationDefinitionNode([]), []);
62
        $actual = $resolver($source, $args, null, $info);
63
        self::assertSame($expected, $actual);
64
    }
65
}
66