Completed
Push — master ( 6fd728...1b0e9e )
by David
14s queued 11s
created

SchemaFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 47
dl 0
loc 100
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testException2() 0 10 1
A doTestSchema() 0 36 1
A testException() 0 9 1
A testSetters() 0 20 1
A testCreateSchema() 0 14 1
1
<?php
2
3
namespace TheCodingMachine\GraphQL\Controllers;
4
5
use GraphQL\Error\Debug;
6
use GraphQL\GraphQL;
7
use GraphQL\Type\SchemaConfig;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Cache\Simple\PhpFilesCache;
10
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
11
use TheCodingMachine\GraphQL\Controllers\Containers\EmptyContainer;
12
use TheCodingMachine\GraphQL\Controllers\Hydrators\FactoryHydrator;
13
use TheCodingMachine\GraphQL\Controllers\Mappers\CompositeTypeMapper;
14
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthenticationService;
15
use TheCodingMachine\GraphQL\Controllers\Security\VoidAuthorizationService;
16
17
class SchemaFactoryTest extends TestCase
18
{
19
20
    public function testCreateSchema(): void
21
    {
22
        $container = new BasicAutoWiringContainer(new EmptyContainer());
23
        $cache = new PhpFilesCache();
24
25
        $factory = new SchemaFactory($cache, $container);
26
27
        $factory->addControllerNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers');
28
        $factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration');
29
        $factory->addQueryProvider(new AggregateQueryProvider([]));
30
31
        $schema = $factory->createSchema();
32
33
        $this->doTestSchema($schema);
34
    }
35
36
    public function testSetters(): void
37
    {
38
        $container = new BasicAutoWiringContainer(new EmptyContainer());
39
        $cache = new PhpFilesCache();
40
41
        $factory = new SchemaFactory($cache, $container);
42
43
        $factory->addControllerNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration\\Controllers');
44
        $factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration');
45
        $factory->setDoctrineAnnotationReader(new \Doctrine\Common\Annotations\AnnotationReader())
46
                ->setHydrator(new FactoryHydrator())
47
                ->setAuthenticationService(new VoidAuthenticationService())
48
                ->setAuthorizationService(new VoidAuthorizationService())
49
                ->setNamingStrategy(new NamingStrategy())
50
                ->addTypeMapper(new CompositeTypeMapper([]))
51
                ->setSchemaConfig(new SchemaConfig());
52
53
        $schema = $factory->createSchema();
54
55
        $this->doTestSchema($schema);
56
    }
57
58
    public function testException(): void
59
    {
60
        $container = new BasicAutoWiringContainer(new EmptyContainer());
61
        $cache = new PhpFilesCache();
62
63
        $factory = new SchemaFactory($cache, $container);
64
65
        $this->expectException(GraphQLException::class);
66
        $factory->createSchema();
67
    }
68
69
    public function testException2(): void
70
    {
71
        $container = new BasicAutoWiringContainer(new EmptyContainer());
72
        $cache = new PhpFilesCache();
73
74
        $factory = new SchemaFactory($cache, $container);
75
        $factory->addTypeNamespace('TheCodingMachine\\GraphQL\\Controllers\\Fixtures\\Integration');
76
77
        $this->expectException(GraphQLException::class);
78
        $factory->createSchema();
79
    }
80
81
    private function doTestSchema(Schema $schema): void
82
    {
83
84
        $schema->assertValid();
85
86
        $queryString = '
87
        query {
88
            contacts {
89
                name
90
                uppercaseName
91
                ... on User {
92
                    email
93
                }
94
            }
95
        }
96
        ';
97
98
        $result = GraphQL::executeQuery(
99
            $schema,
100
            $queryString
101
        );
102
103
        $this->assertSame([
104
            'contacts' => [
105
                [
106
                    'name' => 'Joe',
107
                    'uppercaseName' => 'JOE'
108
                ],
109
                [
110
                    'name' => 'Bill',
111
                    'uppercaseName' => 'BILL',
112
                    'email' => '[email protected]'
113
                ]
114
115
            ]
116
        ], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);
117
    }
118
}
119