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

Commit

Permalink
Merge pull request #455 from commercetools/order-setCustomerId-#435
Browse files Browse the repository at this point in the history
feat(Order): support setCustomerId

Closes #435
  • Loading branch information
Jens Schulze committed Feb 15, 2019
2 parents 75f654d + 756811d commit c9f5c55
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Core/Builder/Update/OrdersActionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemShippingDetailsAction;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomTypeAction;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerEmail;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerIdAction;
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryAddressAction;
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryItemsAction;
use Commercetools\Core\Request\Orders\Command\OrderSetLineItemCustomFieldAction;
Expand Down Expand Up @@ -279,6 +280,17 @@ public function setCustomerEmail($action = null)
return $this;
}

/**
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-customer-id
* @param OrderSetCustomerIdAction|callable $action
* @return $this
*/
public function setCustomerId($action = null)
{
$this->addAction($this->resolveAction(OrderSetCustomerIdAction::class, $action));
return $this;
}

/**
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-delivery-address
* @param OrderSetDeliveryAddressAction|callable $action
Expand Down
56 changes: 56 additions & 0 deletions src/Core/Model/Message/OrderCustomerSetMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
*/

namespace Commercetools\Core\Model\Message;

use Commercetools\Core\Model\Customer\CustomerReference;
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
use Commercetools\Core\Model\Common\DateTimeDecorator;
use DateTime;
use Commercetools\Core\Model\Common\Reference;

/**
* @package Commercetools\Core\Model\Message
* @link https://docs.commercetools.com/http-api-projects-messages.html#ordercustomerset-message
*
* @method string getId()
* @method OrderCustomerSetMessage setId(string $id = null)
* @method int getVersion()
* @method OrderCustomerSetMessage setVersion(int $version = null)
* @method DateTimeDecorator getCreatedAt()
* @method OrderCustomerSetMessage setCreatedAt(DateTime $createdAt = null)
* @method DateTimeDecorator getLastModifiedAt()
* @method OrderCustomerSetMessage setLastModifiedAt(DateTime $lastModifiedAt = null)
* @method int getSequenceNumber()
* @method OrderCustomerSetMessage setSequenceNumber(int $sequenceNumber = null)
* @method Reference getResource()
* @method OrderCustomerSetMessage setResource(Reference $resource = null)
* @method int getResourceVersion()
* @method OrderCustomerSetMessage setResourceVersion(int $resourceVersion = null)
* @method string getType()
* @method OrderCustomerSetMessage setType(string $type = null)
* @method CustomerReference getCustomer()
* @method OrderCustomerSetMessage setCustomer(CustomerReference $customer = null)
* @method CustomerGroupReference getCustomerGroup()
* @method OrderCustomerSetMessage setCustomerGroup(CustomerGroupReference $customerGroup = null)
* @method CustomerReference getOldCustomer()
* @method OrderCustomerSetMessage setOldCustomer(CustomerReference $oldCustomer = null)
* @method CustomerGroupReference getOldCustomerGroup()
* @method OrderCustomerSetMessage setOldCustomerGroup(CustomerGroupReference $oldCustomerGroup = null)
*/
class OrderCustomerSetMessage extends Message
{
const MESSAGE_TYPE = 'OrderCustomerSet';

public function fieldDefinitions()
{
$definitions = parent::fieldDefinitions();
$definitions['customer'] = [static::TYPE => CustomerReference::class];
$definitions['customerGroup'] = [static::TYPE => CustomerGroupReference::class];
$definitions['oldCustomer'] = [static::TYPE => CustomerReference::class];
$definitions['oldCustomerGroup'] = [static::TYPE => CustomerGroupReference::class];

return $definitions;
}
}
38 changes: 38 additions & 0 deletions src/Core/Request/Orders/Command/OrderSetCustomerIdAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
*/

namespace Commercetools\Core\Request\Orders\Command;

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

/**
* @package Commercetools\Core\Request\Orders\Command
*
* @link https://docs.commercetools.com/http-api-projects-orders.html#set-customer-id
* @method string getAction()
* @method OrderSetCustomerIdAction setAction(string $action = null)
* @method string getCustomerId()
* @method OrderSetCustomerIdAction setCustomerId(string $customerId = null)
*/
class OrderSetCustomerIdAction extends AbstractAction
{
public function fieldDefinitions()
{
return [
'action' => [static::TYPE => 'string'],
'customerId' => [static::TYPE => 'string'],
];
}

/**
* @param array $data
* @param Context|callable $context
*/
public function __construct(array $data = [], $context = null)
{
parent::__construct($data, $context);
$this->setAction('setCustomerId');
}
}
41 changes: 41 additions & 0 deletions tests/integration/Order/OrderUpdateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Commercetools\Core\Model\Common\PriceDraft;
use Commercetools\Core\Model\Common\PriceDraftCollection;
use Commercetools\Core\Model\Customer\Customer;
use Commercetools\Core\Model\Customer\CustomerDraft;
use Commercetools\Core\Model\Customer\CustomerSigninResult;
use Commercetools\Core\Model\Order\DeliveryItem;
use Commercetools\Core\Model\Order\DeliveryItemCollection;
use Commercetools\Core\Model\Order\Order;
Expand All @@ -43,6 +45,8 @@
use Commercetools\Core\Request\Carts\CartCreateRequest;
use Commercetools\Core\Request\Carts\CartDeleteRequest;
use Commercetools\Core\Request\Carts\CartReplicateRequest;
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
use Commercetools\Core\Request\Orders\Command\OrderAddDeliveryAction;
use Commercetools\Core\Request\Orders\Command\OrderAddItemShippingAddressAction;
use Commercetools\Core\Request\Orders\Command\OrderAddParcelToDeliveryAction;
Expand All @@ -57,6 +61,7 @@
use Commercetools\Core\Request\Orders\Command\OrderRemovePaymentAction;
use Commercetools\Core\Request\Orders\Command\OrderSetBillingAddress;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerEmail;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomerIdAction;
use Commercetools\Core\Request\Orders\Command\OrderSetCustomLineItemShippingDetailsAction;
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryAddressAction;
use Commercetools\Core\Request\Orders\Command\OrderSetDeliveryItemsAction;
Expand Down Expand Up @@ -669,6 +674,42 @@ public function testSetCustomerEmail()
$this->assertNotSame($draft->getCustomerEmail(), $order->getCustomerEmail());
}

public function testSetCustomerId()
{
$draft = $this->getCartDraft();
$order = $this->createOrder($draft);

$customerDraft = CustomerDraft::ofEmailNameAndPassword(
$this->getTestRun() . '-another@example.com',
'firstName',
'lastName',
'password'
);
$request = CustomerCreateRequest::ofDraft($customerDraft);
$response = $request->executeWithClient($this->getClient());
$customerSignInResult = $request->mapResponse($response);
$customer = $customerSignInResult->getCustomer();

$this->cleanupRequests[] = CustomerDeleteRequest::ofIdAndVersion(
$customer->getId(),
$customer->getVersion()
);

$this->assertNotSame($order->getCustomerId(), $customer->getId());

$request = OrderUpdateRequest::ofIdAndVersion($order->getId(), $order->getVersion())
->addAction(OrderSetCustomerIdAction::of()->setCustomerId($customer->getId()))
;

$response = $request->executeWithClient($this->getClient());
$order = $request->mapResponse($response);
$this->deleteRequest->setVersion($order->getVersion());

$this->assertInstanceOf(Order::class, $order);
$this->assertNotSame($draft->getCustomerId(), $order->getCustomerId());
$this->assertSame($customer->getId(), $order->getCustomerId());
}

public function testSetShippingAddress()
{
$draft = $this->getCartDraft();
Expand Down

0 comments on commit c9f5c55

Please sign in to comment.