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

Commit b3bc4ad

Browse files
committed
feat(Query): add support for parametrized queries
Closes #505
1 parent 14589ec commit b3bc4ad

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/Core/Request/QueryTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ abstract public function addParamObject(ParameterInterface $param);
2222

2323
/**
2424
* @param string $where
25+
* @param array $params
2526
* @return $this
2627
*/
27-
public function where($where)
28+
public function where($where, array $params = [])
2829
{
2930
if (!is_null($where)) {
3031
$this->addParamObject(new MultiParameter('where', $where));
32+
foreach ($params as $paramName => $paramValue) {
33+
$this->addParam($paramName, $paramValue);
34+
}
3135
}
3236

3337
return $this;

tests/integration/Product/ProductQueryRequestTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Commercetools\Core\Model\Product\ProductDraft;
1616
use Commercetools\Core\Model\Product\ProductProjection;
1717
use Commercetools\Core\Model\Product\ProductVariantDraft;
18+
use Commercetools\Core\Model\Product\ProductVariantDraftCollection;
1819
use Commercetools\Core\Request\Products\ProductByIdGetRequest;
1920
use Commercetools\Core\Request\Products\ProductByKeyGetRequest;
2021
use Commercetools\Core\Request\Products\ProductCreateRequest;
@@ -60,7 +61,8 @@ public function testQuery()
6061
$draft = $this->getDraft();
6162
$product = $this->createProduct($draft);
6263

63-
$request = ProductQueryRequest::of()->where('masterData(current(name(en="' . $draft->getName()->en . '")))');
64+
$request = ProductQueryRequest::of()->where('masterData(current(name(en=:name)))', ['name' => $draft->getName()->en]);
65+
6466
$response = $request->executeWithClient($this->getClient());
6567
$result = $request->mapResponse($response);
6668

@@ -125,7 +127,7 @@ public function testPriceSelectProductQuery()
125127
$this->createProduct($draft);
126128

127129
$request = ProductQueryRequest::of()
128-
->where('masterData(current(name(en="' . $draft->getName()->en . '")))')
130+
->where('masterData(current(name(en=:name)))', ['name' => $draft->getName()->en])
129131
->currency('EUR')
130132
;
131133
$response = $request->executeWithClient($this->getClient());
@@ -197,6 +199,33 @@ public function testPriceSelectProductProjectionQuery()
197199
$this->assertSame(100, $result->current()->getMasterVariant()->getPrice()->getValue()->getCentAmount());
198200
}
199201

202+
public function testSkuParametrized()
203+
{
204+
$draft = $this->getDraft();
205+
$sku1 = 'sku1' . uniqid();
206+
$sku2 = 'sku2' . uniqid();
207+
$draft->setMasterVariant(ProductVariantDraft::of()->setSku($sku1));
208+
$draft->setVariants(ProductVariantDraftCollection::of()->add(ProductVariantDraft::of()->setSku($sku2)));
209+
$this->createProduct($draft);
210+
211+
$request = ProductProjectionQueryRequest::of()
212+
->where(
213+
'masterVariant(sku in (:skus1, :skus2)) or variants(sku in (:skus1, :skus2))',
214+
[
215+
'skus1' => 'whatever',
216+
'skus2' => $sku2
217+
]
218+
)
219+
->staged(true)
220+
;
221+
$response = $request->executeWithClient($this->getClient());
222+
$result = $request->mapResponse($response);
223+
$this->assertCount(1, $result);
224+
$this->assertInstanceOf(ProductProjection::class, $result->getAt(0));
225+
$this->assertSame($sku1, $result->current()->getMasterVariant()->getSku());
226+
$this->assertSame($sku2, $result->current()->getVariants()->current()->getSku());
227+
}
228+
200229
public function testPriceSelectProductProjectionById()
201230
{
202231
$draft = $this->getDraft();

0 commit comments

Comments
 (0)