Failed Conditions
Push — master ( 9327e7...7f99bf )
by Vladimir
04:21
created

Root::immediatelyChangeTheNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests\Executor;
6
7
use GraphQL\Executor\Executor;
8
use GraphQL\Language\Parser;
9
use GraphQL\Tests\Executor\TestClasses\Root;
10
use GraphQL\Type\Definition\ObjectType;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Type\Schema;
13
use PHPUnit\Framework\TestCase;
14
15
class MutationsTest extends TestCase
16
{
17
    // Execute: Handles mutation execution ordering
18
    /**
19
     * @see it('evaluates mutations serially')
20
     */
21
    public function testEvaluatesMutationsSerially() : void
22
    {
23
        $doc            = 'mutation M {
24
      first: immediatelyChangeTheNumber(newNumber: 1) {
25
        theNumber
26
      },
27
      second: promiseToChangeTheNumber(newNumber: 2) {
28
        theNumber
29
      },
30
      third: immediatelyChangeTheNumber(newNumber: 3) {
31
        theNumber
32
      }
33
      fourth: promiseToChangeTheNumber(newNumber: 4) {
34
        theNumber
35
      },
36
      fifth: immediatelyChangeTheNumber(newNumber: 5) {
37
        theNumber
38
      }
39
    }';
40
        $ast            = Parser::parse($doc);
41
        $mutationResult = Executor::execute($this->schema(), $ast, new Root(6));
42
        $expected       = [
43
            'data' => [
44
                'first'  => ['theNumber' => 1],
45
                'second' => ['theNumber' => 2],
46
                'third'  => ['theNumber' => 3],
47
                'fourth' => ['theNumber' => 4],
48
                'fifth'  => ['theNumber' => 5],
49
            ],
50
        ];
51
        $this->assertEquals($expected, $mutationResult->toArray());
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on GraphQL\Executor\Promise\Promise. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->assertEquals($expected, $mutationResult->/** @scrutinizer ignore-call */ toArray());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
    }
53
54
    private function schema() : Schema
55
    {
56
        $numberHolderType = new ObjectType([
57
            'fields' => [
58
                'theNumber' => ['type' => Type::int()],
59
            ],
60
            'name'   => 'NumberHolder',
61
        ]);
62
        $schema           = new Schema([
63
            'query'    => new ObjectType([
64
                'fields' => [
65
                    'numberHolder' => ['type' => $numberHolderType],
66
                ],
67
                'name'   => 'Query',
68
            ]),
69
            'mutation' => new ObjectType([
70
                'fields' => [
71
                    'immediatelyChangeTheNumber'      => [
72
                        'type'    => $numberHolderType,
73
                        'args'    => ['newNumber' => ['type' => Type::int()]],
74
                        'resolve' => function (Root $obj, $args) {
75
                            return $obj->immediatelyChangeTheNumber($args['newNumber']);
76
                        },
77
                    ],
78
                    'promiseToChangeTheNumber'        => [
79
                        'type'    => $numberHolderType,
80
                        'args'    => ['newNumber' => ['type' => Type::int()]],
81
                        'resolve' => function (Root $obj, $args) {
82
                            return $obj->promiseToChangeTheNumber($args['newNumber']);
83
                        },
84
                    ],
85
                    'failToChangeTheNumber'           => [
86
                        'type'    => $numberHolderType,
87
                        'args'    => ['newNumber' => ['type' => Type::int()]],
88
                        'resolve' => function (Root $obj, $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
                        'resolve' => function (Root $obj, /** @scrutinizer ignore-unused */ $args) {

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

Loading history...
89
                            $obj->failToChangeTheNumber();
90
                        },
91
                    ],
92
                    'promiseAndFailToChangeTheNumber' => [
93
                        'type'    => $numberHolderType,
94
                        'args'    => ['newNumber' => ['type' => Type::int()]],
95
                        'resolve' => function (Root $obj, $args) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
                        'resolve' => function (Root $obj, /** @scrutinizer ignore-unused */ $args) {

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

Loading history...
96
                            return $obj->promiseAndFailToChangeTheNumber();
97
                        },
98
                    ],
99
                ],
100
                'name'   => 'Mutation',
101
            ]),
102
        ]);
103
104
        return $schema;
105
    }
106
107
    /**
108
     * @see it('evaluates mutations correctly in the presense of a failed mutation')
109
     */
110
    public function testEvaluatesMutationsCorrectlyInThePresenseOfAFailedMutation() : void
111
    {
112
        $doc            = 'mutation M {
113
      first: immediatelyChangeTheNumber(newNumber: 1) {
114
        theNumber
115
      },
116
      second: promiseToChangeTheNumber(newNumber: 2) {
117
        theNumber
118
      },
119
      third: failToChangeTheNumber(newNumber: 3) {
120
        theNumber
121
      }
122
      fourth: promiseToChangeTheNumber(newNumber: 4) {
123
        theNumber
124
      },
125
      fifth: immediatelyChangeTheNumber(newNumber: 5) {
126
        theNumber
127
      }
128
      sixth: promiseAndFailToChangeTheNumber(newNumber: 6) {
129
        theNumber
130
      }
131
    }';
132
        $ast            = Parser::parse($doc);
133
        $mutationResult = Executor::execute($this->schema(), $ast, new Root(6));
134
        $expected       = [
135
            'data'   => [
136
                'first'  => ['theNumber' => 1],
137
                'second' => ['theNumber' => 2],
138
                'third'  => null,
139
                'fourth' => ['theNumber' => 4],
140
                'fifth'  => ['theNumber' => 5],
141
                'sixth'  => null,
142
            ],
143
            'errors' => [
144
                [
145
                    'debugMessage' => 'Cannot change the number',
146
                    'locations'    => [['line' => 8, 'column' => 7]],
147
                ],
148
                [
149
                    'debugMessage' => 'Cannot change the number',
150
                    'locations'    => [['line' => 17, 'column' => 7]],
151
                ],
152
            ],
153
        ];
154
        $this->assertArraySubset($expected, $mutationResult->toArray(true));
155
    }
156
}
157