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

Commit 211573d

Browse files
author
Jens Schulze
committed
feat(Customer): support setKey for customers
Closes #342
1 parent 3b81973 commit 211573d

File tree

9 files changed

+239
-0
lines changed

9 files changed

+239
-0
lines changed

src/Core/Model/Customer/Customer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
* @method Customer setBillingAddressIds(array $billingAddressIds = null)
6868
* @method string getSalutation()
6969
* @method Customer setSalutation(string $salutation = null)
70+
* @method string getKey()
71+
* @method Customer setKey(string $key = null)
7072
* @method CustomerReference getReference()
7173
*/
7274
class Customer extends Resource
@@ -110,6 +112,7 @@ public function fieldDefinitions()
110112
'shippingAddressIds' => [static::TYPE => 'array'],
111113
'billingAddressIds' => [static::TYPE => 'array'],
112114
'salutation' => [static::TYPE => 'string'],
115+
'key' => [static::TYPE => 'string'],
113116
];
114117
}
115118

src/Core/Model/Customer/CustomerDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
* @method CustomerDraft setShippingAddresses(array $shippingAddresses = null)
6363
* @method string getSalutation()
6464
* @method CustomerDraft setSalutation(string $salutation = null)
65+
* @method string getKey()
66+
* @method CustomerDraft setKey(string $key = null)
6567
*/
6668
class CustomerDraft extends JsonObject
6769
{
@@ -95,6 +97,7 @@ public function fieldDefinitions()
9597
'billingAddresses' => [static::TYPE => 'array'],
9698
'shippingAddresses' => [static::TYPE => 'array'],
9799
'salutation' => [static::TYPE => 'string'],
100+
'key' => [static::TYPE => 'string'],
98101
];
99102
}
100103

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Customers\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\Customers\Command
13+
* @link https://dev.commercetools.com/http-api-projects-customers.html#set-key
14+
* @method string getAction()
15+
* @method CustomerSetKeyAction setAction(string $action = null)
16+
* @method string getKey()
17+
* @method CustomerSetKeyAction setKey(string $key = null)
18+
*/
19+
class CustomerSetKeyAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'key' => [static::TYPE => 'string'],
26+
];
27+
}
28+
29+
/**
30+
* @param array $data
31+
* @param Context|callable $context
32+
*/
33+
public function __construct(array $data = [], $context = null)
34+
{
35+
parent::__construct($data, $context);
36+
$this->setAction('setKey');
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Customers;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\Type\Type;
10+
use Commercetools\Core\Request\AbstractByKeyGetRequest;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\Customer\Customer;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Customers
17+
* @link https://dev.commercetools.com/http-api-projects-customers.html#get-customer-by-key
18+
* @method Customer mapResponse(ApiResponseInterface $response)
19+
* @method Customer mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CustomerByKeyGetRequest extends AbstractByKeyGetRequest
22+
{
23+
protected $resultClass = Customer::class;
24+
25+
/**
26+
* @param string $key
27+
* @param Context $context
28+
*/
29+
public function __construct($key, Context $context = null)
30+
{
31+
parent::__construct(CustomersEndpoint::endpoint(), $key, $context);
32+
}
33+
34+
/**
35+
* @param string $key
36+
* @param Context $context
37+
* @return static
38+
*/
39+
public static function ofKey($key, Context $context = null)
40+
{
41+
return new static($key, $context);
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Customers;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
10+
use Commercetools\Core\Model\Type\Type;
11+
use Commercetools\Core\Response\ApiResponseInterface;
12+
use Commercetools\Core\Model\Customer\Customer;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Customers
17+
* @link https://dev.commercetools.com/http-api-projects-customers.html#delete-customer-by-key
18+
* @method Customer mapResponse(ApiResponseInterface $response)
19+
* @method Customer mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CustomerDeleteByKeyRequest extends AbstractDeleteByKeyRequest
22+
{
23+
protected $resultClass = Customer::class;
24+
25+
/**
26+
* @param string $id
27+
* @param int $version
28+
* @param Context $context
29+
*/
30+
public function __construct($id, $version, Context $context = null)
31+
{
32+
parent::__construct(CustomersEndpoint::endpoint(), $id, $version, $context);
33+
}
34+
35+
/**
36+
* @param string $key
37+
* @param int $version
38+
* @param Context $context
39+
* @return static
40+
*/
41+
public static function ofKeyAndVersion($key, $version, Context $context = null)
42+
{
43+
return new static($key, $version, $context);
44+
}
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Customers;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
10+
use Commercetools\Core\Request\AbstractUpdateRequest;
11+
use Commercetools\Core\Model\Customer\Customer;
12+
use Commercetools\Core\Response\ApiResponseInterface;
13+
use Commercetools\Core\Model\MapperInterface;
14+
15+
/**
16+
* @package Commercetools\Core\Request\Customers
17+
* @link https://dev.commercetools.com/http-api-projects-customers.html#update-customer-by-key
18+
* @method Customer mapResponse(ApiResponseInterface $response)
19+
* @method Customer mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
20+
*/
21+
class CustomerUpdateByKeyRequest extends AbstractUpdateByKeyRequest
22+
{
23+
protected $resultClass = Customer::class;
24+
25+
/**
26+
* @param string $key
27+
* @param string $version
28+
* @param array $actions
29+
* @param Context $context
30+
*/
31+
public function __construct($key, $version, array $actions = [], Context $context = null)
32+
{
33+
parent::__construct(CustomersEndpoint::endpoint(), $key, $version, $actions, $context);
34+
}
35+
36+
/**
37+
* @param string $key
38+
* @param int $version
39+
* @param Context $context
40+
* @return static
41+
*/
42+
public static function ofKeyAndVersion($key, $version, Context $context = null)
43+
{
44+
return new static($key, $version, [], $context);
45+
}
46+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ customer:
308308
- shippingAddressIds
309309
- billingAddressIds
310310
- salutation
311+
- key
311312
customerSigninResult:
312313
domain: customer
313314
model: CustomerSigninResult

tests/integration/Customer/CustomerQueryRequestTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Commercetools\Core\Model\Customer\Customer;
1111
use Commercetools\Core\Model\Customer\CustomerDraft;
1212
use Commercetools\Core\Request\Customers\CustomerByIdGetRequest;
13+
use Commercetools\Core\Request\Customers\CustomerByKeyGetRequest;
1314
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
1415
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
1516
use Commercetools\Core\Request\Customers\CustomerQueryRequest;
@@ -72,4 +73,19 @@ public function testGetById()
7273
$this->assertSame($customer->getId(), $result->getId());
7374

7475
}
76+
77+
public function testGetByKey()
78+
{
79+
$draft = $this->getDraft();
80+
$draft->setKey('test-'. $this->getTestRun());
81+
$customer = $this->createCustomer($draft);
82+
83+
$request = CustomerByKeyGetRequest::ofKey($customer->getKey());
84+
$response = $request->executeWithClient($this->getClient());
85+
$result = $request->mapResponse($response);
86+
87+
$this->assertInstanceOf(Customer::class, $customer);
88+
$this->assertSame($customer->getId(), $result->getId());
89+
90+
}
7591
}

tests/integration/Customer/CustomerUpdateRequestTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Commercetools\Core\ApiTestCase;
1010
use Commercetools\Core\Model\Common\Address;
1111
use Commercetools\Core\Model\Common\AddressCollection;
12+
use Commercetools\Core\Model\Customer\Customer;
1213
use Commercetools\Core\Model\Customer\CustomerDraft;
1314
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
1415
use Commercetools\Core\Model\CustomField\FieldContainer;
@@ -28,6 +29,7 @@
2829
use Commercetools\Core\Request\Customers\Command\CustomerSetDefaultShippingAddressAction;
2930
use Commercetools\Core\Request\Customers\Command\CustomerSetExternalIdAction;
3031
use Commercetools\Core\Request\Customers\Command\CustomerSetFirstNameAction;
32+
use Commercetools\Core\Request\Customers\Command\CustomerSetKeyAction;
3133
use Commercetools\Core\Request\Customers\Command\CustomerSetLastNameAction;
3234
use Commercetools\Core\Request\Customers\Command\CustomerSetLocaleAction;
3335
use Commercetools\Core\Request\Customers\Command\CustomerSetMiddleNameAction;
@@ -36,9 +38,11 @@
3638
use Commercetools\Core\Request\Customers\Command\CustomerSetVatIdAction;
3739
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
3840
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
41+
use Commercetools\Core\Request\Customers\CustomerUpdateByKeyRequest;
3942
use Commercetools\Core\Request\Customers\CustomerUpdateRequest;
4043
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
4144
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
45+
use function GuzzleHttp\Psr7\str;
4246

4347
class CustomerUpdateRequestTest extends ApiTestCase
4448
{
@@ -70,6 +74,46 @@ protected function createCustomer(CustomerDraft $draft)
7074
return $result->getCustomer();
7175
}
7276

77+
public function testUpdateByKey()
78+
{
79+
$draft = $this->getDraft('update-by-key');
80+
$draft->setKey('test-'. $this->getTestRun());
81+
$customer = $this->createCustomer($draft);
82+
83+
$firstName = 'test-' . $this->getTestRun() . '-new firstName';
84+
$request = CustomerUpdateByKeyRequest::ofKeyAndVersion($customer->getKey(), $customer->getVersion())
85+
->addAction(
86+
CustomerSetFirstNameAction::of()->setFirstName($firstName)
87+
)
88+
;
89+
$response = $request->executeWithClient($this->getClient());
90+
$result = $request->mapResponse($response);
91+
$this->deleteRequest->setVersion($result->getVersion());
92+
93+
$this->assertInstanceOf(Customer::class, $result);
94+
$this->assertSame($firstName, $result->getFirstName());
95+
}
96+
97+
public function testSetKey()
98+
{
99+
$draft = $this->getDraft('set-key');
100+
$customer = $this->createCustomer($draft);
101+
102+
$key = 'new-' . $this->getTestRun();
103+
$request = CustomerUpdateRequest::ofIdAndVersion($customer->getId(), $customer->getVersion())
104+
->addAction(
105+
CustomerSetKeyAction::of()->setKey($key)
106+
)
107+
;
108+
$response = $request->executeWithClient($this->getClient());
109+
$result = $request->mapResponse($response);
110+
$this->deleteRequest->setVersion($result->getVersion());
111+
112+
$this->assertInstanceOf(Customer::class, $result);
113+
$this->assertSame($key, $result->getKey());
114+
$this->assertNotSame($customer->getVersion(), $result->getVersion());
115+
}
116+
73117
public function testCustomerEmail()
74118
{
75119
$draft = $this->getDraft('email');

0 commit comments

Comments
 (0)