From ebac224427d26a85a6fe9e81d149c440550b6580 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Tue, 28 Jun 2016 17:00:41 +0200 Subject: [PATCH] feat(Product): support product price selection Closes #225 --- .../Products/ProductByIdGetRequest.php | 3 + .../ProductProjectionByIdGetRequest.php | 2 + .../ProductProjectionBySkuGetRequest.php | 3 +- .../ProductProjectionBySlugGetRequest.php | 3 +- .../ProductProjectionQueryRequest.php | 2 + src/Request/Products/ProductQueryRequest.php | 3 + .../Product/ProductQueryRequestTest.php | 120 ++++++++++++++++++ 7 files changed, 134 insertions(+), 2 deletions(-) diff --git a/src/Request/Products/ProductByIdGetRequest.php b/src/Request/Products/ProductByIdGetRequest.php index 1308ddeb96..6ea24d7aea 100644 --- a/src/Request/Products/ProductByIdGetRequest.php +++ b/src/Request/Products/ProductByIdGetRequest.php @@ -9,6 +9,7 @@ use Commercetools\Core\Model\Common\Context; use Commercetools\Core\Request\AbstractByIdGetRequest; use Commercetools\Core\Model\Product\Product; +use Commercetools\Core\Request\PriceSelectTrait; use Commercetools\Core\Response\ApiResponseInterface; /** @@ -18,6 +19,8 @@ */ class ProductByIdGetRequest extends AbstractByIdGetRequest { + use PriceSelectTrait; + protected $resultClass = '\Commercetools\Core\Model\Product\Product'; /** diff --git a/src/Request/Products/ProductProjectionByIdGetRequest.php b/src/Request/Products/ProductProjectionByIdGetRequest.php index 77f7b49ee0..98db70ecba 100644 --- a/src/Request/Products/ProductProjectionByIdGetRequest.php +++ b/src/Request/Products/ProductProjectionByIdGetRequest.php @@ -9,6 +9,7 @@ use Commercetools\Core\Model\Common\Context; use Commercetools\Core\Model\Product\ProductProjection; use Commercetools\Core\Request\AbstractByIdGetRequest; +use Commercetools\Core\Request\PriceSelectTrait; use Commercetools\Core\Request\StagedTrait; use Commercetools\Core\Response\ApiResponseInterface; @@ -19,6 +20,7 @@ */ class ProductProjectionByIdGetRequest extends AbstractByIdGetRequest { + use PriceSelectTrait; use StagedTrait; protected $resultClass = '\Commercetools\Core\Model\Product\ProductProjection'; diff --git a/src/Request/Products/ProductProjectionBySkuGetRequest.php b/src/Request/Products/ProductProjectionBySkuGetRequest.php index 20b5bd0487..1ae51daefc 100644 --- a/src/Request/Products/ProductProjectionBySkuGetRequest.php +++ b/src/Request/Products/ProductProjectionBySkuGetRequest.php @@ -5,6 +5,7 @@ namespace Commercetools\Core\Request\Products; +use Commercetools\Core\Request\PriceSelectTrait; use Psr\Http\Message\ResponseInterface; use Commercetools\Core\Client\HttpMethod; use Commercetools\Core\Client\HttpRequest; @@ -16,7 +17,6 @@ use Commercetools\Core\Request\StagedTrait; use Commercetools\Core\Response\ResourceResponse; use Commercetools\Core\Model\Product\ProductProjection; -use Commercetools\Core\Model\Common\JsonObject; use Commercetools\Core\Response\ApiResponseInterface; /** @@ -31,6 +31,7 @@ class ProductProjectionBySkuGetRequest extends AbstractApiRequest use StagedTrait; use PageTrait; use ExpandTrait; + use PriceSelectTrait; /** * @param string $sku diff --git a/src/Request/Products/ProductProjectionBySlugGetRequest.php b/src/Request/Products/ProductProjectionBySlugGetRequest.php index b72bb135a3..cacfef681b 100644 --- a/src/Request/Products/ProductProjectionBySlugGetRequest.php +++ b/src/Request/Products/ProductProjectionBySlugGetRequest.php @@ -5,6 +5,7 @@ namespace Commercetools\Core\Request\Products; +use Commercetools\Core\Request\PriceSelectTrait; use Psr\Http\Message\ResponseInterface; use Commercetools\Core\Client\HttpMethod; use Commercetools\Core\Client\HttpRequest; @@ -18,7 +19,6 @@ use Commercetools\Core\Request\QueryTrait; use Commercetools\Core\Request\StagedTrait; use Commercetools\Core\Response\ResourceResponse; -use Commercetools\Core\Model\Common\JsonObject; use Commercetools\Core\Response\ApiResponseInterface; /** @@ -35,6 +35,7 @@ class ProductProjectionBySlugGetRequest extends AbstractApiRequest use StagedTrait; use PageTrait; use ExpandTrait; + use PriceSelectTrait; /** * @param string $slug diff --git a/src/Request/Products/ProductProjectionQueryRequest.php b/src/Request/Products/ProductProjectionQueryRequest.php index d9f078f931..f3ff9e2586 100644 --- a/src/Request/Products/ProductProjectionQueryRequest.php +++ b/src/Request/Products/ProductProjectionQueryRequest.php @@ -9,6 +9,7 @@ use Commercetools\Core\Model\Common\Context; use Commercetools\Core\Model\Product\ProductProjectionCollection; use Commercetools\Core\Request\AbstractQueryRequest; +use Commercetools\Core\Request\PriceSelectTrait; use Commercetools\Core\Request\StagedTrait; use Commercetools\Core\Response\ApiResponseInterface; @@ -20,6 +21,7 @@ class ProductProjectionQueryRequest extends AbstractQueryRequest { use StagedTrait; + use PriceSelectTrait; protected $resultClass = '\Commercetools\Core\Model\Product\ProductProjectionCollection'; diff --git a/src/Request/Products/ProductQueryRequest.php b/src/Request/Products/ProductQueryRequest.php index 5d9ce4d10f..eaaa6ade5c 100644 --- a/src/Request/Products/ProductQueryRequest.php +++ b/src/Request/Products/ProductQueryRequest.php @@ -9,6 +9,7 @@ use Commercetools\Core\Model\Common\Context; use Commercetools\Core\Request\AbstractQueryRequest; use Commercetools\Core\Model\Product\ProductCollection; +use Commercetools\Core\Request\PriceSelectTrait; use Commercetools\Core\Response\ApiResponseInterface; /** @@ -18,6 +19,8 @@ */ class ProductQueryRequest extends AbstractQueryRequest { + use PriceSelectTrait; + protected $resultClass = '\Commercetools\Core\Model\Product\ProductCollection'; /** diff --git a/tests/integration/Product/ProductQueryRequestTest.php b/tests/integration/Product/ProductQueryRequestTest.php index 236bd09562..f8fa77f470 100644 --- a/tests/integration/Product/ProductQueryRequestTest.php +++ b/tests/integration/Product/ProductQueryRequestTest.php @@ -8,10 +8,16 @@ use Commercetools\Core\ApiTestCase; use Commercetools\Core\Model\Common\LocalizedString; +use Commercetools\Core\Model\Common\Money; +use Commercetools\Core\Model\Common\PriceDraft; +use Commercetools\Core\Model\Common\PriceDraftCollection; use Commercetools\Core\Model\Product\ProductDraft; +use Commercetools\Core\Model\Product\ProductVariantDraft; use Commercetools\Core\Request\Products\ProductByIdGetRequest; use Commercetools\Core\Request\Products\ProductCreateRequest; use Commercetools\Core\Request\Products\ProductDeleteRequest; +use Commercetools\Core\Request\Products\ProductProjectionByIdGetRequest; +use Commercetools\Core\Request\Products\ProductProjectionQueryRequest; use Commercetools\Core\Request\Products\ProductQueryRequest; class ProductQueryRequestTest extends ApiTestCase @@ -70,5 +76,119 @@ public function testGetById() $this->assertInstanceOf('\Commercetools\Core\Model\Product\Product', $product); $this->assertSame($product->getId(), $result->getId()); } + + public function testPriceSelectProductQuery() + { + $draft = $this->getDraft(); + $draft->setMasterVariant( + ProductVariantDraft::of()->setSku('sku' . uniqid()) + ->setPrices( + PriceDraftCollection::of()->add( + PriceDraft::ofMoney(Money::ofCurrencyAndAmount('EUR', 100)) + ) + ) + ); + $this->createProduct($draft); + + $request = ProductQueryRequest::of() + ->where('masterData(current(name(en="' . $draft->getName()->en . '")))') + ->currency('EUR') + ; + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertCount(1, $result); + $this->assertInstanceOf('\Commercetools\Core\Model\Product\Product', $result->getAt(0)); + $this->assertEmpty($result->current()->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getCountry()); + $this->assertEmpty($result->current()->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getChannel()); + $this->assertEmpty($result->current()->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getCustomerGroup()); + $this->assertSame('EUR', $result->current()->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getValue()->getCurrencyCode()); + $this->assertSame(100, $result->current()->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getValue()->getCentAmount()); + } + + public function testPriceSelectProductById() + { + $draft = $this->getDraft(); + $draft->setMasterVariant( + ProductVariantDraft::of()->setSku('sku' . uniqid()) + ->setPrices( + PriceDraftCollection::of()->add( + PriceDraft::ofMoney(Money::ofCurrencyAndAmount('EUR', 100)) + ) + ) + ); + $product = $this->createProduct($draft); + + $request = ProductByIdGetRequest::ofId($product->getId()) + ->currency('EUR') + ; + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertInstanceOf('\Commercetools\Core\Model\Product\Product', $result); + $this->assertEmpty($result->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getCountry()); + $this->assertEmpty($result->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getChannel()); + $this->assertEmpty($result->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getCustomerGroup()); + $this->assertSame('EUR', $result->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getValue()->getCurrencyCode()); + $this->assertSame(100, $result->getMasterData()->getStaged()->getMasterVariant()->getPrice()->getValue()->getCentAmount()); + } + + public function testPriceSelectProductProjectionQuery() + { + $draft = $this->getDraft(); + $draft->setMasterVariant( + ProductVariantDraft::of()->setSku('sku' . uniqid()) + ->setPrices( + PriceDraftCollection::of()->add( + PriceDraft::ofMoney(Money::ofCurrencyAndAmount('EUR', 100)) + ) + ) + ); + $this->createProduct($draft); + + $request = ProductProjectionQueryRequest::of() + ->where('name(en="' . $draft->getName()->en . '")') + ->currency('EUR') + ->staged(true) + ; + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertCount(1, $result); + $this->assertInstanceOf('\Commercetools\Core\Model\Product\ProductProjection', $result->getAt(0)); + $this->assertEmpty($result->current()->getMasterVariant()->getPrice()->getCountry()); + $this->assertEmpty($result->current()->getMasterVariant()->getPrice()->getChannel()); + $this->assertEmpty($result->current()->getMasterVariant()->getPrice()->getCustomerGroup()); + $this->assertSame('EUR', $result->current()->getMasterVariant()->getPrice()->getValue()->getCurrencyCode()); + $this->assertSame(100, $result->current()->getMasterVariant()->getPrice()->getValue()->getCentAmount()); + } + + public function testPriceSelectProductProjectionById() + { + $draft = $this->getDraft(); + $draft->setMasterVariant( + ProductVariantDraft::of()->setSku('sku' . uniqid()) + ->setPrices( + PriceDraftCollection::of()->add( + PriceDraft::ofMoney(Money::ofCurrencyAndAmount('EUR', 100)) + ) + ) + ); + $product = $this->createProduct($draft); + + $request = ProductProjectionByIdGetRequest::ofId($product->getId()) + ->currency('EUR') + ->staged(true) + ; + $response = $request->executeWithClient($this->getClient()); + $result = $request->mapResponse($response); + + $this->assertInstanceOf('\Commercetools\Core\Model\Product\ProductProjection', $result); + $this->assertEmpty($result->getMasterVariant()->getPrice()->getCountry()); + $this->assertEmpty($result->getMasterVariant()->getPrice()->getChannel()); + $this->assertEmpty($result->getMasterVariant()->getPrice()->getCustomerGroup()); + $this->assertSame('EUR', $result->getMasterVariant()->getPrice()->getValue()->getCurrencyCode()); + $this->assertSame(100, $result->getMasterVariant()->getPrice()->getValue()->getCentAmount()); + } }