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

Commit

Permalink
feat(TaxCategory): support setKey for tax categories
Browse files Browse the repository at this point in the history
Closes #339
  • Loading branch information
Jens Schulze committed Sep 11, 2017
1 parent 67a1246 commit 6c41b97
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Core/Model/TaxCategory/TaxCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* @method TaxCategory setDescription(string $description = null)
* @method TaxRateCollection getRates()
* @method TaxCategory setRates(TaxRateCollection $rates = null)
* @method string getKey()
* @method TaxCategory setKey(string $key = null)
* @method TaxCategoryReference getReference()
*/
class TaxCategory extends Resource
Expand All @@ -46,7 +48,8 @@ public function fieldDefinitions()
],
'name' => [self::TYPE => 'string'],
'description' => [self::TYPE => 'string'],
'rates' => [self::TYPE => TaxRateCollection::class]
'rates' => [self::TYPE => TaxRateCollection::class],
'key' => [self::TYPE => 'string'],
];
}
}
3 changes: 3 additions & 0 deletions src/Core/Model/TaxCategory/TaxCategoryDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @method TaxCategoryDraft setDescription(string $description = null)
* @method TaxRateCollection getRates()
* @method TaxCategoryDraft setRates(TaxRateCollection $rates = null)
* @method string getKey()
* @method TaxCategoryDraft setKey(string $key = null)
*/
class TaxCategoryDraft extends JsonObject
{
Expand All @@ -26,6 +28,7 @@ public function fieldDefinitions()
'name' => [static::TYPE => 'string'],
'description' => [static::TYPE => 'string'],
'rates' => [static::TYPE => TaxRateCollection::class],
'key' => [self::TYPE => 'string'],
];
}

Expand Down
48 changes: 48 additions & 0 deletions src/Core/Request/TaxCategories/Command/TaxCategorySetKeyAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\TaxCategories\Command;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractAction;

/**
* @package Commercetools\Core\Request\TaxCategories\Command
* @link http://dev.commercetools.com/http-api-projects-taxCategories.html#set-key
* @method string getKey()
* @method TaxCategorySetKeyAction setKey(string $key = null)
* @method string getAction()
* @method TaxCategorySetKeyAction setAction(string $action = null)
*/
class TaxCategorySetKeyAction extends AbstractAction
{
public function fieldDefinitions()
{
return [
'action' => [static::TYPE => 'string'],
'key' => [static::TYPE => 'string']
];
}

/**
* @param array $data
* @param Context|callable $context
*/
public function __construct(array $data = [], $context = null)
{
parent::__construct($data, $context);
$this->setAction('setKey');
}

/**
* @param string $key
* @param Context|callable $context
* @return TaxCategorySetKeyAction
*/
public static function ofKey($key, $context = null)
{
return static::of($context)->setKey($key);
}
}
41 changes: 41 additions & 0 deletions src/Core/Request/TaxCategories/TaxCategoryByKeyGetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\TaxCategories;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Model\TaxCategory\TaxCategory;
use Commercetools\Core\Request\AbstractByKeyGetRequest;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\TaxCategories
* @method TaxCategory mapResponse(ApiResponseInterface $response)
* @method TaxCategory mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class TaxCategoryByKeyGetRequest extends AbstractByKeyGetRequest
{
protected $resultClass = TaxCategory::class;

/**
* @param string $key
* @param Context $context
*/
public function __construct($key, Context $context = null)
{
parent::__construct(TaxCategoriesEndpoint::endpoint(), $key, $context);
}

/**
* @param string $key
* @param Context $context
* @return TaxCategoryByKeyGetRequest
*/
public static function ofKey($key, Context $context = null)
{
return new static($key, $context);
}
}
44 changes: 44 additions & 0 deletions src/Core/Request/TaxCategories/TaxCategoryDeleteByKeyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\TaxCategories;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractDeleteByKeyRequest;
use Commercetools\Core\Model\TaxCategory\TaxCategory;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\TaxCategories
* @link http://dev.commercetools.com/http-api-projects-taxCategories.html#delete-taxcategory
* @method TaxCategory mapResponse(ApiResponseInterface $response)
* @method TaxCategory mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class TaxCategoryDeleteByKeyRequest extends AbstractDeleteByKeyRequest
{
protected $resultClass = TaxCategory::class;

/**
* @param string $key
* @param int $version
* @param Context $context
*/
public function __construct($key, $version, Context $context = null)
{
parent::__construct(TaxCategoriesEndpoint::endpoint(), $key, $version, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, $context);
}
}
45 changes: 45 additions & 0 deletions src/Core/Request/TaxCategories/TaxCategoryUpdateByKeyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @author @jayS-de <jens.schulze@commercetools.de>
*/

namespace Commercetools\Core\Request\TaxCategories;

use Commercetools\Core\Model\Common\Context;
use Commercetools\Core\Request\AbstractUpdateByKeyRequest;
use Commercetools\Core\Model\TaxCategory\TaxCategory;
use Commercetools\Core\Response\ApiResponseInterface;
use Commercetools\Core\Model\MapperInterface;

/**
* @package Commercetools\Core\Request\TaxCategories
* @link http://dev.commercetools.com/http-api-projects-taxCategories.html#update-taxcategory
* @method TaxCategory mapResponse(ApiResponseInterface $response)
* @method TaxCategory mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null)
*/
class TaxCategoryUpdateByKeyRequest extends AbstractUpdateByKeyRequest
{
protected $resultClass = TaxCategory::class;

/**
* @param string $key
* @param int $version
* @param array $actions
* @param Context $context
*/
public function __construct($key, $version, array $actions = [], Context $context = null)
{
parent::__construct(TaxCategoriesEndpoint::endpoint(), $key, $version, $actions, $context);
}

/**
* @param string $key
* @param int $version
* @param Context $context
* @return static
*/
public static function ofKeyAndVersion($key, $version, Context $context = null)
{
return new static($key, $version, [], $context);
}
}
1 change: 1 addition & 0 deletions tests/fixtures/models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ taxCategory:
- name
- description
- rates
- key

taxRate:
domain: taxCategory
Expand Down
31 changes: 31 additions & 0 deletions tests/integration/TaxCategory/TaxCategoryQueryRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Commercetools\Core\Model\TaxCategory\TaxRate;
use Commercetools\Core\Model\TaxCategory\TaxRateCollection;
use Commercetools\Core\Request\TaxCategories\TaxCategoryByIdGetRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryByKeyGetRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryCreateRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryDeleteByKeyRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryDeleteRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryQueryRequest;

Expand Down Expand Up @@ -78,4 +80,33 @@ public function testGetById()
$this->assertSame($taxCategory->getId(), $result->getId());

}

public function testGetByKey()
{
$draft = $this->getDraft()->setKey($this->getTestRun());
$taxCategory = $this->createTaxCategory($draft);

$request = TaxCategoryByKeyGetRequest::ofKey($taxCategory->getKey());
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);

$this->assertInstanceOf(TaxCategory::class, $taxCategory);
$this->assertSame($taxCategory->getId(), $result->getId());
}


public function testDeleteByKey()
{
$draft = $this->getDraft()->setKey($this->getTestRun());
$taxCategory = $this->createTaxCategory($draft);

$request = TaxCategoryDeleteByKeyRequest::ofKeyAndVersion(
$taxCategory->getKey(),
$taxCategory->getVersion()
);
$result = $this->getClient()->execute($request);
$response = $request->mapFromResponse($result);

$this->assertSame($taxCategory->getId(), $response->getId());
}
}
43 changes: 43 additions & 0 deletions tests/integration/TaxCategory/TaxCategoryUpdateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
use Commercetools\Core\Request\TaxCategories\Command\TaxCategoryRemoveTaxRateAction;
use Commercetools\Core\Request\TaxCategories\Command\TaxCategoryReplaceTaxRateAction;
use Commercetools\Core\Request\TaxCategories\Command\TaxCategorySetDescriptionAction;
use Commercetools\Core\Request\TaxCategories\Command\TaxCategorySetKeyAction;
use Commercetools\Core\Request\TaxCategories\TaxCategoryCreateRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryDeleteRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryUpdateRequest;
use Commercetools\Core\Request\TaxCategories\TaxCategoryUpdateByKeyRequest;
use function GuzzleHttp\Psr7\str;

class TaxCategoryUpdateRequestTest extends ApiTestCase
{
Expand Down Expand Up @@ -78,6 +81,46 @@ public function testChangeName()
$this->assertNotSame($taxCategory->getVersion(), $result->getVersion());
}

public function testUpdateNameByKey()
{
$draft = $this->getDraft('update name')->setKey('key-' . $this->getTestRun());
$taxCategory = $this->createTaxCategory($draft);

$name = $this->getTestRun() . '-new-name';
$request = TaxCategoryUpdateByKeyRequest::ofKeyAndVersion($taxCategory->getKey(), $taxCategory->getVersion())
->addAction(TaxCategoryChangeNameAction::ofName($name));

$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);
$this->deleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(TaxCategory::class, $result);
$this->assertSame($name, $result->getName());
$this->assertNotSame($taxCategory->getVersion(), $result->getVersion());
}

public function testSetKey()
{
$draft = $this->getDraft('set-key');
$taxCategory = $this->createTaxCategory($draft);

$key = $this->getTestRun() . '-new-key';
$request = TaxCategoryUpdateRequest::ofIdAndVersion($taxCategory->getId(), $taxCategory->getVersion())
->addAction(
TaxCategorySetKeyAction::ofKey($key)
)
;
$response = $request->executeWithClient($this->getClient());
$result = $request->mapResponse($response);

$this->deleteRequest->setVersion($result->getVersion());

$this->assertInstanceOf(TaxCategory::class, $result);
$this->assertNotSame($key, $draft->getKey());
$this->assertSame($key, $result->getKey());
$this->assertNotSame($taxCategory->getVersion(), $result->getVersion());
}

public function testSetDescription()
{
$draft = $this->getDraft('set-description');
Expand Down

0 comments on commit 6c41b97

Please sign in to comment.