Completed
Push — master ( e75494...e13501 )
by Alexandr
03:05
created

SchemaTest::testCustomTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0
cc 1
eloc 29
nc 1
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
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/12/16 10:11 PM
7
*/
8
9
namespace Youshido\Tests\Schema;
10
11
12
use Youshido\GraphQL\Execution\Processor;
13
use Youshido\GraphQL\Schema\Schema;
14
use Youshido\GraphQL\Type\NonNullType;
15
use Youshido\GraphQL\Type\Object\ObjectType;
16
use Youshido\GraphQL\Type\Scalar\IntType;
17
use Youshido\GraphQL\Type\Scalar\StringType;
18
use Youshido\Tests\DataProvider\TestEmptySchema;
19
use Youshido\Tests\DataProvider\TestObjectType;
20
use Youshido\Tests\DataProvider\TestSchema;
21
22
class SchemaTest extends \PHPUnit_Framework_TestCase
23
{
24
25
    public function testInlineSchema()
26
    {
27
        $queryType = new ObjectType([
0 ignored issues
show
Unused Code introduced by
$queryType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
            'name'   => 'RootQueryType',
29
            'fields' => [
30
                'currentTime' => [
31
                    'type'    => new StringType(),
32
                    'resolve' => function ($value, $args, $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
                        return 'May 5, 9:00am';
34
                    },
35
                    'args'    => [
36
                        'gmt' => [
37
                            'type'    => new IntType(),
38
                            'default' => -5
39
                        ],
40
                    ],
41
                ]
42
            ]
43
        ]);
44
//        $this->assertEquals('May 5, 9:00am', $queryType->getField('currentTime')->resolve([], [],));
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
    }
46
47
    public function testStandaloneEmptySchema()
48
    {
49
        $schema = new TestEmptySchema();
50
        $this->assertFalse($schema->getQueryType()->hasFields());
51
    }
52
53
    public function testStandaloneSchema()
54
    {
55
        $schema = new TestSchema();
56
        $this->assertTrue($schema->getQueryType()->hasFields());
57
        $this->assertTrue($schema->getMutationType()->hasFields());
58
59
        $this->assertEquals(1, count($schema->getMutationType()->getFields()));
60
61
        $schema->addMutationField('changeUser', ['type' => new TestObjectType(), 'resolve' => function () { }]);
62
        $this->assertEquals(2, count($schema->getMutationType()->getFields()));
63
64
    }
65
66
    public function testSchemaWithoutClosuresSerializable()
67
    {
68
        $schema = new TestEmptySchema();
69
        $schema->getQueryType()->addField('randomInt', [
70
            'type'    => new NonNullType(new IntType()),
71
            'resolve' => 'rand',
72
        ]);
73
74
        $serialized = serialize($schema);
75
        /** @var Schema $unserialized */
76
        $unserialized = unserialize($serialized);
77
78
        $this->assertTrue($unserialized->getQueryType()->hasFields());
79
        $this->assertFalse($unserialized->getMutationType()->hasFields());
80
        $this->assertEquals(1, count($unserialized->getQueryType()->getFields()));
81
    }
82
83
    public function testCustomTypes()
84
    {
85
        $authorType = null;
86
87
        $userInterface = new ObjectType([
88
            'name'        => 'UserInterface',
89
            'fields'      => [
90
                'name' => new StringType(),
91
            ],
92
            'resolveType' => function () use ($authorType) {
93
                return $authorType;
94
            }
95
        ]);
96
97
        $authorType = new ObjectType([
98
            'name'       => 'Author',
99
            'fields'     => [
100
                'name' => new StringType(),
101
            ],
102
            'interfaces' => [$userInterface]
103
        ]);
104
105
        $schema    = new Schema([
106
            'query' => new ObjectType([
107
                'name'   => 'QueryType',
108
                'fields' => [
109
                    'user' => [
110
                        'type'    => $userInterface,
111
                        'resolve' => function () {
112
                            return [
113
                                'name' => 'Alex'
114
                            ];
115
                        }
116
                    ]
117
                ]
118
            ])
119
        ]);
120
        $schema->getTypesList()->addType($authorType);
121
        $processor = new Processor($schema);
122
        $processor->processPayload('{ user { name } }');
123
        $this->assertEquals(['data' => ['user' => ['name' => 'Alex']]], $processor->getResponseData());
124
125
        $processor->processPayload('{
126
                    __schema {
127
                        types {
128
                            name
129
                        }
130
                    }
131
                }');
132
        $data = $processor->getResponseData();
133
        $this->assertArraySubset([11 => ['name' => 'Author']], $data['data']['__schema']['types']);
134
    }
135
136
}
137