Failed Conditions
Pull Request — master (#10)
by Adrien
02:32
created

TypesTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertAllTypes() 0 6 1
A setUp() 0 18 1
B getSchemaForType() 0 26 2
A assertType() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQLTests\Doctrine;
6
7
use DateTime;
8
use GraphQL\Doctrine\Types;
9
use GraphQL\Type\Definition\BooleanType;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\OutputType;
12
use GraphQL\Type\Definition\Type;
13
use GraphQL\Type\Schema;
14
use GraphQL\Utils\SchemaPrinter;
15
use GraphQLTests\Doctrine\Blog\Filtering\Search;
16
use GraphQLTests\Doctrine\Blog\Types\CustomType;
17
use GraphQLTests\Doctrine\Blog\Types\DateTimeType;
18
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
19
use stdClass;
20
use Zend\ServiceManager\ServiceManager;
21
22
/**
23
 * Trait to easily set up types and assert them
24
 */
25
trait TypesTrait
26
{
27
    use EntityManagerTrait;
28
29
    /**
30
     * @var Types
31
     */
32
    private $types;
33
34
    public function setUp(): void
35
    {
36
        $this->setUpEntityManager();
37
38
        $customTypes = new ServiceManager([
39
            'invokables' => [
40
                BooleanType::class => BooleanType::class,
41
                DateTime::class => DateTimeType::class,
42
                stdClass::class => CustomType::class,
43
                'PostStatus' => PostStatusType::class,
44
                Search::class,
45
            ],
46
            'aliases' => [
47
                'datetime' => DateTime::class, // Declare alias for Doctrine type to be used for filters
48
            ],
49
        ]);
50
51
        $this->types = new Types($this->entityManager, $customTypes);
52
    }
53
54
    private function assertType(string $expectedFile, Type $type): void
55
    {
56
        $actual = SchemaPrinter::printType($type) . PHP_EOL;
57
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
58
    }
59
60
    private function assertAllTypes(string $expectedFile, Type $type): void
61
    {
62
        $schema = $this->getSchemaForType($type);
63
        $actual = SchemaPrinter::doPrint($schema);
64
65
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
66
    }
67
68
    /**
69
     * Create a temporary schema for the given type
70
     *
71
     * @param Type $type
72
     *
73
     * @return Schema
74
     */
75
    private function getSchemaForType(Type $type): Schema
76
    {
77
        if ($type instanceof OutputType) {
78
            $config = [
79
                'query' => new ObjectType([
80
                    'name' => 'query',
81
                    'fields' => [
82
                        'defaultField' => $type,
83
                    ],
84
                ]),
85
            ];
86
        } else {
87
            $config = [
88
                'query' => new ObjectType([
89
                    'name' => 'query',
90
                ]),
91
                'mutation' => new ObjectType([
92
                    'name' => 'mutation',
93
                    'fields' => [
94
                        'defaultField' => $type,
95
                    ],
96
                ]),
97
            ];
98
        }
99
100
        return new Schema($config);
101
    }
102
}
103