Skip to content

Commit afeac37

Browse files
committed
NewCustomerCheckingOutCept updated.
1 parent b7f52e2 commit afeac37

File tree

4 files changed

+223
-37
lines changed

4 files changed

+223
-37
lines changed

includes/utils/class-ql-session-handler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
namespace WPGraphQL\Extensions\WooCommerce\Utils;
1010

11+
use WC_Session_Handler;
12+
1113
/**
1214
* Class - QL_Session_Handler
1315
*/
14-
class QL_Session_Handler extends \WC_Session_Handler {
16+
class QL_Session_Handler extends WC_Session_Handler {
1517
/**
1618
* Encrypt and decrypt
1719
*

tests/_support/Helper/Acceptance.php

Lines changed: 101 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ class Acceptance extends \Codeception\Module {
1212
* @return array
1313
*/
1414
public function addToCart( array $input, $session_header = null ) {
15-
$rest = $this->getModule( 'REST' );
16-
1715
// Add to cart mutation
1816
$mutation = '
19-
mutation addToCart( $input: AddToCartInput! ) {
17+
mutation ( $input: AddToCartInput! ) {
2018
addToCart( input: $input ) {
2119
clientMutationId
2220
cartItem {
@@ -37,33 +35,82 @@ public function addToCart( array $input, $session_header = null ) {
3735
}
3836
';
3937

40-
// Add item to cart.
41-
$rest->haveHttpHeader( 'Content-Type', 'application/json' );
42-
if ( ! empty( $session_header ) ) {
43-
$rest->haveHttpHeader( 'woocommerce-session', $session_header );
44-
}
38+
// Execute query.
39+
$response = $this->executeQuery( $mutation, $input, $session_header, true );
4540

46-
// Send request.
47-
$rest->sendPOST(
48-
'/graphql',
49-
json_encode(
50-
array(
51-
'query' => $mutation,
52-
'variables' => array( 'input' => $input ),
53-
)
54-
)
55-
);
41+
// Return response.
42+
return $response;
43+
}
5644

57-
// Confirm success.
58-
$rest->seeResponseCodeIs( 200 );
59-
$rest->seeResponseIsJson();
45+
/**
46+
* Update cart items quantities.
47+
*
48+
* @param array $input
49+
* @param string $session_header
50+
* @return array
51+
*/
52+
public function updateQuantity( array $input, $session_header = null ) {
53+
// Update cart items mutation
54+
$mutation = '
55+
mutation updateItemQuantities( $input: UpdateItemQuantitiesInput! ) {
56+
updateItemQuantities( input: $input ) {
57+
clientMutationId
58+
updated {
59+
key
60+
quantity
61+
}
62+
removed {
63+
key
64+
quantity
65+
}
66+
items {
67+
key
68+
quantity
69+
}
70+
}
71+
}
72+
';
6073

61-
// Get response.
62-
$response = json_decode( $rest->grabResponse(), true );
74+
// Execute query.
75+
$response = $this->executeQuery( $mutation, $input, $session_header );
6376

64-
// Update session header.
65-
$rest->seeHttpHeaderOnce('woocommerce-session');
66-
$response['session_header'] = $rest->grabHttpHeader( 'woocommerce-session' );
77+
// Return response.
78+
return $response;
79+
}
80+
81+
/**
82+
* Removes an items from the cart.
83+
*
84+
* @param array $input
85+
* @param string $session_header
86+
* @return array
87+
*/
88+
public function removeFromCart( array $input, $session_header = null ) {
89+
// Remove item from cart mutation
90+
$mutation = '
91+
mutation ( $input: RemoveItemsFromCartInput! ) {
92+
removeItemsFromCart( input: $input ) {
93+
clientMutationId
94+
cartItems {
95+
key
96+
product {
97+
id
98+
}
99+
variation {
100+
id
101+
}
102+
quantity
103+
subtotal
104+
subtotalTax
105+
total
106+
tax
107+
}
108+
}
109+
}
110+
';
111+
112+
// Execute query.
113+
$response = $this->executeQuery( $mutation, $input, $session_header );
67114

68115
// Return response.
69116
return $response;
@@ -78,8 +125,6 @@ public function addToCart( array $input, $session_header = null ) {
78125
* @return array
79126
*/
80127
public function checkout( array $input, $session_header = null ) {
81-
$rest = $this->getModule( 'REST' );
82-
83128
// Checkout mutation.
84129
$mutation = '
85130
mutation checkout( $input: CheckoutInput! ) {
@@ -230,11 +275,33 @@ public function checkout( array $input, $session_header = null ) {
230275
}
231276
';
232277

233-
// Checkout customer order.
278+
// Execute query.
279+
$response = $this->executeQuery( $mutation, $input, $session_header );
280+
281+
// Return response.
282+
return $response;
283+
}
284+
285+
/**
286+
* Executes GraphQL query and returns a response
287+
*
288+
* @param string $mutation
289+
* @param array $input
290+
* @param string|null $session_header
291+
* @param bool $update_header
292+
*
293+
* @return array
294+
*/
295+
public function executeQuery( string $mutation, array $input, $session_header = null, $update_header = false ) {
296+
$rest = $this->getModule( 'REST' );
297+
298+
// Add item to cart.
234299
$rest->haveHttpHeader( 'Content-Type', 'application/json' );
235300
if ( ! empty( $session_header ) ) {
236301
$rest->haveHttpHeader( 'woocommerce-session', $session_header );
237302
}
303+
304+
// Send request.
238305
$rest->sendPOST(
239306
'/graphql',
240307
json_encode(
@@ -252,11 +319,12 @@ public function checkout( array $input, $session_header = null ) {
252319
// Get response.
253320
$response = json_decode( $rest->grabResponse(), true );
254321

255-
// Update session header.
256-
$rest->seeHttpHeaderOnce('woocommerce-session');
257-
$response['session_header'] = $rest->grabHttpHeader( 'woocommerce-session' );
322+
if ( $update_header ) {
323+
// Update session header.
324+
$rest->seeHttpHeaderOnce('woocommerce-session');
325+
$response['session_header'] = $rest->grabHttpHeader( 'woocommerce-session' );
326+
}
258327

259-
// Return response.
260328
return $response;
261329
}
262330

tests/acceptance/NewCustomerCheckingOutCept.php

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
$I = new AcceptanceTester($scenario);
33
$product_catalog = $I->getCatalog();
4-
$I->wantTo('add an some items to the cart and checkout.');
4+
$I->wantTo('add items to the cart');
55

66
$add_to_cart_input = array(
77
'clientMutationId' => 'someId',
@@ -14,11 +14,127 @@
1414
// use --debug flag to view
1515
codecept_debug( $success );
1616

17+
if ( ! empty ( $success['session_header'] ) ) {
18+
$session = $success['session_header'];
19+
}
20+
21+
$I->assertArrayNotHasKey( 'error', $success );
22+
$I->assertArrayHasKey('data', $success );
23+
$I->assertArrayHasKey('addToCart', $success['data'] );
24+
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
25+
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
26+
$shirt_key = $success['data']['addToCart']['cartItem']['key'];
27+
28+
$add_to_cart_input = array(
29+
'clientMutationId' => 'someId',
30+
'productId' => $product_catalog['belt'],
31+
'quantity' => 2,
32+
);
33+
34+
$success = $I->addToCart( $add_to_cart_input, $session );
35+
36+
// use --debug flag to view
37+
codecept_debug( $success );
38+
39+
if ( ! empty ( $success['session_header'] ) ) {
40+
$session = $success['session_header'];
41+
}
42+
43+
$I->assertArrayNotHasKey( 'error', $success );
44+
$I->assertArrayHasKey('data', $success );
45+
$I->assertArrayHasKey('addToCart', $success['data'] );
46+
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
47+
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
48+
$belt_key = $success['data']['addToCart']['cartItem']['key'];
49+
50+
$add_to_cart_input = array(
51+
'clientMutationId' => 'someId',
52+
'productId' => $product_catalog['jeans'],
53+
'quantity' => 4,
54+
);
55+
56+
$success = $I->addToCart( $add_to_cart_input, $session );
57+
58+
// use --debug flag to view
59+
codecept_debug( $success );
60+
61+
if ( ! empty ( $success['session_header'] ) ) {
62+
$session = $success['session_header'];
63+
}
64+
65+
$I->assertArrayNotHasKey( 'error', $success );
66+
$I->assertArrayHasKey('data', $success );
67+
$I->assertArrayHasKey('addToCart', $success['data'] );
68+
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
69+
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
70+
$jeans_key = $success['data']['addToCart']['cartItem']['key'];
71+
72+
$add_to_cart_input = array(
73+
'clientMutationId' => 'someId',
74+
'productId' => $product_catalog['socks'],
75+
'quantity' => 1,
76+
);
77+
78+
$success = $I->addToCart( $add_to_cart_input, $session );
79+
80+
// use --debug flag to view
81+
codecept_debug( $success );
82+
83+
if ( ! empty ( $success['session_header'] ) ) {
84+
$session = $success['session_header'];
85+
}
86+
1787
$I->assertArrayNotHasKey( 'error', $success );
1888
$I->assertArrayHasKey('data', $success );
1989
$I->assertArrayHasKey('addToCart', $success['data'] );
2090
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
2191
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
92+
$socks_key = $success['data']['addToCart']['cartItem']['key'];
93+
94+
$I->wantTo('remove some items from the cart');
95+
96+
$remove_from_cart_input = array(
97+
'clientMutationId' => 'someId',
98+
'keys' => $socks_key,
99+
);
100+
101+
$success = $I->removeFromCart( $remove_from_cart_input, $session );
102+
103+
// use --debug flag to view
104+
codecept_debug( $success );
105+
106+
$I->assertArrayNotHasKey( 'error', $success );
107+
$I->assertArrayHasKey('data', $success );
108+
$I->assertArrayHasKey('removeItemsFromCart', $success['data'] );
109+
$I->assertArrayHasKey('cartItems', $success['data']['removeItemsFromCart'] );
110+
$I->assertCount( 1, $success['data']['removeItemsFromCart']['cartItems'] );
111+
112+
$I->wantTo('update an item in the cart');
113+
114+
$update_quantity_input = array(
115+
'clientMutationId' => 'someId',
116+
'items' => array(
117+
array( 'key' => $belt_key, 'quantity' => 0 ),
118+
array( 'key' => $jeans_key, 'quantity' => 1 ),
119+
),
120+
);
121+
122+
$success = $I->updateQuantity( $update_quantity_input, $session );
123+
124+
// use --debug flag to view
125+
codecept_debug( $success );
126+
127+
$I->assertArrayNotHasKey( 'error', $success );
128+
$I->assertArrayHasKey('data', $success );
129+
$I->assertArrayHasKey('updateItemQuantities', $success['data'] );
130+
$I->assertArrayHasKey('removed', $success['data']['updateItemQuantities'] );
131+
$I->assertCount( 1, $success['data']['updateItemQuantities']['removed'] );
132+
$I->assertArrayHasKey('updated', $success['data']['updateItemQuantities'] );
133+
$I->assertCount( 1, $success['data']['updateItemQuantities']['updated'] );
134+
$I->assertArrayHasKey('items', $success['data']['updateItemQuantities'] );
135+
$I->assertCount( 2, $success['data']['updateItemQuantities']['items'] );
136+
137+
$I->wantTo('checkout');
22138

23139
$checkout_input = array(
24140
'clientMutationId' => 'someId',
@@ -46,7 +162,7 @@
46162
),
47163
);
48164

49-
$success = $I->checkout( $checkout_input, $success['session_header'] );
165+
$success = $I->checkout( $checkout_input, $session );
50166

51167
// use --debug flag to view
52168
codecept_debug( $success );

vendor/composer/ClassLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function isClassMapAuthoritative()
279279
*/
280280
public function setApcuPrefix($apcuPrefix)
281281
{
282-
$this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
282+
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
283283
}
284284

285285
/**

0 commit comments

Comments
 (0)