From 83989916a3a60be8b82459bbf54d19059b810691 Mon Sep 17 00:00:00 2001 From: lubiana Date: Wed, 14 Aug 2024 19:41:04 +0200 Subject: [PATCH 1/2] #45 sort orderitems aplhabetically in order view show --- src/Entity/FoodOrder.php | 16 ++++++ templates/food_order/show.html.twig | 2 +- tests/Controller/FoodOrderControllerTest.php | 57 ++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index fe16eac..eaf8d50 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -99,6 +99,22 @@ class FoodOrder return $this->orderItems; } + /** + * @return Collection + */ + public function getOrderItemsSortedByName(): Collection + { + $iterator = $this->getOrderItems()->getIterator(); + $iterator->uasort( + fn (OrderItem $a, OrderItem $b): int => $a->getName() <=> $b->getName() + ); + return new ArrayCollection( + iterator_to_array( + $iterator + ) + ); + } + public function addOrderItem(OrderItem $orderItem): static { if (! $this->orderItems->contains($orderItem)) { diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index 1e05313..48f4b01 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -43,7 +43,7 @@ - {% for item in food_order.orderItems %} + {% for item in food_order.orderItemsSortedByName %} {{ item.createdBy }} {{ item.name }} diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index 6eda822..6f36149 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -4,6 +4,8 @@ namespace App\Tests\Controller; use App\Entity\FoodOrder; use App\Entity\FoodVendor; +use App\Entity\MenuItem; +use App\Entity\OrderItem; use App\Tests\DbWebTest; use Override; use Symfony\Component\DomCrawler\Crawler; @@ -52,6 +54,61 @@ final class FoodOrderControllerTest extends DbWebTest ); } + public function testOrderedItems(): void + { + $order = new FoodOrder; + $order->setFoodVendor($this->vendor); + $this->manager->persist($order); + $this->manager->persist($this->vendor); + + $menuItemA = new MenuItem(); + $menuItemA->setName('A'); + $menuItemA->setFoodVendor($this->vendor); + $this->manager->persist($menuItemA); + $itemA = new OrderItem; + $itemA->setMenuItem($menuItemA); + $itemA->setName($menuItemA->getName()); + $order->addOrderItem($itemA); + $this->manager->persist($itemA); + + $menuItemC = new MenuItem(); + $menuItemC->setName('C'); + $menuItemC->setFoodVendor($this->vendor); + $this->manager->persist($menuItemC); + $itemC = new OrderItem; + $itemC->setMenuItem($menuItemC); + $itemC->setName($menuItemC->getName()); + $order->addOrderItem($itemC); + $this->manager->persist($itemC); + + $menuItemB = new MenuItem(); + $menuItemB->setName('B'); + $menuItemB->setFoodVendor($this->vendor); + $this->manager->persist($menuItemB); + $itemB = new OrderItem; + $itemB->setMenuItem($menuItemB); + $itemB->setName($menuItemB->getName()); + $order->addOrderItem($itemB); + $this->manager->persist($itemB); + + $this->manager->flush(); + + $crawler = $this->client->request('GET', "{$this->path}{$order->getId()}"); + self::assertResponseIsSuccessful(); + $tdContent = $crawler->filter( + 'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)' + )->text(); + $this->assertEquals('A', $tdContent); + $tdContent = $crawler->filter( + 'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(2) > td:nth-child(2)' + )->text(); + $this->assertEquals('B', $tdContent); + $tdContent = $crawler->filter( + 'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(3) > td:nth-child(2)' + )->text(); + $this->assertEquals('C', $tdContent); + } + public function testPaginatedIndex(): void { $this->generatePaginatedOrders(); -- 2.39.5 From 25b73e7da5b507837137408bf6ac00e99066c59b Mon Sep 17 00:00:00 2001 From: Continuous Integration Date: Wed, 14 Aug 2024 17:41:59 +0000 Subject: [PATCH 2/2] Continuous Integration Fixes --- src/Entity/FoodOrder.php | 7 +++++-- tests/Controller/FoodOrderControllerTest.php | 13 ++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index eaf8d50..c09fe16 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -11,6 +11,8 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Types\UlidType; use Symfony\Component\Uid\Ulid; +use function iterator_to_array; + #[ORM\Entity(repositoryClass: FoodOrderRepository::class)] class FoodOrder { @@ -104,9 +106,10 @@ class FoodOrder */ public function getOrderItemsSortedByName(): Collection { - $iterator = $this->getOrderItems()->getIterator(); + $iterator = $this->getOrderItems() + ->getIterator(); $iterator->uasort( - fn (OrderItem $a, OrderItem $b): int => $a->getName() <=> $b->getName() + static fn(OrderItem $a, OrderItem $b): int => $a->getName() <=> $b->getName() ); return new ArrayCollection( iterator_to_array( diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index 6f36149..e262ccf 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -58,36 +58,43 @@ final class FoodOrderControllerTest extends DbWebTest { $order = new FoodOrder; $order->setFoodVendor($this->vendor); + $this->manager->persist($order); $this->manager->persist($this->vendor); - $menuItemA = new MenuItem(); + $menuItemA = new MenuItem; $menuItemA->setName('A'); $menuItemA->setFoodVendor($this->vendor); + $this->manager->persist($menuItemA); $itemA = new OrderItem; $itemA->setMenuItem($menuItemA); $itemA->setName($menuItemA->getName()); + $order->addOrderItem($itemA); $this->manager->persist($itemA); - $menuItemC = new MenuItem(); + $menuItemC = new MenuItem; $menuItemC->setName('C'); $menuItemC->setFoodVendor($this->vendor); + $this->manager->persist($menuItemC); $itemC = new OrderItem; $itemC->setMenuItem($menuItemC); $itemC->setName($menuItemC->getName()); + $order->addOrderItem($itemC); $this->manager->persist($itemC); - $menuItemB = new MenuItem(); + $menuItemB = new MenuItem; $menuItemB->setName('B'); $menuItemB->setFoodVendor($this->vendor); + $this->manager->persist($menuItemB); $itemB = new OrderItem; $itemB->setMenuItem($menuItemB); $itemB->setName($menuItemB->getName()); + $order->addOrderItem($itemB); $this->manager->persist($itemB); -- 2.39.5