Skip to content

Commit

Permalink
feat: oneOf() implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Aug 8, 2024
1 parent 81cab02 commit 73a0eef
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/Constraint/QueryConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ protected function expectedDataFound( array $response, array $expected_data, str
return false;
}

if ( 'ONE_OF' === $expected_data['type'] ) {
$one_of_passing = false;
foreach ( $expected_data['rules'] as $one_of_rule ) {
$one_of_passing = $this->expectedDataFound( $response, $one_of_rule, $current_path );
if ( $one_of_passing ) {
break;
}
}

if ( ! $one_of_passing ) {
$this->error_details[] = sprintf(
'None of the provided rules passed for path "%s"',
$current_path
);
return false;
}

return true;
}

// Deconstruct $expected_data.
extract( $expected_data );

Expand Down
5 changes: 5 additions & 0 deletions src/TestCase/WPGraphQLTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function expectedEdge( string $path, array $expected_value, $expected_ind
return compact( 'type', 'path', 'expected_value', 'expected_index' );
}

public function oneOf( $rules ) {
$type = 'ONE_OF';
return compact( 'type', 'rules' );
}

/**
* Triggers the "not" flag for the next expect*() call.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/codeception/wpunit/QuerySuccessfulConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ public function testPassingValidationRules() {
'path' => 'nullField',
'expected_value' => 'codecept_field_value_is_null',
],
[
'type' => 'ONE_OF',
'rules' => [
[
'type' => 'FIELD',
'path' => 'slug',
'expected_value' => 'test_post',
],
[
'type' => 'FIELD',
'path' => 'title',
'expected_value' => 'hello world',
],
],
],
],
'expected_index' => null,
]
Expand Down Expand Up @@ -230,6 +245,28 @@ public function testFailingValidationRules() {
'path' => '',
'expected_value' => [],
],
[
'type' => 'ONE_OF',
'rules' => [
[
'type' => 'INVALID_TYPE',
'path' => '',
'expected_value' => [],
],
[
'type' => '!NODE',
'path' => 'posts.nodes',
'expected_value' => [
[
'type' => 'FIELD',
'path' => 'databaseId',
'expected_value' => 'post_id',
],
],
'expected_index' => 0,
],
],
],
]
);
$this->assertFalse($constraint->matches($response));
Expand Down
13 changes: 12 additions & 1 deletion tests/codeception/wpunit/WPGraphQLTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ public function testAssertQuerySuccessful() {
),
)
),

$this->oneOf(
[
$this->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', $post_id ) )
),
$this->not()->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', 10001 ) )
),
]
),
);

// Assert query successful.
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/unit/test-querysuccessfulconstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ public function test_PassingValidationRules() {
'path' => 'nullField',
'expected_value' => 'phpunit_field_value_is_null',
],
[
'type' => 'ONE_OF',
'rules' => [
[
'type' => 'FIELD',
'path' => 'slug',
'expected_value' => 'test_post',
],
[
'type' => 'FIELD',
'path' => 'title',
'expected_value' => 'hello world',
],
],
],
],
'expected_index' => null,
]
Expand Down Expand Up @@ -247,6 +262,28 @@ public function test_FailingValidationRules() {
'path' => '',
'expected_value' => [],
],
[
'type' => 'ONE_OF',
'rules' => [
[
'type' => 'INVALID_TYPE',
'path' => '',
'expected_value' => [],
],
[
'type' => '!NODE',
'path' => 'posts.nodes',
'expected_value' => [
[
'type' => 'FIELD',
'path' => 'databaseId',
'expected_value' => 'post_id',
],
],
'expected_index' => 0,
],
],
],
]
);
$this->assertFalse($constraint->matches($response));
Expand Down
13 changes: 12 additions & 1 deletion tests/phpunit/unit/test-wpgraphqlunittestcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,18 @@ public function test_AssertQuerySuccessful() {
),
)
),

$this->oneOf(
[
$this->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', $post_id ) )
),
$this->not()->expectedNode(
'posts.nodes',
array( 'id' => $this->toRelayId( 'post', 10001 ) )
),
]
),
);

// Assert query successful.
Expand Down

0 comments on commit 73a0eef

Please sign in to comment.