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

Commit 53a8f05

Browse files
author
Jens Schulze
committed
feat: support groups for discount codes
Closes #362
1 parent 23d1737 commit 53a8f05

File tree

5 files changed

+85
-39
lines changed

5 files changed

+85
-39
lines changed

src/Core/Model/DiscountCode/DiscountCode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* @method DiscountCode setMaxApplicationsPerCustomer(int $maxApplicationsPerCustomer = null)
4545
* @method CustomFieldObject getCustom()
4646
* @method DiscountCode setCustom(CustomFieldObject $custom = null)
47+
* @method array getGroups()
48+
* @method DiscountCode setGroups(array $groups = null)
4749
* @method DiscountCodeReference getReference()
4850
*/
4951
class DiscountCode extends Resource
@@ -73,6 +75,7 @@ public function fieldDefinitions()
7375
'maxApplications' => [static::TYPE => 'int'],
7476
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
7577
'custom' => [static::TYPE => CustomFieldObject::class],
78+
'groups' => [static::TYPE => 'array'],
7679
];
7780
}
7881
}

src/Core/Model/DiscountCode/DiscountCodeDraft.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* @method DiscountCodeDraft setMaxApplicationsPerCustomer(int $maxApplicationsPerCustomer = null)
3333
* @method CustomFieldObjectDraft getCustom()
3434
* @method DiscountCodeDraft setCustom(CustomFieldObjectDraft $custom = null)
35+
* @method array getGroups()
36+
* @method DiscountCodeDraft setGroups(array $groups = null)
3537
*/
3638
class DiscountCodeDraft extends JsonObject
3739
{
@@ -49,6 +51,7 @@ public function fieldDefinitions()
4951
'maxApplications' => [static::TYPE => 'int'],
5052
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
5153
'custom' => [static::TYPE => CustomFieldObjectDraft::class],
54+
'groups' => [static::TYPE => 'array'],
5255
];
5356
}
5457

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\DiscountCodes\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Request\AbstractAction;
10+
11+
/**
12+
* @package Commercetools\Core\Request\DiscountCodes\Command
13+
* @link https://dev.commercetools.com/http-api-projects-discountCodes.html#change-groups
14+
* @method string getAction()
15+
* @method DiscountCodeChangeGroupsAction setAction(string $action = null)
16+
* @method array getGroups()
17+
* @method DiscountCodeChangeGroupsAction setGroups(array $groups = null)
18+
*/
19+
class DiscountCodeChangeGroupsAction extends AbstractAction
20+
{
21+
public function fieldDefinitions()
22+
{
23+
return [
24+
'action' => [static::TYPE => 'string'],
25+
'groups' => [static::TYPE => 'array'],
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('changeGroups');
37+
}
38+
39+
/**
40+
* @param bool $groups
41+
* @param Context|callable $context
42+
* @return DiscountCodeChangeGroupsAction
43+
*/
44+
public static function ofGroups($groups, $context = null)
45+
{
46+
return static::of($context)->setGroups($groups);
47+
}
48+
}

tests/fixtures/models.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ discountCode:
733733
- maxApplications
734734
- maxApplicationsPerCustomer
735735
- custom
736+
- groups
736737
productDiscount:
737738
domain: productDiscount
738739
model: ProductDiscount

tests/integration/DiscountCode/DiscountCodeUpdateRequestTest.php

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
2525
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
2626
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeCartDiscountsAction;
27+
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeGroupsAction;
2728
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeChangeIsActiveAction;
2829
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetCartPredicateAction;
2930
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetDescriptionAction;
@@ -50,7 +51,7 @@ protected function createCartDiscount($draft)
5051
$response = $request->executeWithClient($this->getClient());
5152
$cartDiscount = $request->mapResponse($response);
5253

53-
$this->discountDeleteRequests[] = CartDiscountDeleteRequest::ofIdAndVersion(
54+
$this->discountDeleteRequests[] = $this->deleteRequest = CartDiscountDeleteRequest::ofIdAndVersion(
5455
$cartDiscount->getId(),
5556
$cartDiscount->getVersion()
5657
);
@@ -155,11 +156,7 @@ public function testChangeCartDiscountAction()
155156
$this->assertSame($cartDiscount->getId(), $result->getCartDiscounts()->current()->getId());
156157
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
157158

158-
$deleteRequest = array_pop($this->cleanupRequests);
159-
$deleteRequest->setVersion($result->getVersion());
160-
$result = $this->getClient()->execute($deleteRequest)->toObject();
161-
162-
$this->assertInstanceOf(DiscountCode::class, $result);
159+
$this->deleteRequest->setVersion($result->getVersion());
163160
}
164161

165162
public function testChangeCartDiscountsAction()
@@ -190,11 +187,7 @@ public function testChangeCartDiscountsAction()
190187
$this->assertSame($expectedIds, $ids);
191188
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
192189

193-
$deleteRequest = array_pop($this->cleanupRequests);
194-
$deleteRequest->setVersion($result->getVersion());
195-
$result = $this->getClient()->execute($deleteRequest)->toObject();
196-
197-
$this->assertInstanceOf(DiscountCode::class, $result);
190+
$this->deleteRequest->setVersion($result->getVersion());
198191
}
199192

200193
public function testSetDescription()
@@ -218,11 +211,7 @@ public function testSetDescription()
218211
$this->assertSame($description, $result->getDescription()->en);
219212
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
220213

221-
$deleteRequest = array_pop($this->cleanupRequests);
222-
$deleteRequest->setVersion($result->getVersion());
223-
$result = $this->getClient()->execute($deleteRequest)->toObject();
224-
225-
$this->assertInstanceOf(DiscountCode::class, $result);
214+
$this->deleteRequest->setVersion($result->getVersion());
226215
}
227216

228217
public function testChangeIsActive()
@@ -244,11 +233,29 @@ public function testChangeIsActive()
244233
$this->assertSame($isActive, $result->getIsActive());
245234
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
246235

247-
$deleteRequest = array_pop($this->cleanupRequests);
248-
$deleteRequest->setVersion($result->getVersion());
249-
$result = $this->getClient()->execute($deleteRequest)->toObject();
236+
$this->deleteRequest->setVersion($result->getVersion());
237+
}
238+
239+
public function testChangeGroups()
240+
{
241+
$draft = $this->getDraft('change-groups');
242+
$draft->setGroups(['test']);
243+
$discountCode = $this->createDiscountCode($draft);
244+
$this->assertSame('test', current($discountCode->getGroups()));
245+
246+
247+
$request = DiscountCodeUpdateRequest::ofIdAndVersion($discountCode->getId(), $discountCode->getVersion())
248+
->addAction(
249+
DiscountCodeChangeGroupsAction::of()->setGroups(['test2'])
250+
)
251+
;
252+
$response = $request->executeWithClient($this->getClient());
253+
$result = $request->mapResponse($response);
254+
$this->deleteRequest->setVersion($result->getVersion());
250255

251256
$this->assertInstanceOf(DiscountCode::class, $result);
257+
$this->assertSame('test2', current($result->getGroups()));
258+
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
252259
}
253260

254261
public function testSetCartPredicate()
@@ -270,11 +277,7 @@ public function testSetCartPredicate()
270277
$this->assertSame($cartPredicate, $result->getCartPredicate());
271278
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
272279

273-
$deleteRequest = array_pop($this->cleanupRequests);
274-
$deleteRequest->setVersion($result->getVersion());
275-
$result = $this->getClient()->execute($deleteRequest)->toObject();
276-
277-
$this->assertInstanceOf(DiscountCode::class, $result);
280+
$this->deleteRequest->setVersion($result->getVersion());
278281
}
279282

280283
public function testSetMaxApplications()
@@ -296,11 +299,7 @@ public function testSetMaxApplications()
296299
$this->assertSame($maxApplications, $result->getMaxApplications());
297300
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
298301

299-
$deleteRequest = array_pop($this->cleanupRequests);
300-
$deleteRequest->setVersion($result->getVersion());
301-
$result = $this->getClient()->execute($deleteRequest)->toObject();
302-
303-
$this->assertInstanceOf(DiscountCode::class, $result);
302+
$this->deleteRequest->setVersion($result->getVersion());
304303
}
305304

306305
public function testSetMaxApplicationsPerCustomer()
@@ -323,11 +322,7 @@ public function testSetMaxApplicationsPerCustomer()
323322
$this->assertSame($maxApplicationsPerCustomer, $result->getMaxApplicationsPerCustomer());
324323
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
325324

326-
$deleteRequest = array_pop($this->cleanupRequests);
327-
$deleteRequest->setVersion($result->getVersion());
328-
$result = $this->getClient()->execute($deleteRequest)->toObject();
329-
330-
$this->assertInstanceOf(DiscountCode::class, $result);
325+
$this->deleteRequest->setVersion($result->getVersion());
331326
}
332327

333328
public function testSetName()
@@ -350,10 +345,6 @@ public function testSetName()
350345
$this->assertSame($name->en, $result->getName()->en);
351346
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
352347

353-
$deleteRequest = array_pop($this->cleanupRequests);
354-
$deleteRequest->setVersion($result->getVersion());
355-
$result = $this->getClient()->execute($deleteRequest)->toObject();
356-
357-
$this->assertInstanceOf(DiscountCode::class, $result);
348+
$this->deleteRequest->setVersion($result->getVersion());
358349
}
359350
}

0 commit comments

Comments
 (0)