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

Commit c226e1f

Browse files
committed
feat(Discounts): add setValidFromAndUntil action
1 parent 7ba30dd commit c226e1f

File tree

7 files changed

+172
-1
lines changed

7 files changed

+172
-1
lines changed

src/Core/Builder/Update/CartDiscountsActionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetCustomTypeAction;
1717
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetDescriptionAction;
1818
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAction;
19+
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAndUntilAction;
1920
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidUntilAction;
2021

2122
class CartDiscountsActionBuilder
@@ -154,6 +155,17 @@ public function setValidFrom($action = null)
154155
return $this;
155156
}
156157

158+
/**
159+
* @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-from-and-until
160+
* @param CartDiscountSetValidFromAndUntilAction|callable $action
161+
* @return $this
162+
*/
163+
public function setValidFromAndUntil($action = null)
164+
{
165+
$this->addAction($this->resolveAction(CartDiscountSetValidFromAndUntilAction::class, $action));
166+
return $this;
167+
}
168+
157169
/**
158170
* @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-until
159171
* @param CartDiscountSetValidUntilAction|callable $action

src/Core/Builder/Update/DiscountCodesActionBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsPerCustomerAction;
1616
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetNameAction;
1717
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAction;
18+
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAndUntilAction;
1819
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidUntilAction;
1920

2021
class DiscountCodesActionBuilder
@@ -142,6 +143,17 @@ public function setValidFrom($action = null)
142143
return $this;
143144
}
144145

146+
/**
147+
* @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-from-and-until
148+
* @param DiscountCodeSetValidFromAndUntilAction|callable $action
149+
* @return $this
150+
*/
151+
public function setValidFromAndUntil($action = null)
152+
{
153+
$this->addAction($this->resolveAction(DiscountCodeSetValidFromAndUntilAction::class, $action));
154+
return $this;
155+
}
156+
145157
/**
146158
* @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-until
147159
* @param DiscountCodeSetValidUntilAction|callable $action
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\CartDiscounts\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Request\AbstractAction;
9+
use Commercetools\Core\Model\Common\DateTimeDecorator;
10+
use DateTime;
11+
12+
/**
13+
* @package Commercetools\Core\Request\CartDiscounts\Command
14+
* @link https://docs.commercetools.com/http-api-projects-cartDiscounts.html#set-valid-from-and-until
15+
* @method string getAction()
16+
* @method CartDiscountSetValidFromAndUntilAction setAction(string $action = null)
17+
* @method DateTimeDecorator getValidFrom()
18+
* @method CartDiscountSetValidFromAndUntilAction setValidFrom(DateTime $validFrom = null)
19+
* @method DateTimeDecorator getValidUntil()
20+
* @method CartDiscountSetValidFromAndUntilAction setValidUntil(DateTime $validUntil = null)
21+
*/
22+
class CartDiscountSetValidFromAndUntilAction extends AbstractAction
23+
{
24+
public function fieldDefinitions()
25+
{
26+
return [
27+
'action' => [static::TYPE => 'string'],
28+
'validFrom' => [
29+
static::TYPE => DateTime::class,
30+
static::DECORATOR => DateTimeDecorator::class
31+
],
32+
'validUntil' => [
33+
static::TYPE => DateTime::class,
34+
static::DECORATOR => DateTimeDecorator::class
35+
],
36+
];
37+
}
38+
39+
/**
40+
* @param array $data
41+
* @param Context|callable $context
42+
*/
43+
public function __construct(array $data = [], $context = null)
44+
{
45+
parent::__construct($data, $context);
46+
$this->setAction('setValidFromAndUntil');
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
*/
4+
5+
namespace Commercetools\Core\Request\DiscountCodes\Command;
6+
7+
use Commercetools\Core\Model\Common\Context;
8+
use Commercetools\Core\Request\AbstractAction;
9+
use Commercetools\Core\Model\Common\DateTimeDecorator;
10+
use DateTime;
11+
12+
/**
13+
* @package Commercetools\Core\Request\DiscountCodes\Command
14+
* @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-from-and-until
15+
* @method string getAction()
16+
* @method DiscountCodeSetValidFromAndUntilAction setAction(string $action = null)
17+
* @method DateTimeDecorator getValidFrom()
18+
* @method DiscountCodeSetValidFromAndUntilAction setValidFrom(DateTime $validFrom = null)
19+
* @method DateTimeDecorator getValidUntil()
20+
* @method DiscountCodeSetValidFromAndUntilAction setValidUntil(DateTime $validUntil = null)
21+
*/
22+
class DiscountCodeSetValidFromAndUntilAction extends AbstractAction
23+
{
24+
public function fieldDefinitions()
25+
{
26+
return [
27+
'action' => [static::TYPE => 'string'],
28+
'validFrom' => [
29+
static::TYPE => DateTime::class,
30+
static::DECORATOR => DateTimeDecorator::class
31+
],
32+
'validUntil' => [
33+
static::TYPE => DateTime::class,
34+
static::DECORATOR => DateTimeDecorator::class
35+
],
36+
];
37+
}
38+
39+
/**
40+
* @param array $data
41+
* @param Context|callable $context
42+
*/
43+
public function __construct(array $data = [], $context = null)
44+
{
45+
parent::__construct($data, $context);
46+
$this->setAction('setValidFromAndUntil');
47+
}
48+
}

tests/integration/CartDiscount/CartDiscountUpdateRequestTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Commercetools\Core\Request\CartDiscounts\CartDiscountDeleteRequest;
2727
use Commercetools\Core\Request\CartDiscounts\CartDiscountUpdateRequest;
2828
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAction;
29+
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidFromAndUntilAction;
2930
use Commercetools\Core\Request\CartDiscounts\Command\CartDiscountSetValidUntilAction;
3031

3132
class CartDiscountUpdateRequestTest extends ApiTestCase
@@ -299,6 +300,31 @@ public function testStackingMode()
299300
$this->assertSame(CartDiscount::MODE_STACKING, $result->getStackingMode());
300301
}
301302

303+
public function testSetValidFromAndUntil()
304+
{
305+
$draft = $this->getDraft('set-valid-from-until');
306+
$cartDiscount = $this->createCartDiscount($draft);
307+
308+
$validFrom = new \DateTime();
309+
$validUntil = new \DateTime('+1 second');
310+
$request = CartDiscountUpdateRequest::ofIdAndVersion(
311+
$cartDiscount->getId(),
312+
$cartDiscount->getVersion()
313+
)
314+
->addAction(CartDiscountSetValidFromAndUntilAction::of()->setValidFrom($validFrom)->setValidUntil($validUntil))
315+
;
316+
$response = $request->executeWithClient($this->getClient());
317+
$result = $request->mapResponse($response);
318+
$this->deleteRequest->setVersion($result->getVersion());
319+
320+
$this->assertInstanceOf(CartDiscount::class, $result);
321+
$validUntil->setTimezone(new \DateTimeZone('UTC'));
322+
$validFrom->setTimezone(new \DateTimeZone('UTC'));
323+
$this->assertSame($validUntil->format('c'), $result->getValidUntil()->format('c'));
324+
$this->assertSame($validFrom->format('c'), $result->getValidFrom()->format('c'));
325+
$this->assertNotSame($cartDiscount->getVersion(), $result->getVersion());
326+
}
327+
302328
/**
303329
* @param $name
304330
* @return CartDiscountDraft

tests/integration/DiscountCode/DiscountCodeUpdateRequestTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsPerCustomerAction;
3333
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetNameAction;
3434
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAction;
35+
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAndUntilAction;
3536
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidUntilAction;
3637
use Commercetools\Core\Request\DiscountCodes\DiscountCodeCreateRequest;
3738
use Commercetools\Core\Request\DiscountCodes\DiscountCodeDeleteRequest;
@@ -395,4 +396,29 @@ public function testValidUntilFrom()
395396
$this->assertSame($validUntil->format('c'), $result->getValidUntil()->format('c'));
396397
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
397398
}
399+
400+
public function testSetValidFromAndUntil()
401+
{
402+
$draft = $this->getDraft('set-valid-until');
403+
$discountCode = $this->createDiscountCode($draft);
404+
405+
$validFrom = new \DateTime();
406+
$validUntil = new \DateTime('+1 second');
407+
$request = DiscountCodeUpdateRequest::ofIdAndVersion(
408+
$discountCode->getId(),
409+
$discountCode->getVersion()
410+
)
411+
->addAction(DiscountCodeSetValidFromAndUntilAction::of()->setValidFrom($validFrom)->setValidUntil($validUntil))
412+
;
413+
$response = $request->executeWithClient($this->getClient());
414+
$result = $request->mapResponse($response);
415+
$this->deleteRequest->setVersion($result->getVersion());
416+
417+
$this->assertInstanceOf(DiscountCode::class, $result);
418+
$validUntil->setTimezone(new \DateTimeZone('UTC'));
419+
$validFrom->setTimezone(new \DateTimeZone('UTC'));
420+
$this->assertSame($validUntil->format('c'), $result->getValidUntil()->format('c'));
421+
$this->assertSame($validFrom->format('c'), $result->getValidFrom()->format('c'));
422+
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
423+
}
398424
}

tests/integration/ProductDiscount/ProductDiscountUpdateRequestTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public function testSetValidFromAndUntil()
262262
$draft = $this->getDraft('set-valid-from-until');
263263
$productDiscount = $this->createProductDiscount($draft);
264264

265-
266265
$validFrom = new \DateTime();
267266
$validUntil = new \DateTime('+1 second');
268267
$request = ProductDiscountUpdateRequest::ofIdAndVersion(

0 commit comments

Comments
 (0)