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

Commit

Permalink
feat(Product): support product price selection
Browse files Browse the repository at this point in the history
Closes #225
  • Loading branch information
Jens Schulze committed Jun 28, 2016
1 parent e61d29d commit ebac224
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Request/Products/ProductByIdGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,6 +19,8 @@
*/
class ProductByIdGetRequest extends AbstractByIdGetRequest
{
use PriceSelectTrait;

protected $resultClass = '\Commercetools\Core\Model\Product\Product';

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Request/Products/ProductProjectionByIdGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,6 +20,7 @@
*/
class ProductProjectionByIdGetRequest extends AbstractByIdGetRequest
{
use PriceSelectTrait;
use StagedTrait;

protected $resultClass = '\Commercetools\Core\Model\Product\ProductProjection';
Expand Down
3 changes: 2 additions & 1 deletion src/Request/Products/ProductProjectionBySkuGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -31,6 +31,7 @@ class ProductProjectionBySkuGetRequest extends AbstractApiRequest
use StagedTrait;
use PageTrait;
use ExpandTrait;
use PriceSelectTrait;

/**
* @param string $sku
Expand Down
3 changes: 2 additions & 1 deletion src/Request/Products/ProductProjectionBySlugGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -35,6 +35,7 @@ class ProductProjectionBySlugGetRequest extends AbstractApiRequest
use StagedTrait;
use PageTrait;
use ExpandTrait;
use PriceSelectTrait;

/**
* @param string $slug
Expand Down
2 changes: 2 additions & 0 deletions src/Request/Products/ProductProjectionQueryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,6 +21,7 @@
class ProductProjectionQueryRequest extends AbstractQueryRequest
{
use StagedTrait;
use PriceSelectTrait;

protected $resultClass = '\Commercetools\Core\Model\Product\ProductProjectionCollection';

Expand Down
3 changes: 3 additions & 0 deletions src/Request/Products/ProductQueryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,6 +19,8 @@
*/
class ProductQueryRequest extends AbstractQueryRequest
{
use PriceSelectTrait;

protected $resultClass = '\Commercetools\Core\Model\Product\ProductCollection';

/**
Expand Down
120 changes: 120 additions & 0 deletions tests/integration/Product/ProductQueryRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit ebac224

Please # to comment.