Skip to content

Commit 8485fff

Browse files
committed
CartQueriesTest updated.
1 parent 8c3a2de commit 8485fff

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ branches:
1717
- master
1818
- release-v0.1.2
1919
- release-v0.2.2
20-
- release-v0.3.1
20+
- release-v0.3.3
2121

2222
cache:
2323
apt: true

includes/connection/class-cart-items.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function get_connection_config( $args = array() ) {
8484
*/
8585
public static function get_connection_args() {
8686
return array(
87-
'needShipping' => array(
87+
'needsShipping' => array(
8888
'type' => 'Boolean',
8989
'description' => __( 'Limit results to cart items that require shipping', 'wp-graphql-woocommerce' ),
9090
),

includes/data/connection/class-cart-item-connection-resolver.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,37 @@ public function should_execute() {
3030
}
3131

3232
/**
33-
* Creates query arguments array
33+
* Creates cart item filters.
34+
*
35+
* @return array
3436
*/
3537
public function get_query_args() {
3638
$query_args = array();
3739
if ( ! empty( $this->args['where'] ) ) {
3840
$where_args = $this->args['where'];
39-
if ( ! empty( $where_args['needShipping'] ) ) {
41+
if ( isset( $where_args['needsShipping'] ) ) {
42+
$needs_shipping = $where_args['needsShipping'];
4043
$query_args['filters'] = array();
41-
$query_args['filters'][] = function( $cart_item ) {
44+
$query_args['filters'][] = function( $cart_item ) use ( $needs_shipping ) {
4245
$product = \WC()->product_factory->get_product( $cart_item['product_id'] );
4346
if ( $product ) {
44-
return $product->needs_shipping();
47+
return $needs_shipping === (bool) $product->needs_shipping();
4548
}
4649
};
4750
}
4851
}
4952

53+
/**
54+
* Filter the $query_args to allow folks to customize queries programmatically.
55+
*
56+
* @param array $query_args The args that will be passed to the WP_Query.
57+
* @param mixed $source The source that's passed down the GraphQL queries.
58+
* @param array $args The inputArgs on the field.
59+
* @param AppContext $context The AppContext passed down the GraphQL tree.
60+
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree.
61+
*/
62+
$query_args = apply_filters( 'graphql_cart_item_connection_query_args', $query_args, $this->source, $this->args, $this->context, $this->info );
63+
5064
return $query_args;
5165
}
5266

tests/wpunit/CartQueriesTest.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testCartItemQuery() {
117117

118118
public function testCartItemConnection() {
119119
$cart = WC()->cart;
120-
$cart->add_to_cart( $this->product_helper->create_simple(), 2 );
120+
$cart->add_to_cart( $this->product_helper->create_simple( array( 'virtual' => true ) ), 2 );
121121
$cart->add_to_cart( $this->product_helper->create_simple(), 1 );
122122
$cart->add_to_cart( $this->product_helper->create_simple(), 10 );
123123

@@ -132,9 +132,9 @@ public function testCartItemConnection() {
132132
$cart->apply_coupon( $code );
133133

134134
$query = '
135-
query {
135+
query($needsShipping: Boolean) {
136136
cart {
137-
contents {
137+
contents (where: {needsShipping: $needsShipping}) {
138138
nodes {
139139
key
140140
}
@@ -161,6 +161,76 @@ public function testCartItemConnection() {
161161
codecept_debug( $actual );
162162

163163
$this->assertEquals( $expected, $actual );
164+
165+
/**
166+
* Assertion Two
167+
*
168+
* Tests "needsShipping" parameter.
169+
*/
170+
$variables = array( 'needsShipping' => true );
171+
$actual = graphql(
172+
array(
173+
'query' => $query,
174+
'variables' => $variables,
175+
)
176+
);
177+
$expected = array(
178+
'data' => array(
179+
'cart' => array(
180+
'contents' => array(
181+
'nodes' => $this->helper->print_nodes(
182+
array(
183+
'filter' => function( $key ) {
184+
$item = WC()->cart->get_cart_item( $key );
185+
$product = WC()->product_factory->get_product( $item['product_id'] );
186+
return $product->needs_shipping();
187+
}
188+
)
189+
),
190+
),
191+
),
192+
),
193+
);
194+
195+
// use --debug flag to view.
196+
codecept_debug( $actual );
197+
198+
$this->assertEquals( $expected, $actual );
199+
200+
/**
201+
* Assertion Three
202+
*
203+
* Tests "needsShipping" parameter reversed.
204+
*/
205+
$variables = array( 'needsShipping' => false );
206+
$actual = graphql(
207+
array(
208+
'query' => $query,
209+
'variables' => $variables,
210+
)
211+
);
212+
$expected = array(
213+
'data' => array(
214+
'cart' => array(
215+
'contents' => array(
216+
'nodes' => $this->helper->print_nodes(
217+
array(
218+
'filter' => function( $key ) {
219+
$item = WC()->cart->get_cart_item( $key );
220+
$product = WC()->product_factory->get_product( $item['product_id'] );
221+
return ! $product->needs_shipping();
222+
}
223+
)
224+
),
225+
),
226+
),
227+
),
228+
);
229+
230+
// use --debug flag to view.
231+
codecept_debug( $actual );
232+
233+
$this->assertEquals( $expected, $actual );
164234
}
165235

166236
public function testCartFeeQuery() {

0 commit comments

Comments
 (0)