100% test coverage #55

Merged
lubiana merged 1 commit from improve-test-coverage into main 2024-08-16 11:45:40 +00:00
8 changed files with 214 additions and 16 deletions

View file

@ -28,6 +28,10 @@
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">src/Command</directory>
<file>src/Service/FakeData.php</file>
</exclude>
</coverage>
<listeners>

View file

@ -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', [

View file

@ -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;
}

View file

@ -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;
}

View file

@ -0,0 +1,98 @@
<?php declare(strict_types=1);
namespace App\Tests\Controller;
use App\Entity\FoodVendor;
use App\Entity\MenuItem;
use App\Tests\DbWebTest;
use Override;
use function sprintf;
final class MenuItemControllerTest extends DbWebTest
{
private string $path = '/menu/item/';
private FoodVendor $vendor;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->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());
}
}

View file

@ -0,0 +1,23 @@
<?php declare(strict_types=1);
namespace App\Tests\Entity;
use App\Entity\FoodOrder;
use App\Entity\OrderItem;
use PHPUnit\Framework\TestCase;
final class FoodOrderTest extends TestCase
{
public function testFoodOrder(): void
{
$order = new FoodOrder;
$orderItem = new OrderItem;
$this->assertCount(0, $order->getOrderItems());
$order->addOrderItem($orderItem);
$order->addOrderItem($orderItem);
$this->assertCount(1, $order->getOrderItems());
$order->removeOrderItem($orderItem);
$this->assertCount(0, $order->getOrderItems());
}
}

View file

@ -0,0 +1,43 @@
<?php declare(strict_types=1);
namespace App\Tests\Entity;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use App\Entity\MenuItem;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid;
final class FoodVendorTest extends TestCase
{
public function testFoodVendor(): void
{
$vendor = new FoodVendor;
$vendor->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));
}
}

View file

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace App\Tests\Entity;
use App\Entity\FoodVendor;
use App\Entity\MenuItem;
use PHPUnit\Framework\TestCase;
final class MenuItemTest extends TestCase
{
public function testMenuItem(): void
{
$item = new MenuItem;
$item->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());
}
}