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

Commit 5eee954

Browse files
Barbara PalumboBarbara Palumbo
authored andcommitted
feat(Customer): add store field add tests for CustomerQuery
1 parent 14589ec commit 5eee954

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

src/Core/Model/Customer/Customer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Commercetools\Core\Model\CustomField\CustomFieldObject;
1616
use Commercetools\Core\Model\Common\DateTimeDecorator;
1717
use Commercetools\Core\Model\Common\DateDecorator;
18+
use Commercetools\Core\Model\Store\StoreReference;
19+
use Commercetools\Core\Model\Store\StoreReferenceCollection;
1820
use DateTime;
1921

2022
/**
@@ -75,6 +77,8 @@
7577
* @method Customer setCreatedBy(CreatedBy $createdBy = null)
7678
* @method LastModifiedBy getLastModifiedBy()
7779
* @method Customer setLastModifiedBy(LastModifiedBy $lastModifiedBy = null)
80+
* @method StoreReferenceCollection getStores()
81+
* @method Customer setStores(StoreReferenceCollection $stores = null)
7882
* @method CustomerReference getReference()
7983
*/
8084
class Customer extends Resource
@@ -121,6 +125,7 @@ public function fieldDefinitions()
121125
'key' => [static::TYPE => 'string'],
122126
'createdBy' => [static::TYPE => CreatedBy::class],
123127
'lastModifiedBy' => [static::TYPE => LastModifiedBy::class],
128+
'stores' => [static::TYPE => StoreReferenceCollection::class],
124129
];
125130
}
126131

src/Core/Model/Customer/CustomerDraft.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Commercetools\Core\Model\CustomerGroup\CustomerGroupReference;
1414
use Commercetools\Core\Model\Common\AddressCollection;
1515
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
16+
use Commercetools\Core\Model\Store\StoreReferenceCollection;
1617
use DateTime;
1718
use Commercetools\Core\Model\Common\DateDecorator;
1819

@@ -66,6 +67,8 @@
6667
* @method CustomerDraft setKey(string $key = null)
6768
* @method string getAnonymousId()
6869
* @method CustomerDraft setAnonymousId(string $anonymousId = null)
70+
* @method StoreReferenceCollection getStores()
71+
* @method CustomerDraft setStores(StoreReferenceCollection $stores = null)
6972
*/
7073
class CustomerDraft extends JsonObject
7174
{
@@ -101,6 +104,7 @@ public function fieldDefinitions()
101104
'salutation' => [static::TYPE => 'string'],
102105
'key' => [static::TYPE => 'string'],
103106
'anonymousId' => [static::TYPE => 'string'],
107+
'stores' => [static::TYPE => StoreReferenceCollection::class],
104108
];
105109
}
106110

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Commercetools\Core\Model\Store;
4+
5+
use Commercetools\Core\Model\Common\Collection;
6+
7+
/**
8+
* @package Commercetools\Core\Model\Store
9+
*
10+
* @link https://docs.commercetools.com/http-api-types.html#reference-types
11+
* @link https://docs.commercetools.com/http-api-projects-stores#stores
12+
* @method StoreReferenceCollection add(StoreReference $element)
13+
* @method StoreReference current()
14+
* @method StoreReference getAt($offset)
15+
* @method StoreReference getById($offset)
16+
*/
17+
class StoreReferenceCollection extends Collection
18+
{
19+
protected $type = StoreReference::class;
20+
}

src/Core/Request/InStores/InStoreRequests.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
use Commercetools\Core\Request\Carts\CartDeleteRequest;
1212
use Commercetools\Core\Request\Carts\CartQueryRequest;
1313
use Commercetools\Core\Request\Carts\CartUpdateRequest;
14+
use Commercetools\Core\Request\Customers\CustomerByIdGetRequest;
15+
use Commercetools\Core\Request\Customers\CustomerByKeyGetRequest;
16+
use Commercetools\Core\Request\Customers\CustomerByTokenGetRequest;
17+
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
18+
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
19+
use Commercetools\Core\Request\Customers\CustomerLoginRequest;
20+
use Commercetools\Core\Request\Customers\CustomerPasswordResetRequest;
21+
use Commercetools\Core\Request\Customers\CustomerPasswordTokenRequest;
22+
use Commercetools\Core\Request\Customers\CustomerQueryRequest;
1423
use Commercetools\Core\Request\Me\MeActiveCartRequest;
1524
use Commercetools\Core\Request\Me\MeCartByIdRequest;
1625
use Commercetools\Core\Request\Me\MeCartCreateRequest;
@@ -55,6 +64,11 @@ class InStoreRequests
5564
MeOrderByIdRequest::class => 1,
5665
MeOrderQueryRequest::class => 1,
5766
MeOrderCreateFromCartRequest::class => 1,
67+
CustomerByKeyGetRequest::class => 1,
68+
CustomerQueryRequest::class => 1,
69+
// CustomerPasswordTokenRequest::class => 1,
70+
// CustomerCreateRequest::class => 1,
71+
// CustomerDeleteRequest::class => 1,
5872
];
5973

6074
public function can($request)

tests/integration/Customer/CustomerQueryRequestTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use Commercetools\Core\IntegrationTests\ApiTestCase;
1010
use Commercetools\Core\Model\Customer\Customer;
1111
use Commercetools\Core\Model\Customer\CustomerDraft;
12+
use Commercetools\Core\Model\Store\StoreReferenceCollection;
1213
use Commercetools\Core\Request\Customers\CustomerByIdGetRequest;
1314
use Commercetools\Core\Request\Customers\CustomerByKeyGetRequest;
1415
use Commercetools\Core\Request\Customers\CustomerCreateRequest;
1516
use Commercetools\Core\Request\Customers\CustomerDeleteRequest;
1617
use Commercetools\Core\Request\Customers\CustomerQueryRequest;
18+
use Commercetools\Core\Request\InStores\InStoreRequestDecorator;
1719

1820
class CustomerQueryRequestTest extends ApiTestCase
1921
{
@@ -86,4 +88,63 @@ public function testGetByKey()
8688
$this->assertInstanceOf(Customer::class, $customer);
8789
$this->assertSame($customer->getId(), $result->getId());
8890
}
91+
92+
public function testInstoreGetById()
93+
{
94+
$store = $this->getStore();
95+
$draft = $this->getDraft()->setStores(StoreReferenceCollection::of()->add($store->getReference()));
96+
$customer = $this->createCustomer($draft);
97+
98+
$request = InStoreRequestDecorator::ofStoreKeyAndRequest(
99+
$store->getKey(),
100+
CustomerQueryRequest::of()
101+
);
102+
103+
$response = $request->executeWithClient($this->getClient());
104+
$result = $request->mapResponse($response);
105+
106+
$this->assertInstanceOf(Customer::class, $customer);
107+
$this->assertSame($customer->getId(), $result->getId());
108+
$this->assertSame($customer->getStores()->current()->getId(), $result->getStores()->current()->getId());
109+
}
110+
111+
public function testInstoreGetByKey()
112+
{
113+
$store = $this->getStore();
114+
$draft = $this->getDraft()->setStores(StoreReferenceCollection::of()->add($store->getReference()));
115+
$draft->setKey('test-'. $this->getTestRun());
116+
$customer = $this->createCustomer($draft);
117+
118+
$request = InStoreRequestDecorator::ofStoreKeyAndRequest(
119+
$store->getKey(),
120+
CustomerQueryRequest::of()
121+
);
122+
$response = $request->executeWithClient($this->getClient());
123+
/**
124+
* @var Customer $result
125+
*/
126+
$result = $request->mapFromResponse($response);
127+
128+
$this->assertInstanceOf(Customer::class, $customer);
129+
$this->assertSame($customer->getId(), $result->getId());
130+
$this->assertSame($customer->getStores()->current()->getKey(), $result->getStores()->current()->getKey());
131+
}
132+
133+
public function testInstoreQueryCustomer()
134+
{
135+
$store = $this->getStore();
136+
$draft = $this->getDraft();
137+
$customer = $this->createCustomer($draft);
138+
139+
$request = InStoreRequestDecorator::ofStoreKeyAndRequest(
140+
$store->getKey(),
141+
CustomerQueryRequest::of()->where('email="' . $draft->getEmail() . '"')
142+
);
143+
$response = $request->executeWithClient($this->getClient());
144+
$result = $request->mapResponse($response);
145+
146+
$this->assertCount(1, $result);
147+
$this->assertInstanceOf(Customer::class, $result->getAt(0));
148+
$this->assertSame($customer->getId(), $result->getAt(0)->getId());
149+
}
89150
}

0 commit comments

Comments
 (0)