Skip to content

Commit

Permalink
Merge pull request #38 from wp-graphql/feat/one-of
Browse files Browse the repository at this point in the history
feat: oneOf() implemented
  • Loading branch information
kidunot89 authored Aug 8, 2024
2 parents 81cab02 + c1d5c41 commit 572d4c5
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: continuous_integration
on:
workflow_dispatch:
schedule:
- cron: "0 4 * * 5"
- cron: "0 4 1 * *"
push:
branches:
- master
Expand Down Expand Up @@ -71,7 +71,7 @@ jobs:
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
docker-compose run --rm \
docker compose run --rm \
--user $(id -u) \
-e COVERALLS_REPO_TOKEN=$COVERALLS_REPO_TOKEN \
codeception_testing \
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"wpackagist-plugin/wp-graphql": "^1.26"
},
"scripts": {
"run_phpunit_env": "docker-compose run --rm --workdir=/var/www/html/wp-content/plugins/wp-graphql-testcase --user $(id -u) wp_phpunit_testing wait-for-it $TEST_DB -s -t 300 --",
"run_codecept_env": "docker-compose run --rm --user $(id -u) codeception_testing wait-for-it $TEST_DB -s -t 300 --",
"run_phpunit_env": "docker compose run --rm --workdir=/var/www/html/wp-content/plugins/wp-graphql-testcase --user $(id -u) wp_phpunit_testing wait-for-it $TEST_DB -s -t 300 --",
"run_codecept_env": "docker compose run --rm --user $(id -u) codeception_testing wait-for-it $TEST_DB -s -t 300 --",
"codeception": "codecept run --",
"phpunit": "phpunit --",
"run-codeception": "env TEST_DB=codecept_db:3306 composer run_codecept_env vendor/bin/codecept run",
Expand Down
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 572d4c5

Please sign in to comment.