Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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);
    }
}