From 73a0eef56088bf6672c33589cb2d45edaaad77e7 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 8 Aug 2024 12:08:58 -0400 Subject: [PATCH 1/3] feat: oneOf() implemented --- src/Constraint/QueryConstraint.php | 20 ++++++++++ src/TestCase/WPGraphQLTestCommon.php | 5 +++ .../wpunit/QuerySuccessfulConstraintTest.php | 37 +++++++++++++++++++ .../wpunit/WPGraphQLTestCaseTest.php | 13 ++++++- .../unit/test-querysuccessfulconstraint.php | 37 +++++++++++++++++++ .../unit/test-wpgraphqlunittestcase.php | 13 ++++++- 6 files changed, 123 insertions(+), 2 deletions(-) diff --git a/src/Constraint/QueryConstraint.php b/src/Constraint/QueryConstraint.php index ae85556..675a3f3 100644 --- a/src/Constraint/QueryConstraint.php +++ b/src/Constraint/QueryConstraint.php @@ -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 ); diff --git a/src/TestCase/WPGraphQLTestCommon.php b/src/TestCase/WPGraphQLTestCommon.php index 6bb6dc5..a5dd1d6 100644 --- a/src/TestCase/WPGraphQLTestCommon.php +++ b/src/TestCase/WPGraphQLTestCommon.php @@ -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. * diff --git a/tests/codeception/wpunit/QuerySuccessfulConstraintTest.php b/tests/codeception/wpunit/QuerySuccessfulConstraintTest.php index a123b25..6f07ed1 100644 --- a/tests/codeception/wpunit/QuerySuccessfulConstraintTest.php +++ b/tests/codeception/wpunit/QuerySuccessfulConstraintTest.php @@ -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, ] @@ -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)); diff --git a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php index 9ccb942..8ba8088 100644 --- a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php +++ b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php @@ -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. diff --git a/tests/phpunit/unit/test-querysuccessfulconstraint.php b/tests/phpunit/unit/test-querysuccessfulconstraint.php index c64343d..06ba6a7 100644 --- a/tests/phpunit/unit/test-querysuccessfulconstraint.php +++ b/tests/phpunit/unit/test-querysuccessfulconstraint.php @@ -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, ] @@ -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)); diff --git a/tests/phpunit/unit/test-wpgraphqlunittestcase.php b/tests/phpunit/unit/test-wpgraphqlunittestcase.php index d542599..d6c859c 100644 --- a/tests/phpunit/unit/test-wpgraphqlunittestcase.php +++ b/tests/phpunit/unit/test-wpgraphqlunittestcase.php @@ -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. From 3de940c5bad8cbc773c62113c7864bd949bd2a3a Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 8 Aug 2024 12:14:55 -0400 Subject: [PATCH 2/3] devops: Scheduled CI moved to once a month --- .github/workflows/continous-integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continous-integration.yml b/.github/workflows/continous-integration.yml index 3bd1b99..5936bdd 100644 --- a/.github/workflows/continous-integration.yml +++ b/.github/workflows/continous-integration.yml @@ -3,7 +3,7 @@ name: continuous_integration on: workflow_dispatch: schedule: - - cron: "0 4 * * 5" + - cron: "0 4 1 * *" push: branches: - master From c1d5c419273561e07efafbb9c9730f5136c003b5 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 8 Aug 2024 12:18:22 -0400 Subject: [PATCH 3/3] devops: "docker-compose" replaced with "docker compose" everywhere --- .github/workflows/continous-integration.yml | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/continous-integration.yml b/.github/workflows/continous-integration.yml index 5936bdd..dae72c3 100644 --- a/.github/workflows/continous-integration.yml +++ b/.github/workflows/continous-integration.yml @@ -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 \ diff --git a/composer.json b/composer.json index 1d62007..de0a69d 100644 --- a/composer.json +++ b/composer.json @@ -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",