diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index c09fe16..4fe29f5 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,10 @@ class FoodOrder public function isClosed(): bool { - return $this->closedAt instanceof DateTimeImmutable && $this->closedAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp(); + if (! $this->closedAt instanceof DateTimeImmutable) { + return false; + } + return $this->closedAt < new DateTimeImmutable; } public function close(): static diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index e262ccf..a7c222e 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -182,6 +182,38 @@ 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..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; @@ -63,6 +64,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 +119,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 +162,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(); + + $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 +217,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 {