AbstractDriverTest::createIdField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace Arthem\GraphQLMapper\Test\Mapping\Driver;
4
5
use Arthem\GraphQLMapper\Mapping\Field;
6
use Arthem\GraphQLMapper\Mapping\InterfaceType;
7
use Arthem\GraphQLMapper\Mapping\Query;
8
use Arthem\GraphQLMapper\Mapping\SchemaContainer;
9
use Arthem\GraphQLMapper\Mapping\Type;
10
11
abstract class AbstractDriverTest extends \PHPUnit_Framework_TestCase
12
{
13
    /**
14
     * @var SchemaContainer
15
     */
16
    static private $expectedSchema;
17
18
    /**
19
     * @param SchemaContainer $schema
20
     */
21
    protected function assertSchemaContainer(SchemaContainer $schema)
22
    {
23
        $this->assertEquals($this->getExceptedSchemaContainer(), $schema, 'Invalid schema');
24
    }
25
26
    /**
27
     * @return SchemaContainer
28
     */
29
    private function getExceptedSchemaContainer()
30
    {
31
        if (self::$expectedSchema !== null) {
32
            return self::$expectedSchema;
33
        }
34
35
        $episodeType = new Type();
36
        $episodeType
37
            ->setName('Episode')
38
            ->setDescription('One of the films in the Star Wars Trilogy')
39
            ->setValues([
40
                'NEWHOPE' => [
41
                    'value'       => 4,
42
                    'description' => 'Released in 1977.',
43
                ],
44
                'EMPIRE'  => [
45
                    'value'       => 5,
46
                    'description' => 'Released in 1980.',
47
                ],
48
                'JEDI'    => [
49
                    'value'       => 6,
50
                    'description' => 'Released in 1983.',
51
                ],
52
            ]);
53
54
        $characterInterface = new InterfaceType();
55
        $characterInterface
56
            ->setName('Character')
57
            ->setDescription('A character in the Star Wars Trilogy')
58
            ->setFields([
59
                $this->createIdField('The id of the character.'),
60
                $this->createNameField('The name of the character.'),
61
                $this->createFriendsField('The friends of the character, or an empty list if they have none.'),
62
                $this->createAppearsInField(),
63
            ]);
64
65
        $homePlanet = new Field();
66
        $homePlanet
67
            ->setName('homePlanet')
68
            ->setType('String')
69
            ->setDescription('The home planet of the human, or null if unknown.');
70
71
        $humanType = new Type();
72
        $humanType->setName('Human')
73
            ->setDescription('A humanoid creature in the Star Wars universe.')
74
            ->setInterfaces(['Character'])
75
            ->setFields([
76
                $this->createIdField('The id of the human.'),
77
                $this->createNameField('The name of the human.'),
78
                $this->createFriendsField('The friends of the human, or an empty list if they have none.'),
79
                $this->createAppearsInField(),
80
                $homePlanet
81
            ]);
82
83
        $primaryFunction = new Field();
84
        $primaryFunction
85
            ->setName('primaryFunction')
86
            ->setType('String')
87
            ->setDescription('The primary function of the droid.');
88
89
        $droidType = new Type();
90
        $droidType->setName('Droid')
91
            ->setDescription('A mechanical creature in the Star Wars universe.')
92
            ->setInterfaces(['Character'])
93
            ->setFields([
94
                $this->createIdField('The id of the droid.'),
95
                $this->createNameField('The name of the droid.'),
96
                $this->createFriendsField('The friends of the droid, or an empty list if they have none.'),
97
                $this->createAppearsInField(),
98
                $primaryFunction
99
            ]);
100
101
        $episodeField = new Field();
102
        $episodeField
103
            ->setName('episode')
104
            ->setType('Episode')
105
            ->setDescription('If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.');
106
107
        $heroField = new Field();
108
        $heroField->setName('hero')
109
            ->setType('Character')
110
            ->setArguments([
111
                $episodeField,
112
            ]);
113
114
        $humanField = new Field();
115
        $humanField->setName('human')
116
            ->setType('Human')
117
            ->setArguments([
118
                $this->createIdField('id of the human'),
119
            ]);
120
121
        $droidField = new Field();
122
        $droidField->setName('droid')
123
            ->setType('Droid')
124
            ->setArguments([
125
                $this->createIdField('id of the droid'),
126
            ]);
127
128
        $query = new Query();
129
        $query
130
            ->setFields([
131
                $heroField,
132
                $humanField,
133
                $droidField,
134
            ]);
135
136
        $schema = new SchemaContainer();
137
        $schema
138
            ->addType($episodeType)
139
            ->addType($humanType)
140
            ->addType($droidType)
141
            ->addInterface($characterInterface)
142
            ->setQuerySchema($query);
143
144
        return self::$expectedSchema = $schema;
145
    }
146
147
    /**
148
     * @param $description
149
     * @return Field
150
     */
151
    private function createIdField($description)
152
    {
153
        $field = new Field();
154
        $field
155
            ->setName('id')
156
            ->setDescription($description)
157
            ->setType('String!');
158
159
        return $field;
160
    }
161
162
    /**
163
     * @param $description
164
     * @return Field
165
     */
166
    private function createNameField($description)
167
    {
168
        $field = new Field();
169
        $field
170
            ->setName('name')
171
            ->setDescription($description)
172
            ->setType('String');
173
174
        return $field;
175
    }
176
177
    /**
178
     * @param $description
179
     * @return Field
180
     */
181
    private function createFriendsField($description)
182
    {
183
        $field = new Field();
184
        $field
185
            ->setName('friends')
186
            ->setDescription($description)
187
            ->setType('[Character]');
188
189
        return $field;
190
    }
191
192
    /**
193
     * @return Field
194
     */
195
    private function createAppearsInField()
196
    {
197
        $field = new Field();
198
        $field
199
            ->setName('appearsIn')
200
            ->setDescription('Which movies they appear in.')
201
            ->setType('[Episode]');
202
203
        return $field;
204
    }
205
}
206