Skip to content

Commit

Permalink
fixed object constraint conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Nov 18, 2020
1 parent 5dd7b71 commit a9fc323
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
27 changes: 18 additions & 9 deletions src/Constraint/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ public function validate(\Graphpinator\Value\Value $value) : void
$valid = false;

foreach ($this->atLeastOne as $fieldName) {
if (isset($value->{$fieldName}) &&
!$value->{$fieldName}->getValue() instanceof \Graphpinator\Value\NullValue) {
$valid = true;

break;
if (isset($value->{$fieldName}) && $value->{$fieldName}->getValue() instanceof \Graphpinator\Value\NullValue) {
continue;
}

$valid = true;

break;
}

if (!$valid) {
Expand All @@ -82,15 +83,23 @@ public function validate(\Graphpinator\Value\Value $value) : void
}

$count = 0;
$notRequested = 0;

foreach ($this->exactlyOne as $fieldName) {
if (isset($value->{$fieldName}) &&
!$value->{$fieldName}->getValue() instanceof \Graphpinator\Value\NullValue) {
++$count;
if (!isset($value->{$fieldName})) {
++$notRequested;

continue;
}

if ($value->{$fieldName}->getValue() instanceof \Graphpinator\Value\NullValue) {
continue;
}

++$count;
}

if ($count !== 1) {
if ($count > 1 || ($count === 0 && $notRequested === 0)) {
throw new \Graphpinator\Exception\Constraint\ExactlyOneConstraintNotSatisfied();
}
}
Expand Down
28 changes: 25 additions & 3 deletions tests/Spec/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ public function simpleDataProvider() : array
]),
\Graphpinator\Json::fromObject((object) ['data' => ['fieldExactlyOne' => 1]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldAOrB { fieldA fieldB } }',
]),
\Graphpinator\Json::fromObject((object) ['data' => ['fieldAOrB' => ['fieldA' => null, 'fieldB' => 1]]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldAOrB { fieldB } }',
]),
\Graphpinator\Json::fromObject((object) ['data' => ['fieldAOrB' => ['fieldB' => 1]]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldAOrB { fieldA } }',
]),
\Graphpinator\Json::fromObject((object) ['data' => ['fieldAOrB' => ['fieldA' => null]]]),
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName {
Expand Down Expand Up @@ -244,6 +262,12 @@ public function invalidDataProvider() : array
]),
\Graphpinator\Exception\Constraint\ExactlyOneConstraintNotSatisfied::class,
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldExactlyOne(arg: {int2: null}) }',
]),
\Graphpinator\Exception\Constraint\ExactlyOneConstraintNotSatisfied::class,
],
[
\Graphpinator\Json::fromObject((object) [
'query' => 'query queryName { fieldExactlyOne(arg: {int1: null, int2: null}) }',
Expand Down Expand Up @@ -1058,9 +1082,7 @@ public function __construct(
{
parent::__construct();
$this->settings = $settings;
$this->addConstraint(new \Graphpinator\Constraint\ObjectConstraint([
'field1',
]));
$this->addConstraint(new \Graphpinator\Constraint\ObjectConstraint(['field1']));
}

protected function validateNonNullValue($rawValue) : bool
Expand Down
4 changes: 4 additions & 0 deletions tests/Spec/PrintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ interfaceListType(
fieldRequiredArgumentInvalid(
name: String!
): SimpleType
fieldAOrB: AOrB!
}
"""
Expand Down Expand Up @@ -1309,6 +1310,7 @@ interfaceListType(
fieldRequiredArgumentInvalid(
name: String!
): SimpleType
fieldAOrB: AOrB!
}
"""
Expand Down Expand Up @@ -1722,6 +1724,7 @@ interfaceListType(
fieldRequiredArgumentInvalid(
name: String!
): SimpleType
fieldAOrB: AOrB!
}
"""
Expand Down Expand Up @@ -2397,6 +2400,7 @@ interfaceListType(
fieldRequiredArgumentInvalid(
name: String!
): SimpleType
fieldAOrB: AOrB!
}
"""
Expand Down
48 changes: 48 additions & 0 deletions tests/Spec/TestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ static function ($parent, $name) : void {
),
]),
),
new \Graphpinator\Field\ResolvableField(
'fieldAOrB',
TestSchema::getAOrBType()->notNull(),
static function ($parent) : int {
return 0;
},
),
]);
}

Expand Down Expand Up @@ -1255,6 +1262,47 @@ protected function getFieldDefinition() : \Graphpinator\Argument\ArgumentSet
};
}

public static function getAOrBType() : \Graphpinator\Type\Type
{
return new class extends \Graphpinator\Type\Type
{
protected const NAME = 'AOrB';
protected const DESCRIPTION = 'Graphpinator Constraints: AOrB type';

public function __construct()
{
parent::__construct();

$this->addConstraint(new \Graphpinator\Constraint\ObjectConstraint(null, ['fieldA', 'fieldB']));
}

protected function validateNonNullValue($rawValue) : bool
{
return \is_int($rawValue) && \in_array($rawValue, [0, 1], true);
}

protected function getFieldDefinition() : \Graphpinator\Field\ResolvableFieldSet
{
return new \Graphpinator\Field\ResolvableFieldSet([
new \Graphpinator\Field\ResolvableField(
'fieldA',
\Graphpinator\Container\Container::Int(),
function (?int $parent) : ?int {
return $parent === 1 ? 1 : null;
},
),
new \Graphpinator\Field\ResolvableField(
'fieldB',
\Graphpinator\Container\Container::Int(),
function (int $parent) : ?int {
return $parent === 0 ? 1 : null;
},
),
]);
}
};
}

public static function getAddonType() : \Graphpinator\Type\Type
{
return new class extends \Graphpinator\Type\Type
Expand Down

0 comments on commit a9fc323

Please sign in to comment.