Skip to content

Commit

Permalink
Merge pull request #29 from wp-graphql/feat/assert-query-successful-fix
Browse files Browse the repository at this point in the history
fix: Assert query successful fix
  • Loading branch information
kidunot89 authored Apr 30, 2024
2 parents 168f5b8 + 12b1c1f commit 0c3fa9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/TestCase/WPGraphQLTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down
12 changes: 12 additions & 0 deletions tests/codeception/wpunit/WPGraphQLTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -170,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() {
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/unit/test-wpgraphqlunittestcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -165,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() {
Expand Down

0 comments on commit 0c3fa9d

Please sign in to comment.