|
8 | 8 | use GraphQL\Type\Definition\ObjectType;
|
9 | 9 | use GraphQL\Type\Definition\Type;
|
10 | 10 | use GraphQL\Type\Schema;
|
| 11 | +use GraphQL\Utils\Value; |
11 | 12 | use PHPUnit\Framework\TestCase;
|
12 | 13 |
|
13 | 14 | class OneOfInputObjectTest extends TestCase
|
@@ -198,4 +199,39 @@ public function testOneOfIntrospection(): void
|
198 | 199 | self::assertTrue($oneOfType['isOneOf']);
|
199 | 200 | self::assertFalse($regularType['isOneOf']); // Should be false for regular input objects
|
200 | 201 | }
|
| 202 | + |
| 203 | + public function testOneOfCoercionValidation(): void |
| 204 | + { |
| 205 | + $oneOfType = new InputObjectType([ |
| 206 | + 'name' => 'OneOfInput', |
| 207 | + 'fields' => [ |
| 208 | + 'stringField' => Type::string(), |
| 209 | + 'intField' => Type::int(), |
| 210 | + ], |
| 211 | + 'isOneOf' => true, |
| 212 | + ]); |
| 213 | + |
| 214 | + // Test valid input (exactly one field) |
| 215 | + $validResult = Value::coerceInputValue(['stringField' => 'test'], $oneOfType); |
| 216 | + $this->assertNull($validResult['errors']); |
| 217 | + $this->assertEquals(['stringField' => 'test'], $validResult['value']); |
| 218 | + |
| 219 | + // Test invalid input (no fields) |
| 220 | + $noFieldsResult = Value::coerceInputValue([], $oneOfType); |
| 221 | + $this->assertNotNull($noFieldsResult['errors']); |
| 222 | + $this->assertCount(1, $noFieldsResult['errors']); |
| 223 | + $this->assertEquals('OneOf input object "OneOfInput" must specify exactly one field.', $noFieldsResult['errors'][0]->getMessage()); |
| 224 | + |
| 225 | + // Test invalid input (multiple fields) |
| 226 | + $multipleFieldsResult = Value::coerceInputValue(['stringField' => 'test', 'intField' => 42], $oneOfType); |
| 227 | + $this->assertNotNull($multipleFieldsResult['errors']); |
| 228 | + $this->assertCount(1, $multipleFieldsResult['errors']); |
| 229 | + $this->assertEquals('OneOf input object "OneOfInput" must specify exactly one field.', $multipleFieldsResult['errors'][0]->getMessage()); |
| 230 | + |
| 231 | + // Test invalid input (null field value) |
| 232 | + $nullFieldResult = Value::coerceInputValue(['stringField' => null], $oneOfType); |
| 233 | + $this->assertNotNull($nullFieldResult['errors']); |
| 234 | + $this->assertCount(1, $nullFieldResult['errors']); |
| 235 | + $this->assertEquals('OneOf input object "OneOfInput" field "stringField" must be non-null.', $nullFieldResult['errors'][0]->getMessage()); |
| 236 | + } |
201 | 237 | }
|
0 commit comments