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: lodashGet access restored #32

Merged
merged 2 commits into from
May 2, 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
24 changes: 6 additions & 18 deletions src/Constraint/QueryConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

use PHPUnit\Framework\Exception;
use PHPUnit\Framework\Constraint\Constraint;
use Test\WPGraphQL\Logger\CodeceptLogger;
use Test\WPGraphQL\Logger\PHPUnitLogger;
use Tests\WPGraphQL\Logger\CodeceptLogger;
use Tests\WPGraphQL\Logger\PHPUnitLogger;
use Tests\WPGraphQL\Utils\Utils;

class QueryConstraint extends Constraint {

Expand Down Expand Up @@ -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 );
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/TestCase/WPGraphQLTestCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -211,14 +212,29 @@ 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 ),
$message
);
}

/**
* 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
*
Expand Down
39 changes: 39 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Utils class
*
* @since TBD
* @package Tests\WPGraphQL\Utils
*/

namespace Tests\WPGraphQL\Utils;

class Utils {
/**
* 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 void
*/
public static 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;
}
}
Loading