Skip to content

Commit dd874ed

Browse files
sabinebaerwachterjohannes
authored andcommitted
Load products in the correct order (#103)
* Load products in the correct order * Fixed product views array and tests * Fix order in product content selection
1 parent 6f117a6 commit dd874ed

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

Model/Product/Handler/Query/FindProductViewsQueryHandler.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ public function __invoke(FindProductViewsQuery $query): void
6363
[$liveDimension, $localizedLiveDimension]
6464
);
6565

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

71-
$query->setProductViews($productViews);
76+
$query->setProductViews(array_values(array_filter($productViews)));
7277
}
7378
}

Model/Product/Handler/Query/ListProductsQueryHandler.php

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function __invoke(ListProductsQuery $query): void
7171
$listBuilder->addSelectField($fieldDescriptors['stage']);
7272
$listBuilder->setParameter('stage', DimensionInterface::ATTRIBUTE_VALUE_DRAFT);
7373

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

8182
$listResponse = $listBuilder->execute();
8283

84+
if (is_array($ids)) {
85+
// Creates an array with the product ids as keys and null as value.
86+
// This is needed to keep the correct order of products.
87+
$orderedListResponse = array_fill_keys(array_keys(array_flip($ids)), null);
88+
foreach ($listResponse as $product) {
89+
$orderedListResponse[$product['id']] = $product;
90+
}
91+
92+
$listResponse = array_values(array_filter($orderedListResponse));
93+
}
94+
8395
$products = new ListRepresentation(
8496
$listResponse,
8597
ProductInterface::RESOURCE_KEY,

Tests/Functional/Controller/Product/ProductControllerTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,32 @@ public function testCGetAction(): void
7575
$this->assertEquals($productInformation1->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['id']);
7676
$this->assertEquals($productInformation1->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['name']);
7777
}
78+
79+
public function testCGetActionWithIds(): void
80+
{
81+
$product = $this->createProduct('product-1');
82+
$productInformation1 = $this->createProductInformation($product->getId(), 'en');
83+
$productInformation1->setName('Product One');
84+
$this->getEntityManager()->flush();
85+
86+
$product2 = $this->createProduct('product-2');
87+
$productInformation2 = $this->createProductInformation($product2->getId(), 'en');
88+
$productInformation2->setName('Product Two');
89+
$this->getEntityManager()->flush();
90+
91+
$client = $this->createAuthenticatedClient();
92+
$client->request('GET', '/api/products?locale=en&ids=' . $product2->getId() . ',' . $product->getId());
93+
94+
/** @var Response $response */
95+
$response = $client->getResponse();
96+
$this->assertInstanceOf(Response::class, $response);
97+
$result = json_decode((string) $response->getContent(), true);
98+
$this->assertEquals(200, $response->getStatusCode());
99+
100+
$this->assertCount(2, $result['_embedded'][ProductInterface::RESOURCE_KEY]);
101+
$this->assertEquals($productInformation2->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['id']);
102+
$this->assertEquals($productInformation2->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][0]['name']);
103+
$this->assertEquals($productInformation1->getProductId(), $result['_embedded'][ProductInterface::RESOURCE_KEY][1]['id']);
104+
$this->assertEquals($productInformation1->getName(), $result['_embedded'][ProductInterface::RESOURCE_KEY][1]['name']);
105+
}
78106
}

Tests/Unit/Model/Product/Handler/Query/FindProductViewsQueryHandlerTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ public function testInvoke(): void
5555
)->willReturn($localizedDimension->reveal());
5656

5757
$product1 = $this->prophesize(ProductInterface::class);
58+
$product1->getId()->willReturn('111-111-111');
5859
$product2 = $this->prophesize(ProductInterface::class);
60+
$product2->getId()->willReturn('222-222-222');
5961

6062
$productRepository->findByIdsAndDimensionIds(
6163
['111-111-111', '222-222-222'],
6264
[$dimension->reveal(), $localizedDimension->reveal()]
6365
)
64-
->willReturn([$product1->reveal(), $product2->reveal()])
66+
->willReturn([$product2->reveal(), $product1->reveal()])
6567
->shouldBeCalled();
6668

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

0 commit comments

Comments
 (0)