futtern/tests/Controller/OrderItemControllerTest.php

248 lines
7.5 KiB
PHP
Raw Permalink Normal View History

2024-06-14 15:41:00 +00:00
<?php declare(strict_types=1);
2024-06-14 18:43:11 +00:00
namespace App\Tests\Controller;
2024-06-14 15:41:00 +00:00
2024-06-15 14:59:48 +00:00
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
2024-06-26 18:31:27 +00:00
use App\Entity\MenuItem;
2024-06-14 15:41:00 +00:00
use App\Entity\OrderItem;
use App\Repository\MenuItemRepository;
2024-06-14 15:41:00 +00:00
use App\Tests\DbWebTest;
2024-08-16 09:15:23 +00:00
use DateTimeImmutable;
2024-06-14 15:41:00 +00:00
use Override;
use function sprintf;
final class OrderItemControllerTest extends DbWebTest
{
2024-06-15 14:59:48 +00:00
public FoodVendor $vendor;
public FoodOrder $order;
2024-06-26 18:31:27 +00:00
public Menuitem $menuItem;
public MenuItemRepository $menuItemRepository;
2024-06-14 15:41:00 +00:00
private string $path = '/order/item/';
2024-06-15 14:59:48 +00:00
#[Override]
public function setUp(): void
{
parent::setUp();
$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);
2024-06-26 18:31:27 +00:00
2024-06-26 18:32:46 +00:00
$this->menuItem = new MenuItem;
2024-06-26 18:31:27 +00:00
$this->menuItem->setName('Testing');
$this->menuItem->setFoodVendor($this->vendor);
2024-06-26 18:32:46 +00:00
2024-06-26 18:31:27 +00:00
$this->manager->persist($this->menuItem);
2024-06-15 14:59:48 +00:00
$this->manager->flush();
$this->menuItemRepository = static::getContainer()->get(MenuItemRepository::class);
2024-06-15 14:59:48 +00:00
}
2024-06-14 15:41:00 +00:00
public function testNew(): void
{
2024-06-15 16:37:22 +00:00
$this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
2024-06-14 15:41:00 +00:00
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'order_item[name]' => 'Testing',
'order_item[extras]' => 'Testing',
]);
2024-06-15 14:59:48 +00:00
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
2024-06-14 15:41:00 +00:00
self::assertSame(1, $this->repository->count([]));
self::assertSame(1, $this->menuItemRepository->count([]));
}
2024-08-16 09:14:41 +00:00
public function testNewOrderClosed(): void
{
2024-08-16 09:15:23 +00:00
$this->order->setClosedAt(new DateTimeImmutable('-1 Hour'));
2024-08-16 09:14:41 +00:00
$this->manager->persist($this->order);
$this->manager->flush();
$this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(0, $this->repository->count([]));
}
public function testNewCreateMenuItem(): void
{
$this->client->request(
'GET',
sprintf('%snew/%s', $this->path, $this->order->getId())
);
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'order_item[name]' => 'Testing-1',
'order_item[extras]' => 'Testing-1',
]);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([]));
self::assertSame(2, $this->menuItemRepository->count([]));
2024-06-14 15:41:00 +00:00
}
2024-06-15 14:59:48 +00:00
public function testRemove(): void
2024-06-14 15:41:00 +00:00
{
$fixture = new OrderItem;
2024-06-26 18:31:27 +00:00
$fixture->setName('Testing');
2024-06-14 15:41:00 +00:00
$fixture->setExtras('Value');
2024-06-26 18:31:27 +00:00
$fixture->setMenuItem($this->menuItem);
2024-06-15 14:59:48 +00:00
$fixture->setFoodOrder($this->order);
2024-06-14 15:41:00 +00:00
$this->manager->persist($fixture);
$this->manager->flush();
2024-06-15 14:59:48 +00:00
$this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId()));
2024-06-14 15:41:00 +00:00
2024-06-15 14:59:48 +00:00
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(0, $this->repository->count([]));
2024-06-14 15:41:00 +00:00
}
2024-08-16 09:14:41 +00:00
public function testOrderClosed(): 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()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([]));
}
public function testEdit(): 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')
);
2024-08-16 09:14:41 +00:00
$this->client->submitForm('Update', [
'order_item[name]' => 'Testing-1',
'order_item[extras]' => 'Testing-1',
]);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([]));
self::assertSame(2, $this->menuItemRepository->count([]));
}
public function testEditOrderClosed(): void
{
$orderItem = new OrderItem;
$orderItem->setName('Testing');
$orderItem->setExtras('My Extra');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
2024-08-16 09:15:23 +00:00
2024-08-16 09:14:41 +00:00
$this->order->close();
$this->manager->persist($orderItem);
$this->manager->persist($this->order);
$this->manager->flush();
2024-08-16 09:15:23 +00:00
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId()));
2024-08-16 09:14:41 +00:00
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
}
2024-06-15 14:59:48 +00:00
public function testCopy(): void
2024-06-14 15:41:00 +00:00
{
2024-06-15 14:59:48 +00:00
$orderItem = new OrderItem;
$orderItem->setName('My Title');
$orderItem->setExtras('My Title');
$orderItem->setFoodOrder($this->order);
2024-06-26 18:31:27 +00:00
$orderItem->setMenuItem($this->menuItem);
2024-06-14 15:41:00 +00:00
2024-06-15 14:59:48 +00:00
$this->manager->persist($orderItem);
2024-06-14 15:41:00 +00:00
$this->manager->flush();
2024-06-15 14:59:48 +00:00
$this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId()));
self::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());
}
2024-06-14 15:41:00 +00:00
}
2024-08-16 09:14:41 +00:00
public function testCopyOrderClosed(): 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()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$result = $this->repository->findBy([
'foodOrder' => $this->order->getId(),
]);
$this->assertCount(1, $result);
}
2024-06-14 15:41:00 +00:00
#[Override]
public function getEntityClass(): string
{
return OrderItem::class;
}
}