Failed Conditions
Pull Request — master (#63)
by Adrien
14:14
created

TypesTrait::assertType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine;
6
7
use DateTimeImmutable;
8
use Exception;
9
use GraphQL\Doctrine\Types;
10
use GraphQL\Type\Definition\BooleanType;
11
use GraphQL\Type\Definition\InputType;
12
use GraphQL\Type\Definition\ObjectType;
13
use GraphQL\Type\Definition\OutputType;
14
use GraphQL\Type\Definition\Type;
15
use GraphQL\Type\Definition\WrappingType;
16
use GraphQL\Type\Schema;
17
use GraphQL\Utils\SchemaPrinter;
18
use GraphQLTests\Doctrine\Blog\Types\CustomType;
19
use GraphQLTests\Doctrine\Blog\Types\DateTimeType;
20
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
21
use Laminas\ServiceManager\ServiceManager;
22
use stdClass;
23
24
/**
25
 * Trait to easily set up types and assert them.
26
 */
27
trait TypesTrait
28
{
29
    use EntityManagerTrait;
30
31
    private Types $types;
32
33
    public function setUp(): void
34
    {
35
        $this->setUpEntityManager();
36
37
        $customTypes = new ServiceManager([
38
            'invokables' => [
39
                BooleanType::class => BooleanType::class,
40
                DateTimeImmutable::class => DateTimeType::class,
41
                stdClass::class => CustomType::class,
42
                'PostStatus' => PostStatusType::class,
43
            ],
44
            'aliases' => [
45
                'datetime_immutable' => DateTimeImmutable::class, // Declare alias for Doctrine type to be used for filters
46
            ],
47
        ]);
48
49
        $this->types = new Types($this->entityManager, $customTypes);
50
    }
51
52
    public function setUpWithAttributes(): void
53
    {
54
        $this->setUpAttributeEntityManager();
55
56
        $customTypes = new ServiceManager([
57
            'invokables' => [
58
                BooleanType::class => BooleanType::class,
59
                DateTimeImmutable::class => DateTimeType::class,
60
                stdClass::class => CustomType::class,
61
                'PostStatus' => PostStatusType::class,
62
            ],
63
            'aliases' => [
64
                'datetime_immutable' => DateTimeImmutable::class, // Declare alias for Doctrine type to be used for filters
65
            ],
66
        ]);
67
68
        $this->types = new Types($this->entityManager, $customTypes);
69
    }
70
71
    private function assertType(string $expectedFile, Type $type): void
72
    {
73
        $actual = SchemaPrinter::printType($type) . "\n";
74
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
75
    }
76
77
    private function assertAllTypes(string $expectedFile, Type $type): void
78
    {
79
        $schema = $this->getSchemaForType($type);
80
        $actual = SchemaPrinter::doPrint($schema);
81
82
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
83
    }
84
85
    /**
86
     * Create a temporary schema for the given type.
87
     */
88
    private function getSchemaForType(Type $type): Schema
89
    {
90
        if ($type instanceof WrappingType) {
91
            $wrappedType = $type->getWrappedType(true);
92
        } else {
93
            $wrappedType = $type;
94
        }
95
96
        if ($wrappedType instanceof OutputType) {
97
            $outputType = $type;
98
            $args = [];
99
        } elseif ($wrappedType instanceof InputType) {
100
            $outputType = Type::boolean();
101
            $args = [
102
                'defaultArg' => $type,
103
            ];
104
        } else {
105
            throw new Exception('Unsupported type: ' . $wrappedType::class);
106
        }
107
108
        $config = [
109
            'query' => new ObjectType([
110
                'name' => 'query',
111
                'fields' => [
112
                    'defaultField' => [
113
                        'type' => $outputType,
114
                        'args' => $args,
115
                    ],
116
                ],
117
            ]),
118
        ];
119
120
        $schema = new Schema($config);
121
        $schema->assertValid();
122
123
        return $schema;
124
    }
125
}
126