Skip to content

Commit

Permalink
Added optional directive
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Dec 1, 2021
1 parent 9c3ddf2 commit 0ee8305
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/OptionalDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes;

final class OptionalDirective extends \Graphpinator\Typesystem\Directive implements
\Graphpinator\Typesystem\Location\ArgumentDefinitionLocation
{
protected const NAME = 'optional';
protected const DESCRIPTION = 'Input value for this argument can be either omitted or have non-null value.';

public static function isPure() : bool
{
return true;
}

public function validateArgumentUsage(
\Graphpinator\Typesystem\Argument\Argument $argument,
\Graphpinator\Value\ArgumentValueSet $arguments,
) : bool
{
return true;
}

public function validateVariance(
?\Graphpinator\Value\ArgumentValueSet $biggerSet,
?\Graphpinator\Value\ArgumentValueSet $smallerSet,
) : void
{
if ($biggerSet instanceof \Graphpinator\Value\ArgumentValueSet &&
$smallerSet instanceof \Graphpinator\Value\ArgumentValueSet &&
$biggerSet->isSame($smallerSet)) {
return;
}

throw new \Exception();
}

public function resolveArgumentDefinition(
\Graphpinator\Value\ArgumentValueSet $arguments,
\Graphpinator\Value\ArgumentValue $argumentValue,
) : void
{
if ($argumentValue->getValue() instanceof \Graphpinator\Value\NullInputedValue) {
throw new class extends \Graphpinator\Exception\GraphpinatorBase {
public const MESSAGE = 'Input field is @optional and therefore cannot contain null value.';

public function isOutputable(): bool
{
return true;
}
};
}
}

protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet
{
return new \Graphpinator\Typesystem\Argument\ArgumentSet();
}
}
56 changes: 56 additions & 0 deletions tests/Unit/OptionalDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\ExtraTypes\Tests\Unit;

final class OptionalDirectiveTest extends \PHPUnit\Framework\TestCase
{
public function testValue() : void
{
$value = self::getInput()->accept(
new \Graphpinator\Value\ConvertRawValueVisitor((object) ['arg1' => 'Value'], new \Graphpinator\Common\Path()),
);
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([]));

self::assertInstanceOf(\Graphpinator\Value\InputValue::class, $value);
}

public function testOmitted() : void
{
$value = self::getInput()->accept(
new \Graphpinator\Value\ConvertRawValueVisitor((object) [], new \Graphpinator\Common\Path()),
);
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([]));

self::assertInstanceOf(\Graphpinator\Value\InputValue::class, $value);
}

public function testInvalid() : void
{
$this->expectException(\Graphpinator\Exception\GraphpinatorBase::class);
$this->expectExceptionMessage('Input field is @optional and therefore cannot contain null value.');

$value = self::getInput()->accept(
new \Graphpinator\Value\ConvertRawValueVisitor((object) ['arg1' => null], new \Graphpinator\Common\Path()),
);
$value->applyVariables(new \Graphpinator\Normalizer\VariableValueSet([]));
}

private static function getInput() : \Graphpinator\Typesystem\InputType
{
return new class extends \Graphpinator\Typesystem\InputType {
protected const NAME = 'ConstraintInput';

protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet
{
return new \Graphpinator\Typesystem\Argument\ArgumentSet([
\Graphpinator\Typesystem\Argument\Argument::create(
'arg1',
\Graphpinator\Typesystem\Container::String(),
)->addDirective(new \Graphpinator\ExtraTypes\OptionalDirective()),
]);
}
};
}
}

0 comments on commit 0ee8305

Please sign in to comment.