From 34171d6c2bc76e928501d47b7b2abeba2a236663 Mon Sep 17 00:00:00 2001 From: lubiana Date: Fri, 16 Aug 2024 13:44:16 +0200 Subject: [PATCH] 100% test coverage --- phpunit.xml.dist | 4 + src/Controller/MenuItemController.php | 4 +- src/Entity/FoodVendor.php | 15 ++-- src/Entity/MenuItem.php | 16 ++-- tests/Controller/MenuItemControllerTest.php | 98 +++++++++++++++++++++ tests/Entity/FoodOrderTest.php | 23 +++++ tests/Entity/FoodVendorTest.php | 43 +++++++++ tests/Entity/MenuItemTest.php | 27 ++++++ 8 files changed, 214 insertions(+), 16 deletions(-) create mode 100644 tests/Controller/MenuItemControllerTest.php create mode 100644 tests/Entity/FoodOrderTest.php create mode 100644 tests/Entity/FoodVendorTest.php create mode 100644 tests/Entity/MenuItemTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3417900..a57e50a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,6 +28,10 @@ src + + src/Command + src/Service/FakeData.php + diff --git a/src/Controller/MenuItemController.php b/src/Controller/MenuItemController.php index ce85e9c..dc3f63c 100644 --- a/src/Controller/MenuItemController.php +++ b/src/Controller/MenuItemController.php @@ -30,7 +30,9 @@ final class MenuItemController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $entityManager->flush(); - return $this->redirectToRoute('app_menu_item_index', [], Response::HTTP_SEE_OTHER); + return $this->redirectToRoute('app_menu_item_show', [ + 'id' => $menuItem->getId(), + ], Response::HTTP_SEE_OTHER); } return $this->render('menu_item/edit.html.twig', [ diff --git a/src/Entity/FoodVendor.php b/src/Entity/FoodVendor.php index c867c9c..e05d777 100644 --- a/src/Entity/FoodVendor.php +++ b/src/Entity/FoodVendor.php @@ -13,12 +13,6 @@ use Symfony\Component\Uid\Ulid; #[ORM\Entity(repositoryClass: FoodVendorRepository::class)] class FoodVendor { - #[ORM\Id] - #[ORM\GeneratedValue(strategy: 'CUSTOM')] - #[ORM\Column(type: UlidType::NAME, unique: true)] - #[ORM\CustomIdGenerator(class: UlidGenerator::class)] - private Ulid|null $id = null; - #[ORM\Column(length: 50)] private string|null $name = null; @@ -34,8 +28,13 @@ class FoodVendor #[ORM\OneToMany(targetEntity: MenuItem::class, mappedBy: 'foodVendor', orphanRemoval: true)] private Collection $menuItems; - public function __construct() - { + public function __construct( + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'CUSTOM')] + #[ORM\Column(type: UlidType::NAME, unique: true)] + #[ORM\CustomIdGenerator(class: UlidGenerator::class)] + private Ulid|null $id = new Ulid + ) { $this->foodOrders = new ArrayCollection; $this->menuItems = new ArrayCollection; } diff --git a/src/Entity/MenuItem.php b/src/Entity/MenuItem.php index 6e54dc3..259b175 100644 --- a/src/Entity/MenuItem.php +++ b/src/Entity/MenuItem.php @@ -12,12 +12,6 @@ use Symfony\Component\Uid\Ulid; #[ORM\Entity(repositoryClass: MenuItemRepository::class)] class MenuItem { - #[ORM\Id] - #[ORM\GeneratedValue(strategy: 'CUSTOM')] - #[ORM\Column(type: UlidType::NAME, unique: true)] - #[ORM\CustomIdGenerator(class: UlidGenerator::class)] - private Ulid|null $id = null; - #[ORM\Column(length: 255)] private string|null $name = null; @@ -28,6 +22,14 @@ class MenuItem #[ORM\Column(nullable: true)] private DateTimeImmutable|null $deletedAt = null; + public function __construct( + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'CUSTOM')] + #[ORM\Column(type: UlidType::NAME, unique: true)] + #[ORM\CustomIdGenerator(class: UlidGenerator::class)] + private Ulid|null $id = new Ulid + ) {} + public function getId(): Ulid|null { return $this->id; @@ -64,7 +66,7 @@ class MenuItem public function delete(): static { - $this->setDeletedAt(); + $this->setDeletedAt(new DateTimeImmutable); return $this; } diff --git a/tests/Controller/MenuItemControllerTest.php b/tests/Controller/MenuItemControllerTest.php new file mode 100644 index 0000000..9dc0058 --- /dev/null +++ b/tests/Controller/MenuItemControllerTest.php @@ -0,0 +1,98 @@ +vendor = new FoodVendor; + $this->vendor->setName('Food Vendor'); + + $this->manager->persist($this->vendor); + $this->manager->flush(); + } + + #[Override] + public function getEntityClass(): string + { + return MenuItem::class; + } + + public function testShow(): void + { + $menuItem = new MenuItem; + $menuItem->setName('Testing 1 2'); + + $this->vendor->addMenuItem($menuItem); + $this->manager->persist($this->vendor); + $this->manager->persist($menuItem); + $this->manager->flush(); + + $crawler = $this->client->request('GET', "{$this->path}{$menuItem->getId()}"); + $idValue = $crawler->filter( + '.table > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)' + )->text(); + $nameValue = $crawler->filter( + '.table > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2)' + )->text(); + self::assertResponseStatusCodeSame(200); + $this->assertEquals($idValue, $menuItem->getId()); + $this->assertEquals($nameValue, $menuItem->getName()); + } + + public function testEdit(): void + { + $menuItem = new MenuItem; + $menuItem->setName('Testing 1 2'); + + $this->vendor->addMenuItem($menuItem); + $this->manager->persist($this->vendor); + $this->manager->persist($menuItem); + $this->manager->flush(); + + $crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $menuItem->getId())); + $nameElem = $crawler->filter('#menu_item_name'); + $this->assertEquals( + 'Testing 1 2', + $nameElem->attr('value') + ); + + $this->client->submitForm('Update', [ + 'menu_item[name]' => 'Testing-1', + ]); + + self::assertResponseRedirects(sprintf('/menu/item/%s', $menuItem->getId())); + } + + public function testDelete(): void + { + $menuItem = new MenuItem; + $menuItem->setName('Testing 1 2'); + + $this->vendor->addMenuItem($menuItem); + $this->manager->persist($this->vendor); + $this->manager->persist($menuItem); + $this->manager->flush(); + $this->assertFalse($menuItem->isDeleted()); + + $this->client->request('GET', "{$this->path}{$menuItem->getId()}"); + $this->client->submitForm('Delete', []); + + $menuItem = $this->repository->find($menuItem->getId()); + + $this->assertTrue($menuItem->isDeleted()); + } +} diff --git a/tests/Entity/FoodOrderTest.php b/tests/Entity/FoodOrderTest.php new file mode 100644 index 0000000..08fe6e6 --- /dev/null +++ b/tests/Entity/FoodOrderTest.php @@ -0,0 +1,23 @@ +assertCount(0, $order->getOrderItems()); + $order->addOrderItem($orderItem); + $order->addOrderItem($orderItem); + $this->assertCount(1, $order->getOrderItems()); + $order->removeOrderItem($orderItem); + $this->assertCount(0, $order->getOrderItems()); + + } +} diff --git a/tests/Entity/FoodVendorTest.php b/tests/Entity/FoodVendorTest.php new file mode 100644 index 0000000..8df1836 --- /dev/null +++ b/tests/Entity/FoodVendorTest.php @@ -0,0 +1,43 @@ +setName('Test'); + $this->assertEquals('Test', $vendor->getName()); + $this->assertInstanceOf(Ulid::class, $vendor->getId()); + + $this->assertCount(0, $vendor->getFoodOrders()); + $order1 = new FoodOrder; + $vendor->addFoodOrder($order1); + $vendor->addFoodOrder($order1); + $this->assertCount(1, $vendor->getFoodOrders()); + $vendor->removeFoodOrder($order1); + $this->assertCount(0, $vendor->getFoodOrders()); + + $menuItem1 = new MenuItem; + $menuItem2 = new MenuItem; + $this->assertCount(0, $vendor->getMenuItems()); + $vendor->addMenuItem($menuItem1); + $vendor->addMenuItem($menuItem1); + $this->assertCount(1, $vendor->getMenuItems()); + $vendor->removeMenuItem($menuItem1); + $this->assertCount(0, $vendor->getMenuItems()); + $vendor->addMenuItem($menuItem1); + $menuItem2->delete(); + $vendor->addMenuItem($menuItem2); + $this->assertCount(1, $vendor->getMenuItems()); + $this->assertCount(2, $vendor->getMenuItems(true)); + + } +} diff --git a/tests/Entity/MenuItemTest.php b/tests/Entity/MenuItemTest.php new file mode 100644 index 0000000..4a0fd91 --- /dev/null +++ b/tests/Entity/MenuItemTest.php @@ -0,0 +1,27 @@ +setName('Test'); + $this->assertEquals('Test', $item->getName()); + + $vendor = new FoodVendor; + $vendor->setName('Test'); + + $item->setFoodVendor($vendor); + $this->assertEquals($vendor, $item->getFoodVendor()); + + $this->assertFalse($item->isDeleted()); + $item->delete(); + $this->assertTrue($item->isDeleted()); + } +} -- 2.39.5