add food vendor tests #8

Merged
lubiana merged 1 commit from food_vendor_tests into main 2024-06-14 18:27:04 +00:00
8 changed files with 39 additions and 53 deletions
Showing only changes of commit a7bec45c48 - Show all commits

View file

@ -4,4 +4,4 @@ APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
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
{
$io = new SymfonyStyle($input, $output);
$this->fakeData->resetDb();
$this->fakeData->generate();
$io->success('Added some fake data to database');
return Command::SUCCESS;
}

View file

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

View file

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

View file

@ -5,6 +5,9 @@ namespace App\Service;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use App\Entity\OrderItem;
use App\Repository\FoodOrderRepository;
use App\Repository\FoodVendorRepository;
use App\Repository\OrderItemRepository;
use Doctrine\ORM\EntityManagerInterface;
use function range;
@ -12,16 +15,32 @@ use function range;
final readonly class FakeData
{
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) {
$orders = $this->generateOrdersForVendor($vendor);
$orders = $this->generateOrdersForVendor($vendor, $orderAmount);
foreach ($orders as $order) {
$this->generateItemsForOrder($order);
$this->generateItemsForOrder($order, $itemAmount);
}
}
$this->entityManager->flush();

View file

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

View file

@ -18,14 +18,10 @@ final class FoodVendorControllerTest extends DbWebTest
self::assertResponseStatusCodeSame(200);
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
{
$this->markTestIncomplete();
$this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200);
@ -34,31 +30,27 @@ final class FoodVendorControllerTest extends DbWebTest
'food_vendor[name]' => 'Testing',
]);
self::assertResponseRedirects($this->path);
self::assertSame(1, $this->repository->count([]));
}
public function testShow(): void
{
$this->markTestIncomplete();
$fixture = new FoodVendor;
$fixture->setName('My Title');
$this->manager->persist($fixture);
$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);
self::assertPageTitleContains('FoodVendor');
// Use assertions to check that the properties are properly displayed.
$this->assertResponseIsSuccessful();
$nameNode = $crawler->filter('td')
->last();
$this->assertSame('My Title', $nameNode->text());
}
public function testEdit(): void
{
$this->markTestIncomplete();
$fixture = new FoodVendor;
$fixture->setName('Value');
@ -78,22 +70,6 @@ final class FoodVendorControllerTest extends DbWebTest
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]
public function getEntityClass(): string
{

View file

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