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

Commit 523929a

Browse files
author
Jens Schulze
committed
feat(DiscountCode): support validFrom and validUntil for discount codes
Closes #367
1 parent f1658bc commit 523929a

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

src/Core/Model/DiscountCode/DiscountCode.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
* @method DiscountCode setCustom(CustomFieldObject $custom = null)
4747
* @method array getGroups()
4848
* @method DiscountCode setGroups(array $groups = null)
49+
* @method DateTimeDecorator getValidFrom()
50+
* @method DiscountCode setValidFrom(DateTime $validFrom = null)
51+
* @method DateTimeDecorator getValidUntil()
52+
* @method DiscountCode setValidUntil(DateTime $validUntil = null)
4953
* @method DiscountCodeReference getReference()
5054
*/
5155
class DiscountCode extends Resource
@@ -76,6 +80,14 @@ public function fieldDefinitions()
7680
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
7781
'custom' => [static::TYPE => CustomFieldObject::class],
7882
'groups' => [static::TYPE => 'array'],
83+
'validFrom' => [
84+
static::TYPE => DateTime::class,
85+
static::DECORATOR => DateTimeDecorator::class
86+
],
87+
'validUntil' => [
88+
static::TYPE => DateTime::class,
89+
static::DECORATOR => DateTimeDecorator::class
90+
],
7991
];
8092
}
8193
}

src/Core/Model/DiscountCode/DiscountCodeDraft.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
use Commercetools\Core\Model\CartDiscount\CartDiscountReferenceCollection;
99
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Model\Common\DateTimeDecorator;
1011
use Commercetools\Core\Model\Common\JsonObject;
1112
use Commercetools\Core\Model\Common\LocalizedString;
1213
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
14+
use DateTime;
1315

1416
/**
1517
* @package Commercetools\Core\Model\DiscountCode
@@ -34,6 +36,10 @@
3436
* @method DiscountCodeDraft setCustom(CustomFieldObjectDraft $custom = null)
3537
* @method array getGroups()
3638
* @method DiscountCodeDraft setGroups(array $groups = null)
39+
* @method DateTimeDecorator getValidFrom()
40+
* @method DiscountCodeDraft setValidFrom(DateTime $validFrom = null)
41+
* @method DateTimeDecorator getValidUntil()
42+
* @method DiscountCodeDraft setValidUntil(DateTime $validUntil = null)
3743
*/
3844
class DiscountCodeDraft extends JsonObject
3945
{
@@ -52,6 +58,14 @@ public function fieldDefinitions()
5258
'maxApplicationsPerCustomer' => [static::TYPE => 'int'],
5359
'custom' => [static::TYPE => CustomFieldObjectDraft::class],
5460
'groups' => [static::TYPE => 'array'],
61+
'validFrom' => [
62+
static::TYPE => DateTime::class,
63+
static::DECORATOR => DateTimeDecorator::class
64+
],
65+
'validUntil' => [
66+
static::TYPE => DateTime::class,
67+
static::DECORATOR => DateTimeDecorator::class
68+
],
5569
];
5670
}
5771

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jenschude <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\Model\Common\DateTimeDecorator;
10+
use Commercetools\Core\Request\AbstractAction;
11+
use DateTime;
12+
13+
/**
14+
* @package Commercetools\Core\Request\DiscountCodes\Command
15+
* @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-from
16+
* @method string getAction()
17+
* @method DiscountCodeSetValidFromAction setAction(string $action = null)
18+
* @method DiscountCodeSetValidFromAction setValidFrom(DateTime $validFrom = null)
19+
* @method DateTimeDecorator getValidFrom()
20+
*/
21+
class DiscountCodeSetValidFromAction extends AbstractAction
22+
{
23+
public function fieldDefinitions()
24+
{
25+
return [
26+
'action' => [static::TYPE => 'string'],
27+
'validFrom' => [
28+
static::TYPE => DateTime::class,
29+
static::DECORATOR => DateTimeDecorator::class
30+
],
31+
];
32+
}
33+
34+
/**
35+
* @param array $data
36+
* @param Context|callable $context
37+
*/
38+
public function __construct(array $data = [], $context = null)
39+
{
40+
parent::__construct($data, $context);
41+
$this->setAction('setValidFrom');
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jenschude <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\Model\Common\DateTimeDecorator;
10+
use Commercetools\Core\Request\AbstractAction;
11+
use DateTime;
12+
13+
/**
14+
* @package Commercetools\Core\Request\DiscountCodes\Command
15+
* @link https://docs.commercetools.com/http-api-projects-discountCodes.html#set-valid-until
16+
* @method string getAction()
17+
* @method DiscountCodeSetValidUntilAction setAction(string $action = null)
18+
* @method DiscountCodeSetValidUntilAction setValidUntil(DateTime $validUntil = null)
19+
* @method DateTimeDecorator getValidUntil()
20+
*/
21+
class DiscountCodeSetValidUntilAction extends AbstractAction
22+
{
23+
public function fieldDefinitions()
24+
{
25+
return [
26+
'action' => [static::TYPE => 'string'],
27+
'validUntil' => [
28+
static::TYPE => DateTime::class,
29+
static::DECORATOR => DateTimeDecorator::class
30+
],
31+
];
32+
}
33+
34+
/**
35+
* @param array $data
36+
* @param Context|callable $context
37+
*/
38+
public function __construct(array $data = [], $context = null)
39+
{
40+
parent::__construct($data, $context);
41+
$this->setAction('setValidUntil');
42+
}
43+
}

tests/fixtures/models.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ discountCode:
736736
- maxApplicationsPerCustomer
737737
- custom
738738
- groups
739+
- validFrom
740+
- validUntil
739741
productDiscount:
740742
domain: productDiscount
741743
model: ProductDiscount

tests/integration/DiscountCode/DiscountCodeUpdateRequestTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsAction;
3232
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetMaxApplicationsPerCustomerAction;
3333
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetNameAction;
34+
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidFromAction;
35+
use Commercetools\Core\Request\DiscountCodes\Command\DiscountCodeSetValidUntilAction;
3436
use Commercetools\Core\Request\DiscountCodes\DiscountCodeCreateRequest;
3537
use Commercetools\Core\Request\DiscountCodes\DiscountCodeDeleteRequest;
3638
use Commercetools\Core\Request\DiscountCodes\DiscountCodeUpdateRequest;
@@ -347,4 +349,50 @@ public function testSetName()
347349

348350
$this->deleteRequest->setVersion($result->getVersion());
349351
}
352+
353+
public function testSetValidFrom()
354+
{
355+
$draft = $this->getDraft('set-valid-from');
356+
$discountCode = $this->createDiscountCode($draft);
357+
358+
359+
$validFrom = new \DateTime();
360+
$request = DiscountCodeUpdateRequest::ofIdAndVersion(
361+
$discountCode->getId(),
362+
$discountCode->getVersion()
363+
)
364+
->addAction(DiscountCodeSetValidFromAction::of()->setValidFrom($validFrom))
365+
;
366+
$response = $request->executeWithClient($this->getClient());
367+
$result = $request->mapResponse($response);
368+
$this->deleteRequest->setVersion($result->getVersion());
369+
370+
$this->assertInstanceOf(DiscountCode::class, $result);
371+
$validFrom->setTimezone(new \DateTimeZone('UTC'));
372+
$this->assertSame($validFrom->format('c'), $result->getValidFrom()->format('c'));
373+
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
374+
}
375+
376+
public function testValidUntilFrom()
377+
{
378+
$draft = $this->getDraft('set-valid-until');
379+
$discountCode = $this->createDiscountCode($draft);
380+
381+
382+
$validUntil = new \DateTime();
383+
$request = DiscountCodeUpdateRequest::ofIdAndVersion(
384+
$discountCode->getId(),
385+
$discountCode->getVersion()
386+
)
387+
->addAction(DiscountCodeSetValidUntilAction::of()->setValidUntil($validUntil))
388+
;
389+
$response = $request->executeWithClient($this->getClient());
390+
$result = $request->mapResponse($response);
391+
$this->deleteRequest->setVersion($result->getVersion());
392+
393+
$this->assertInstanceOf(DiscountCode::class, $result);
394+
$validUntil->setTimezone(new \DateTimeZone('UTC'));
395+
$this->assertSame($validUntil->format('c'), $result->getValidUntil()->format('c'));
396+
$this->assertNotSame($discountCode->getVersion(), $result->getVersion());
397+
}
350398
}

0 commit comments

Comments
 (0)