Passed
Pull Request — master (#9)
by Mariusz
02:39
created

DoctrineOrmHelper::createSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\AssociateTests\Functional;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\Setup;
8
use Doctrine\ORM\EntityManager;
9
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\DataProvider;
10
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Category;
11
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Product;
12
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Store;
13
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\User;
14
use Xsolve\AssociateTests\Functional\DoctrineOrm\Mock\Entity\Variant;
15
16
class DoctrineOrmHelper
17
{
18
    /** @var EntityManagerInterface */
19
    protected $entityManager;
20
21
    protected function setupEntityManager()
22
    {
23
        $entityPaths = [
24
            __DIR__ . '/DoctrineOrm/Mock/Entity',
25
        ];
26
27
        $isDevMode = false;
28
29
        $databaseOptions = [
30
            'driver' => 'pdo_sqlite',
31
            'dbname' => 'sifter_test', // TODO Move to some config file.
32
            'user' => 'root', // TODO Move to some config file.
33
            'password' => '', // TODO Move to some config file.
34
            'memory' => true, // TODO Move to some config file.
35
        ];
36
37
        $metadataConfiguration = Setup::createAnnotationMetadataConfiguration($entityPaths, $isDevMode);
38
        $this->entityManager = EntityManager::create($databaseOptions, $metadataConfiguration);
39
    }
40
41
    /**
42
     * @return EntityManagerInterface
43
     */
44
    public function getEntityManager(): EntityManagerInterface
45
    {
46
        if (!$this->entityManager instanceof EntityManagerInterface) {
0 ignored issues
show
introduced by
$this->entityManager is always a sub-type of Doctrine\ORM\EntityManagerInterface. If $this->entityManager can have other possible types, add them to tests/Functional/DoctrineOrmHelper.php:18.
Loading history...
47
            $this->setupEntityManager();
48
        }
49
50
        return $this->entityManager;
51
    }
52
53
    /**
54
     * @throws \Doctrine\ORM\Tools\ToolsException
55
     */
56
    public function createSchema()
57
    {
58
        $this->getEntityManager();
59
60
        $entityClassMetadatas = [];
61
        foreach ($this->getEntityClassNames() as $entityClassName) {
62
            $entityClassMetadatas[] = $this->entityManager->getClassMetadata($entityClassName);
63
        }
64
65
        $tool = new SchemaTool($this->entityManager);
66
        $tool->createSchema($entityClassMetadatas);
67
    }
68
69
    public function loadData()
70
    {
71
        $this->getEntityManager();
72
73
        $dataProvider = new DataProvider();
74
        foreach ($dataProvider->getAll() as $entity) {
75
            $this->entityManager->persist($entity);
76
        }
77
        $this->entityManager->flush();
78
    }
79
80
    /**
81
     * @return string[]
82
     */
83
    protected function getEntityClassNames(): array
84
    {
85
        return [
86
            Category::class,
87
            User::class,
88
            Store::class,
89
            Product::class,
90
            Variant::class,
91
        ];
92
    }
93
}
94