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: TraversableContains replaced with TraversableContainsIdentical #21

Merged
merged 4 commits into from
May 25, 2022
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
9 changes: 5 additions & 4 deletions .github/workflows/continous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
if: ${{ matrix.php != '8.0' }}
run: |
composer require wp-phpunit/wp-phpunit \
phpunit/phpunit:~7.5
yoast/phpunit-polyfills \
phpunit/phpunit
composer install

- name: Run PHPUnit Tests w/ Docker.
Expand All @@ -43,9 +44,9 @@ jobs:
- name: Install Codeception dependencies
run: |
composer install --ignore-platform-reqs
composer require codeception/module-asserts \
codeception/util-universalframework \
codeception/module-rest \
composer require codeception/module-asserts:* \
codeception/util-universalframework:* \
codeception/module-rest:* \
lucatume/wp-browser:3.1.0 --ignore-platform-reqs

- name: Run Codeception Tests w/ Docker.
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cache/*
src/Codeception/Module/patchwork.json
src/includes/patchwork.json
composer.lock
.phpunit.result.cache

# Some local vim files
tags
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ services:

mysql:
image: mysql
platform: linux/amd64
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: password
Expand Down Expand Up @@ -34,7 +33,6 @@ services:

mysql_phpunit:
image: mysql:5.7
platform: linux/amd64
restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
Expand Down
60 changes: 49 additions & 11 deletions src/TestCase/WPGraphQLTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\Constraint\IsEqual;
use PHPUnit\Framework\Constraint\IsEmpty;
use PHPUnit\Framework\Constraint\IsTrue;
use PHPUnit\Framework\Constraint\TraversableContains;
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;

/**
* Trait WPGraphQLTestCommon
Expand Down Expand Up @@ -307,7 +307,9 @@ public static function _assertExpectedDataFound( array $response, array $expecte
static::$actual = $actual_data;

// Handle if "$expected_value" set to field value constants.
switch( is_array( $expected_value ) ? $expected_value : "{$expected_value}" ) {
$reverse = static::startsWith( '!', $type );
$possible_constraint = is_array( $expected_value ) ? $expected_value : "{$expected_value}";
switch( $possible_constraint ) {
case static::IS_NULL:
// Set IS_NULL constraint.
static::$last_constraint = static::isNull();
Expand All @@ -318,36 +320,63 @@ public static function _assertExpectedDataFound( array $response, array $expecte

case static::NOT_FALSY:
// Set NOT_FALSY constraint.
static::$last_constraint = static::logicalNot( static::isEmpty() );
if ( $reverse ) {
static::$last_constraint = static::isEmpty();
} else {
static::$last_constraint = static::logicalNot( static::isEmpty() );
}

// Fail if data found at path is a falsy value (null, false, []).
if ( empty( $actual_data ) ) {
if ( empty( $actual_data ) && ! $reverse ) {
$message = $message
?? sprintf(
'Expected data at path "%s" not to be falsy value. "%s" Given',
$full_path,
is_array( $actual_data ) ? '[]' : (string) $actual_data
);

return false;
} elseif ( ! empty( $actual_data ) && $reverse ) {
$message = $message
?? 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
);

return false;
}

// Return true because target value not falsy.
return true;

case static::IS_FALSY:

// Set IS_FALSY constraint.
static::$last_constraint = static::isEmpty();
if ( $reverse ) {
static::$last_constraint = static::logicalNot( static::isEmpty() );
} else {
static::$last_constraint = static::isEmpty();
}

// Fail if data found at path is not falsy value (null, false, 0, []).
if ( ! empty( $actual_data ) ) {
if ( ! empty( $actual_data ) && ! $reverse ) {
$message = $message
?? 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
);

return false;
} elseif ( empty( $actual_data ) && $reverse ) {
$message = $message
?? 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
);

return false;
}

Expand All @@ -360,9 +389,18 @@ public static function _assertExpectedDataFound( array $response, array $expecte
static::$last_constraint = static::logicalNot( static::isNull() );

// Fail if no data found at path.
if ( is_null( $actual_data ) ) {
if ( is_null( $actual_data ) && ! $reverse ) {
$message = $message ?? sprintf( 'No data found at path "%s"', $full_path );

return false;
} elseif (
! is_null( $actual_data )
&& $reverse
&& $expected_value === static::NOT_NULL
) {
$message = $message ?? sprintf( 'Unexpected data found at path "%s"', $full_path );

static::$last_constraint = static::isNull();
return false;
}

Expand Down Expand Up @@ -437,7 +475,7 @@ public static function _assertExpectedDataFound( array $response, array $expecte
$message = $message
?? sprintf(
'%1$s found in %2$s list at path "%3$s"',
$match_wanted ? 'Undesired data ' : 'Expected data not ',
$match_wanted ? 'Unexpected data ' : 'Expected data not ',
strtolower( $type ),
$full_path
);
Expand Down Expand Up @@ -828,10 +866,10 @@ public static function isEqual( $value ): IsEqual {
*
* @param mixed $value Desired contained value
*
* @return TraversableContains
* @return TraversableContainsIdentical
*/
public static function contains( $value, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false ): TraversableContains {
return new TraversableContains( $value, $checkForObjectIdentity, $checkForNonObjectIdentity );
public static function contains( $value ): TraversableContainsIdentical {
return new TraversableContainsIdentical( $value );
}

/**
Expand Down