Skip to content

Commit

Permalink
Merge 38be5fb into c18eb27
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed May 3, 2024
2 parents c18eb27 + 38be5fb commit af726f1
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 89 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ jobs:

- name: Install WP PHPUnit Dependencies
run: |
composer install --ignore-platform-reqs
composer install
composer require wp-phpunit/wp-phpunit \
yoast/phpunit-polyfills \
phpunit/phpunit:^9.6 \
wp-phpunit/wp-phpunit \
- name: Run PHPUnit Tests w/ Docker.
env:
PHP_VERSION: ${{ matrix.php }}
run: composer run-phpunit -- -- --coverage-text

- name: Install WPBrowser Dependencies
run: |
composer install --ignore-platform-reqs
composer install
composer require codeception/module-asserts:* \
codeception/util-universalframework:* \
codeception/module-rest:* \
lucatume/wp-browser:^3.1 --ignore-platform-reqs
lucatume/wp-browser:^3.1
- name: Run Codeception Tests w/ Docker.
env:
PHP_VERSION: ${{ matrix.php }}
run: composer run-codeception -- -- --coverage --coverage-xml


Expand Down
3 changes: 2 additions & 1 deletion codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ actor_suffix: Tester
coverage:
enabled: true
include:
- src/TestCase/*
- src/*
exclude:
- src/TestCase/WPGraphQLUnitTestCase.php
- src/Logger/PHPUnitLogger.php
show_only_summary: false
extensions:
enabled:
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ services:
MYSQL_PASSWORD: password

wordpress:
image: wp-graphql/wordpress:${WORDPRESS_IMAGE_VERSION:-latest}
image: wp-graphql/wordpress:${WP_VERSION:-latest}
build:
context: ./docker
args:
PHP_VERSION: ${PHP_VERSION:-8.0}
depends_on:
- mysql
- mysql_phpunit
Expand Down
5 changes: 3 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<file>src/TestCase/WPGraphQLTestCommon.php</file>
<file>src/TestCase/WPGraphQLUnitTestCase.php</file>
<directory>src/</directory>
<exclude>
<file>src/TestCase/WPGraphQLTestCase.php</file>
<file>src/Logger/CodeceptLogger.php</file>
<directory>vendor/</directory>
<directory>local/</directory>
</exclude>
Expand Down
34 changes: 18 additions & 16 deletions src/Constraint/QueryConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* QueryConstraint interface
*
* Defines shared logic for QueryConstraint classes.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Constraint
*/

Expand All @@ -22,7 +22,7 @@ class QueryConstraint extends Constraint {
*
* @var PHPUnitLogger|CodeceptLogger
*/
private $logger;
protected $logger;

/**
* Stores the validation steps for the assertion.
Expand Down Expand Up @@ -64,13 +64,13 @@ public function __construct($logger, array $expected = []) {
* @return bool
*/
protected function responseIsValid( $response, &$message = null ) {
if ( array_keys( $response ) === range( 0, count( $response ) - 1 ) ) {
$this->error_messages[] = 'The GraphQL query response must be provided as an associative array.';
if ( empty( $response ) ) {
$this->error_messages[] = 'GraphQL query response is invalid.';
return false;
}

if ( empty( $response ) ) {
$this->error_messages[] = 'GraphQL query response is empty.';
if ( array_keys( $response ) === range( 0, count( $response ) - 1 ) ) {
$this->error_messages[] = 'The GraphQL query response must be provided as an associative array.';
return false;
}

Expand All @@ -86,17 +86,16 @@ protected function responseIsValid( $response, &$message = null ) {
* Evaluates the response "data" against a validation rule.
*
* @param array $response GraphQL query response object
* @param array $expected_data Validation Rule.
*
* @throws Exception Invalid rule object provided for evaluation.
* @param array $expected_data Validation Rule.valid rule object provided for evaluation.
*
* @return bool
*/
protected function expectedDataFound( array $response, array $expected_data, string $current_path = null ) {
// Throw if "$expected_data" invalid.
if ( empty( $expected_data['type'] ) ) {
$this->logger->logData( [ 'INVALID_DATA_OBJECT' => $expected_data ] );
throw new Exception( 'Invalid rule object provided for evaluation.' );
$this->error_messages[] = "Invalid rule object provided for evaluation: \n\t " . json_encode( $expected_data, JSON_PRETTY_PRINT );
return false;
}

// Deconstruct $expected_data.
Expand Down Expand Up @@ -144,7 +143,7 @@ protected function expectedDataFound( array $response, array $expected_data, str
$this->error_messages[] = sprintf(
'Expected data at path "%s" to be falsy value. "%s" Given',
$full_path,
is_array( $actual_data ) ? json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
is_array( $actual_data ) ? "\n\n" . json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
);

return false;
Expand All @@ -159,15 +158,15 @@ protected function expectedDataFound( array $response, array $expected_data, str
$this->error_messages[] = sprintf(
'Expected data at path "%s" to be falsy value. "%s" Given',
$full_path,
is_array( $actual_data ) ? json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
is_array( $actual_data ) ? "\n\n" .json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
);

return false;
} elseif ( empty( $actual_data ) && $reverse ) {
$this->error_messages[] = sprintf(
'Expected data at path "%s" not to be falsy value. "%s" Given',
$full_path,
is_array( $actual_data ) ? json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
is_array( $actual_data ) ? "\n\n" .json_encode( $actual_data, JSON_PRETTY_PRINT ) : $actual_data
);

return false;
Expand Down Expand Up @@ -267,7 +266,8 @@ protected function expectedDataFound( array $response, array $expected_data, str
return true;
default:
$this->logger->logData( ['INVALID_DATA_OBJECT', $expected_data ] );
throw new Exception( 'Invalid data object provided for evaluation.' );
$this->error_messages[] = "Invalid data object provided for evaluation. \n\t" . json_encode( $expected_data, JSON_PRETTY_PRINT );
return false;
}
}

Expand Down Expand Up @@ -369,7 +369,9 @@ function( $v ) {

return false;
default:
throw new Exception( 'Invalid data object provided for evaluation.' );
$this->logger->logData( ['INVALID_DATA_OBJECT', $expected_data ] );
$this->error_messages[] = "Invalid data object provided for evaluation. \n\t" . json_encode( $expected_data, JSON_PRETTY_PRINT );
return false;
}
}

Expand Down Expand Up @@ -572,7 +574,7 @@ public function matches($response): bool {
}

public function failureDescription($other): string {
return "GraphQL response failed validation: \n" . implode( "\n\t", $this->error_messages );
return "GraphQL response failed validation: \n\n\t" . implode( "\n\n\t", $this->error_messages );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Constraint/QueryErrorConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* QueryErrorConstraint
*
* Assertion validating successful WPGraphQL query response.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Constraint
*/

Expand Down Expand Up @@ -38,7 +38,7 @@ public function matches($response): bool {
if ( empty( $expected_data['type'] ) ) {
$this->logger->logData( array( 'INVALID_DATA_OBJECT' => $expected_data ) );
$this->error_messages[] = 'Invalid data object provided for evaluation.';
return false;
continue;
}

if ( str_starts_with( $expected_data['type'], 'ERROR_' ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Constraint/QuerySuccessfulConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* QuerySuccessfulConstraint
*
* Assertion validating successful WPGraphQL query response.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Constraint
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Logger/CodeceptLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* CodeceptLogger
*
* Console logging for Codeception tests.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Logger
*/
namespace Tests\WPGraphQL\Logger;
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Logger interface
*
* Defines shared logic for Logger classes.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Logger
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Logger/PHPUnitLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHPUnitLogger
*
* Console logging for PHPUnit tests.
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Logger
*/
namespace Tests\WPGraphQL\Logger;
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Utils class
*
* @since TBD
* @since v3.0.0
* @package Tests\WPGraphQL\Utils
*/

Expand Down
28 changes: 27 additions & 1 deletion tests/codeception/wpunit/QueryConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ public function testGraphQLResponseWithErrors() {
$constraint = new QueryConstraint($this->logger);
$this->assertTrue($constraint->matches($response));
}
}

public function testInvalidGraphQLResponse() {
$response1 = [4, 5, 6];
$constraint = new QueryConstraint($this->logger);
$this->assertFalse($constraint->matches($response1));

$response2 = null;
$constraint = new QueryConstraint($this->logger);
$this->assertFalse($constraint->matches($response1));

$response3 = [ 'something' => [] ];
$constraint = new QueryConstraint($this->logger);
$this->assertFalse($constraint->matches($response3));
}

public function testFailureDescription() {
$constraint = new QueryConstraint($this->logger);
$response = [4, 5, 6];
$this->assertFalse($constraint->matches($response));
$this->assertEquals("GraphQL response failed validation: \n\n\t• The GraphQL query response must be provided as an associative array.", $constraint->failureDescription($response));
}

public function testToString() {
$constraint = new QueryConstraint($this->logger);
$this->assertEquals('is a valid WPGraphQL response', $constraint->toString());
}
}
34 changes: 27 additions & 7 deletions tests/codeception/wpunit/QueryErrorConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ public function testPassingValidationRules() {
}

public function testFailingValidationRules() {
// Register broken field
register_graphql_field( 'Post', 'invalidField', [
'type' => 'String',
'resolve' => function() {
throw new \GraphQL\Error\UserError('Explosion!');
}
]);
// Register broken field.
register_graphql_field(
'Post',
'invalidField',
[
'type' => 'String',
'resolve' => function() {
throw new \GraphQL\Error\UserError('Explosion!');
},
]
);

// Create some posts.
$this->factory()->post->create();
Expand All @@ -159,6 +163,7 @@ public function testFailingValidationRules() {
$constraint = new QueryErrorConstraint(
$this->logger,
[
['InvalidRuleObject'],
[
'type' => 'FIELD',
'path' => 'posts.nodes.#.content',
Expand All @@ -173,8 +178,23 @@ public function testFailingValidationRules() {
'type' => 'ERROR_PATH',
'path' => 'posts.nodes.#.id',
],
[
'type' => 'ERROR_INVALID',
'path' => '',
]
]
);
$this->assertFalse($constraint->matches($response));
}

public function testInvalidGraphQLResponse() {
$response = [4, 5, 6];
$constraint = new QueryErrorConstraint($this->logger);
$this->assertFalse($constraint->matches($response));
}

public function testToString() {
$constraint = new QueryErrorConstraint($this->logger);
$this->assertEquals('is a WPGraphQL query response with errors', $constraint->toString());
}
}

0 comments on commit af726f1

Please sign in to comment.