Completed
Branch master (895aee)
by Arthur
04:23 queued 02:09
created

AbstractDriverTest::getExceptedSchemaContainer()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 117
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 117
rs 8.2857
cc 2
eloc 90
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    protected function assertSchemaContainer(SchemaContainer $schema)
19
    {
20
        $this->assertEquals($this->getExceptedSchemaContainer(), $schema, 'Invalid schema');
21
    }
22
23
    private function getExceptedSchemaContainer()
24
    {
25
        if (self::$expectedSchema !== null) {
26
            return self::$expectedSchema;
27
        }
28
29
        $episodeType = new Type();
30
        $episodeType
31
            ->setName('Episode')
32
            ->setDescription('One of the films in the Star Wars Trilogy')
33
            ->setValues([
34
                'NEWHOPE' => [
35
                    'value'       => 4,
36
                    'description' => 'Released in 1977.',
37
                ],
38
                'EMPIRE'  => [
39
                    'value'       => 5,
40
                    'description' => 'Released in 1980.',
41
                ],
42
                'JEDI'    => [
43
                    'value'       => 6,
44
                    'description' => 'Released in 1983.',
45
                ],
46
            ]);
47
48
        $characterInterface = new InterfaceType();
49
        $characterInterface
50
            ->setName('Character')
51
            ->setDescription('A character in the Star Wars Trilogy')
52
            ->setFields([
53
                $this->createIdField('The id of the character.'),
54
                $this->createNameField('The name of the character.'),
55
                $this->createFriendsField('The friends of the character, or an empty list if they have none.'),
56
                $this->createAppearsInField(),
57
            ]);
58
59
        $homePlanet = new Field();
60
        $homePlanet
61
            ->setName('homePlanet')
62
            ->setType('String')
63
            ->setDescription('The home planet of the human, or null if unknown.');
64
65
        $humanType = new Type();
66
        $humanType->setName('Human')
67
            ->setDescription('A humanoid creature in the Star Wars universe.')
68
            ->setExtends('Character')
69
            ->setFields([
70
                $this->createIdField('The id of the human.'),
71
                $this->createNameField('The name of the human.'),
72
                $this->createFriendsField('The friends of the human, or an empty list if they have none.'),
73
                $this->createAppearsInField(),
74
                $homePlanet
75
            ]);
76
77
        $primaryFunction = new Field();
78
        $primaryFunction
79
            ->setName('primaryFunction')
80
            ->setType('String')
81
            ->setDescription('The primary function of the droid.');
82
83
        $droidType = new Type();
84
        $droidType->setName('Droid')
85
            ->setDescription('A mechanical creature in the Star Wars universe.')
86
            ->setExtends('Character')
87
            ->setFields([
88
                $this->createIdField('The id of the droid.'),
89
                $this->createNameField('The name of the droid.'),
90
                $this->createFriendsField('The friends of the droid, or an empty list if they have none.'),
91
                $this->createAppearsInField(),
92
                $primaryFunction
93
            ]);
94
95
        $episodeField = new Field();
96
        $episodeField
97
            ->setName('episode')
98
            ->setType('Episode')
99
            ->setDescription('If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.');
100
101
        $heroField = new Field();
102
        $heroField->setName('hero')
103
            ->setType('Character')
104
            ->setArguments([
105
                $episodeField,
106
            ]);
107
108
        $humanField = new Field();
109
        $humanField->setName('human')
110
            ->setType('Human')
111
            ->setArguments([
112
                $this->createIdField('id of the human'),
113
            ]);
114
115
        $droidField = new Field();
116
        $droidField->setName('droid')
117
            ->setType('Droid')
118
            ->setArguments([
119
                $this->createIdField('id of the droid'),
120
            ]);
121
122
        $query = new Query();
123
        $query
124
            ->setFields([
125
                $heroField,
126
                $humanField,
127
                $droidField,
128
            ]);
129
130
        $schema = new SchemaContainer();
131
        $schema
132
            ->addType($episodeType)
133
            ->addType($humanType)
134
            ->addType($droidType)
135
            ->addInterface($characterInterface)
136
            ->setQuerySchema($query);
137
138
        return self::$expectedSchema = $schema;
139
    }
140
141
    private function createIdField($description)
142
    {
143
        $field = new Field();
144
        $field
145
            ->setName('id')
146
            ->setDescription($description)
147
            ->setType('String!');
148
149
        return $field;
150
    }
151
152
    private function createNameField($description)
153
    {
154
        $field = new Field();
155
        $field
156
            ->setName('name')
157
            ->setDescription($description)
158
            ->setType('String');
159
160
        return $field;
161
    }
162
163
    private function createFriendsField($description)
164
    {
165
        $field = new Field();
166
        $field
167
            ->setName('friends')
168
            ->setDescription($description)
169
            ->setType('[Character]');
170
171
        return $field;
172
    }
173
174
    private function createAppearsInField()
175
    {
176
        $field = new Field();
177
        $field
178
            ->setName('appearsIn')
179
            ->setDescription('Which movies they appear in.')
180
            ->setType('[Episode]');
181
182
        return $field;
183
    }
184
}
185