add food vendor tests
All checks were successful
/ ls (pull_request) Successful in 28s
/ ls (push) Successful in 28s

This commit is contained in:
Jonas 2024-06-14 20:21:16 +02:00
parent 63bf367ab0
commit a7bec45c48
Signed by: lubiana
SSH key fingerprint: SHA256:gkqM8DUX4Blf6P52fycW8ISTd+4eAHH+Uzu9iyc8hAM
8 changed files with 39 additions and 53 deletions

View file

@ -4,4 +4,4 @@ APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999 SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
DATABASE_URL="sqlite:///:memory:" DATABASE_URL="sqlite:///%kernel.project_dir%/var/test-data.db"

View file

@ -26,7 +26,9 @@ final class FakeDataCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$this->fakeData->resetDb();
$this->fakeData->generate(); $this->fakeData->generate();
$io->success('Added some fake data to database'); $io->success('Added some fake data to database');
return Command::SUCCESS; return Command::SUCCESS;
} }

View file

@ -20,9 +20,6 @@ class FoodOrder
#[ORM\CustomIdGenerator(class: UlidGenerator::class)] #[ORM\CustomIdGenerator(class: UlidGenerator::class)]
private Ulid|null $id = null; private Ulid|null $id = null;
#[ORM\Column]
private DateTimeImmutable $createdAt;
#[ORM\Column(nullable: true)] #[ORM\Column(nullable: true)]
private DateTimeImmutable|null $closedAt = null; private DateTimeImmutable|null $closedAt = null;
@ -38,7 +35,6 @@ class FoodOrder
public function __construct() public function __construct()
{ {
$this->createdAt = new DateTimeImmutable;
$this->orderItems = new ArrayCollection; $this->orderItems = new ArrayCollection;
} }
@ -47,16 +43,9 @@ class FoodOrder
return $this->id; return $this->id;
} }
public function getCreatedAt(): DateTimeImmutable|null public function getCreatedAt(): DateTimeImmutable
{ {
return $this->createdAt; return $this->id->getDateTime();
}
public function setCreatedAt(DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
} }
public function getClosedAt(): DateTimeImmutable|null public function getClosedAt(): DateTimeImmutable|null

View file

@ -31,7 +31,7 @@ final class FoodOrderRepository extends ServiceEntityRepository
{ {
$qb = $this->createQueryBuilder('alias'); $qb = $this->createQueryBuilder('alias');
$qb->orderBy('alias.createdAt', 'DESC'); $qb->orderBy('alias.id', 'DESC');
$qb->setMaxResults($limit); $qb->setMaxResults($limit);
$query = $qb->getQuery(); $query = $qb->getQuery();

View file

@ -5,6 +5,9 @@ namespace App\Service;
use App\Entity\FoodOrder; use App\Entity\FoodOrder;
use App\Entity\FoodVendor; use App\Entity\FoodVendor;
use App\Entity\OrderItem; use App\Entity\OrderItem;
use App\Repository\FoodOrderRepository;
use App\Repository\FoodVendorRepository;
use App\Repository\OrderItemRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use function range; use function range;
@ -12,16 +15,32 @@ use function range;
final readonly class FakeData final readonly class FakeData
{ {
public function __construct( public function __construct(
private EntityManagerInterface $entityManager private EntityManagerInterface $entityManager,
private FoodVendorRepository $foodVendorRepository,
private FoodOrderRepository $foodOrderRepository,
private OrderItemRepository $orderItemRepository,
) {} ) {}
public function generate(): void public function resetDb(): void
{ {
$vendors = $this->generateVendors(); foreach ($this->orderItemRepository->findAll() as $item) {
$this->entityManager->remove($item);
}
foreach ($this->foodOrderRepository->findAll() as $item) {
$this->entityManager->remove($item);
}
foreach ($this->foodVendorRepository->findAll() as $item) {
$this->entityManager->remove($item);
}
}
public function generate(int $vendorAmount = 3, int $orderAmount = 4, int $itemAmount = 10): void
{
$vendors = $this->generateVendors($vendorAmount);
foreach ($vendors as $vendor) { foreach ($vendors as $vendor) {
$orders = $this->generateOrdersForVendor($vendor); $orders = $this->generateOrdersForVendor($vendor, $orderAmount);
foreach ($orders as $order) { foreach ($orders as $order) {
$this->generateItemsForOrder($order); $this->generateItemsForOrder($order, $itemAmount);
} }
} }
$this->entityManager->flush(); $this->entityManager->flush();

View file

@ -8,6 +8,4 @@
{{ include('food_vendor/_form.html.twig', {'button_label': 'Update'}) }} {{ include('food_vendor/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_food_vendor_index') }}">back to list</a> <a href="{{ path('app_food_vendor_index') }}">back to list</a>
{{ include('food_vendor/_delete_form.html.twig') }}
{% endblock %} {% endblock %}

View file

@ -18,14 +18,10 @@ final class FoodVendorControllerTest extends DbWebTest
self::assertResponseStatusCodeSame(200); self::assertResponseStatusCodeSame(200);
self::assertPageTitleContains('FoodVendor index'); self::assertPageTitleContains('FoodVendor index');
// Use the $crawler to perform additional assertions e.g.
// self::assertSame('Some text on the page', $crawler->filter('.p')->first());
} }
public function testNew(): void public function testNew(): void
{ {
$this->markTestIncomplete();
$this->client->request('GET', sprintf('%snew', $this->path)); $this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200); self::assertResponseStatusCodeSame(200);
@ -34,31 +30,27 @@ final class FoodVendorControllerTest extends DbWebTest
'food_vendor[name]' => 'Testing', 'food_vendor[name]' => 'Testing',
]); ]);
self::assertResponseRedirects($this->path);
self::assertSame(1, $this->repository->count([])); self::assertSame(1, $this->repository->count([]));
} }
public function testShow(): void public function testShow(): void
{ {
$this->markTestIncomplete();
$fixture = new FoodVendor; $fixture = new FoodVendor;
$fixture->setName('My Title'); $fixture->setName('My Title');
$this->manager->persist($fixture); $this->manager->persist($fixture);
$this->manager->flush(); $this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId())); $crawler = $this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
self::assertResponseStatusCodeSame(200); $this->assertResponseIsSuccessful();
self::assertPageTitleContains('FoodVendor'); $nameNode = $crawler->filter('td')
->last();
// Use assertions to check that the properties are properly displayed. $this->assertSame('My Title', $nameNode->text());
} }
public function testEdit(): void public function testEdit(): void
{ {
$this->markTestIncomplete();
$fixture = new FoodVendor; $fixture = new FoodVendor;
$fixture->setName('Value'); $fixture->setName('Value');
@ -78,22 +70,6 @@ final class FoodVendorControllerTest extends DbWebTest
self::assertSame('Something New', $fixture[0]->getName()); self::assertSame('Something New', $fixture[0]->getName());
} }
public function testRemove(): void
{
$this->markTestIncomplete();
$fixture = new FoodVendor;
$fixture->setName('Value');
$this->manager->persist($fixture);
$this->manager->flush();
$this->client->request('GET', sprintf('%s%s', $this->path, $fixture->getId()));
$this->client->submitForm('Delete');
self::assertResponseRedirects('/food/vendor/');
self::assertSame(0, $this->repository->count([]));
}
#[Override] #[Override]
public function getEntityClass(): string public function getEntityClass(): string
{ {

View file

@ -22,8 +22,10 @@ abstract class DbWebTest extends WebTestCase
{ {
$this->client = static::createClient(); $this->client = static::createClient();
$this->manager = static::getContainer()->get('doctrine')->getManager(); $this->manager = static::getContainer()->get('doctrine')->getManager();
(new SchemaTool($this->manager)) $schemaTool = new SchemaTool($this->manager);
->createSchema($this->manager->getMetadataFactory()->getAllMetadata()); $metadata = $this->manager->getMetadataFactory()
->getAllMetadata();
$schemaTool->updateSchema($metadata);
$this->repository = $this->manager->getRepository($this->getEntityClass()); $this->repository = $this->manager->getRepository($this->getEntityClass());
foreach ($this->repository->findAll() as $object) { foreach ($this->repository->findAll() as $object) {