Completed
Pull Request — master (#204)
by Ryan
11:34
created

SchemaValidatorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 53.1 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 9
dl 60
loc 113
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 5/15/16 4:04 PM
18
 */
19
20
namespace Youshido\Tests\Library\Validator;
21
22
use Youshido\GraphQL\Schema\Schema;
23
use Youshido\GraphQL\Type\NonNullType;
24
use Youshido\GraphQL\Type\Object\ObjectType;
25
use Youshido\GraphQL\Type\Scalar\IntType;
26
use Youshido\GraphQL\Type\Scalar\StringType;
27
use Youshido\GraphQL\Validator\SchemaValidator\SchemaValidator;
28
use Youshido\Tests\DataProvider\TestEmptySchema;
29
use Youshido\Tests\DataProvider\TestInterfaceType;
30
31
class SchemaValidatorTest extends \PHPUnit_Framework_TestCase
32
{
33
    public function testInvalidSchema(): void
34
    {
35
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
36
37
        $validator = new SchemaValidator();
38
        $validator->validate(new TestEmptySchema());
39
    }
40
41
42
    public function testInvalidInterfacesSimpleType(): void
43
    {
44
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
45
        $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name');
46
47
        $schema = new Schema([
48
            'query' => new ObjectType([
49
                'name'   => 'RootQuery',
50
                'fields' => [
51
                    'user' => new ObjectType([
52
                        'name'   => 'User',
53
                        'fields' => [
54
                            'name' => new IntType(),
55
                        ],
56
                        'interfaces' => [new TestInterfaceType()],
57
                    ]),
58
                ],
59
            ]),
60
        ]);
61
62
        $validator = new SchemaValidator();
63
        $validator->validate($schema);
64
    }
65
66
67
    public function testInvalidInterfacesCompositeType(): void
68
    {
69
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
70
        $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name');
71
72
        $schema = new Schema([
73
            'query' => new ObjectType([
74
                'name'   => 'RootQuery',
75
                'fields' => [
76
                    'user' => new ObjectType([
77
                        'name'   => 'User',
78
                        'fields' => [
79
                            'name' => new NonNullType(new StringType()),
80
                        ],
81
                        'interfaces' => [new TestInterfaceType()],
82
                    ]),
83
                ],
84
            ]),
85
        ]);
86
87
        $validator = new SchemaValidator();
88
        $validator->validate($schema);
89
    }
90
91
92
    public function testInvalidInterfaces(): void
93
    {
94
        $this->expectException(\Youshido\GraphQL\Exception\ConfigurationException::class);
95
        $this->expectExceptionMessage('Implementation of TestInterface is invalid for the field name');
96
97
        $schema = new Schema([
98
            'query' => new ObjectType([
99
                'name'   => 'RootQuery',
100
                'fields' => [
101
                    'user' => new ObjectType([
102
                        'name'   => 'User',
103
                        'fields' => [
104
                            'name' => new IntType(),
105
                        ],
106
                        'interfaces' => [new TestInterfaceType()],
107
                    ]),
108
                ],
109
            ]),
110
        ]);
111
112
        $validator = new SchemaValidator();
113
        $validator->validate($schema);
114
    }
115
116
    public function testValidSchema(): void
117
    {
118
        $schema = new Schema([
119
            'query' => new ObjectType([
120
                'name'   => 'RootQuery',
121
                'fields' => [
122
                    'user' => new ObjectType([
123
                        'name'   => 'User',
124
                        'fields' => [
125
                            'name' => new StringType(),
126
                        ],
127
                        'interfaces' => [new TestInterfaceType()],
128
                    ]),
129
                ],
130
            ]),
131
        ]);
132
133
        $validator = new SchemaValidator();
134
135
        try {
136
            $validator->validate($schema);
137
            $this->assertTrue(true);
138
        } catch (\Exception $e) {
139
            $this->assertTrue(false);
140
        }
141
    }
142
}
143