AstTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 12
dl 0
loc 183
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testArgument() 0 13 1
A testField() 0 17 1
A testFragment() 0 28 1
A testFragmentReference() 0 9 1
A testTypedFragmentReference() 0 22 1
A testQuery() 0 33 1
A testArgumentValues() 0 17 1
A testVariable() 0 24 1
A testVariableLogicException() 0 5 1
1
<?php
2
/**
3
 * Date: 13.05.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\Tests\Parser;
9
10
11
use Youshido\GraphQL\Parser\Ast\Argument;
12
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputList;
13
use Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject;
14
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Literal;
15
use Youshido\GraphQL\Parser\Ast\ArgumentValue\Variable;
16
use Youshido\GraphQL\Parser\Ast\Field;
17
use Youshido\GraphQL\Parser\Ast\Fragment;
18
use Youshido\GraphQL\Parser\Ast\FragmentReference;
19
use Youshido\GraphQL\Parser\Ast\Query;
20
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
21
use Youshido\GraphQL\Parser\Location;
22
23
class AstTest extends \PHPUnit_Framework_TestCase
24
{
25
26
    public function testArgument()
27
    {
28
        $argument = new Argument('test', new Literal('test', new Location(1,1)), new Location(1,1));
29
30
        $this->assertNotNull($argument->getValue());
31
        $this->assertEquals($argument->getName(), 'test');
32
33
        $argument->setName('test2');
34
        $argument->setValue('some value');
35
36
        $this->assertEquals($argument->getName(), 'test2');
37
        $this->assertEquals($argument->getValue(), 'some value');
38
    }
39
40
    public function testField()
41
    {
42
        $field = new Field('field', null, [], [], new Location(1,1));
43
44
        $this->assertEquals($field->getName(), 'field');
45
        $this->assertEmpty($field->getArguments());
46
        $this->assertFalse($field->hasArguments());
47
48
        $field->setAlias('alias');
49
        $field->setName('alias');
50
        $this->assertEquals($field->getAlias(), 'alias');
51
        $this->assertEquals($field->getName(), 'alias');
52
53
        $field->addArgument(new Argument('argument', new Literal('argument value', new Location(1,1)), new Location(1,1)));
54
        $this->assertTrue($field->hasArguments());
55
        $this->assertEquals(['argument' => 'argument value'], $field->getKeyValueArguments());
56
    }
57
58
    public function testFragment()
59
    {
60
        $fields = [
61
            new Field('field', null, [], [], new Location(1,1))
62
        ];
63
64
        $fragment = new Fragment('shipInfo', 'Ship', [], $fields, new Location(1,1));
65
66
        $this->assertEquals('shipInfo', $fragment->getName());
67
        $this->assertEquals('Ship', $fragment->getModel());
68
        $this->assertEquals($fields, $fragment->getFields());
69
70
        $fragment->setName('largeShipInfo');
71
        $this->assertEquals('largeShipInfo', $fragment->getName());
72
73
        $fragment->setModel('Boat');
74
        $this->assertEquals('Boat', $fragment->getModel());
75
76
        $newField = [
77
            new Field('id', null, [], [], new Location(1,1))
78
        ];
79
        $fragment->setFields($newField);
80
        $this->assertEquals($newField, $fragment->getFields());
81
82
        $this->assertFalse($fragment->isUsed());
83
        $fragment->setUsed(true);
84
        $this->assertTrue($fragment->isUsed());
85
    }
86
87
    public function testFragmentReference()
88
    {
89
        $reference = new FragmentReference('shipInfo', new Location(1,1));
90
91
        $this->assertEquals('shipInfo', $reference->getName());
92
93
        $reference->setName('largeShipInfo');
94
        $this->assertEquals('largeShipInfo', $reference->getName());
95
    }
96
97
    public function testTypedFragmentReference()
98
    {
99
        $fields = [
100
            new Field('id', null, [], [], new Location(1,1))
101
        ];
102
103
        $reference = new TypedFragmentReference('Ship', $fields, [], new Location(1,1));
104
105
        $this->assertEquals('Ship', $reference->getTypeName());
106
        $this->assertEquals($fields, $reference->getFields());
107
108
        $reference->setTypeName('BigBoat');
109
        $this->assertEquals('BigBoat', $reference->getTypeName());
110
111
        $newFields = [
112
            new Field('name', null, [], [], new Location(1,1)),
113
            new Field('id', null, [], [], new Location(1,1))
114
        ];
115
116
        $reference->setFields($newFields);
117
        $this->assertEquals($newFields, $reference->getFields());
118
    }
119
120
    public function testQuery()
121
    {
122
        $arguments = [
123
            new Argument('limit', new Literal('10', new Location(1,1)), new Location(1,1))
124
        ];
125
126
        $fields = [
127
            new Field('id', null, [], [], new Location(1,1))
128
        ];
129
130
        $query = new Query('ships', 'lastShips', $arguments, $fields,[], new Location(1,1));
131
132
        $this->assertEquals('ships', $query->getName());
133
        $this->assertEquals('lastShips', $query->getAlias());
134
        $this->assertEquals(['limit' => $arguments[0]], $query->getArguments());
135
        $this->assertEquals(['limit' => '10'], $query->getKeyValueArguments());
136
        $this->assertEquals($fields, $query->getFields());
137
        $this->assertTrue($query->hasArguments());
138
        $this->assertTrue($query->hasFields());
139
140
        $query->setFields([]);
141
        $query->setArguments([]);
142
143
        $this->assertEmpty($query->getArguments());
144
        $this->assertEmpty($query->getFields());
145
        $this->assertEmpty($query->getKeyValueArguments());
146
147
        $this->assertFalse($query->hasArguments());
148
        $this->assertFalse($query->hasFields());
149
150
        $query->addArgument(new Argument('offset', new Literal(10, new Location(1,1)), new Location(1,1)));
151
        $this->assertTrue($query->hasArguments());
152
    }
153
154
    public function testArgumentValues()
155
    {
156
        $list = new InputList(['a', 'b'], new Location(1,1));
157
        $this->assertEquals(['a', 'b'], $list->getValue());
158
        $list->setValue(['a']);
159
        $this->assertEquals(['a'], $list->getValue());
160
161
        $inputObject = new InputObject(['a', 'b'], new Location(1,1));
162
        $this->assertEquals(['a', 'b'], $inputObject->getValue());
163
        $inputObject->setValue(['a']);
164
        $this->assertEquals(['a'], $inputObject->getValue());
165
166
        $literal = new Literal('text', new Location(1,1));
167
        $this->assertEquals('text', $literal->getValue());
168
        $literal->setValue('new text');
169
        $this->assertEquals('new text', $literal->getValue());
170
    }
171
172
    public function testVariable()
173
    {
174
        $variable = new Variable('id', 'int', false, false, true, new Location(1,1));
175
176
        $this->assertEquals('id', $variable->getName());
177
        $this->assertEquals('int', $variable->getTypeName());
178
        $this->assertFalse($variable->isNullable());
179
        $this->assertFalse($variable->isArray());
180
181
        $variable->setTypeName('string');
182
        $this->assertEquals('string', $variable->getTypeName());
183
184
        $variable->setName('limit');
185
        $this->assertEquals('limit', $variable->getName());
186
187
        $variable->setIsArray(true);
188
        $variable->setNullable(true);
189
190
        $this->assertTrue($variable->isNullable());
191
        $this->assertTrue($variable->isArray());
192
193
        $variable->setValue(new Literal('text', new Location(1,1)));
194
        $this->assertEquals(new Literal('text', new Location(1,1)), $variable->getValue());
195
    }
196
197
    /**
198
     * @expectedException \LogicException
199
     */
200
    public function testVariableLogicException()
201
    {
202
        $variable = new Variable('id', 'int', false, false, true, new Location(1,1));
203
        $variable->getValue();
204
    }
205
}
206