|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Graphpinator\Tests\Feature; |
| 6 | + |
| 7 | +final class OneOfDirectiveTest extends \PHPUnit\Framework\TestCase |
| 8 | +{ |
| 9 | + public static function createInputWithDefaults() : \Graphpinator\Typesystem\InputType |
| 10 | + { |
| 11 | + return new class extends \Graphpinator\Typesystem\InputType { |
| 12 | + protected const NAME = 'Foo'; |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + parent::__construct(); |
| 17 | + |
| 18 | + $this->addDirective(\Graphpinator\Typesystem\Container::directiveOneOf()); |
| 19 | + } |
| 20 | + |
| 21 | + protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet |
| 22 | + { |
| 23 | + return new \Graphpinator\Typesystem\Argument\ArgumentSet([ |
| 24 | + \Graphpinator\Typesystem\Argument\Argument::create('one', \Graphpinator\Typesystem\Container::Int()), |
| 25 | + \Graphpinator\Typesystem\Argument\Argument::create('two', \Graphpinator\Typesystem\Container::Int()) |
| 26 | + ->setDefaultValue(1), |
| 27 | + ]); |
| 28 | + } |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + public static function createInputWithNotNull() : \Graphpinator\Typesystem\InputType |
| 33 | + { |
| 34 | + return new class extends \Graphpinator\Typesystem\InputType { |
| 35 | + protected const NAME = 'Bar'; |
| 36 | + |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + parent::__construct(); |
| 40 | + |
| 41 | + $this->addDirective(\Graphpinator\Typesystem\Container::directiveOneOf()); |
| 42 | + } |
| 43 | + |
| 44 | + protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet |
| 45 | + { |
| 46 | + return new \Graphpinator\Typesystem\Argument\ArgumentSet([ |
| 47 | + \Graphpinator\Typesystem\Argument\Argument::create('one', \Graphpinator\Typesystem\Container::Int()), |
| 48 | + \Graphpinator\Typesystem\Argument\Argument::create('two', \Graphpinator\Typesystem\Container::Int()->notNull()), |
| 49 | + ]); |
| 50 | + } |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + public static function createInput() : \Graphpinator\Typesystem\InputType |
| 55 | + { |
| 56 | + return new class extends \Graphpinator\Typesystem\InputType { |
| 57 | + protected const NAME = 'Baz'; |
| 58 | + |
| 59 | + public function __construct() |
| 60 | + { |
| 61 | + parent::__construct(); |
| 62 | + |
| 63 | + $this->addDirective(\Graphpinator\Typesystem\Container::directiveOneOf()); |
| 64 | + } |
| 65 | + |
| 66 | + protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet |
| 67 | + { |
| 68 | + return new \Graphpinator\Typesystem\Argument\ArgumentSet([ |
| 69 | + \Graphpinator\Typesystem\Argument\Argument::create('one', \Graphpinator\Typesystem\Container::Int()), |
| 70 | + \Graphpinator\Typesystem\Argument\Argument::create('two', \Graphpinator\Typesystem\Container::Int()), |
| 71 | + ]); |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + public static function invalidValueDataProvider() : array |
| 77 | + { |
| 78 | + return [ |
| 79 | + [(object) ['one' => 1, 'two' => null], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 80 | + [(object) ['one' => null, 'two' => 1], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 81 | + [(object) ['one' => 1, 'two' => 2], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 82 | + [(object) ['one' => null, 'two' => null], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 83 | + [(object) ['one' => null], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 84 | + [(object) ['two' => null], \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied::class], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + public function testOneOfWithDefault() : void |
| 89 | + { |
| 90 | + $this->expectException(\Graphpinator\Typesystem\Exception\OneOfInputInvalidFields::class); |
| 91 | + |
| 92 | + self::createInputWithDefaults()->getArguments(); |
| 93 | + } |
| 94 | + |
| 95 | + public function testOneOfWithNotNull() : void |
| 96 | + { |
| 97 | + $this->expectException(\Graphpinator\Typesystem\Exception\OneOfInputInvalidFields::class); |
| 98 | + |
| 99 | + self::createInputWithNotNull()->getArguments(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @dataProvider invalidValueDataProvider |
| 104 | + */ |
| 105 | + public function testCreateValueInvalid(\stdClass $rawValue, string $exception) : void |
| 106 | + { |
| 107 | + $this->expectException($exception); |
| 108 | + |
| 109 | + $value = self::createInput()->accept(new \Graphpinator\Value\ConvertRawValueVisitor($rawValue, new \Graphpinator\Common\Path())); |
| 110 | + \assert($value instanceof \Graphpinator\Value\InputValue); |
| 111 | + $value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); |
| 112 | + } |
| 113 | + |
| 114 | + public function testCreateValue() : void |
| 115 | + { |
| 116 | + $rawValueOne = (object) ['one' => 1]; |
| 117 | + $rawValueTwo = (object) ['two' => 1]; |
| 118 | + |
| 119 | + $value = self::createInput()->accept(new \Graphpinator\Value\ConvertRawValueVisitor($rawValueOne, new \Graphpinator\Common\Path())); |
| 120 | + \assert($value instanceof \Graphpinator\Value\InputValue); |
| 121 | + $value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); |
| 122 | + |
| 123 | + self::assertEquals($value->getRawValue(true), $rawValueOne); |
| 124 | + self::assertNotEquals($value->getRawValue(true), $rawValueTwo); |
| 125 | + |
| 126 | + $value = self::createInput()->accept(new \Graphpinator\Value\ConvertRawValueVisitor($rawValueTwo, new \Graphpinator\Common\Path())); |
| 127 | + \assert($value instanceof \Graphpinator\Value\InputValue); |
| 128 | + $value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([])); |
| 129 | + |
| 130 | + self::assertEquals($value->getRawValue(true), $rawValueTwo); |
| 131 | + self::assertNotEquals($value->getRawValue(true), $rawValueOne); |
| 132 | + } |
| 133 | +} |
0 commit comments