Skip to content

Commit

Permalink
Merge pull request #312 from kidunot89/feature/better-add-to-cart-err…
Browse files Browse the repository at this point in the history
…or-handling

New error handling method introduced in the "addToCart" mutation
  • Loading branch information
kidunot89 committed Aug 5, 2020
2 parents 37faa95 + 6929941 commit 1b78f92
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 148 deletions.
10 changes: 8 additions & 2 deletions includes/mutation/class-cart-add-item.php
Expand Up @@ -107,8 +107,14 @@ public static function mutate_and_get_payload() {
// Add item to cart and get item key.
$cart_item_key = \WC()->cart->add_to_cart( ...$cart_item_args );

if ( empty( $cart_item_key ) ) {
throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) );
if ( false === $cart_item_key ) {
$notices = \WC()->session->get( 'wc_notices' );
if ( ! empty( $notices['error'] ) ) {
$cart_error_messages = implode( ' ', array_column( $notices['error'], 'notice' ) );
throw new UserError( $cart_error_messages );
} else {
throw new UserError( __( 'Failed to add cart item. Please check input.', 'wp-graphql-woocommerce' ) );
}
}

// Return payload.
Expand Down
203 changes: 57 additions & 146 deletions tests/wpunit/CartMutationsTest.php
Expand Up @@ -23,7 +23,17 @@ public function tearDown() {
\WC()->cart->empty_cart( true );

parent::tearDown();
}
}

private function graphql( $query, $operation_name = null, $variables = null ) {
// Run GraphQL request.
$results = graphql( compact( 'query', 'operation_name', 'variables' ) );

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

return $results;
}

private function addToCart( $input ) {
$mutation = '
Expand Down Expand Up @@ -53,15 +63,7 @@ private function addToCart( $input ) {
}
';

$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'addToCart',
'variables' => array( 'input' => $input ),
)
);

return $actual;
return $this->graphql( $mutation, 'addToCart', compact( 'input' ) );
}

private function removeItemsFromCart( $input ) {
Expand Down Expand Up @@ -92,15 +94,7 @@ private function removeItemsFromCart( $input ) {
}
';

$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'removeItemsFromCart',
'variables' => array( 'input' => $input ),
)
);

return $actual;
return $this->graphql( $mutation, 'removeItemsFromCart', compact( 'input' ) );
}

private function restoreItems( $input ) {
Expand Down Expand Up @@ -129,17 +123,9 @@ private function restoreItems( $input ) {
}
}
}
';

$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'restoreCartItems',
'variables' => array( 'input' => $input ),
)
);
';

return $actual;
return $this->graphql( $mutation, 'restoreCartItems', compact( 'input' ) );
}

// tests
Expand All @@ -153,9 +139,6 @@ public function testAddToCartMutationWithProduct() {
)
);

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

// Retrieve cart item key.
$this->assertArrayHasKey('data', $actual );
$this->assertArrayHasKey('addToCart', $actual['data'] );
Expand Down Expand Up @@ -202,9 +185,6 @@ public function testAddToCartMutationWithProductVariation() {
)
);

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

// Retrieve cart item key.
$this->assertArrayHasKey('data', $actual );
$this->assertArrayHasKey('addToCart', $actual['data'] );
Expand Down Expand Up @@ -297,26 +277,21 @@ public function testUpdateCartItemQuantitiesMutation() {
}
';

$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'updateItemQuantities',
'variables' => array(
'input' => array(
'clientMutationId' => 'someId',
'items' => array(
array( 'key' => $key_1, 'quantity' => 4 ),
array( 'key' => $key_2, 'quantity' => 2 ),
array( 'key' => $key_3, 'quantity' => 0 ),
),
),
),
)
$actual = $this->graphql(
$mutation,
'updateItemQuantities',
array(
'input' => array(
'clientMutationId' => 'someId',
'items' => array(
array( 'key' => $key_1, 'quantity' => 4 ),
array( 'key' => $key_2, 'quantity' => 2 ),
array( 'key' => $key_3, 'quantity' => 0 ),
),
),
)
);

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

// Check cart item data.
$expected = array(
'data' => array(
Expand Down Expand Up @@ -365,9 +340,6 @@ public function testRemoveItemsFromCartMutation() {
)
);

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

$expected = array(
'data' => array(
'removeItemsFromCart' => array(
Expand Down Expand Up @@ -425,9 +397,6 @@ public function testRemoveItemsFromCartMutationWithMultipleItems() {
)
);

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

$expected = array(
'data' => array(
'removeItemsFromCart' => array(
Expand Down Expand Up @@ -486,9 +455,6 @@ public function testRemoveItemsFromCartMutationUsingAllField() {
)
);

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

$expected = array(
'data' => array(
'removeItemsFromCart' => array(
Expand Down Expand Up @@ -537,9 +503,6 @@ public function testRestoreCartItemsMutation() {
)
);

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

$expected = array(
'data' => array(
'restoreCartItems' => array(
Expand Down Expand Up @@ -604,9 +567,6 @@ public function testRestoreCartItemsMutationWithMultipleItems() {
)
);

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

$expected = array(
'data' => array(
'restoreCartItems' => array(
Expand Down Expand Up @@ -666,16 +626,7 @@ public function testEmptyCartMutation() {
$variables = array(
'input' => array( 'clientMutationId' => 'someId' ),
);
$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'emptyCart',
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, 'emptyCart', $variables );

$expected = array(
'data' => array(
Expand Down Expand Up @@ -771,16 +722,7 @@ public function testApplyCouponMutation() {
'code' => $coupon_code,
),
);
$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'applyCoupon',
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, 'applyCoupon', $variables );

// Get updated cart item.
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
Expand Down Expand Up @@ -877,15 +819,7 @@ public function testApplyCouponMutationWithInvalidCoupons() {
'code' => $coupon_id,
),
);
$actual = graphql(
array(
'query' => $mutation,
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, null, $variables );

$this->assertNotEmpty( $actual['errors'] );
$this->assertEmpty( $actual['data']['applyCoupon'] );
Expand All @@ -901,15 +835,7 @@ public function testApplyCouponMutationWithInvalidCoupons() {
'code' => $expired_coupon_code,
),
);
$actual = graphql(
array(
'query' => $mutation,
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, null, $variables );

$this->assertNotEmpty( $actual['errors'] );
$this->assertEmpty( $actual['data']['applyCoupon'] );
Expand All @@ -925,15 +851,7 @@ public function testApplyCouponMutationWithInvalidCoupons() {
'code' => $applied_coupon_code,
),
);
$actual = graphql(
array(
'query' => $mutation,
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, null, $variables );

$this->assertNotEmpty( $actual['errors'] );
$this->assertEmpty( $actual['data']['applyCoupon'] );
Expand Down Expand Up @@ -995,16 +913,7 @@ public function testRemoveCouponMutation() {
'codes' => array( $coupon_code ),
),
);
$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'removeCoupons',
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, 'removeCoupons', $variables );

// Get updated cart item.
$cart_item = \WC()->cart->get_cart_item( $cart_item_key );
Expand Down Expand Up @@ -1078,30 +987,12 @@ public function testAddFeeMutation() {
'amount' => 49.99,
),
);
$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'addFee',
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, 'addFee', $variables );

$this->assertArrayHasKey('errors', $actual );

wp_set_current_user( $this->shop_manager );
$actual = graphql(
array(
'query' => $mutation,
'operation_name' => 'addFee',
'variables' => $variables,
)
);

// use --debug flag to view.
codecept_debug( $actual );
$actual = $this->graphql( $mutation, 'addFee', $variables );

$expected = array(
'data' => array(
Expand All @@ -1113,5 +1004,25 @@ public function testAddFeeMutation() {
);

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

public function testAddToCartMutationErrors() {
// Create products.
$product_id = $this->product->create_simple(
array(
'manage_stock' => true,
'stock_quantity' => 1,
)
);

$failed = $this->addToCart(
array(
'clientMutationId' => 'someId',
'productId' => $product_id,
'quantity' => 5,
)
);

$this->assertArrayHasKey( 'errors', $failed );
}
}

0 comments on commit 1b78f92

Please sign in to comment.