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

Commit

Permalink
feat(Cart): support setAnonymousId
Browse files Browse the repository at this point in the history
Closes #338
  • Loading branch information
Jens Schulze committed Sep 6, 2017
1 parent afcaeda commit 1a1e472
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Core/Request/Carts/Command/CartSetAnonymousIdAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\Carts\Command;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractAction;

/**
* @package Commercetools\Core\Request\Carts\Command
* @link https://dev.commercetools.com/http-api-projects-carts.html#set-anonymous-id
* @method string getAction()
* @method CartSetAnonymousIdAction setAction(string $action = null)
* @method string getAnonymousId()
* @method CartSetAnonymousIdAction setAnonymousId(string $anonymousId = null)
*/
class CartSetAnonymousIdAction extends AbstractAction
{
public function fieldDefinitions()
{
return [
'action' => [static::TYPE => 'string'],
'anonymousId' => [static::TYPE => 'string'],
];
}

/**
* @param array $data
* @param Context|callable $context
*/
public function __construct(array $data = [], $context = null)
{
parent::__construct($data, $context);
$this->setAction('setAnonymousId');
}
}
31 changes: 31 additions & 0 deletions tests/integration/Cart/CartUpdateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use Commercetools\Core\Request\Carts\Command\CartRemoveDiscountCodeAction;
use Commercetools\Core\Request\Carts\Command\CartRemoveLineItemAction;
use Commercetools\Core\Request\Carts\Command\CartRemovePaymentAction;
use Commercetools\Core\Request\Carts\Command\CartSetAnonymousIdAction;
use Commercetools\Core\Request\Carts\Command\CartSetBillingAddressAction;
use Commercetools\Core\Request\Carts\Command\CartSetCountryAction;
use Commercetools\Core\Request\Carts\Command\CartSetCustomerEmailAction;
Expand Down Expand Up @@ -654,6 +655,36 @@ public function testSetCountry()
$this->assertSame($country, $cart->getCountry());
}

public function testSetAnonymousId()
{
$anonymousId = uniqid();
$draft = $this->getDraft();
$draft->setAnonymousId($anonymousId);
$cart = $this->createCart($draft);
$this->assertSame($anonymousId, $cart->getAnonymousId());

$newAnonymousId = uniqid();

$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
->addAction(CartSetAnonymousIdAction::of()->setAnonymousId($newAnonymousId))
;
$response = $request->executeWithClient($this->getClient());
$cart = $request->mapResponse($response);
$this->deleteRequest->setVersion($cart->getVersion());

$this->assertSame($newAnonymousId, $cart->getAnonymousId());

$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
->addAction(CartSetAnonymousIdAction::of())
;
$response = $request->executeWithClient($this->getClient());
$cart = $request->mapResponse($response);
$this->deleteRequest->setVersion($cart->getVersion());

$this->assertNull($cart->getAnonymousId());

}

public function testSetShippingMethod()
{
$draft = $this->getDraft();
Expand Down

0 comments on commit 1a1e472

Please # to comment.