Skip to content

Commit

Permalink
Merge a33aab2 into 724a70a
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed May 7, 2019
2 parents 724a70a + a33aab2 commit 43a0273
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 17 deletions.
30 changes: 30 additions & 0 deletions src/data/class-factory.php
Expand Up @@ -165,6 +165,36 @@ public static function resolve_shipping_method( $id ) {
return new Shipping_Method( $method );
}

/**
* Resolves a cart item by key.
*
* @param string $id cart item key.
*
* @return object
*/
public static function resolve_cart_item( $id ) {
$item = WC()->cart->get_cart_item( $id );

return $item;
}

/**
* Resolves a fee object by ID.
*
* @param int $id Fee object generated ID.
*
* @return object
*/
public static function resolve_cart_fee( $id ) {
$fees = WC()->cart->get_fees();

if ( ! empty( $fees[ $id ] ) ) {
return $fees[ $id ];
}

return null;
}

/**
* Resolves Coupon connections
*
Expand Down
90 changes: 88 additions & 2 deletions src/type/object/class-cart-type.php
Expand Up @@ -25,6 +25,7 @@ class Cart_Type {
* Register Cart-related types and queries to the WPGraphQL schema
*/
public static function register() {
self::register_cart_fee();
self::register_cart_item();
self::register_cart();

Expand Down Expand Up @@ -52,8 +53,7 @@ public static function register() {
),
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$cart = WC()->cart;
$item = $cart->get_cart_item( $args['key'] );
$item = Factory::resolve_cart_item( $args['key'] );
if ( empty( $item ) ) {
throw new UserError( __( 'The key input is invalid', 'wp-graphql-woocommerce' ) );
}
Expand All @@ -62,6 +62,28 @@ public static function register() {
},
)
);

register_graphql_field(
'RootQuery',
'cartFee',
array(
'type' => 'CartFee',
'args' => array(
'id' => array(
'type' => array( 'non_null' => 'ID' ),
),
),
'description' => __( 'The cart object', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$fee = Factory::resolve_cart_fee( $args['id'] );
if ( empty( $fee ) ) {
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
}

return $fee;
},
)
);
}

/**
Expand Down Expand Up @@ -189,6 +211,14 @@ public static function register_cart() {
: null;
},
),
'fees' => array(
'type' => array( 'list_of' => 'CartFee' ),
'description' => __( 'Additional fees on the cart.', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$fees = $source->get_fees();
return ! empty( $fees ) ? array_values( $fees ) : null;
},
),
),
)
);
Expand Down Expand Up @@ -267,4 +297,60 @@ public static function register_cart_item() {
)
);
}

/**
* Registers CartFee type
*/
public static function register_cart_fee() {
register_graphql_object_type(
'CartFee',
array(
'description' => __( 'An additional fee', 'wp-graphql-woocommerce' ),
'fields' => array(
'id' => array(
'type' => array( 'non_null' => 'ID' ),
'description' => __( 'Fee ID', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->id ) ? $source->id : null;
},
),
'name' => array(
'type' => array( 'non_null' => 'String' ),
'description' => __( 'Fee name', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->name ) ? $source->name : null;
},
),
'taxClass' => array(
'type' => 'TaxClassEnum',
'description' => __( 'Fee tax class', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->tax_class ) ? $source->tax_class : null;
},
),
'taxable' => array(
'type' => 'Boolean',
'description' => __( 'Is fee taxable?', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->taxable ) ? $source->taxable : null;
},
),
'amount' => array(
'type' => 'Float',
'description' => __( 'Fee amount', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->amount ) ? $source->amount : null;
},
),
'total' => array(
'type' => 'Float',
'description' => __( 'Fee total', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
return ! empty( $source->total ) ? $source->total : null;
},
),
),
)
);
}
}
42 changes: 42 additions & 0 deletions tests/_support/Helper/crud-helpers/cart.php
Expand Up @@ -49,6 +49,23 @@ public function print_item_query( $key ) {
);
}

public function print_fee_query( $id ) {
$cart = WC()->cart;
$fees = $cart->get_fees();
$fee = ! empty( $fees[ $id ] ) ? $fees[ $id ] : null;

return !empty( $fee )
? array(
'id' => $fee->id,
'name' => $fee->name,
'taxable' => $fee->taxable,
'taxClass' => $fee->tax_class,
'amount' => $fee->amount,
'total' => $fee->total,
)
: null;
}

public function print_nodes( $processors = array(), $_ = null ) {
$cart = WC()->cart;
$ids = array_keys( $cart->get_cart_contents() );
Expand All @@ -73,4 +90,29 @@ public function print_nodes( $processors = array(), $_ = null ) {

return array_values( array_map( $processors['mapper'], $results ) );
}

public function print_fee_nodes( $processors = array(), $_ = null ) {
$cart = WC()->cart;
$ids = array_keys( $cart->get_fees() );
$default_processors = array(
'mapper' => function( $id ) {
return array( 'id' => $id );
},
'sorter' => function( $id_a, $id_b ) {
return 0;
},
'filter' => function( $id ) {
return true;
}
);

$processors = array_merge( $default_processors, $processors );

$results = array_filter( $ids, $processors['filter'] );
if( ! empty( $results ) ) {
usort( $results, $processors['sorter'] );
}

return array_values( array_map( $processors['mapper'], $results ) );
}
}
76 changes: 70 additions & 6 deletions tests/wpunit/CartQueriesTest.php
Expand Up @@ -12,12 +12,12 @@ public function setUp() {
// before
parent::setUp();

$this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
$this->customer = $this->factory->user->create( array( 'role' => 'customer' ) );
$this->product_helper = $this->getModule('\Helper\Wpunit')->product();
$this->variation_helper = $this->getModule('\Helper\Wpunit')->product_variation();
$this->coupon_helper = $this->getModule('\Helper\Wpunit')->coupon();
$this->helper = $this->getModule('\Helper\Wpunit')->cart();
$this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
$this->customer = $this->factory->user->create( array( 'role' => 'customer' ) );
$this->product_helper = $this->getModule('\Helper\Wpunit')->product();
$this->variation_helper = $this->getModule('\Helper\Wpunit')->product_variation();
$this->coupon_helper = $this->getModule('\Helper\Wpunit')->coupon();
$this->helper = $this->getModule('\Helper\Wpunit')->cart();
}

public function tearDown()
Expand Down Expand Up @@ -159,4 +159,68 @@ public function testCartItemConnection() {
$this->assertEqualSets( $expected, $actual );
}

public function testCartFeeQuery() {
$product_id = $this->product_helper->create_simple();
WC()->cart->add_to_cart( $product_id, 3 );
WC()->cart->add_fee( 'Test fee', 30.50 );
$fee_ids = array_keys( WC()->cart->get_fees() );

$query = '
query cartFeeQuery( $id: ID! ) {
cartFee( id: $id ) {
id
name
taxClass
taxable
amount
total
}
}
';

/**
* Assertion One
*/
$variables = array( 'id' => $fee_ids[0] );
$actual = do_graphql_request( $query, 'cartFeeQuery', $variables );
$expected = array( 'data' => array( 'cartFee' => $this->helper->print_fee_query( $fee_ids[0] ) ) );

// use --debug flag to view.
codecept_debug( $actual );

$this->assertEqualSets( $expected, $actual );
}

public function testCartToCartFeeQuery() {
$product_id = $this->product_helper->create_simple();
WC()->cart->add_to_cart( $product_id, 3 );
WC()->cart->add_fee( 'Test fee', 30.50 );

$query = '
query cartQuery {
cart {
fees {
id
}
}
}
';

/**
* Assertion One
*/
$actual = do_graphql_request( $query, 'cartFeeQuery' );
$expected = array(
'data' => array(
'cart' => array(
'fees' => $this->helper->print_fee_nodes(),
),
),
);

// use --debug flag to view.
codecept_debug( $actual );

$this->assertEqualSets( $expected, $actual );
}
}
2 changes: 1 addition & 1 deletion vendor/autoload.php
Expand Up @@ -4,4 +4,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitb7bf723c502fa62b66818be8b05c5974::getLoader();
return ComposerAutoloaderInitb86120d91acd1402be294c91a0f7a14a::getLoader();
8 changes: 4 additions & 4 deletions vendor/composer/autoload_real.php
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInitb7bf723c502fa62b66818be8b05c5974
class ComposerAutoloaderInitb86120d91acd1402be294c91a0f7a14a
{
private static $loader;

Expand All @@ -19,15 +19,15 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInitb7bf723c502fa62b66818be8b05c5974', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitb86120d91acd1402be294c91a0f7a14a', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitb7bf723c502fa62b66818be8b05c5974', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitb86120d91acd1402be294c91a0f7a14a', 'loadClassLoader'));

$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';

call_user_func(\Composer\Autoload\ComposerStaticInitb7bf723c502fa62b66818be8b05c5974::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitb86120d91acd1402be294c91a0f7a14a::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInitb7bf723c502fa62b66818be8b05c5974
class ComposerStaticInitb86120d91acd1402be294c91a0f7a14a
{
public static $prefixLengthsPsr4 = array (
'W' =>
Expand Down Expand Up @@ -108,9 +108,9 @@ class ComposerStaticInitb7bf723c502fa62b66818be8b05c5974
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitb7bf723c502fa62b66818be8b05c5974::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb7bf723c502fa62b66818be8b05c5974::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb7bf723c502fa62b66818be8b05c5974::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitb86120d91acd1402be294c91a0f7a14a::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb86120d91acd1402be294c91a0f7a14a::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb86120d91acd1402be294c91a0f7a14a::$classMap;

}, null, ClassLoader::class);
}
Expand Down

0 comments on commit 43a0273

Please sign in to comment.