InputObjectDefaultValuesTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B testDefaultEnum() 0 80 1
1
<?php
2
3
namespace Youshido\Tests\Schema;
4
5
use Youshido\GraphQL\Execution\Processor;
6
use Youshido\GraphQL\Schema\Schema;
7
use Youshido\GraphQL\Type\Enum\EnumType;
8
use Youshido\GraphQL\Type\InputObject\InputObjectType;
9
use Youshido\GraphQL\Type\NonNullType;
10
use Youshido\GraphQL\Type\Object\ObjectType;
11
use Youshido\GraphQL\Type\Scalar\DateTimeType;
12
use Youshido\GraphQL\Type\Scalar\DateTimeTzType;
13
use Youshido\GraphQL\Type\Scalar\IntType;
14
use Youshido\GraphQL\Type\Scalar\StringType;
15
16
class InputObjectDefaultValuesTest extends \PHPUnit_Framework_TestCase
17
{
18
19
    public function testDefaultEnum()
20
    {
21
        $enumType = new EnumType([
22
            'name'   => 'InternalStatus',
23
            'values' => [
24
                [
25
                    'name'  => 'ACTIVE',
26
                    'value' => 1,
27
                ],
28
                [
29
                    'name'  => 'DISABLED',
30
                    'value' => 0,
31
                ],
32
            ]
33
        ]);
34
        $schema   = new Schema([
35
            'query' => new ObjectType([
36
                'name'   => 'RootQuery',
37
                'fields' => [
38
                    'stringQuery' => [
39
                        'type'       => new StringType(),
40
                        'args'       => [
41
                            'statObject' => new InputObjectType([
42
                                'name'   => 'StatObjectType',
43
                                'fields' => [
44
                                    'status' => [
45
                                        'type'    => $enumType,
46
                                        'defaultValue' => 1
47
                                    ],
48
                                    'level'  => new NonNullType(new IntType())
49
                                ]
50
                            ])
51
                        ],
52
                        'resolve'    => function ($source, $args) {
53
                            return sprintf('Result with level %s and status %s',
54
                                $args['statObject']['level'], $args['statObject']['status']
55
                            );
56
                        },
57
                    ],
58
                    'enumObject' => [
59
                        'type' => new ObjectType([
60
                            'name'   => 'EnumObject',
61
                            'fields' => [
62
                                'status' => $enumType
63
                            ]
64
                        ]),
65
                        'resolve' => function() {
66
                            return [
67
                                'status' => null
68
                            ];
69
                        }
70
                    ],
71
72
                ]
73
            ])
74
        ]);
75
76
        $processor = new Processor($schema);
77
        $processor->processPayload('{ stringQuery(statObject: { level: 1 }) }');
78
        $result = $processor->getResponseData();
79
        $this->assertEquals(['data' => [
80
            'stringQuery' => 'Result with level 1 and status 1'
81
        ]], $result);
82
83
        $processor->processPayload('{ stringQuery(statObject: { level: 1, status: DISABLED }) }');
84
        $result = $processor->getResponseData();
85
86
        $this->assertEquals(['data' => [
87
            'stringQuery' => 'Result with level 1 and status 0'
88
        ]], $result);
89
90
        $processor->processPayload('{ enumObject { status } }');
91
        $result = $processor->getResponseData();
92
93
        $this->assertEquals(['data' => [
94
            'enumObject' => [
95
                'status' => null
96
            ]
97
        ]], $result);
98
    }
99
100
}