Skip to content

Commit

Permalink
Load products in the correct order (#103)
Browse files Browse the repository at this point in the history
* Load products in the correct order

* Fixed product views array and tests

* Fix order in product content selection
  • Loading branch information
sabinebaer authored and wachterjohannes committed Nov 11, 2019
1 parent 6f117a6 commit dd874ed
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Model/Product/Handler/Query/FindProductViewsQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ public function __invoke(FindProductViewsQuery $query): void
[$liveDimension, $localizedLiveDimension]
);

$productViews = [];
// Creates an array with the product ids as keys and null as value.
// This is needed to keep the correct order of products.
$productViews = array_fill_keys(array_keys(array_flip($query->getIds())), null);
foreach ($products as $product) {
$productViews[] = $this->productViewFactory->create($product, [$liveDimension, $localizedLiveDimension]);
$productViews[$product->getId()] = $this->productViewFactory->create(
$product,
[$liveDimension, $localizedLiveDimension]
);
}

$query->setProductViews($productViews);
$query->setProductViews(array_values(array_filter($productViews)));
}
}
12 changes: 12 additions & 0 deletions Model/Product/Handler/Query/ListProductsQueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function __invoke(ListProductsQuery $query): void
$listBuilder->addSelectField($fieldDescriptors['stage']);
$listBuilder->setParameter('stage', DimensionInterface::ATTRIBUTE_VALUE_DRAFT);

$ids = null;
if (array_key_exists('ids', $query->getQuery())) {
$ids = explode(',', $query->getQuery()['ids']);
$listBuilder->in($fieldDescriptors['id'], $ids);
Expand All @@ -80,6 +81,17 @@ public function __invoke(ListProductsQuery $query): void

$listResponse = $listBuilder->execute();

if (is_array($ids)) {
// Creates an array with the product ids as keys and null as value.
// This is needed to keep the correct order of products.
$orderedListResponse = array_fill_keys(array_keys(array_flip($ids)), null);
foreach ($listResponse as $product) {
$orderedListResponse[$product['id']] = $product;
}

$listResponse = array_values(array_filter($orderedListResponse));
}

$products = new ListRepresentation(
$listResponse,
ProductInterface::RESOURCE_KEY,
Expand Down
28 changes: 28 additions & 0 deletions Tests/Functional/Controller/Product/ProductControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,32 @@ public function testCGetAction(): void
$this->assertEquals($productInformation1->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['id']);
$this->assertEquals($productInformation1->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['name']);
}

public function testCGetActionWithIds(): void
{
$product = $this->createProduct('product-1');
$productInformation1 = $this->createProductInformation($product->getId(), 'en');
$productInformation1->setName('Product One');
$this->getEntityManager()->flush();

$product2 = $this->createProduct('product-2');
$productInformation2 = $this->createProductInformation($product2->getId(), 'en');
$productInformation2->setName('Product Two');
$this->getEntityManager()->flush();

$client = $this->createAuthenticatedClient();
$client->request('GET', '/api/products?locale=en&ids=' . $product2->getId() . ',' . $product->getId());

/** @var Response $response */
$response = $client->getResponse();
$this->assertInstanceOf(Response::class, $response);
$result = json_decode((string) $response->getContent(), true);
$this->assertEquals(200, $response->getStatusCode());

$this->assertCount(2, $result['_embedded'][ProductInterface::RESOURCE_KEY]);
$this->assertEquals($productInformation2->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['id']);
$this->assertEquals($productInformation2->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['name']);
$this->assertEquals($productInformation1->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][1]['id']);
$this->assertEquals($productInformation1->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][1]['name']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ public function testInvoke(): void
)->willReturn($localizedDimension->reveal());

$product1 = $this->prophesize(ProductInterface::class);
$product1->getId()->willReturn('111-111-111');
$product2 = $this->prophesize(ProductInterface::class);
$product2->getId()->willReturn('222-222-222');

$productRepository->findByIdsAndDimensionIds(
['111-111-111', '222-222-222'],
[$dimension->reveal(), $localizedDimension->reveal()]
)
->willReturn([$product1->reveal(), $product2->reveal()])
->willReturn([$product2->reveal(), $product1->reveal()])
->shouldBeCalled();

$productView1 = $this->prophesize(ProductViewInterface::class);
Expand Down

0 comments on commit dd874ed

Please sign in to comment.