|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace GraphQL\Tests\Type; |
| 4 | + |
| 5 | +use GraphQL\Error\InvariantViolation; |
| 6 | +use GraphQL\GraphQL; |
| 7 | +use GraphQL\Type\Definition\InputObjectType; |
| 8 | +use GraphQL\Type\Definition\ObjectType; |
| 9 | +use GraphQL\Type\Definition\Type; |
| 10 | +use GraphQL\Type\Schema; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class OneOfInputObjectTest extends TestCase |
| 14 | +{ |
| 15 | + public function testOneOfInputObjectBasicDefinition(): void |
| 16 | + { |
| 17 | + $oneOfInput = new InputObjectType([ |
| 18 | + 'name' => 'OneOfInput', |
| 19 | + 'isOneOf' => true, |
| 20 | + 'fields' => [ |
| 21 | + 'stringField' => Type::string(), |
| 22 | + 'intField' => Type::int(), |
| 23 | + ], |
| 24 | + ]); |
| 25 | + |
| 26 | + self::assertTrue($oneOfInput->isOneOf()); |
| 27 | + self::assertCount(2, $oneOfInput->getFields()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testOneOfInputObjectValidation(): void |
| 31 | + { |
| 32 | + $oneOfInput = new InputObjectType([ |
| 33 | + 'name' => 'OneOfInput', |
| 34 | + 'isOneOf' => true, |
| 35 | + 'fields' => [ |
| 36 | + 'stringField' => Type::string(), |
| 37 | + 'intField' => Type::int(), |
| 38 | + ], |
| 39 | + ]); |
| 40 | + |
| 41 | + // Should not throw for valid oneOf input |
| 42 | + $oneOfInput->assertValid(); |
| 43 | + self::addToAssertionCount(1); // Test passes if no exception thrown |
| 44 | + } |
| 45 | + |
| 46 | + public function testOneOfInputObjectRejectsNonNullFields(): void |
| 47 | + { |
| 48 | + $this->expectException(InvariantViolation::class); |
| 49 | + $this->expectExceptionMessage('OneOf input object type OneOfInput field stringField must be nullable'); |
| 50 | + |
| 51 | + $oneOfInput = new InputObjectType([ |
| 52 | + 'name' => 'OneOfInput', |
| 53 | + 'isOneOf' => true, |
| 54 | + 'fields' => [ |
| 55 | + 'stringField' => Type::nonNull(Type::string()), // This should fail |
| 56 | + 'intField' => Type::int(), |
| 57 | + ], |
| 58 | + ]); |
| 59 | + |
| 60 | + $oneOfInput->assertValid(); |
| 61 | + } |
| 62 | + |
| 63 | + public function testOneOfInputObjectRejectsDefaultValues(): void |
| 64 | + { |
| 65 | + $this->expectException(InvariantViolation::class); |
| 66 | + $this->expectExceptionMessage('OneOf input object type OneOfInput field stringField cannot have a default value'); |
| 67 | + |
| 68 | + $oneOfInput = new InputObjectType([ |
| 69 | + 'name' => 'OneOfInput', |
| 70 | + 'isOneOf' => true, |
| 71 | + 'fields' => [ |
| 72 | + 'stringField' => [ |
| 73 | + 'type' => Type::string(), |
| 74 | + 'defaultValue' => 'default', // This should fail |
| 75 | + ], |
| 76 | + 'intField' => Type::int(), |
| 77 | + ], |
| 78 | + ]); |
| 79 | + |
| 80 | + $oneOfInput->assertValid(); |
| 81 | + } |
| 82 | + |
| 83 | + public function testOneOfInputObjectSchemaValidation(): void |
| 84 | + { |
| 85 | + $oneOfInput = new InputObjectType([ |
| 86 | + 'name' => 'OneOfInput', |
| 87 | + 'isOneOf' => true, |
| 88 | + 'fields' => [ |
| 89 | + 'stringField' => Type::string(), |
| 90 | + 'intField' => Type::int(), |
| 91 | + ], |
| 92 | + ]); |
| 93 | + |
| 94 | + $query = new ObjectType([ |
| 95 | + 'name' => 'Query', |
| 96 | + 'fields' => [ |
| 97 | + 'test' => [ |
| 98 | + 'type' => Type::string(), |
| 99 | + 'args' => [ |
| 100 | + 'input' => $oneOfInput, |
| 101 | + ], |
| 102 | + 'resolve' => static fn ($source, $args) => 'test', |
| 103 | + ], |
| 104 | + ], |
| 105 | + ]); |
| 106 | + |
| 107 | + $schema = new Schema(['query' => $query]); |
| 108 | + |
| 109 | + // Valid query with exactly one field |
| 110 | + $validQuery = '{ test(input: { stringField: "hello" }) }'; |
| 111 | + $result = GraphQL::executeQuery($schema, $validQuery); |
| 112 | + self::assertEmpty($result->errors ?? []); |
| 113 | + |
| 114 | + // Invalid query with multiple fields |
| 115 | + $invalidQuery = '{ test(input: { stringField: "hello", intField: 42 }) }'; |
| 116 | + $result = GraphQL::executeQuery($schema, $invalidQuery); |
| 117 | + self::assertNotEmpty($result->errors ?? []); |
| 118 | + self::assertCount(1, $result->errors); |
| 119 | + self::assertStringContainsString('must specify exactly one field', $result->errors[0]->getMessage()); |
| 120 | + |
| 121 | + // Invalid query with no fields |
| 122 | + $emptyQuery = '{ test(input: {}) }'; |
| 123 | + $result = GraphQL::executeQuery($schema, $emptyQuery); |
| 124 | + self::assertNotEmpty($result->errors ?? []); |
| 125 | + self::assertCount(1, $result->errors); |
| 126 | + self::assertStringContainsString('must specify exactly one field', $result->errors[0]->getMessage()); |
| 127 | + } |
| 128 | + |
| 129 | + public function testOneOfIntrospection(): void |
| 130 | + { |
| 131 | + $oneOfInput = new InputObjectType([ |
| 132 | + 'name' => 'OneOfInput', |
| 133 | + 'isOneOf' => true, |
| 134 | + 'fields' => [ |
| 135 | + 'stringField' => Type::string(), |
| 136 | + 'intField' => Type::int(), |
| 137 | + ], |
| 138 | + ]); |
| 139 | + |
| 140 | + $regularInput = new InputObjectType([ |
| 141 | + 'name' => 'RegularInput', |
| 142 | + 'fields' => [ |
| 143 | + 'stringField' => Type::string(), |
| 144 | + 'intField' => Type::int(), |
| 145 | + ], |
| 146 | + ]); |
| 147 | + |
| 148 | + $query = new ObjectType([ |
| 149 | + 'name' => 'Query', |
| 150 | + 'fields' => [ |
| 151 | + 'test' => [ |
| 152 | + 'type' => Type::string(), |
| 153 | + 'resolve' => static fn () => 'test', |
| 154 | + ], |
| 155 | + ], |
| 156 | + ]); |
| 157 | + |
| 158 | + $schema = new Schema([ |
| 159 | + 'query' => $query, |
| 160 | + 'types' => [$oneOfInput, $regularInput], |
| 161 | + ]); |
| 162 | + |
| 163 | + $introspectionQuery = ' |
| 164 | + { |
| 165 | + __schema { |
| 166 | + types { |
| 167 | + name |
| 168 | + isOneOf |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + '; |
| 173 | + |
| 174 | + $result = GraphQL::executeQuery($schema, $introspectionQuery); |
| 175 | + self::assertEmpty($result->errors ?? []); |
| 176 | + |
| 177 | + $types = $result->data['__schema']['types'] ?? []; |
| 178 | + $oneOfType = null; |
| 179 | + $regularType = null; |
| 180 | + |
| 181 | + foreach ($types as $type) { |
| 182 | + if ($type['name'] === 'OneOfInput') { |
| 183 | + $oneOfType = $type; |
| 184 | + } elseif ($type['name'] === 'RegularInput') { |
| 185 | + $regularType = $type; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + self::assertNotNull($oneOfType); |
| 190 | + self::assertNotNull($regularType); |
| 191 | + self::assertTrue($oneOfType['isOneOf']); |
| 192 | + self::assertFalse($regularType['isOneOf']); // Should be false for regular input objects |
| 193 | + } |
| 194 | +} |
0 commit comments