Skip to content

Commit

Permalink
Merge pull request #32 from wp-graphql/fix/lodash-get-restored
Browse files Browse the repository at this point in the history
fix: lodashGet access restored
  • Loading branch information
kidunot89 authored May 2, 2024
2 parents 706c2d7 + 8ed3e07 commit c18eb27
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
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;
}
}

0 comments on commit c18eb27

Please sign in to comment.