Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 555ccb3

Browse files
author
Jens Schulze
committed
feat: support updateProductData of cart on customer login
Closes #377
1 parent 37db8de commit 555ccb3

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
lines changed

src/Core/Request/Customers/CustomerLoginRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CustomerLoginRequest extends AbstractApiRequest
3030
const ANONYMOUS_CART_SIGN_IN_MODE = 'anonymousCartSignInMode';
3131
const SIGN_IN_MODE_MERGE = 'MergeWithExistingCustomerCart';
3232
const SIGN_IN_MODE_NEW = 'UseAsNewActiveCustomerCart';
33+
const UPDATE_PRODUCT_DATA = 'updateProductData';
3334

3435
/**
3536
* @var string
@@ -48,6 +49,11 @@ class CustomerLoginRequest extends AbstractApiRequest
4849

4950
protected $anonymousCartSignInMode;
5051

52+
/**
53+
* @var bool
54+
*/
55+
protected $updateProductData;
56+
5157
protected $resultClass = CustomerSigninResult::class;
5258

5359
/**
@@ -140,6 +146,24 @@ public function setAnonymousCartSignInMode($anonymousCartSignInMode)
140146
return $this;
141147
}
142148

149+
/**
150+
* @return bool
151+
*/
152+
public function getUpdateProductData()
153+
{
154+
return $this->updateProductData;
155+
}
156+
157+
/**
158+
* @param bool $updateProductData
159+
* @return $this
160+
*/
161+
public function setUpdateProductData($updateProductData)
162+
{
163+
$this->updateProductData = $updateProductData;
164+
return $this;
165+
}
166+
143167
/**
144168
* @param string $email
145169
* @param string $password
@@ -152,6 +176,27 @@ public static function ofEmailAndPassword($email, $password, $anonymousCartId =
152176
return new static($email, $password, $anonymousCartId, $context);
153177
}
154178

179+
/**
180+
* @param string $email
181+
* @param string $password
182+
* @param bool $updateProductData
183+
* @param string $anonymousCartId
184+
* @param Context $context
185+
* @return static
186+
*/
187+
public static function ofEmailPasswordAndUpdateProductData(
188+
$email,
189+
$password,
190+
$updateProductData,
191+
$anonymousCartId = null,
192+
Context $context = null
193+
) {
194+
$request = new static($email, $password, $anonymousCartId, $context);
195+
$request->setUpdateProductData($updateProductData);
196+
197+
return $request;
198+
}
199+
155200
/**
156201
* @return JsonRequest
157202
* @internal
@@ -168,6 +213,9 @@ public function httpRequest()
168213
if (!is_null($this->anonymousCartSignInMode)) {
169214
$payload[static::ANONYMOUS_CART_SIGN_IN_MODE] = $this->anonymousCartSignInMode;
170215
}
216+
if (!is_null($this->updateProductData)) {
217+
$payload[static::UPDATE_PRODUCT_DATA] = $this->updateProductData;
218+
}
171219
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
172220
}
173221

src/Core/Request/Me/MeLoginRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class MeLoginRequest extends AbstractApiRequest
3333
const ANONYMOUS_CART_SIGN_IN_MODE = 'anonymousCartSignInMode';
3434
const SIGN_IN_MODE_MERGE = 'MergeWithExistingCustomerCart';
3535
const SIGN_IN_MODE_NEW = 'UseAsNewActiveCustomerCart';
36+
const UPDATE_PRODUCT_DATA = 'updateProductData';
3637

3738
/**
3839
* @var string
@@ -51,6 +52,11 @@ class MeLoginRequest extends AbstractApiRequest
5152

5253
protected $anonymousCartSignInMode;
5354

55+
/**
56+
* @var bool
57+
*/
58+
protected $updateProductData;
59+
5460
protected $resultClass = CustomerSigninResult::class;
5561

5662
/**
@@ -143,6 +149,24 @@ public function setAnonymousCartSignInMode($anonymousCartSignInMode)
143149
return $this;
144150
}
145151

152+
/**
153+
* @return bool
154+
*/
155+
public function getUpdateProductData()
156+
{
157+
return $this->updateProductData;
158+
}
159+
160+
/**
161+
* @param bool $updateProductData
162+
* @return $this
163+
*/
164+
public function setUpdateProductData($updateProductData)
165+
{
166+
$this->updateProductData = $updateProductData;
167+
return $this;
168+
}
169+
146170
/**
147171
* @param string $email
148172
* @param string $password
@@ -155,6 +179,27 @@ public static function ofEmailAndPassword($email, $password, $anonymousCartId =
155179
return new static($email, $password, $anonymousCartId, $context);
156180
}
157181

182+
/**
183+
* @param string $email
184+
* @param string $password
185+
* @param bool $updateProductData
186+
* @param string $anonymousCartId
187+
* @param Context $context
188+
* @return static
189+
*/
190+
public static function ofEmailPasswordAndUpdateProductData(
191+
$email,
192+
$password,
193+
$updateProductData,
194+
$anonymousCartId = null,
195+
Context $context = null
196+
) {
197+
$request = new static($email, $password, $anonymousCartId, $context);
198+
$request->setUpdateProductData($updateProductData);
199+
200+
return $request;
201+
}
202+
158203
/**
159204
* @return string
160205
* @internal
@@ -180,6 +225,9 @@ public function httpRequest()
180225
if (!is_null($this->anonymousCartSignInMode)) {
181226
$payload[static::ANONYMOUS_CART_SIGN_IN_MODE] = $this->anonymousCartSignInMode;
182227
}
228+
if (!is_null($this->updateProductData)) {
229+
$payload[static::UPDATE_PRODUCT_DATA] = $this->updateProductData;
230+
}
183231
return new JsonRequest(HttpMethod::POST, $this->getPath(), $payload);
184232
}
185233

tests/integration/Customer/CustomerLoginRequestTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,47 @@ public function testCartMergeOnLogin()
390390
$this->assertSame(CartState::MERGED, $anonCart->getCartState());
391391
$this->assertSame($customer->getId(), $loggedInCart->getCustomerId());
392392
}
393+
394+
public function testCartUpdateProductDataOnLogin()
395+
{
396+
$client = $this->getClient();
397+
398+
$customerDraft = $this->getCustomerDraft();
399+
$customer = $this->createCustomer($customerDraft);
400+
401+
$customerCartDraft = $this->getCartDraft();
402+
$customerCartDraft->setCustomerId($customer->getId());
403+
$customerCart = $this->getCart($customerCartDraft);
404+
405+
$anonCartDraft = $this->getCartDraft();
406+
$request = CartCreateRequest::ofDraft($anonCartDraft);
407+
$response = $request->executeWithClient($client);
408+
$anonCart = $request->mapResponse($response);
409+
410+
$request = CustomerLoginRequest::ofEmailPasswordAndUpdateProductData(
411+
$customer->getEmail(),
412+
$customerDraft->getPassword(),
413+
true,
414+
$anonCart->getId()
415+
)->setAnonymousCartSignInMode(CustomerLoginRequest::SIGN_IN_MODE_MERGE);
416+
$body = json_decode((string)$request->httpRequest()->getBody(), true);
417+
$this->assertTrue($body['updateProductData']);
418+
$response = $request->executeWithClient($client);
419+
$result = $request->mapResponse($response);
420+
421+
$loggedInCart = $result->getCart();
422+
423+
$request = CartByIdGetRequest::ofId($anonCart->getId());
424+
$response = $request->executeWithClient($client);
425+
$anonCart = $request->mapResponse($response);
426+
427+
$request = CartDeleteRequest::ofIdAndVersion($anonCart->getId(), $anonCart->getVersion());
428+
$response = $request->executeWithClient($client);
429+
$anonCart = $request->mapResponse($response);
430+
431+
$this->assertNotSame($anonCart->getId(), $loggedInCart->getId());
432+
$this->assertSame($customerCart->getId(), $loggedInCart->getId());
433+
$this->assertSame(CartState::MERGED, $anonCart->getCartState());
434+
$this->assertSame($customer->getId(), $loggedInCart->getCustomerId());
435+
}
393436
}

tests/integration/Me/MeRequestTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function testMeDelete()
263263
$this->customer = null;
264264

265265
$this->assertFalse($response->isError());
266-
266+
267267
$request = CustomerByIdGetRequest::ofId($customer->getId());
268268
$response = $request->executeWithClient($this->getClient());
269269
$this->assertTrue($response->isError());
@@ -299,6 +299,38 @@ public function testLoginSuccess()
299299
$this->assertSame($customer->getId(), $result->getCustomer()->getId());
300300
}
301301

302+
public function testLoginSuccessUpdateProductData()
303+
{
304+
$customerDraft = $this->getCustomerDraft();
305+
$customer = $this->getCustomer($customerDraft);
306+
307+
$config = $this->getClientConfig(['manage_my_profile']);
308+
$config->setGrantType(Config::GRANT_TYPE_PASSWORD)
309+
->setUsername($customer->getEmail())
310+
->setPassword($customerDraft->getPassword())
311+
;
312+
313+
$handler = new TestHandler();
314+
$logger = new Logger('testOauth');
315+
$logger->pushHandler($handler);
316+
317+
$client = Client::ofConfigCacheAndLogger($config, $this->getCache(), $this->getLogger());
318+
$client->getOauthManager()->getHttpClient(['verify' => $this->getVerifySSL()])->setLogger($logger);
319+
$client->getHttpClient(['verify' => $this->getVerifySSL()]);
320+
321+
$request = MeLoginRequest::ofEmailPasswordAndUpdateProductData(
322+
$customer->getEmail(),
323+
$customerDraft->getPassword(),
324+
true
325+
);
326+
$body = json_decode((string)$request->httpRequest()->getBody(), true);
327+
$this->assertTrue($body['updateProductData']);
328+
$response = $request->executeWithClient($this->getClient());
329+
$result = $request->mapResponse($response);
330+
331+
$this->assertSame($customer->getId(), $result->getCustomer()->getId());
332+
}
333+
302334
public function testLoginSuccessLowerCased()
303335
{
304336
$customerDraft = $this->getCustomerDraft();

0 commit comments

Comments
 (0)