<?php
|
declare(strict_types=1);
|
|
namespace App\Tests;
|
|
use App\Entity\Book;
|
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\Tools\SchemaTool;
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
abstract class DatabaseTestCase extends WebTestCase {
|
protected KernelBrowser $client;
|
|
protected function setUp(): void {
|
$this->client = static::createClient();
|
|
$entityManager = $this->entityManager();
|
$metadata = $entityManager->getMetadataFactory()->getAllMetadata();
|
$tool = new SchemaTool($entityManager);
|
$tool->dropSchema($metadata);
|
$tool->createSchema($metadata);
|
}
|
|
protected function entityManager(): EntityManagerInterface {
|
return static::getContainer()->get(EntityManagerInterface::class);
|
}
|
|
protected function createBook(int $id, string $title = 'Title', string $author = 'Author'): Book {
|
$book = (new Book())->setId($id)->setTitle($title)->setAuthor($author);
|
$this->entityManager()->persist($book);
|
$this->entityManager()->flush();
|
|
return $book;
|
}
|
|
protected function findBook(int $id): ?Book {
|
$this->entityManager()->clear();
|
|
return $this->entityManager()->find(Book::class, $id);
|
}
|
}
|