From f9f196c3cd7df0ddd6cf8d46067a3e6134700e46 Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Thu, 2 May 2024 00:22:46 -0400 Subject: [PATCH 1/2] fix: lodashGet access restored --- src/Constraint/QueryConstraint.php | 20 +++----------- src/TestCase/WPGraphQLTestCommon.php | 18 ++++++++++++- src/Utils/Utils.php | 39 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/Utils/Utils.php diff --git a/src/Constraint/QueryConstraint.php b/src/Constraint/QueryConstraint.php index 434b74a..476627d 100644 --- a/src/Constraint/QueryConstraint.php +++ b/src/Constraint/QueryConstraint.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\Constraint\Constraint; use Test\WPGraphQL\Logger\CodeceptLogger; use Test\WPGraphQL\Logger\PHPUnitLogger; +use Test\WPGraphQL\Utils\Utils; class QueryConstraint extends Constraint { @@ -418,24 +419,11 @@ protected function getPossibleDataAtPath( array $data, string $path, &$is_group * @param array $object The object to query. * @param string $path The path of the property to get. * @param mixed $default The value returned for undefined resolved values. - * @return void + * + * @return mixed */ protected function lodashGet( array $data, string $string, $default = null ) { - $arrStr = explode( '.', $string ); - if ( ! is_array( $arrStr ) ) { - $arrStr = [ $arrStr ]; - } - - $result = $data; - foreach ( $arrStr as $lvl ) { - if ( ! is_null( $lvl ) && isset( $result[ $lvl ] ) ) { - $result = $result[ $lvl ]; - } else { - $result = $default; - } - } - - return $result; + return Utils::lodashGet( $data, $string, $default ); } /** diff --git a/src/TestCase/WPGraphQLTestCommon.php b/src/TestCase/WPGraphQLTestCommon.php index 80e89be..6bb6dc5 100644 --- a/src/TestCase/WPGraphQLTestCommon.php +++ b/src/TestCase/WPGraphQLTestCommon.php @@ -12,6 +12,7 @@ use Tests\WPGraphQL\Constraint\QueryConstraint; use Tests\WPGraphQL\Constraint\QueryErrorConstraint; use Tests\WPGraphQL\Constraint\QuerySuccessfulConstraint; +use Tests\WPGraphQL\Utils\Utils; /** * trait WPGraphQLTestCommon @@ -211,7 +212,7 @@ public static function assertQuerySuccessful( array $response, array $expected = * @param string $message Error message. * @return void */ - public function assertQueryError( array $response, array $expected = [], $message = '' ) { + public static function assertQueryError( array $response, array $expected = [], $message = '' ) { static::assertThat( $response, new QueryErrorConstraint( static::getLogger(), $expected ), @@ -219,6 +220,21 @@ public function assertQueryError( array $response, array $expected = [], $messag ); } + /** + * The value returned for undefined resolved values. + * + * Clone of the "get" function from the Lodash JS libra + * + * @param array $object The object to query. + * @param string $path The path of the property to get. + * @param mixed $default The value returned for undefined resolved values. + * + * @return mixed + */ + public static function lodashGet( array $data, string $string, $default = null ) { + return Utils::lodashGet( $data, $string, $default ); + } + /** * Returns the logger instance * diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php new file mode 100644 index 0000000..6e4213a --- /dev/null +++ b/src/Utils/Utils.php @@ -0,0 +1,39 @@ + Date: Thu, 2 May 2024 00:27:05 -0400 Subject: [PATCH 2/2] fix: Syntax errors fixed --- src/Constraint/QueryConstraint.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Constraint/QueryConstraint.php b/src/Constraint/QueryConstraint.php index 476627d..5486deb 100644 --- a/src/Constraint/QueryConstraint.php +++ b/src/Constraint/QueryConstraint.php @@ -11,9 +11,9 @@ use PHPUnit\Framework\Exception; use PHPUnit\Framework\Constraint\Constraint; -use Test\WPGraphQL\Logger\CodeceptLogger; -use Test\WPGraphQL\Logger\PHPUnitLogger; -use Test\WPGraphQL\Utils\Utils; +use Tests\WPGraphQL\Logger\CodeceptLogger; +use Tests\WPGraphQL\Logger\PHPUnitLogger; +use Tests\WPGraphQL\Utils\Utils; class QueryConstraint extends Constraint {