Passed
Pull Request — master (#63)
by Mark
23:06
created

TypesTrait::setUpWithAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    /**
32
     * @var Types
33
     */
34
    private $types;
35
36
    public function setUp(): void
37
    {
38
        $this->setUpEntityManager();
39
40
        $customTypes = new ServiceManager([
41
            'invokables' => [
42
                BooleanType::class => BooleanType::class,
43
                DateTimeImmutable::class => DateTimeType::class,
44
                stdClass::class => CustomType::class,
45
                'PostStatus' => PostStatusType::class,
46
            ],
47
            'aliases' => [
48
                'datetime_immutable' => DateTimeImmutable::class, // Declare alias for Doctrine type to be used for filters
49
            ],
50
        ]);
51
52
        $this->types = new Types($this->entityManager, $customTypes);
53
    }
54
55
    public function setUpWithAttributes(): void
56
    {
57
        $this->setUpAttributeEntityManager();
58
59
        $customTypes = new ServiceManager([
60
            'invokables' => [
61
                BooleanType::class => BooleanType::class,
62
                DateTimeImmutable::class => DateTimeType::class,
63
                stdClass::class => CustomType::class,
64
                'PostStatus' => PostStatusType::class,
65
            ],
66
            'aliases' => [
67
                'datetime_immutable' => DateTimeImmutable::class, // Declare alias for Doctrine type to be used for filters
68
            ],
69
        ]);
70
71
        $this->types = new Types($this->entityManager, $customTypes);
72
    }
73
74
    private function assertType(string $expectedFile, Type $type): void
75
    {
76
        $actual = SchemaPrinter::printType($type) . "\n";
77
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
78
    }
79
80
    private function assertAllTypes(string $expectedFile, Type $type): void
81
    {
82
        $schema = $this->getSchemaForType($type);
83
        $actual = SchemaPrinter::doPrint($schema);
84
85
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
86
    }
87
88
    /**
89
     * Create a temporary schema for the given type.
90
     */
91
    private function getSchemaForType(Type $type): Schema
92
    {
93
        if ($type instanceof WrappingType) {
94
            $wrappedType = $type->getWrappedType(true);
95
        } else {
96
            $wrappedType = $type;
97
        }
98
99
        if ($wrappedType instanceof OutputType) {
100
            $outputType = $type;
101
            $args = [];
102
        } elseif ($wrappedType instanceof InputType) {
103
            $outputType = Type::boolean();
104
            $args = [
105
                'defaultArg' => $type,
106
            ];
107
        } else {
108
            throw new Exception('Unsupported type: ' . $wrappedType::class);
109
        }
110
111
        $config = [
112
            'query' => new ObjectType([
113
                'name' => 'query',
114
                'fields' => [
115
                    'defaultField' => [
116
                        'type' => $outputType,
117
                        'args' => $args,
118
                    ],
119
                ],
120
            ]),
121
        ];
122
123
        $schema = new Schema($config);
124
        $schema->assertValid();
125
126
        return $schema;
127
    }
128
}
129