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

TypesTrait::assertAllTypes()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 2
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\Types\CustomType;
16
use GraphQLTests\Doctrine\Blog\Types\DateTimeType;
17
use GraphQLTests\Doctrine\Blog\Types\PostStatusType;
18
use stdClass;
19
use Zend\ServiceManager\ServiceManager;
20
21
/**
22
 * Trait to easily set up types and assert them
23
 */
24
trait TypesTrait
25
{
26
    use EntityManagerTrait;
27
28
    /**
29
     * @var Types
30
     */
31
    private $types;
32
33
    public function setUp(): void
34
    {
35
        $this->setUpEntityManager();
36
37
        $customTypes = new ServiceManager([
38
            'invokables' => [
39
                BooleanType::class => BooleanType::class,
40
                DateTime::class => DateTimeType::class,
41
                stdClass::class => CustomType::class,
42
                'PostStatus' => PostStatusType::class,
43
            ],
44
            'aliases' => [
45
                'datetime' => DateTime::class, // Declare alias for Doctrine type to be used for filters
46
            ],
47
        ]);
48
49
        $this->types = new Types($this->entityManager, $customTypes);
1 ignored issue
show
Bug introduced by
The property entityManager is declared private in GraphQLTests\Doctrine\EntityManagerTrait and cannot be accessed from this context.
Loading history...
50
    }
51
52
    private function assertType(string $expectedFile, Type $type): void
53
    {
54
        $actual = SchemaPrinter::printType($type) . PHP_EOL;
55
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
56
    }
57
58
    private function assertAllTypes(string $expectedFile, Type $type): void
59
    {
60
        if ($type instanceof OutputType) {
61
            $config = [
62
                'query' => new ObjectType([
63
                    'name' => 'query',
64
                    'fields' => [
65
                        'defaultField' => $type,
66
                    ],
67
                ]),
68
            ];
69
        } else {
70
            $config = [
71
                'query' => new ObjectType([
72
                    'name' => 'query',
73
                ]),
74
                'mutation' => new ObjectType([
75
                    'name' => 'mutation',
76
                    'fields' => [
77
                        'defaultField' => $type,
78
                    ],
79
                ]),
80
            ];
81
        }
82
83
        $schema = new Schema($config);
84
        $actual = SchemaPrinter::doPrint($schema);
85
86
//        echo PHP_EOL . $actual . '-------------------------------------------------------------------' . PHP_EOL;
87
88
        self::assertStringEqualsFile($expectedFile, $actual, 'Should equals expectation from: ' . $expectedFile);
89
    }
90
}
91