Skip to content

Commit

Permalink
tests: add wpunit tests for register_graphql_facet_type (#38)
Browse files Browse the repository at this point in the history
* fix: registration logic

* tests: test base register_graphql_facet_type

* test: fix docker plugin install path

* fix schema artifact workflow
  • Loading branch information
justlevine authored Sep 28, 2022
1 parent d71dd9f commit 5084ddb
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/upload-schema-artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Setup WordPress
run: |
cp .env.dist .env
echo GIT_TOKEN=${{ secrets.GIT_TOKEN }} >> .env
echo GIT_TOKEN=${{ secrets.GIT_TOKEN }} >> .env
composer run install-test-env
- name: Generate the Static Schema
Expand All @@ -45,3 +45,5 @@ jobs:
uses: softprops/action-gh-release@v1
with:
files: /tmp/schema.graphql
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
"WPGraphQL\\FacetWP\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\WPGraphQL\\FacetWP\\": "tests/_support/"
}
},
"require": {
"php": "^7.4||^8.0"
},
Expand Down
4 changes: 1 addition & 3 deletions docker/app.post-setup.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/bin/bash



install_facetwp() {
if [ ! -d $WP_CORE_DIR/wp-content/plugins/facetwp ]; then
echo "Cloning FacetWP: https://${GIT_TOKEN}@${FACET_REPO}"
git clone -b master --single-branch https://${GIT_TOKEN}@${FACET_REPO} $WP_CORE_DIR/wp-content/plugins/facetwp
git clone -b master --single-branch https://${GIT_TOKEN}@${FACET_REPO} $PLUGINS_DIR/facetwp
fi
echo "Activating FacetWP"
wp plugin activate facetwp --allow-root
Expand Down
46 changes: 25 additions & 21 deletions src/Registry/FacetRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace WPGraphQL\FacetWP\Registry;

use FacetWP_API_Fetch;
use WPGraphQL\Connection\PostObjects;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
use WPGraphQL\FacetWP\Type\Input;
Expand Down Expand Up @@ -84,11 +83,13 @@ private static function register_root_field( array $facet_config ) :void {
'per_page' => 10,
'page' => 1,
];

if ( ! empty( $where['pager'] ) ) {
$pagination = array_merge( $pagination, $where['pager'] );
}

$query = self::parse_query( $where['query'] );
$query = ! empty( $where['query'] ) ? $where['query'] : [];
$query = self::parse_query( $query );

// Clean up null args.
foreach ( $query as $key => $value ) {
Expand Down Expand Up @@ -122,8 +123,7 @@ function ( $post_ids ) use ( &$filtered_ids ) {
);
}

$fwp = new FacetWP_API_Fetch();
$payload = $fwp->process_request( $fwp_args );
$payload = FWP()->api->process_request( $fwp_args );

$results = $payload['results'];
if ( $use_graphql_pagination ) {
Expand All @@ -143,16 +143,11 @@ function ( $post_ids ) use ( &$filtered_ids ) {
* The facets array is the resolved payload for this field.
* Results & pager are returned so the connection resolver can use the data.
*/
$return_vals = [
return [
'facets' => array_values( $payload['facets'] ),
'results' => count( $results ) ? $results : [ -1 ],
'results' => count( $results ) ? $results : null,
'pager' => $payload['pager'] ?? [],
];

if ( $use_graphql_pagination ) {
$return_vals['pager'] = $payload['pager'];
}

return $return_vals;
},
]
);
Expand Down Expand Up @@ -434,11 +429,13 @@ private static function register_facet_connection( array $facet_config ) : void
// Manually override the first query arg if per_page > 10, the first default value.
$args['first'] = $source['pager']['per_page'];
}

$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, $type );
if ( ! empty( $source['results'] ) ) {
$resolver->set_query_arg( 'post__in', $source['results'] );
}

return $resolver
->set_query_arg( 'post__in', $source['results'] )
->get_connection();
return $resolver->get_connection();
},
];

Expand All @@ -458,14 +455,19 @@ private static function register_facet_connection( array $facet_config ) : void
/**
* Parse WPGraphQL query into FacetWP query
*
* @param mixed $query @todo.
* @param array $query @todo.
*
* @return mixed FacetWP query
* @return array FacetWP query
*/
private static function parse_query( $query ) {
private static function parse_query( array $query ) : array {
// Bail early if no query set.
if ( empty( $query ) ) {
return [];
}

$facets = FWP()->helper->get_facets();

return array_reduce(
$reduced_query = array_reduce(
$facets,
function ( $prev, $cur ) use ( $query ) {
$name = $cur['name'];
Expand Down Expand Up @@ -509,6 +511,8 @@ function ( $prev, $cur ) use ( $query ) {
},
[]
);

return $reduced_query ?: [];
}

/**
Expand All @@ -528,15 +532,15 @@ private static function to_camel_case( $input ) {
$camel_key = self::to_camel_case( $key );
// @todo refactor recursion to avoid this.
if ( is_string( $camel_key ) ) {
$camel_key = $key;
$key = $camel_key;
}
$out[ $key ] = $value;
}

return $out;
}

return lcfirst( str_replace( '_', '', ucwords( $input, ' /_' ) ) );
return lcfirst( str_replace( '_', '', ucwords( $input, '_' ) ) );
}

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/_support/TestCase/FWPGraphQLTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* WPGraphQL test case
*
* For testing WPGraphQL responses.
*
* @since 0.8.0
* @package Tests\WPGraphQL\TestCase
*/

namespace Tests\WPGraphQL\FacetWP\TestCase;

/**
* Class - GraphQLTestCase
*/
class FWPGraphQLTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
public $fwp;

/**
* Creates users and loads factories.
*/
public function setUp() : void {
parent::setUp();
}

/**
* Post test tear down.
*/
public function tearDown(): void {
$this->clearFacets();
// Then...
parent::tearDown();
}

public function register_facet( array $config = [] ) : void {
$defaults = [
'label' => 'Categories',
'name' => 'categories',
'type' => 'checkboxes',
'source' => 'tax/category',
'parent_term' => '',
'hierarchical' => 'no',
'orderby' => 'count',
'count' => '20',
'show_expanded' => 'no',
'ghosts' => 'no',
'preserve_ghosts' => 'no',
'operator' => 'and',
'soft_limit' => '5'
];

$config = array_merge( $defaults, $config );

FWP()->helper->settings['facets'] = [ $config ];
}

public function clearFacets() : void {
FWP()->helper->settings['facets'] = [];
}
}
2 changes: 1 addition & 1 deletion tests/_support/_generated/WpunitTesterActionsActions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] efa78354d0c6741b589bfff3b6ca3626
<?php //[STAMP] 7a1c5ae97345f86d076a28fbb966d752
namespace _generated;

// This class was automatically generated by build task
Expand Down
Loading

0 comments on commit 5084ddb

Please sign in to comment.