futtern/tests/Controller/MenuItemControllerTest.php

114 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2024-08-16 11:44:16 +00:00
<?php declare(strict_types=1);
namespace App\Tests\Controller;
2024-09-23 17:02:17 +00:00
use App\Entity\FoodOrder;
2024-08-16 11:44:16 +00:00
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);
2024-09-23 17:02:17 +00:00
2024-09-23 17:11:33 +00:00
$order = new FoodOrder;
2024-09-23 17:02:17 +00:00
$order->setFoodVendor($this->vendor);
2024-09-23 17:11:33 +00:00
2024-09-23 17:02:17 +00:00
$this->manager->persist($order);
2024-08-16 11:44:16 +00:00
$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());
2024-09-23 17:02:17 +00: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();
2024-09-23 17:02:17 +00:00
$this->assertSame(0, $count);
$this->assertResponseIsSuccessful();
2024-08-16 11:44:16 +00:00
}
}