From 6f23c3c1b765962931c48013261c09c7b80fce85 Mon Sep 17 00:00:00 2001 From: lubiana Date: Fri, 16 Aug 2024 11:14:41 +0200 Subject: [PATCH 1/2] improve test coverage --- src/Entity/FoodOrder.php | 12 ++- tests/Controller/FoodOrderControllerTest.php | 34 ++++++++ tests/Controller/OrderItemControllerTest.php | 88 ++++++++++++++++++++ 3 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index c09fe16..89ccccf 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -58,7 +58,7 @@ class FoodOrder return $this->closedAt; } - public function setClosedAt(DateTimeImmutable|null $closedAt): static + public function setClosedAt(DateTimeImmutable|null $closedAt = null): static { $this->closedAt = $closedAt; @@ -67,7 +67,15 @@ class FoodOrder public function isClosed(): bool { - return $this->closedAt instanceof DateTimeImmutable && $this->closedAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp(); + if ($this->closedAt === null) { + return false; + } + + if ($this->closedAt < new DateTimeImmutable) { + return true; + } + + return false; } public function close(): static diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index e262ccf..00b225e 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -182,6 +182,40 @@ final class FoodOrderControllerTest extends DbWebTest self::assertSame(1, $this->repository->count([])); } + public function testOpen(): void + { + $order = new FoodOrder; + $order->setFoodVendor($this->vendor); + $order->close(); + + $this->assertTrue($order->isClosed()); + $this->manager->persist($order); + $this->manager->flush(); + + + $this->client->request('GET', sprintf('%s%s/open', $this->path, $order->getId())); + self::assertResponseRedirects("{$this->path}{$order->getId()}"); + $openOrder = $this->repository->find($order->getId()); + $this->assertFalse($openOrder->isClosed()); + } + + public function testClose(): void + { + $order = new FoodOrder; + $order->setClosedAt(); + $order->setFoodVendor($this->vendor); + + $this->assertFalse($order->isClosed()); + $this->manager->persist($order); + $this->manager->flush(); + + + $this->client->request('GET', sprintf('%s%s/close', $this->path, $order->getId())); + self::assertResponseRedirects("{$this->path}{$order->getId()}"); + $openOrder = $this->repository->find($order->getId()); + $this->assertTrue($openOrder->isClosed()); + } + private function generatePaginatedOrders(): void { foreach (range(1, 35) as $i) { diff --git a/tests/Controller/OrderItemControllerTest.php b/tests/Controller/OrderItemControllerTest.php index 0cb58b3..aff847d 100644 --- a/tests/Controller/OrderItemControllerTest.php +++ b/tests/Controller/OrderItemControllerTest.php @@ -63,6 +63,23 @@ final class OrderItemControllerTest extends DbWebTest self::assertSame(1, $this->menuItemRepository->count([])); } + public function testNewOrderClosed(): void + { + + $this->order->setClosedAt(new \DateTimeImmutable('-1 Hour')); + $this->manager->persist($this->order); + $this->manager->flush(); + + $this->client->request( + 'GET', + sprintf('%snew/%s', $this->path, $this->order->getId()) + ); + + self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); + + self::assertSame(0, $this->repository->count([])); + } + public function testNewCreateMenuItem(): void { $this->client->request( @@ -101,6 +118,26 @@ final class OrderItemControllerTest extends DbWebTest self::assertSame(0, $this->repository->count([])); } + public function testOrderClosed(): void + { + $fixture = new OrderItem; + $fixture->setName('Testing'); + $fixture->setExtras('Value'); + $fixture->setMenuItem($this->menuItem); + $fixture->setFoodOrder($this->order); + + $this->order->close(); + + $this->manager->persist($this->order); + $this->manager->persist($fixture); + $this->manager->flush(); + + $this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId())); + + self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); + self::assertSame(1, $this->repository->count([])); + } + public function testEdit(): void { $orderItem = new OrderItem; @@ -124,6 +161,35 @@ final class OrderItemControllerTest extends DbWebTest 'My Extra', $extrasElem->attr('value') ); + + $this->client->submitForm('Update', [ + 'order_item[name]' => 'Testing-1', + 'order_item[extras]' => 'Testing-1', + ]); + + self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); + + self::assertSame(1, $this->repository->count([])); + self::assertSame(2, $this->menuItemRepository->count([])); + + + } + + public function testEditOrderClosed(): void + { + $orderItem = new OrderItem; + $orderItem->setName('Testing'); + $orderItem->setExtras('My Extra'); + $orderItem->setFoodOrder($this->order); + $orderItem->setMenuItem($this->menuItem); + $this->order->close(); + + $this->manager->persist($orderItem); + $this->manager->persist($this->order); + $this->manager->flush(); + + $crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId())); + self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); } public function testCopy(): void @@ -150,6 +216,28 @@ final class OrderItemControllerTest extends DbWebTest } } + public function testCopyOrderClosed(): void + { + $orderItem = new OrderItem; + $orderItem->setName('My Title'); + $orderItem->setExtras('My Title'); + $orderItem->setFoodOrder($this->order); + $orderItem->setMenuItem($this->menuItem); + + $this->order->close(); + $this->manager->persist($this->order); + $this->manager->persist($orderItem); + $this->manager->flush(); + + $this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId())); + self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); + + $result = $this->repository->findBy([ + 'foodOrder' => $this->order->getId(), + ]); + $this->assertCount(1, $result); + } + #[Override] public function getEntityClass(): string { -- 2.39.5 From 2a28465626e414cc3cd8a285d076b3d92f2ea829 Mon Sep 17 00:00:00 2001 From: lubiana Date: Fri, 16 Aug 2024 11:15:23 +0200 Subject: [PATCH 2/2] lint --- src/Entity/FoodOrder.php | 9 ++------- tests/Controller/FoodOrderControllerTest.php | 2 -- tests/Controller/OrderItemControllerTest.php | 7 ++++--- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index 89ccccf..4fe29f5 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -67,15 +67,10 @@ class FoodOrder public function isClosed(): bool { - if ($this->closedAt === null) { + if (! $this->closedAt instanceof DateTimeImmutable) { return false; } - - if ($this->closedAt < new DateTimeImmutable) { - return true; - } - - return false; + return $this->closedAt < new DateTimeImmutable; } public function close(): static diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index 00b225e..a7c222e 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -192,7 +192,6 @@ final class FoodOrderControllerTest extends DbWebTest $this->manager->persist($order); $this->manager->flush(); - $this->client->request('GET', sprintf('%s%s/open', $this->path, $order->getId())); self::assertResponseRedirects("{$this->path}{$order->getId()}"); $openOrder = $this->repository->find($order->getId()); @@ -209,7 +208,6 @@ final class FoodOrderControllerTest extends DbWebTest $this->manager->persist($order); $this->manager->flush(); - $this->client->request('GET', sprintf('%s%s/close', $this->path, $order->getId())); self::assertResponseRedirects("{$this->path}{$order->getId()}"); $openOrder = $this->repository->find($order->getId()); diff --git a/tests/Controller/OrderItemControllerTest.php b/tests/Controller/OrderItemControllerTest.php index aff847d..1c65fb8 100644 --- a/tests/Controller/OrderItemControllerTest.php +++ b/tests/Controller/OrderItemControllerTest.php @@ -8,6 +8,7 @@ use App\Entity\MenuItem; use App\Entity\OrderItem; use App\Repository\MenuItemRepository; use App\Tests\DbWebTest; +use DateTimeImmutable; use Override; use function sprintf; @@ -66,7 +67,7 @@ final class OrderItemControllerTest extends DbWebTest public function testNewOrderClosed(): void { - $this->order->setClosedAt(new \DateTimeImmutable('-1 Hour')); + $this->order->setClosedAt(new DateTimeImmutable('-1 Hour')); $this->manager->persist($this->order); $this->manager->flush(); @@ -172,7 +173,6 @@ final class OrderItemControllerTest extends DbWebTest self::assertSame(1, $this->repository->count([])); self::assertSame(2, $this->menuItemRepository->count([])); - } public function testEditOrderClosed(): void @@ -182,13 +182,14 @@ final class OrderItemControllerTest extends DbWebTest $orderItem->setExtras('My Extra'); $orderItem->setFoodOrder($this->order); $orderItem->setMenuItem($this->menuItem); + $this->order->close(); $this->manager->persist($orderItem); $this->manager->persist($this->order); $this->manager->flush(); - $crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId())); + $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId())); self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); } -- 2.39.5