|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\Api; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
|
9
|
|
|
use Doctrine\Persistence\Proxy; |
|
10
|
|
|
use Ecodev\Felix\Api\FilteredFieldResolver; |
|
11
|
|
|
use EcodevTests\Felix\Blog\Model\User; |
|
12
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode; |
|
13
|
|
|
use GraphQL\Type\Definition\FieldDefinition; |
|
14
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
15
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
16
|
|
|
use GraphQL\Type\Definition\Type; |
|
17
|
|
|
use GraphQL\Type\Schema; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use stdClass; |
|
20
|
|
|
|
|
21
|
|
|
final class FilteredFieldResolverTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider providerLoad |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testLoad(mixed $value, mixed $expected): void |
|
27
|
|
|
{ |
|
28
|
|
|
$model = new class($value) { |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
private mixed $value, |
|
31
|
|
|
) {} |
|
32
|
|
|
|
|
33
|
|
|
public function getField(): mixed |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->value; |
|
36
|
|
|
} |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
$fieldDefinition = new FieldDefinition(['name' => 'field', 'type' => Type::boolean()]); |
|
40
|
|
|
$resolve = new ResolveInfo($fieldDefinition, new ArrayObject(), new ObjectType(['name' => 'foo', 'fields' => []]), [], new Schema([]), [], null, new OperationDefinitionNode([]), []); |
|
41
|
|
|
$resolver = new FilteredFieldResolver(); |
|
42
|
|
|
self::assertSame($expected, $resolver($model, [], [], $resolve)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function providerLoad(): iterable |
|
46
|
|
|
{ |
|
47
|
|
|
$loadableClass = new class() implements Proxy { |
|
48
|
|
|
public function __load(): void {} |
|
49
|
|
|
|
|
50
|
|
|
public function __isInitialized(): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
}; |
|
55
|
|
|
|
|
56
|
|
|
$unLoadableClass = new class() implements Proxy { |
|
57
|
|
|
public function __load(): void |
|
58
|
|
|
{ |
|
59
|
|
|
throw new EntityNotFoundException(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function __isInitialized(): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
|
|
$object = new stdClass(); |
|
69
|
|
|
$user = new User(); |
|
70
|
|
|
$loadable = new $loadableClass(); |
|
71
|
|
|
$unloadable = new $unLoadableClass(); |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
[null, null], |
|
75
|
|
|
[1, 1], |
|
76
|
|
|
['foo', 'foo'], |
|
77
|
|
|
[$object, $object], |
|
78
|
|
|
[$user, $user], |
|
79
|
|
|
[$loadable, $loadable], |
|
80
|
|
|
[$unloadable, null], |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|