From dd874edcb328a0be759e735d24c82d5fbe05ae36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sabine=20B=C3=A4r?= Date: Mon, 11 Nov 2019 16:18:00 +0100 Subject: [PATCH] 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 --- .../Query/FindProductViewsQueryHandler.php | 11 ++++++-- .../Query/ListProductsQueryHandler.php | 12 ++++++++ .../Product/ProductControllerTest.php | 28 +++++++++++++++++++ .../FindProductViewsQueryHandlerTest.php | 4 ++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Model/Product/Handler/Query/FindProductViewsQueryHandler.php b/Model/Product/Handler/Query/FindProductViewsQueryHandler.php index e16920ae..0648613f 100644 --- a/Model/Product/Handler/Query/FindProductViewsQueryHandler.php +++ b/Model/Product/Handler/Query/FindProductViewsQueryHandler.php @@ -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))); } } diff --git a/Model/Product/Handler/Query/ListProductsQueryHandler.php b/Model/Product/Handler/Query/ListProductsQueryHandler.php index daa8052e..a69d0807 100644 --- a/Model/Product/Handler/Query/ListProductsQueryHandler.php +++ b/Model/Product/Handler/Query/ListProductsQueryHandler.php @@ -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); @@ -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, diff --git a/Tests/Functional/Controller/Product/ProductControllerTest.php b/Tests/Functional/Controller/Product/ProductControllerTest.php index 453651f1..6fbdc8d2 100644 --- a/Tests/Functional/Controller/Product/ProductControllerTest.php +++ b/Tests/Functional/Controller/Product/ProductControllerTest.php @@ -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']); + } } diff --git a/Tests/Unit/Model/Product/Handler/Query/FindProductViewsQueryHandlerTest.php b/Tests/Unit/Model/Product/Handler/Query/FindProductViewsQueryHandlerTest.php index 42f005aa..7349c875 100644 --- a/Tests/Unit/Model/Product/Handler/Query/FindProductViewsQueryHandlerTest.php +++ b/Tests/Unit/Model/Product/Handler/Query/FindProductViewsQueryHandlerTest.php @@ -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);