futtern/tests/DbWebTest.php
lubiana 9afa7fe431
All checks were successful
/ ls (pull_request) Successful in 37s
/ ls (push) Successful in 32s
#29: add more tests
2024-07-10 22:18:56 +02:00

59 lines
1.8 KiB
PHP

<?php declare(strict_types=1);
namespace App\Tests;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\SchemaTool;
use Override;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Uid\Ulid;
use function str_contains;
abstract class DbWebTest extends WebTestCase
{
abstract public function getEntityClass(): string;
protected KernelBrowser $client;
protected EntityManagerInterface $manager;
protected EntityRepository $repository;
#[Override]
protected function setUp(): void
{
$this->client = static::createClient();
$this->manager = static::getContainer()->get('doctrine')->getManager();
$schemaTool = new SchemaTool($this->manager);
$metadata = $this->manager->getMetadataFactory()
->getAllMetadata();
$schemaTool->dropDatabase();
$schemaTool->updateSchema($metadata);
if ($this->getEntityClass() !== '') {
$this->repository = $this->manager->getRepository($this->getEntityClass());
}
}
protected function generateOldUlid(int $daysToSubtract = 10): Ulid
{
$date = (new DateTimeImmutable)->sub(new DateInterval('P' . $daysToSubtract . 'D'));
$ulidString = Ulid::generate($date);
return Ulid::fromString($ulidString);
}
protected function assertElementContainsCount(Crawler $crawler, string $element, int $count, string $text): void
{
$this->assertCount(
$count,
$crawler->filter($element)
->reduce(
static fn(Crawler $node, $i): bool => str_contains($node->text(), $text),
)
);
}
}