futtern/tests/Feature/Controller/MenuItemControllerTest.php
lubiana 9c98735db7
All checks were successful
/ ls (pull_request) Successful in 1m27s
/ ls (push) Successful in 1m24s
/ ls (release) Successful in 49s
migrate test cases to use pestphp syntax
2025-02-01 22:37:07 +01:00

159 lines
5.4 KiB
PHP

<?php declare(strict_types=1);
namespace App\Tests\Feature\Controller;
use App\Controller\MenuItemController;
use App\Controller\OrderItemController;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use App\Entity\MenuItem;
use App\Entity\OrderItem;
use App\Form\MenuItemType;
use App\Form\OrderItemType;
use App\Repository\FoodOrderRepository;
use App\Repository\MenuItemRepository;
use function describe;
use function pest;
use function sprintf;
use function test;
pest()
->beforeEach(function (): void {
$this->setEntityClass(MenuItem::class);
$this->setPath('/menu/item/');
$this->repository = $this->manager->getRepository($this->entityClass);
$this->vendor = new FoodVendor;
$this->vendor->setName('Food Vendor');
$this->manager->persist($this->vendor);
$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);
$this->menuItem->addAlias($this->aliasOne);
$this->aliasTwo = new MenuItem;
$this->aliasTwo->setName('AliasTwo');
$this->aliasTwo->setFoodVendor($this->vendor);
$this->aliasTwo->setAliasOf($this->menuItem);
$this->menuItem->addAlias($this->aliasTwo);
$this->manager->persist($this->aliasOne);
$this->manager->persist($this->aliasTwo);
$this->manager->persist($this->menuItem);
$this->manager->flush();
});
describe(MenuItemController::class, function (): void {
test('show', function (): void {
$crawler = $this->client->request('GET', "{$this->path}{$this->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();
$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();
$this->assertResponseStatusCodeSame(200);
$this->assertEquals($idValue, $this->menuItem->getId());
$this->assertEquals($nameValue, $this->menuItem->getName());
$this->assertEquals($aliasTwoNameValue, $this->aliasOne->getName());
$this->assertEquals($aliasOneNameValue, $this->aliasTwo->getName());
});
test('edit', function (): void {
$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')
);
$form = $crawler->selectButton('Update')
->form();
$form['menu_item[name]'] = 'Testing-1';
$form['menu_item[aliases]'][0]->untick();
$this->client->submit($form);
$this->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());
});
test('edit invalid', function (): void {
$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')
);
$form = $crawler->selectButton('Update')
->form();
$form['menu_item[name]'] = 'a';
$this->client->submit($form);
$this->assertResponseStatusCodeSame(422);
});
test('delete', function (): void {
$order = new FoodOrder;
$order->setFoodVendor($this->vendor);
$this->manager->persist($order);
$this->manager->flush();
$this->assertFalse($this->menuItem->isDeleted());
$this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
$this->client->submitForm('Delete', []);
$menuItem = $this->repository->find($this->menuItem->getId());
$this->assertTrue($menuItem->isDeleted());
$crawler = $this->client->request('GET', '/order/item/new/' . $order->getId());
$count = $crawler->filter('body > main:nth-child(2) > div:nth-child(5)')
->children()
->count();
$this->assertSame(2, $count);
$this->assertResponseIsSuccessful();
});
})
->covers(
MenuItemController::class,
OrderItemController::class,
OrderItemType::class,
MenuItemRepository::class,
FoodOrder::class,
FoodVendor::class,
MenuItem::class,
OrderItem::class,
FoodOrderRepository::class,
MenuItemType::class,
);