Skip to content

Commit 9152dc7

Browse files
author
Václav Pelíšek
committed
OneOf directive and tests
1 parent 1d37b44 commit 9152dc7

File tree

8 files changed

+361
-189
lines changed

8 files changed

+361
-189
lines changed

src/SimpleContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct(array $types, array $directives)
4242
'include' => self::directiveInclude(),
4343
'deprecated' => self::directiveDeprecated(),
4444
'specifiedBy' => self::directiveSpecifiedBy(),
45+
'oneOf' => self::directiveOneOf(),
4546
];
4647

4748
foreach ($types as $type) {

src/Typesystem/Container.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,16 @@ public static function directiveSpecifiedBy() : \Graphpinator\Typesystem\Spec\Sp
145145

146146
return self::$builtInDirectives['specifiedBy'];
147147
}
148+
149+
/**
150+
* Built-in SpecifiedBy directive.
151+
*/
152+
public static function directiveOneOf() : \Graphpinator\Typesystem\Spec\OneOfDirective
153+
{
154+
if (!\array_key_exists('oneOf', self::$builtInDirectives)) {
155+
self::$builtInDirectives['oneOf'] = new \Graphpinator\Typesystem\Spec\OneOfDirective();
156+
}
157+
158+
return self::$builtInDirectives['oneOf'];
159+
}
148160
}

src/Typesystem/Spec/OneOfDirective.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function validateInputUsage(\Graphpinator\Typesystem\InputType $inputType
2020
throw new \Graphpinator\Typesystem\Exception\OneOfInputInvalidFields();
2121
}
2222
}
23+
24+
return true;
2325
}
2426

2527
public function resolveInputObject(ArgumentValueSet $arguments, InputValue $inputValue) : void
@@ -35,6 +37,10 @@ public function resolveInputObject(ArgumentValueSet $arguments, InputValue $inpu
3537

3638
++$currentCount;
3739
}
40+
41+
if ($currentCount !== 1) {
42+
throw new \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied();
43+
}
3844
}
3945

4046
protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet

tests/Feature/OneOfDirectiveTest.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)