futtern/tests/Feature/Controller/OrderItemControllerTest.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

261 lines
8.3 KiB
PHP

<?php declare(strict_types=1);
namespace App\Tests\Feature\Controller;
use App\Controller\OrderItemController;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use App\Entity\MenuItem;
use App\Entity\OrderItem;
use App\Form\OrderItemType;
use App\Repository\FoodOrderRepository;
use App\Repository\MenuItemRepository;
use App\Repository\OrderItemRepository;
use DateTimeImmutable;
use function describe;
use function pest;
use function sprintf;
use function test;
pest()
->beforeEach(function (): void {
$this->setEntityClass(OrderItem::class);
$this->setPath('/order/item/');
$this->repository = $this->manager->getRepository($this->entityClass);
$this->vendor = new FoodVendor;
$this->vendor->setName('Food Vendor');
$this->manager->persist($this->vendor);
$this->order = new FoodOrder;
$this->order->setFoodVendor($this->vendor);
$this->manager->persist($this->order);
$this->menuItem = new MenuItem;
$this->menuItem->setName('Testing');
$this->menuItem->setFoodVendor($this->vendor);
$vendor2 = new FoodVendor;
$vendor2->setName('Vendor 2');
$menuItem2 = new MenuItem;
$menuItem2->setName('Testing2');
$menuItem2->setFoodVendor($vendor2);
$this->manager->persist($vendor2);
$this->manager->persist($menuItem2);
$this->manager->persist($this->menuItem);
$this->manager->flush();
$this->menuItemRepository = $this::getContainer()->get(MenuItemRepository::class);
});
describe(OrderItemController::class, function (): void {
test('new', function (): void {
$crawler = $this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
$children = $crawler->filter('body > main:nth-child(2) > div:nth-child(5)')
->children();
$this->assertCount(1, $children);
$this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'order_item[name]' => 'Testing',
'order_item[extras]' => 'Testing',
]);
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(1, $this->repository->count([]));
$this->assertSame(1, $this->menuItemRepository->count([
'foodVendor' => $this->vendor->getId(),
]));
});
test('new order closed', function (): void {
$this->order->setClosedAt(new DateTimeImmutable('-1 Hour'));
$this->manager->persist($this->order);
$this->manager->flush();
$this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(0, $this->repository->count([]));
});
test('new create menu item', function (): void {
$this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
$this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'order_item[name]' => 'Testing-1',
'order_item[extras]' => 'Testing-1',
]);
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(1, $this->repository->count([]));
$this->assertSame(3, $this->menuItemRepository->count([]));
});
test('remove', function (): void {
$fixture = new OrderItem;
$fixture->setName('Testing');
$fixture->setExtras('Value');
$fixture->setMenuItem($this->menuItem);
$fixture->setFoodOrder($this->order);
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId()));
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(0, $this->repository->count([]));
});
test('order closed', function (): void {
$fixture = new OrderItem;
$fixture->setName('Testing');
$fixture->setExtras('Value');
$fixture->setMenuItem($this->menuItem);
$fixture->setFoodOrder($this->order);
$this->order->close();
$this->manager->persist($this->order);
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId()));
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(1, $this->repository->count([]));
});
test('edit', function (): void {
$orderItem = new OrderItem;
$orderItem->setName('Testing');
$orderItem->setExtras('My Extra');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
$this->manager->persist($orderItem);
$this->manager->flush();
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId()));
$nameElem = $crawler->filter('#order_item_name');
$extrasElem = $crawler->filter('#order_item_extras');
$this->assertEquals(
'Testing',
$nameElem->attr('value')
);
$this->assertEquals(
'My Extra',
$extrasElem->attr('value')
);
$this->client->submitForm('Update', [
'order_item[name]' => 'Testing-1',
'order_item[extras]' => 'Testing-1',
]);
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$this->assertSame(1, $this->repository->count([]));
$this->assertSame(3, $this->menuItemRepository->count([]));
});
test('edit order closed', function (): void {
$orderItem = new OrderItem;
$orderItem->setName('Testing');
$orderItem->setExtras('My Extra');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
$this->order->close();
$this->manager->persist($orderItem);
$this->manager->persist($this->order);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId()));
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
});
test('copy', function (): void {
$orderItem = new OrderItem;
$orderItem->setName('My Title');
$orderItem->setExtras('My Title');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
$this->manager->persist($orderItem);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId()));
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$result = $this->repository->findBy([
'foodOrder' => $this->order->getId(),
]);
$this->assertCount(2, $result);
foreach ($result as $item) {
$this->assertSame($orderItem->getName(), $item->getName());
$this->assertSame($orderItem->getExtras(), $item->getExtras());
}
});
test('copy order closed', function (): void {
$orderItem = new OrderItem;
$orderItem->setName('My Title');
$orderItem->setExtras('My Title');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
$this->order->close();
$this->manager->persist($this->order);
$this->manager->persist($orderItem);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId()));
$this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$result = $this->repository->findBy([
'foodOrder' => $this->order->getId(),
]);
$this->assertCount(1, $result);
});
})
->covers(
OrderItemController::class,
MenuItemRepository::class,
OrderItemRepository::class,
OrderItemType::class,
FoodOrder::class,
FoodVendor::class,
MenuItem::class,
OrderItem::class,
FoodOrderRepository::class,
);