|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Overblog\GraphBundle\Tests\Functional\Relay\Mutation; |
| 4 | + |
| 5 | + |
| 6 | +use Overblog\GraphBundle\Tests\Functional\TestCase; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class MutationTest |
| 10 | + * @see https://github.com/graphql/graphql-relay-js/blob/master/src/mutation/__tests__/mutation.js |
| 11 | + */ |
| 12 | +class MutationTest extends TestCase |
| 13 | +{ |
| 14 | + protected function setUp() |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + |
| 18 | + static::$kernel = static::createKernel(['test_case' => 'mutation']); |
| 19 | + static::$kernel->boot(); |
| 20 | + } |
| 21 | + |
| 22 | + public function testRequiresAnArgument() |
| 23 | + { |
| 24 | + $query = <<<EOF |
| 25 | +mutation M { |
| 26 | + simpleMutation { |
| 27 | + result |
| 28 | + } |
| 29 | +} |
| 30 | +EOF; |
| 31 | + $result = $this->executeGraphQlRequest($query); |
| 32 | + |
| 33 | + $this->assertCount(1, $result['errors']); |
| 34 | + $this->assertEquals( |
| 35 | + 'Field "simpleMutation" argument "input" of type "simpleMutationInput!" is required but not provided.', |
| 36 | + $result['errors'][0]['message'] |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function testReturnTheSameClientMutationId() |
| 41 | + { |
| 42 | + $query = <<<EOF |
| 43 | +mutation M { |
| 44 | + simpleMutation(input: {clientMutationId: "abc"}) { |
| 45 | + result |
| 46 | + clientMutationId |
| 47 | + } |
| 48 | +} |
| 49 | +EOF; |
| 50 | + |
| 51 | + $expectedData = [ |
| 52 | + 'simpleMutation' => [ |
| 53 | + 'result' => 1, |
| 54 | + 'clientMutationId' => 'abc' |
| 55 | + ], |
| 56 | + ]; |
| 57 | + |
| 58 | + $this->assertGraphQl($query, $expectedData); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public function testSupportsThunksAsInputAndOutputFields() |
| 63 | + { |
| 64 | + $query = <<<EOF |
| 65 | +mutation M { |
| 66 | + simpleMutationWithThunkFields(input: {inputData: 1234, clientMutationId: "abc"}) { |
| 67 | + result |
| 68 | + clientMutationId |
| 69 | + } |
| 70 | +} |
| 71 | +EOF; |
| 72 | + $expectedData = [ |
| 73 | + 'simpleMutationWithThunkFields' => [ |
| 74 | + 'result' => 1234, |
| 75 | + 'clientMutationId' => 'abc' |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + $this->assertGraphQl($query, $expectedData); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public function testContainsCorrectInput() |
| 84 | + { |
| 85 | + $query = <<<EOF |
| 86 | +{ |
| 87 | + __type(name: "simpleMutationInput") { |
| 88 | + name |
| 89 | + kind |
| 90 | + inputFields { |
| 91 | + name |
| 92 | + type { |
| 93 | + name |
| 94 | + kind |
| 95 | + ofType { |
| 96 | + name |
| 97 | + kind |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | +EOF; |
| 104 | + $expectedData = [ |
| 105 | + '__type' => [ |
| 106 | + 'name' => 'simpleMutationInput', |
| 107 | + 'kind' => 'INPUT_OBJECT', |
| 108 | + 'inputFields' => [ |
| 109 | + [ |
| 110 | + 'name' => 'clientMutationId', |
| 111 | + 'type' => [ |
| 112 | + 'name' => null, |
| 113 | + 'kind' => 'NON_NULL', |
| 114 | + 'ofType' => [ |
| 115 | + 'name' => 'String', |
| 116 | + 'kind' => 'SCALAR' |
| 117 | + ] |
| 118 | + ] |
| 119 | + ] |
| 120 | + ] |
| 121 | + ] |
| 122 | + ]; |
| 123 | + |
| 124 | + $this->assertGraphQl($query, $expectedData); |
| 125 | + } |
| 126 | + |
| 127 | + public function testContainsCorrectPayload() |
| 128 | + { |
| 129 | + $query = <<<EOF |
| 130 | +{ |
| 131 | + __type(name: "simpleMutationPayload") { |
| 132 | + name |
| 133 | + kind |
| 134 | + fields { |
| 135 | + name |
| 136 | + type { |
| 137 | + name |
| 138 | + kind |
| 139 | + ofType { |
| 140 | + name |
| 141 | + kind |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +EOF; |
| 148 | + |
| 149 | + $expectedData = [ |
| 150 | + '__type' => [ |
| 151 | + 'name' => 'simpleMutationPayload', |
| 152 | + 'kind' => 'OBJECT', |
| 153 | + 'fields' => [ |
| 154 | + [ |
| 155 | + 'name' => 'result', |
| 156 | + 'type' => [ |
| 157 | + 'name' => 'Int', |
| 158 | + 'kind' => 'SCALAR', |
| 159 | + 'ofType' => null |
| 160 | + ] |
| 161 | + ], |
| 162 | + [ |
| 163 | + 'name' => 'clientMutationId', |
| 164 | + 'type' => [ |
| 165 | + 'name' => null, |
| 166 | + 'kind' => 'NON_NULL', |
| 167 | + 'ofType' => [ |
| 168 | + 'name' => 'String', |
| 169 | + 'kind' => 'SCALAR' |
| 170 | + ] |
| 171 | + ] |
| 172 | + ] |
| 173 | + |
| 174 | + ] |
| 175 | + ] |
| 176 | + ]; |
| 177 | + |
| 178 | + $this->assertGraphQl($query, $expectedData); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + public function testContainsCorrectField() |
| 183 | + { |
| 184 | + $query = <<<EOF |
| 185 | +{ |
| 186 | + __schema { |
| 187 | + mutationType { |
| 188 | + fields { |
| 189 | + name |
| 190 | + args { |
| 191 | + name |
| 192 | + type { |
| 193 | + name |
| 194 | + kind |
| 195 | + ofType { |
| 196 | + name |
| 197 | + kind |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + type { |
| 202 | + name |
| 203 | + kind |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | +EOF; |
| 210 | + |
| 211 | + $expectedData = [ |
| 212 | + '__schema' => [ |
| 213 | + 'mutationType' => [ |
| 214 | + 'fields' => [ |
| 215 | + [ |
| 216 | + 'name' => 'simpleMutation', |
| 217 | + 'args' => [ |
| 218 | + [ |
| 219 | + 'name' => 'input', |
| 220 | + 'type' => [ |
| 221 | + 'name' => null, |
| 222 | + 'kind' => 'NON_NULL', |
| 223 | + 'ofType' => [ |
| 224 | + 'name' => 'simpleMutationInput', |
| 225 | + 'kind' => 'INPUT_OBJECT' |
| 226 | + ] |
| 227 | + ] |
| 228 | + ] |
| 229 | + ], |
| 230 | + 'type' => [ |
| 231 | + 'name' => 'simpleMutationPayload', |
| 232 | + 'kind' => 'OBJECT' |
| 233 | + ] |
| 234 | + ], |
| 235 | + [ |
| 236 | + 'name' => 'simpleMutationWithThunkFields', |
| 237 | + 'args' => [ |
| 238 | + [ |
| 239 | + 'name' => 'input', |
| 240 | + 'type' => [ |
| 241 | + 'name' => null, |
| 242 | + 'kind' => 'NON_NULL', |
| 243 | + 'ofType' => [ |
| 244 | + 'name' => 'simpleMutationWithThunkFieldsInput', |
| 245 | + 'kind' => 'INPUT_OBJECT' |
| 246 | + ] |
| 247 | + ] |
| 248 | + ] |
| 249 | + ], |
| 250 | + 'type' => [ |
| 251 | + 'name' => 'simpleMutationWithThunkFieldsPayload', |
| 252 | + 'kind' => 'OBJECT' |
| 253 | + ] |
| 254 | + ] |
| 255 | + ] |
| 256 | + ] |
| 257 | + ] |
| 258 | + ]; |
| 259 | + |
| 260 | + $this->assertGraphQl($query, $expectedData); |
| 261 | + } |
| 262 | +} |
0 commit comments