feat(db): add entities
This commit is contained in:
49
tests/Common/DatabaseTestTrait.php
Normal file
49
tests/Common/DatabaseTestTrait.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Common;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
trait DatabaseTestTrait
|
||||
{
|
||||
protected ?EntityManagerInterface $em;
|
||||
|
||||
protected function bootDatabase(): void
|
||||
{
|
||||
/**
|
||||
* @var EntityManagerInterface $em
|
||||
*/
|
||||
$em = $this->getContainer()->get(EntityManagerInterface::class);
|
||||
$this->em = $em;
|
||||
$this->em->getConnection()->executeQuery(<<<EOSQL
|
||||
DO $$
|
||||
DECLARE row RECORD;
|
||||
BEGIN
|
||||
FOR row IN SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_type='BASE TABLE'
|
||||
AND table_schema='public'
|
||||
LOOP
|
||||
EXECUTE format('TRUNCATE TABLE %I CASCADE;',row.table_name);
|
||||
END LOOP;
|
||||
END;
|
||||
$$;
|
||||
EOSQL);
|
||||
}
|
||||
|
||||
protected function getEntityManager(): EntityManagerInterface
|
||||
{
|
||||
if ($this->em === null) {
|
||||
throw new \LogicException('Database has not been booted yet.');
|
||||
}
|
||||
|
||||
return $this->em;
|
||||
}
|
||||
|
||||
protected function getEntityCount(string $entityClass): int
|
||||
{
|
||||
return (int) $this->getEntityManager()->createQuery('SELECT COUNT(e) FROM ' . $entityClass . ' e')->getSingleScalarResult();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user