From e79c9376180b48e5546675cba67a82e5da4a56f4 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 30 Apr 2024 18:37:43 -0400 Subject: [PATCH 1/2] fix: assertQuerySuccessful fixed --- src/TestCase/WPGraphQLTestCommon.php | 4 ++-- tests/codeception/wpunit/WPGraphQLTestCaseTest.php | 6 ++++++ tests/phpunit/unit/test-wpgraphqlunittestcase.php | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/TestCase/WPGraphQLTestCommon.php b/src/TestCase/WPGraphQLTestCommon.php index 707a189..351d55b 100644 --- a/src/TestCase/WPGraphQLTestCommon.php +++ b/src/TestCase/WPGraphQLTestCommon.php @@ -625,11 +625,11 @@ public static function assertIsValidQueryResponse( $response, $message = null ) * @param array $expected List of expected data objects. * @param string $message Error message. */ - public static function assertQuerySuccessful( array $response, array $expected, $message = null ) { + public static function assertQuerySuccessful( array $response, array $expected = [], $message = null ) { static::$actual = null; static::$last_constraint = null; - $data_passing = null; // Create individual data rule evaluation flag for later use. + $data_passing = empty( $expected ); // Create individual data rule evaluation flag for later use. $response_valid = static::_assertIsValidQueryResponse( $response, $message ); // Validate response shape with sub assertion. $response_successful = ! in_array( 'errors', array_keys( $response ) ); // Ensure no errors thrown. diff --git a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php index ffba321..7d1b28b 100644 --- a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php +++ b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php @@ -87,6 +87,12 @@ public function testAssertQuerySuccessful() { // Assert query successful. $this->assertQuerySuccessful( $response, $expected ); + + // Assert query successful with no expected rules. + $this->assertQuerySuccessful( $response ); + + // Assert query successful with no expected rules. + $this->assertQuerySuccessful( $response, [], 'Query returned errors' ); } public function testAssertQueryError() { diff --git a/tests/phpunit/unit/test-wpgraphqlunittestcase.php b/tests/phpunit/unit/test-wpgraphqlunittestcase.php index bfa88a4..6635252 100644 --- a/tests/phpunit/unit/test-wpgraphqlunittestcase.php +++ b/tests/phpunit/unit/test-wpgraphqlunittestcase.php @@ -82,6 +82,12 @@ public function test_AssertQuerySuccessful() { // Assert query successful. $this->assertQuerySuccessful( $response, $expected ); + + // Assert query successful with no expected rules. + $this->assertQuerySuccessful( $response ); + + // Assert query successful with no expected rules. + $this->assertQuerySuccessful( $response, [], 'Query returned errors' ); } public function test_AssertQueryError() { From 12b1c1ffd8a5958e265add857f8a9cf10fcc46ba Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Tue, 30 Apr 2024 18:46:17 -0400 Subject: [PATCH 2/2] fix: assertQueryError fixed --- src/TestCase/WPGraphQLTestCommon.php | 6 +++--- tests/codeception/wpunit/WPGraphQLTestCaseTest.php | 6 ++++++ tests/phpunit/unit/test-wpgraphqlunittestcase.php | 6 ++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/TestCase/WPGraphQLTestCommon.php b/src/TestCase/WPGraphQLTestCommon.php index 351d55b..12b8ffd 100644 --- a/src/TestCase/WPGraphQLTestCommon.php +++ b/src/TestCase/WPGraphQLTestCommon.php @@ -670,9 +670,9 @@ public static function assertQuerySuccessful( array $response, array $expected = * @param string $message Error message. * @return void */ - public function assertQueryError( array $response, array $expected, $message = null ) { - $error_passing = null; // Create individual error rule evaluation flag for later use. - $data_passing = null; // Create individual data rule evaluation flag for later use. + public function assertQueryError( array $response, array $expected = [], $message = null ) { + $error_passing = null; // Create individual error rule evaluation flag for later use. + $data_passing = empty( $expected ); // Create individual data rule evaluation flag for later use. $response_valid = static::_assertIsValidQueryResponse( $response, $message ); // Validate response shape with sub assertion. $response_failed = in_array( 'errors', array_keys( $response ) ); // Ensure no errors thrown. diff --git a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php index 7d1b28b..c433b8d 100644 --- a/tests/codeception/wpunit/WPGraphQLTestCaseTest.php +++ b/tests/codeception/wpunit/WPGraphQLTestCaseTest.php @@ -176,6 +176,12 @@ public function testAssertQueryError() { // Assert response has error. $this->assertQueryError( $response, $expected ); + + // Assert response has error with no expected rules. + $this->assertQueryError( $response ); + + // Assert response has error with no expected rules and a message. + $this->assertQueryError( $response, [], 'Query return with no errors' ); } public function testComplexExpectedNodes() { diff --git a/tests/phpunit/unit/test-wpgraphqlunittestcase.php b/tests/phpunit/unit/test-wpgraphqlunittestcase.php index 6635252..e57bb66 100644 --- a/tests/phpunit/unit/test-wpgraphqlunittestcase.php +++ b/tests/phpunit/unit/test-wpgraphqlunittestcase.php @@ -171,6 +171,12 @@ public function test_AssertQueryError() { // Assert response has error. $this->assertQueryError( $response, $expected ); + + // Assert response has error with no expected rules. + $this->assertQueryError( $response ); + + // Assert response has error with no expected rules and a message. + $this->assertQueryError( $response, [], 'Query return with no errors' ); } public function test_ComplexExpectedNodes() {