2024-08-16 13:44:16 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Tests\Controller;
|
|
|
|
|
2025-02-01 00:09:50 +01:00
|
|
|
use App\Controller\MenuItemController;
|
|
|
|
use App\Controller\OrderItemController;
|
2024-09-23 19:02:17 +02:00
|
|
|
use App\Entity\FoodOrder;
|
2024-08-16 13:44:16 +02:00
|
|
|
use App\Entity\FoodVendor;
|
|
|
|
use App\Entity\MenuItem;
|
2025-02-01 00:09:50 +01:00
|
|
|
use App\Entity\OrderItem;
|
|
|
|
use App\Form\MenuItemType;
|
|
|
|
use App\Form\OrderItemType;
|
|
|
|
use App\Repository\FoodOrderRepository;
|
|
|
|
use App\Repository\MenuItemRepository;
|
2024-08-16 13:44:16 +02:00
|
|
|
use App\Tests\DbWebTest;
|
|
|
|
use Override;
|
2025-02-01 00:09:50 +01:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-08-16 13:44:16 +02:00
|
|
|
|
|
|
|
use function sprintf;
|
|
|
|
|
2025-02-01 00:09:50 +01:00
|
|
|
#[CoversClass(MenuItemController::class)]
|
|
|
|
#[CoversClass(OrderItemController::class)]
|
|
|
|
#[CoversClass(OrderItemType::class)]
|
|
|
|
#[CoversClass(MenuItemRepository::class)]
|
|
|
|
#[CoversClass(FoodOrder::class)]
|
|
|
|
#[CoversClass(FoodVendor::class)]
|
|
|
|
#[CoversClass(MenuItem::class)]
|
|
|
|
#[CoversClass(OrderItem::class)]
|
|
|
|
#[CoversClass(FoodOrderRepository::class)]
|
|
|
|
#[CoversClass(MenuItemType::class)]
|
2024-08-16 13:44:16 +02:00
|
|
|
final class MenuItemControllerTest extends DbWebTest
|
|
|
|
{
|
|
|
|
private string $path = '/menu/item/';
|
|
|
|
private FoodVendor $vendor;
|
2025-01-26 11:48:16 +01:00
|
|
|
private MenuItem $menuItem;
|
|
|
|
private MenuItem $aliasOne;
|
|
|
|
private MenuItem $aliasTwo;
|
2024-08-16 13:44:16 +02:00
|
|
|
|
|
|
|
#[Override]
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->vendor = new FoodVendor;
|
|
|
|
$this->vendor->setName('Food Vendor');
|
|
|
|
|
|
|
|
$this->manager->persist($this->vendor);
|
2025-01-26 11:48:16 +01:00
|
|
|
|
|
|
|
$this->menuItem = new MenuItem;
|
|
|
|
$this->menuItem->setName('Testing 1 2');
|
|
|
|
|
|
|
|
$this->vendor->addMenuItem($this->menuItem);
|
|
|
|
$this->manager->persist($this->vendor);
|
|
|
|
$this->manager->persist($this->menuItem);
|
|
|
|
|
|
|
|
$this->aliasOne = new MenuItem;
|
|
|
|
$this->aliasOne->setName('AliasOne');
|
|
|
|
$this->aliasOne->setFoodVendor($this->vendor);
|
2025-01-26 10:50:50 +00:00
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->menuItem->addAlias($this->aliasOne);
|
|
|
|
|
|
|
|
$this->aliasTwo = new MenuItem;
|
|
|
|
$this->aliasTwo->setName('AliasTwo');
|
|
|
|
$this->aliasTwo->setFoodVendor($this->vendor);
|
|
|
|
$this->aliasTwo->setAliasOf($this->menuItem);
|
2025-01-26 10:50:50 +00:00
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->menuItem->addAlias($this->aliasTwo);
|
|
|
|
|
|
|
|
$this->manager->persist($this->aliasOne);
|
|
|
|
$this->manager->persist($this->aliasTwo);
|
|
|
|
$this->manager->persist($this->menuItem);
|
2024-08-16 13:44:16 +02:00
|
|
|
$this->manager->flush();
|
2025-01-26 11:48:16 +01:00
|
|
|
|
2024-08-16 13:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[Override]
|
|
|
|
public function getEntityClass(): string
|
|
|
|
{
|
|
|
|
return MenuItem::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShow(): void
|
|
|
|
{
|
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
$crawler = $this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
|
2024-08-16 13:44:16 +02:00
|
|
|
$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();
|
2025-01-26 11:48:16 +01:00
|
|
|
|
|
|
|
$aliasTwoNameValue = $crawler->filter(
|
|
|
|
'.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(1)'
|
|
|
|
)->text();
|
|
|
|
$aliasOneNameValue = $crawler->filter(
|
|
|
|
'.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(2)'
|
|
|
|
)->text();
|
2024-08-16 13:44:16 +02:00
|
|
|
self::assertResponseStatusCodeSame(200);
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->assertEquals($idValue, $this->menuItem->getId());
|
|
|
|
$this->assertEquals($nameValue, $this->menuItem->getName());
|
|
|
|
$this->assertEquals($aliasTwoNameValue, $this->aliasOne->getName());
|
|
|
|
$this->assertEquals($aliasOneNameValue, $this->aliasTwo->getName());
|
2024-08-16 13:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEdit(): void
|
|
|
|
{
|
2025-01-26 11:48:16 +01:00
|
|
|
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
|
|
|
|
$nameElem = $crawler->filter('#menu_item_name');
|
|
|
|
$this->assertEquals(
|
|
|
|
'Testing 1 2',
|
|
|
|
$nameElem->attr('value')
|
|
|
|
);
|
2024-08-16 13:44:16 +02:00
|
|
|
|
2025-01-26 10:50:50 +00:00
|
|
|
$form = $crawler->selectButton('Update')
|
|
|
|
->form();
|
2025-01-26 11:48:16 +01:00
|
|
|
$form['menu_item[name]'] = 'Testing-1';
|
|
|
|
$form['menu_item[aliases]'][0]->untick();
|
|
|
|
|
|
|
|
$this->client->submit($form);
|
|
|
|
|
|
|
|
self::assertResponseRedirects(sprintf('/menu/item/%s', $this->menuItem->getId()));
|
|
|
|
$menuItem = $this->repository->find($this->menuItem->getId());
|
|
|
|
$this->assertEquals('Testing-1', $menuItem->getName());
|
|
|
|
$this->assertEquals(1, $menuItem->getAliases()->count());
|
|
|
|
$aliasOne = $this->repository->find($this->aliasOne->getId());
|
|
|
|
$this->assertNull($aliasOne->getAliasOf());
|
|
|
|
}
|
2024-08-16 13:44:16 +02:00
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
public function testEditInvalid(): void
|
|
|
|
{
|
|
|
|
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
|
2024-08-16 13:44:16 +02:00
|
|
|
$nameElem = $crawler->filter('#menu_item_name');
|
|
|
|
$this->assertEquals(
|
|
|
|
'Testing 1 2',
|
|
|
|
$nameElem->attr('value')
|
|
|
|
);
|
|
|
|
|
2025-01-26 10:50:50 +00:00
|
|
|
$form = $crawler->selectButton('Update')
|
|
|
|
->form();
|
2025-01-26 11:48:16 +01:00
|
|
|
$form['menu_item[name]'] = 'a';
|
|
|
|
|
|
|
|
$this->client->submit($form);
|
2024-08-16 13:44:16 +02:00
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
self::assertResponseStatusCodeSame(422);
|
2024-08-16 13:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDelete(): void
|
|
|
|
{
|
2024-09-23 17:11:33 +00:00
|
|
|
$order = new FoodOrder;
|
2024-09-23 19:02:17 +02:00
|
|
|
$order->setFoodVendor($this->vendor);
|
2024-09-23 17:11:33 +00:00
|
|
|
|
2024-09-23 19:02:17 +02:00
|
|
|
$this->manager->persist($order);
|
2024-08-16 13:44:16 +02:00
|
|
|
$this->manager->flush();
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->assertFalse($this->menuItem->isDeleted());
|
2024-08-16 13:44:16 +02:00
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
|
2024-08-16 13:44:16 +02:00
|
|
|
$this->client->submitForm('Delete', []);
|
|
|
|
|
2025-01-26 11:48:16 +01:00
|
|
|
$menuItem = $this->repository->find($this->menuItem->getId());
|
2024-08-16 13:44:16 +02:00
|
|
|
|
|
|
|
$this->assertTrue($menuItem->isDeleted());
|
2024-09-23 19:02:17 +02:00
|
|
|
|
|
|
|
$crawler = $this->client->request('GET', '/order/item/new/' . $order->getId());
|
2024-09-23 17:11:33 +00:00
|
|
|
$count = $crawler->filter('body > main:nth-child(2) > div:nth-child(5)')
|
|
|
|
->children()
|
|
|
|
->count();
|
2025-01-26 11:48:16 +01:00
|
|
|
$this->assertSame(2, $count);
|
2024-09-23 19:02:17 +02:00
|
|
|
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
|
|
|
2024-08-16 13:44:16 +02:00
|
|
|
}
|
|
|
|
}
|