futtern/tests/DbWebTest.php

60 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2024-06-14 15:41:00 +00:00
<?php declare(strict_types=1);
namespace App\Tests;
2024-07-10 20:18:56 +00:00
use DateInterval;
use DateTimeImmutable;
2024-06-14 15:41:00 +00:00
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;
2024-07-10 20:18:56 +00:00
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Uid\Ulid;
use function str_contains;
2024-06-14 15:41:00 +00:00
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();
2024-06-14 18:21:16 +00:00
$schemaTool = new SchemaTool($this->manager);
$metadata = $this->manager->getMetadataFactory()
->getAllMetadata();
$schemaTool->dropDatabase();
2024-06-14 18:21:16 +00:00
$schemaTool->updateSchema($metadata);
2024-07-10 17:05:28 +00:00
if ($this->getEntityClass() !== '') {
$this->repository = $this->manager->getRepository($this->getEntityClass());
}
2024-06-14 15:41:00 +00:00
}
2024-07-10 20:18:56 +00:00
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),
)
);
}
2024-06-14 15:41:00 +00:00
}