migrate test cases to use pestphp syntax
All checks were successful
/ ls (pull_request) Successful in 1m27s
/ ls (push) Successful in 1m24s
/ ls (release) Successful in 49s

This commit is contained in:
lubiana 2025-02-01 22:37:07 +01:00
parent af9354ff22
commit 9c98735db7
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
14 changed files with 365 additions and 415 deletions

View file

@ -14,8 +14,8 @@
failOnWarning="true"> failOnWarning="true">
<testsuites> <testsuites>
<testsuite name="default"> <testsuite name="default">
<directory>tests/Controller</directory> <directory>tests/Feature</directory>
<directory>tests/Entity</directory> <directory>tests/Unit</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
@ -29,7 +29,10 @@
</exclude> </exclude>
</source> </source>
<php> <php>
<env name="APP_ENV" value="test" /> <ini name="display_errors" value="1" />
<env name="KERNEL_CLASS" value="App\Kernel" /> <ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="App\Kernel" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
</php> </php>
</phpunit> </phpunit>

View file

@ -14,15 +14,12 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\NotBlank;
use function assert;
final class MenuItemType extends AbstractType final class MenuItemType extends AbstractType
{ {
#[Override] #[Override]
public function buildForm(FormBuilderInterface $builder, array $options): void public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$item = $options['data']; $item = $options['data'];
assert($item instanceof MenuItem);
$builder->add('name', TextType::class, [ $builder->add('name', TextType::class, [
'constraints' => [ 'constraints' => [

View file

@ -1,81 +0,0 @@
<?php declare(strict_types=1);
namespace App\Tests\Controller;
use App\Controller\HomeController;
use App\Form\UserNameFormType;
use App\Tests\DbWebTest;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(HomeController::class)]
#[CoversClass(UserNameFormType::class)]
final class HomeControllerTest extends DbWebTest
{
public function testIndex(): void
{
$this->client->request(
'GET',
'/'
);
self::assertResponseStatusCodeSame(302);
self::assertResponseHeaderSame('Location', '/food/order/list');
}
public function testSetUsername(): void
{
$this->client->request(
'GET',
'/username',
);
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'user_name_form[username]' => 'Testing-1',
]);
self::assertResponseStatusCodeSame(302);
self::assertResponseHeaderSame('Location', '/food/order/list');
self::assertResponseCookieValueSame('username', 'Testing-1');
$crawler = $this->client->request(
'GET',
'/username',
);
self::assertResponseStatusCodeSame(200);
$this->assertSame(
$crawler->filter('#user_name_form_username')
->last()
->attr('value', ''),
'Testing-1'
);
}
public function testRemoveUsername(): void
{
$this->client->request(
'GET',
'/username',
);
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'user_name_form[username]' => '',
]);
self::assertResponseStatusCodeSame(302);
self::assertResponseHeaderSame('Location', '/food/order/list');
self::assertResponseCookieValueSame('username', '');
}
#[Override]
public function getEntityClass(): string
{
return '';
}
}

View file

@ -17,11 +17,11 @@ use function str_contains;
abstract class DbWebTest extends WebTestCase abstract class DbWebTest extends WebTestCase
{ {
abstract public function getEntityClass(): string;
protected KernelBrowser $client; protected KernelBrowser $client;
protected EntityManagerInterface $manager; protected EntityManagerInterface $manager;
protected EntityRepository $repository; protected EntityRepository $repository;
protected string $entityClass = '';
protected string $path = '';
#[Override] #[Override]
protected function setUp(): void protected function setUp(): void
@ -33,10 +33,6 @@ abstract class DbWebTest extends WebTestCase
->getAllMetadata(); ->getAllMetadata();
$schemaTool->dropDatabase(); $schemaTool->dropDatabase();
$schemaTool->updateSchema($metadata); $schemaTool->updateSchema($metadata);
if ($this->getEntityClass() !== '') {
$this->repository = $this->manager->getRepository($this->getEntityClass());
}
} }
protected function generateOldUlid(int $daysToSubtract = 10): Ulid protected function generateOldUlid(int $daysToSubtract = 10): Ulid
@ -56,4 +52,15 @@ abstract class DbWebTest extends WebTestCase
) )
); );
} }
protected function setEntityClass(string $entityClass): void
{
$this->entityClass = $entityClass;
$this->repository = $this->manager->getRepository($this->entityClass);
}
protected function setPath(string $path): void
{
$this->path = $path;
}
} }

View file

@ -1,27 +0,0 @@
<?php declare(strict_types=1);
namespace App\Tests\Entity;
use App\Entity\FoodOrder;
use App\Entity\OrderItem;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(FoodOrder::class)]
#[CoversClass(OrderItem::class)]
final class FoodOrderTest extends TestCase
{
public function testFoodOrder(): void
{
$order = new FoodOrder;
$orderItem = new OrderItem;
$this->assertCount(0, $order->getOrderItems());
$order->addOrderItem($orderItem);
$order->addOrderItem($orderItem);
$this->assertCount(1, $order->getOrderItems());
$this->assertSame($order, $orderItem->getFoodOrder());
$order->removeOrderItem($orderItem);
$this->assertCount(0, $order->getOrderItems());
$this->assertNull($orderItem->getFoodOrder());
}
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Controller; namespace App\Tests\Feature\Controller;
use App\Controller\FoodOrderController; use App\Controller\FoodOrderController;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
@ -10,51 +10,31 @@ use App\Entity\OrderItem;
use App\Form\FoodOrderType; use App\Form\FoodOrderType;
use App\Repository\FoodOrderRepository; use App\Repository\FoodOrderRepository;
use App\Repository\FoodVendorRepository; use App\Repository\FoodVendorRepository;
use App\Tests\DbWebTest;
use App\Tests\Entity\FoodOrderTest;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\BrowserKit\Cookie; use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
use function assert; use function assert;
use function describe;
use function pest;
use function range; use function range;
use function sprintf; use function sprintf;
use function str_ends_with; use function str_ends_with;
use function test;
#[CoversClass(FoodOrderController::class)] pest()
#[CoversClass(FoodOrder::class)] ->beforeEach(function (): void {
#[CoversClass(FoodOrderTest::class)] $this->setEntityClass(FoodOrder::class);
#[CoversClass(FoodVendor::class)] $this->setPath('/food/order/');
#[CoversClass(FoodOrderRepository::class)] $this->repository = $this->manager->getRepository($this->entityClass);
#[CoversCLass(MenuItem::class)]
#[CoversClass(OrderItem::class)]
#[CoversClass(FoodOrderType::class)]
#[CoversClass(FoodVendorRepository::class)]
final class FoodOrderControllerTest extends DbWebTest
{
private string $path = '/food/order/';
private FoodVendor $vendor;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->vendor = new FoodVendor; $this->vendor = new FoodVendor;
$this->vendor->setName('Food Vendor'); $this->vendor->setName('Food Vendor');
$this->manager->persist($this->vendor); $this->manager->persist($this->vendor);
$this->manager->flush(); $this->manager->flush();
} });
#[Override] describe(FoodOrderController::class, function (): void {
public function getEntityClass(): string test('index', function (): void {
{
return FoodOrder::class;
}
public function testIndex(): void
{
$order = new FoodOrder; $order = new FoodOrder;
$order->setFoodVendor($this->vendor); $order->setFoodVendor($this->vendor);
@ -63,17 +43,16 @@ final class FoodOrderControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$crawler = $this->client->request('GET', "{$this->path}list"); $crawler = $this->client->request('GET', "{$this->path}list");
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodOrder index'); $this->assertPageTitleContains('FoodOrder index');
$this->assertCount( $this->assertCount(
1, 1,
$crawler->filter('td') $crawler->filter('td')
->reduce(fn(Crawler $node, $i): bool => $node->text() === $this->vendor->getName()), ->reduce(fn(Crawler $node, $i): bool => $node->text() === $this->vendor->getName()),
); );
} });
public function testOrderedItems(): void test('orderedItems', function (): void {
{
$order = new FoodOrder; $order = new FoodOrder;
$order->setFoodVendor($this->vendor); $order->setFoodVendor($this->vendor);
@ -119,7 +98,7 @@ final class FoodOrderControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$crawler = $this->client->request('GET', "{$this->path}{$order->getId()}"); $crawler = $this->client->request('GET', "{$this->path}{$order->getId()}");
self::assertResponseIsSuccessful(); $this->assertResponseIsSuccessful();
$tdContent = $crawler->filter( $tdContent = $crawler->filter(
'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)' 'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2)'
)->text(); )->text();
@ -132,14 +111,20 @@ final class FoodOrderControllerTest extends DbWebTest
'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(3) > td:nth-child(2)' 'table.table:nth-child(6) > tbody:nth-child(2) > tr:nth-child(3) > td:nth-child(2)'
)->text(); )->text();
$this->assertEquals('C', $tdContent); $this->assertEquals('C', $tdContent);
} });
public function testPaginatedIndex(): void test('paginatedIndex', function (): void {
{ foreach (range(1, 35) as $i) {
$this->generatePaginatedOrders(); $order = new FoodOrder($this->generateOldUlid());
$order->setFoodVendor($this->vendor);
$order->close();
$this->manager->persist($order);
}
$this->manager->flush();
$crawler = $this->client->request('GET', "{$this->path}list"); $crawler = $this->client->request('GET', "{$this->path}list");
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodOrder index'); $this->assertPageTitleContains('FoodOrder index');
$this->assertElementContainsCount( $this->assertElementContainsCount(
$crawler, $crawler,
'td', 'td',
@ -152,20 +137,20 @@ final class FoodOrderControllerTest extends DbWebTest
0, 0,
'next page' 'next page'
); );
} });
/** test('paginatedFirstPage', function (int $page, int $prevPage, int $nextPage, int $items = 10): void {
* @testWith [1, 0, 2] foreach (range(1, 35) as $i) {
* [2, 1, 3] $order = new FoodOrder($this->generateOldUlid());
* [3, 2, 4] $order->setFoodVendor($this->vendor);
* [4, 3, 0, 5] $order->close();
*/ $this->manager->persist($order);
public function testPaginatedFirstPage(int $page, int $prevPage, int $nextPage, int $items = 10): void
{ }
$this->generatePaginatedOrders(); $this->manager->flush();
$crawler = $this->client->request('GET', "{$this->path}list/archive/{$page}"); $crawler = $this->client->request('GET', "{$this->path}list/archive/{$page}");
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodOrder index'); $this->assertPageTitleContains('FoodOrder index');
$this->assertElementContainsCount( $this->assertElementContainsCount(
$crawler, $crawler,
'td', 'td',
@ -187,30 +172,36 @@ final class FoodOrderControllerTest extends DbWebTest
$target = $node->attr('href'); $target = $node->attr('href');
$this->assertTrue(str_ends_with((string) $target, "/{$nextPage}")); $this->assertTrue(str_ends_with((string) $target, "/{$nextPage}"));
} }
} })
->with(
[
[1, 0, 2],
[2, 1, 3],
[3, 2, 4],
[4, 3, 0, 5],
]
);
public function testNew(): void test('new', function (): void {
{
$this->client->getCookieJar() $this->client->getCookieJar()
->set(new Cookie('username', 'Testing-1')); ->set(new Cookie('username', 'Testing-1'));
$this->client->request('GET', sprintf('%snew', $this->path)); $this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [ $this->client->submitForm('Save', [
'food_order[foodVendor]' => $this->vendor->getId(), 'food_order[foodVendor]' => $this->vendor->getId(),
]); ]);
self::assertResponseRedirects("{$this->path}list"); $this->assertResponseRedirects("{$this->path}list");
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
$order = $this->repository->findOneBy([ $order = $this->repository->findOneBy([
'createdBy' => 'Testing-1', 'createdBy' => 'Testing-1',
]); ]);
assert($order instanceof FoodOrder); assert($order instanceof FoodOrder);
} });
public function testOpen(): void test('open', function (): void {
{
$order = new FoodOrder; $order = new FoodOrder;
$order->setFoodVendor($this->vendor); $order->setFoodVendor($this->vendor);
$order->close(); $order->close();
@ -220,13 +211,12 @@ final class FoodOrderControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s/open', $this->path, $order->getId())); $this->client->request('GET', sprintf('%s%s/open', $this->path, $order->getId()));
self::assertResponseRedirects("{$this->path}{$order->getId()}"); $this->assertResponseRedirects("{$this->path}{$order->getId()}");
$openOrder = $this->repository->find($order->getId()); $openOrder = $this->repository->find($order->getId());
$this->assertFalse($openOrder->isClosed()); $this->assertFalse($openOrder->isClosed());
} });
public function testClose(): void test('close', function (): void {
{
$order = new FoodOrder; $order = new FoodOrder;
$order->setClosedAt(); $order->setClosedAt();
$order->setFoodVendor($this->vendor); $order->setFoodVendor($this->vendor);
@ -236,20 +226,19 @@ final class FoodOrderControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s/close', $this->path, $order->getId())); $this->client->request('GET', sprintf('%s%s/close', $this->path, $order->getId()));
self::assertResponseRedirects("{$this->path}{$order->getId()}"); $this->assertResponseRedirects("{$this->path}{$order->getId()}");
$openOrder = $this->repository->find($order->getId()); $openOrder = $this->repository->find($order->getId());
$this->assertTrue($openOrder->isClosed()); $this->assertTrue($openOrder->isClosed());
} });
private function generatePaginatedOrders(): void })
{ ->covers(
foreach (range(1, 35) as $i) { FoodOrderController::class,
$order = new FoodOrder($this->generateOldUlid()); FoodOrder::class,
$order->setFoodVendor($this->vendor); FoodVendor::class,
$order->close(); FoodOrderRepository::class,
$this->manager->persist($order); MenuItem::class,
OrderItem::class,
} FoodOrderType::class,
$this->manager->flush(); FoodVendorRepository::class
} );
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Controller; namespace App\Tests\Feature\Controller;
use App\Controller\FoodVendorController; use App\Controller\FoodVendorController;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
@ -11,41 +11,32 @@ use App\Form\FoodOrderType;
use App\Form\FoodVendorType; use App\Form\FoodVendorType;
use App\Repository\FoodOrderRepository; use App\Repository\FoodOrderRepository;
use App\Repository\FoodVendorRepository; use App\Repository\FoodVendorRepository;
use App\Tests\DbWebTest;
use App\Tests\Entity\FoodOrderTest;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use function describe;
use function pest;
use function sprintf; use function sprintf;
use function test;
#[CoversClass(FoodOrder::class)] pest()
#[CoversClass(FoodOrderTest::class)] ->beforeEach(function (): void {
#[CoversClass(FoodVendor::class)] $this->setEntityClass(FoodVendor::class);
#[CoversClass(FoodOrderRepository::class)] $this->setPath('/food/vendor/');
#[CoversCLass(MenuItem::class)] $this->repository = $this->manager->getRepository($this->entityClass);
#[CoversClass(OrderItem::class)] });
#[CoversClass(FoodOrderType::class)]
#[CoversClass(FoodVendorRepository::class)]
#[CoversClass(FoodVendorController::class)]
#[CoversClass(FoodVendorType::class)]
final class FoodVendorControllerTest extends DbWebTest
{
private string $path = '/food/vendor/';
public function testIndex(): void describe(FoodVendorController::class, function (): void {
{ test('index', function (): void {
$this->client->request('GET', $this->path); $this->client->request('GET', $this->path);
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodVendor index'); $this->assertPageTitleContains('FoodVendor index');
} });
public function testNew(): void test('new', function (): void {
{ $this->assertSame(0, $this->repository->count([]));
self::assertSame(0, $this->repository->count([]));
$this->client->request('GET', sprintf('%snew', $this->path)); $this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [ $this->client->submitForm('Save', [
'food_vendor[name]' => 'TestingNew', 'food_vendor[name]' => 'TestingNew',
@ -55,12 +46,11 @@ final class FoodVendorControllerTest extends DbWebTest
'name' => 'TestingNew', 'name' => 'TestingNew',
]); ]);
$this->assertInstanceof(FoodVendor::class, $newVendor); $this->assertInstanceof(FoodVendor::class, $newVendor);
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
} });
public function testShow(): void test('show', function (): void {
{
$fixture = new FoodVendor; $fixture = new FoodVendor;
$fixture->setName('My Title'); $fixture->setName('My Title');
$fixture->setMenuLink('https://example.com/'); $fixture->setMenuLink('https://example.com/');
@ -82,10 +72,9 @@ final class FoodVendorControllerTest extends DbWebTest
)->text(); )->text();
$this->assertSame('My Title', $nameNode); $this->assertSame('My Title', $nameNode);
$this->assertSame('https://example.com/', $menuLinkNode); $this->assertSame('https://example.com/', $menuLinkNode);
} });
public function testShowMenuItems(): void test('show with menu items', function (): void {
{
$fixture = new FoodVendor; $fixture = new FoodVendor;
$fixture->setName('My Title'); $fixture->setName('My Title');
@ -133,10 +122,9 @@ final class FoodVendorControllerTest extends DbWebTest
$itemNodes = $crawler->filter('li'); $itemNodes = $crawler->filter('li');
$this->assertCount(4, $itemNodes); $this->assertCount(4, $itemNodes);
} });
public function testEdit(): void test('edit', function (): void {
{
$fixture = new FoodVendor; $fixture = new FoodVendor;
$fixture->setName('Value'); $fixture->setName('Value');
$fixture->setMenuLink('Value'); $fixture->setMenuLink('Value');
@ -171,18 +159,26 @@ final class FoodVendorControllerTest extends DbWebTest
'food_vendor[phone]' => '1234567890', 'food_vendor[phone]' => '1234567890',
]); ]);
self::assertResponseRedirects('/food/vendor/'); $this->assertResponseRedirects('/food/vendor/');
$fixture = $this->repository->findAll(); $fixture = $this->repository->findAll();
self::assertSame('Something New', $fixture[0]->getName()); $this->assertSame('Something New', $fixture[0]->getName());
self::assertSame('https://example.com/', $fixture[0]->getMenuLink()); $this->assertSame('https://example.com/', $fixture[0]->getMenuLink());
self::assertSame('1234567890', $fixture[0]->getPhone()); $this->assertSame('1234567890', $fixture[0]->getPhone());
} });
#[Override] })
public function getEntityClass(): string ->covers(
{ FoodOrder::class,
return FoodVendor::class; FoodVendor::class,
} FoodOrderRepository::class,
} MenuItem::class,
OrderItem::class,
FoodOrderType::class,
FoodVendorRepository::class,
FoodVendorController::class,
FoodVendorType::class
)
;

View file

@ -0,0 +1,71 @@
<?php declare(strict_types=1);
namespace App\Tests\Feature\Controller;
use App\Controller\HomeController;
use App\Form\UserNameFormType;
use function describe;
use function test;
describe(HomeController::class, function (): void {
test('index', function (): void {
$this->client->request(
'GET',
'/'
);
$this->assertResponseStatusCodeSame(302);
$this->assertResponseHeaderSame('Location', '/food/order/list');
});
test('username', function (): void {
$this->client->request(
'GET',
'/username',
);
$this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'user_name_form[username]' => 'Testing-1',
]);
$this->assertResponseStatusCodeSame(302);
$this->assertResponseHeaderSame('Location', '/food/order/list');
$this->assertResponseCookieValueSame('username', 'Testing-1');
$crawler = $this->client->request(
'GET',
'/username',
);
$this->assertResponseStatusCodeSame(200);
$this->assertSame(
$crawler->filter('#user_name_form_username')
->last()
->attr('value', ''),
'Testing-1'
);
});
test('username empty', function (): void {
$this->client->request(
'GET',
'/username',
);
$this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'user_name_form[username]' => '',
]);
$this->assertResponseStatusCodeSame(302);
$this->assertResponseHeaderSame('Location', '/food/order/list');
$this->assertResponseCookieValueSame('username', '');
});
})
->covers(HomeController::class, UserNameFormType::class);

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Controller; namespace App\Tests\Feature\Controller;
use App\Controller\MenuItemController; use App\Controller\MenuItemController;
use App\Controller\OrderItemController; use App\Controller\OrderItemController;
@ -12,39 +12,23 @@ use App\Form\MenuItemType;
use App\Form\OrderItemType; use App\Form\OrderItemType;
use App\Repository\FoodOrderRepository; use App\Repository\FoodOrderRepository;
use App\Repository\MenuItemRepository; use App\Repository\MenuItemRepository;
use App\Tests\DbWebTest;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use function describe;
use function pest;
use function sprintf; use function sprintf;
use function test;
#[CoversClass(MenuItemController::class)] pest()
#[CoversClass(OrderItemController::class)] ->beforeEach(function (): void {
#[CoversClass(OrderItemType::class)] $this->setEntityClass(MenuItem::class);
#[CoversClass(MenuItemRepository::class)] $this->setPath('/menu/item/');
#[CoversClass(FoodOrder::class)]
#[CoversClass(FoodVendor::class)] $this->repository = $this->manager->getRepository($this->entityClass);
#[CoversClass(MenuItem::class)]
#[CoversClass(OrderItem::class)]
#[CoversClass(FoodOrderRepository::class)]
#[CoversClass(MenuItemType::class)]
final class MenuItemControllerTest extends DbWebTest
{
private string $path = '/menu/item/';
private FoodVendor $vendor;
private MenuItem $menuItem;
private MenuItem $aliasOne;
private MenuItem $aliasTwo;
#[Override]
public function setUp(): void
{
parent::setUp();
$this->vendor = new FoodVendor; $this->vendor = new FoodVendor;
$this->vendor->setName('Food Vendor'); $this->vendor->setName('Food Vendor');
$this->manager->persist($this->vendor); $this->manager->persist($this->vendor);
$this->menuItem = new MenuItem; $this->menuItem = new MenuItem;
$this->menuItem->setName('Testing 1 2'); $this->menuItem->setName('Testing 1 2');
@ -70,16 +54,10 @@ final class MenuItemControllerTest extends DbWebTest
$this->manager->persist($this->menuItem); $this->manager->persist($this->menuItem);
$this->manager->flush(); $this->manager->flush();
} });
#[Override] describe(MenuItemController::class, function (): void {
public function getEntityClass(): string test('show', function (): void {
{
return MenuItem::class;
}
public function testShow(): void
{
$crawler = $this->client->request('GET', "{$this->path}{$this->menuItem->getId()}"); $crawler = $this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
$idValue = $crawler->filter( $idValue = $crawler->filter(
@ -95,15 +73,14 @@ final class MenuItemControllerTest extends DbWebTest
$aliasOneNameValue = $crawler->filter( $aliasOneNameValue = $crawler->filter(
'.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(2)' '.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(2)'
)->text(); )->text();
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
$this->assertEquals($idValue, $this->menuItem->getId()); $this->assertEquals($idValue, $this->menuItem->getId());
$this->assertEquals($nameValue, $this->menuItem->getName()); $this->assertEquals($nameValue, $this->menuItem->getName());
$this->assertEquals($aliasTwoNameValue, $this->aliasOne->getName()); $this->assertEquals($aliasTwoNameValue, $this->aliasOne->getName());
$this->assertEquals($aliasOneNameValue, $this->aliasTwo->getName()); $this->assertEquals($aliasOneNameValue, $this->aliasTwo->getName());
} });
public function testEdit(): void test('edit', function (): void {
{
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId())); $crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
$nameElem = $crawler->filter('#menu_item_name'); $nameElem = $crawler->filter('#menu_item_name');
$this->assertEquals( $this->assertEquals(
@ -118,16 +95,15 @@ final class MenuItemControllerTest extends DbWebTest
$this->client->submit($form); $this->client->submit($form);
self::assertResponseRedirects(sprintf('/menu/item/%s', $this->menuItem->getId())); $this->assertResponseRedirects(sprintf('/menu/item/%s', $this->menuItem->getId()));
$menuItem = $this->repository->find($this->menuItem->getId()); $menuItem = $this->repository->find($this->menuItem->getId());
$this->assertEquals('Testing-1', $menuItem->getName()); $this->assertEquals('Testing-1', $menuItem->getName());
$this->assertEquals(1, $menuItem->getAliases()->count()); $this->assertEquals(1, $menuItem->getAliases()->count());
$aliasOne = $this->repository->find($this->aliasOne->getId()); $aliasOne = $this->repository->find($this->aliasOne->getId());
$this->assertNull($aliasOne->getAliasOf()); $this->assertNull($aliasOne->getAliasOf());
} });
public function testEditInvalid(): void test('edit invalid', function (): void {
{
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId())); $crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
$nameElem = $crawler->filter('#menu_item_name'); $nameElem = $crawler->filter('#menu_item_name');
$this->assertEquals( $this->assertEquals(
@ -141,11 +117,10 @@ final class MenuItemControllerTest extends DbWebTest
$this->client->submit($form); $this->client->submit($form);
self::assertResponseStatusCodeSame(422); $this->assertResponseStatusCodeSame(422);
} });
public function testDelete(): void test('delete', function (): void {
{
$order = new FoodOrder; $order = new FoodOrder;
$order->setFoodVendor($this->vendor); $order->setFoodVendor($this->vendor);
@ -168,5 +143,17 @@ final class MenuItemControllerTest extends DbWebTest
$this->assertResponseIsSuccessful(); $this->assertResponseIsSuccessful();
} });
} })
->covers(
MenuItemController::class,
OrderItemController::class,
OrderItemType::class,
MenuItemRepository::class,
FoodOrder::class,
FoodVendor::class,
MenuItem::class,
OrderItem::class,
FoodOrderRepository::class,
MenuItemType::class,
);

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Controller; namespace App\Tests\Feature\Controller;
use App\Controller\OrderItemController; use App\Controller\OrderItemController;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
@ -11,34 +11,19 @@ use App\Form\OrderItemType;
use App\Repository\FoodOrderRepository; use App\Repository\FoodOrderRepository;
use App\Repository\MenuItemRepository; use App\Repository\MenuItemRepository;
use App\Repository\OrderItemRepository; use App\Repository\OrderItemRepository;
use App\Tests\DbWebTest;
use DateTimeImmutable; use DateTimeImmutable;
use Override;
use PHPUnit\Framework\Attributes\CoversClass;
use function describe;
use function pest;
use function sprintf; use function sprintf;
use function test;
#[CoversClass(OrderItemController::class)] pest()
#[CoversClass(MenuItemRepository::class)] ->beforeEach(function (): void {
#[CoversClass(OrderItemRepository::class)] $this->setEntityClass(OrderItem::class);
#[CoversClass(OrderItemType::class)] $this->setPath('/order/item/');
#[CoversClass(FoodOrder::class)] $this->repository = $this->manager->getRepository($this->entityClass);
#[CoversClass(FoodVendor::class)]
#[CoversClass(MenuItem::class)]
#[CoversClass(OrderItem::class)]
#[CoversClass(FoodOrderRepository::class)]
final class OrderItemControllerTest extends DbWebTest
{
public FoodVendor $vendor;
public FoodOrder $order;
public Menuitem $menuItem;
public MenuItemRepository $menuItemRepository;
private string $path = '/order/item/';
#[Override]
public function setUp(): void
{
parent::setUp();
$this->vendor = new FoodVendor; $this->vendor = new FoodVendor;
$this->vendor->setName('Food Vendor'); $this->vendor->setName('Food Vendor');
@ -65,11 +50,11 @@ final class OrderItemControllerTest extends DbWebTest
$this->manager->persist($this->menuItem); $this->manager->persist($this->menuItem);
$this->manager->flush(); $this->manager->flush();
$this->menuItemRepository = static::getContainer()->get(MenuItemRepository::class); $this->menuItemRepository = $this::getContainer()->get(MenuItemRepository::class);
} });
public function testNew(): void describe(OrderItemController::class, function (): void {
{ test('new', function (): void {
$crawler = $this->client->request( $crawler = $this->client->request(
'GET', 'GET',
sprintf('%snew/%s', $this->path, $this->order->getId()) sprintf('%snew/%s', $this->path, $this->order->getId())
@ -80,23 +65,22 @@ final class OrderItemControllerTest extends DbWebTest
$this->assertCount(1, $children); $this->assertCount(1, $children);
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [ $this->client->submitForm('Save', [
'order_item[name]' => 'Testing', 'order_item[name]' => 'Testing',
'order_item[extras]' => 'Testing', 'order_item[extras]' => 'Testing',
]); ]);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
self::assertSame(1, $this->menuItemRepository->count([ $this->assertSame(1, $this->menuItemRepository->count([
'foodVendor' => $this->vendor->getId(), 'foodVendor' => $this->vendor->getId(),
])); ]));
} });
public function testNewOrderClosed(): void test('new order closed', function (): void {
{
$this->order->setClosedAt(new DateTimeImmutable('-1 Hour')); $this->order->setClosedAt(new DateTimeImmutable('-1 Hour'));
$this->manager->persist($this->order); $this->manager->persist($this->order);
@ -107,33 +91,31 @@ final class OrderItemControllerTest extends DbWebTest
sprintf('%snew/%s', $this->path, $this->order->getId()) sprintf('%snew/%s', $this->path, $this->order->getId())
); );
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(0, $this->repository->count([])); $this->assertSame(0, $this->repository->count([]));
} });
public function testNewCreateMenuItem(): void test('new create menu item', function (): void {
{
$this->client->request( $this->client->request(
'GET', 'GET',
sprintf('%snew/%s', $this->path, $this->order->getId()) sprintf('%snew/%s', $this->path, $this->order->getId())
); );
self::assertResponseStatusCodeSame(200); $this->assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [ $this->client->submitForm('Save', [
'order_item[name]' => 'Testing-1', 'order_item[name]' => 'Testing-1',
'order_item[extras]' => 'Testing-1', 'order_item[extras]' => 'Testing-1',
]); ]);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
self::assertSame(3, $this->menuItemRepository->count([])); $this->assertSame(3, $this->menuItemRepository->count([]));
} });
public function testRemove(): void test('remove', function (): void {
{
$fixture = new OrderItem; $fixture = new OrderItem;
$fixture->setName('Testing'); $fixture->setName('Testing');
$fixture->setExtras('Value'); $fixture->setExtras('Value');
@ -146,12 +128,11 @@ final class OrderItemControllerTest extends DbWebTest
$this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId())); $this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(0, $this->repository->count([])); $this->assertSame(0, $this->repository->count([]));
} });
public function testOrderClosed(): void test('order closed', function (): void {
{
$fixture = new OrderItem; $fixture = new OrderItem;
$fixture->setName('Testing'); $fixture->setName('Testing');
$fixture->setExtras('Value'); $fixture->setExtras('Value');
@ -166,12 +147,11 @@ final class OrderItemControllerTest extends DbWebTest
$this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId())); $this->client->request('GET', sprintf('%sdelete/%s', $this->path, $fixture->getId()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
} });
public function testEdit(): void test('edit', function (): void {
{
$orderItem = new OrderItem; $orderItem = new OrderItem;
$orderItem->setName('Testing'); $orderItem->setName('Testing');
$orderItem->setExtras('My Extra'); $orderItem->setExtras('My Extra');
@ -199,15 +179,14 @@ final class OrderItemControllerTest extends DbWebTest
'order_item[extras]' => 'Testing-1', 'order_item[extras]' => 'Testing-1',
]); ]);
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
self::assertSame(1, $this->repository->count([])); $this->assertSame(1, $this->repository->count([]));
self::assertSame(3, $this->menuItemRepository->count([])); $this->assertSame(3, $this->menuItemRepository->count([]));
} });
public function testEditOrderClosed(): void test('edit order closed', function (): void {
{
$orderItem = new OrderItem; $orderItem = new OrderItem;
$orderItem->setName('Testing'); $orderItem->setName('Testing');
$orderItem->setExtras('My Extra'); $orderItem->setExtras('My Extra');
@ -221,11 +200,10 @@ final class OrderItemControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId())); $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
} });
public function testCopy(): void test('copy', function (): void {
{
$orderItem = new OrderItem; $orderItem = new OrderItem;
$orderItem->setName('My Title'); $orderItem->setName('My Title');
$orderItem->setExtras('My Title'); $orderItem->setExtras('My Title');
@ -236,7 +214,7 @@ final class OrderItemControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId())); $this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$result = $this->repository->findBy([ $result = $this->repository->findBy([
'foodOrder' => $this->order->getId(), 'foodOrder' => $this->order->getId(),
@ -246,10 +224,9 @@ final class OrderItemControllerTest extends DbWebTest
$this->assertSame($orderItem->getName(), $item->getName()); $this->assertSame($orderItem->getName(), $item->getName());
$this->assertSame($orderItem->getExtras(), $item->getExtras()); $this->assertSame($orderItem->getExtras(), $item->getExtras());
} }
} });
public function testCopyOrderClosed(): void test('copy order closed', function (): void {
{
$orderItem = new OrderItem; $orderItem = new OrderItem;
$orderItem->setName('My Title'); $orderItem->setName('My Title');
$orderItem->setExtras('My Title'); $orderItem->setExtras('My Title');
@ -262,17 +239,23 @@ final class OrderItemControllerTest extends DbWebTest
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId())); $this->client->request('GET', sprintf('%s%s/copy', $this->path, $orderItem->getId()));
self::assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId())); $this->assertResponseRedirects(sprintf('/food/order/%s', $this->order->getId()));
$result = $this->repository->findBy([ $result = $this->repository->findBy([
'foodOrder' => $this->order->getId(), 'foodOrder' => $this->order->getId(),
]); ]);
$this->assertCount(1, $result); $this->assertCount(1, $result);
} });
#[Override] })
public function getEntityClass(): string ->covers(
{ OrderItemController::class,
return OrderItem::class; MenuItemRepository::class,
} OrderItemRepository::class,
} OrderItemType::class,
FoodOrder::class,
FoodVendor::class,
MenuItem::class,
OrderItem::class,
FoodOrderRepository::class,
);

View file

@ -1,5 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use App\Tests\DbWebTest;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Test Case | Test Case
@ -11,6 +13,9 @@
| |
*/ */
pest()
->extends(DbWebTest::class)->in('Feature/Controller/*.php');
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Functions | Functions

View file

@ -0,0 +1,32 @@
<?php declare(strict_types=1);
namespace App\Tests\Unit\Entity;
use App\Entity\FoodOrder;
use App\Entity\OrderItem;
use function expect;
use function test;
test('FoodOrder Entity', function (): void {
$order = new FoodOrder;
$orderItem = new OrderItem;
expect($order->getOrderItems())
->toBeEmpty();
$order->addOrderItem($orderItem);
$order->addOrderItem($orderItem);
expect($order->getOrderItems())
->toHaveCount(1)
->and($orderItem->getFoodOrder())
->toBe($order);
$order->removeOrderItem($orderItem);
expect($order->getOrderItems())
->toBeEmpty()
->and($orderItem->getFoodOrder())
->toBeNull();
})
->covers(FoodOrder::class, OrderItem::class);

View file

@ -1,21 +1,17 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Entity; namespace App\Tests\Unit\Entity;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
use App\Entity\FoodVendor; use App\Entity\FoodVendor;
use App\Entity\MenuItem; use App\Entity\MenuItem;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Uid\Ulid; use Symfony\Component\Uid\Ulid;
#[CoversClass(FoodVendor::class)] use function describe;
#[CoversClass(FoodOrder::class)] use function test;
#[CoversClass(MenuItem::class)]
final class FoodVendorTest extends TestCase describe(FoodVendor::class, function (): void {
{ test('FoodVendor entity', function (): void {
public function testFoodVendor(): void
{
$vendor = new FoodVendor; $vendor = new FoodVendor;
$vendor->setName('Test'); $vendor->setName('Test');
$this->assertEquals('Test', $vendor->getName()); $this->assertEquals('Test', $vendor->getName());
@ -33,11 +29,9 @@ final class FoodVendorTest extends TestCase
$vendor->removeFoodOrder($order1); $vendor->removeFoodOrder($order1);
$this->assertCount(0, $vendor->getFoodOrders()); $this->assertCount(0, $vendor->getFoodOrders());
$this->assertNull($order1->getFoodVendor()); $this->assertNull($order1->getFoodVendor());
});
} test('MenutItem entity', function (): void {
public function testMenuItem(): void
{
$vendor = new FoodVendor; $vendor = new FoodVendor;
$menuItem1 = new MenuItem; $menuItem1 = new MenuItem;
$menuItem2 = new MenuItem; $menuItem2 = new MenuItem;
@ -53,10 +47,9 @@ final class FoodVendorTest extends TestCase
$vendor->addMenuItem($menuItem2); $vendor->addMenuItem($menuItem2);
$this->assertCount(1, $vendor->getMenuItems()); $this->assertCount(1, $vendor->getMenuItems());
$this->assertCount(2, $vendor->getMenuItems(true)); $this->assertCount(2, $vendor->getMenuItems(true));
} });
public function testRemoveForeignMenuItem(): void test('remove foreign menu item', function (): void {
{
$vendor1 = new FoodVendor; $vendor1 = new FoodVendor;
$vendor2 = new FoodVendor; $vendor2 = new FoodVendor;
$item1 = new MenuItem; $item1 = new MenuItem;
@ -66,5 +59,6 @@ final class FoodVendorTest extends TestCase
$vendor2->removeMenuItem($item1); $vendor2->removeMenuItem($item1);
$this->assertCount(1, $vendor1->getMenuItems()); $this->assertCount(1, $vendor1->getMenuItems());
$this->assertSame($vendor1, $item1->getFoodVendor()); $this->assertSame($vendor1, $item1->getFoodVendor());
} });
}
})->covers(FoodVendor::class, FoodOrder::class, MenuItem::class);

View file

@ -1,19 +1,16 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace App\Tests\Entity; namespace App\Tests\Unit\Entity;
use App\Entity\FoodVendor; use App\Entity\FoodVendor;
use App\Entity\MenuItem; use App\Entity\MenuItem;
use DateTimeImmutable; use DateTimeImmutable;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(MenuItem::class)] use function describe;
#[CoversClass(FoodVendor::class)] use function test;
final class MenuItemTest extends TestCase
{ describe(MenuItem::class, function (): void {
public function testMenuItem(): void test('MenuItem entity', function (): void {
{
$item = new MenuItem; $item = new MenuItem;
$item->setName('Test'); $item->setName('Test');
$this->assertEquals('Test', $item->getName()); $this->assertEquals('Test', $item->getName());
@ -29,10 +26,8 @@ final class MenuItemTest extends TestCase
$item->delete(); $item->delete();
$this->assertTrue($item->isDeleted()); $this->assertTrue($item->isDeleted());
$this->assertInstanceOf(DateTimeImmutable::class, $item->getDeletedAt()); $this->assertInstanceOf(DateTimeImmutable::class, $item->getDeletedAt());
} });
test('MenuItem alias', function (): void {
public function testMenuItemAlias(): void
{
$item = new MenuItem; $item = new MenuItem;
$item->setName('Test'); $item->setName('Test');
$this->assertEquals('Test', $item->getName()); $this->assertEquals('Test', $item->getName());
@ -54,6 +49,5 @@ final class MenuItemTest extends TestCase
$item->removeAlias($item2); $item->removeAlias($item2);
$this->assertCount(0, $item->getAliases()); $this->assertCount(0, $item->getAliases());
$this->assertNull($item2->getAliasOf()); $this->assertNull($item2->getAliasOf());
});
} })->covers(MenuItem::class, FoodVendor::class);
}