Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Assert query successful fix #29

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading