Completed
Push — master ( e7c5cf...05695e )
by Alejandro
17s
created

TestHelper::createTestDb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common;
5
6
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
7
use Doctrine\Common\DataFixtures\Loader;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Symfony\Component\Process\Process;
10
use function file_exists;
11
use function realpath;
12
use function sys_get_temp_dir;
13
use function unlink;
14
15
class TestHelper
16
{
17
    public function createTestDb(): void
18
    {
19
        $shlinkDbPath = realpath(sys_get_temp_dir()) . '/shlink-tests.db';
20
        if (file_exists($shlinkDbPath)) {
21
            unlink($shlinkDbPath);
22
        }
23
24
        $process = new Process(['vendor/bin/doctrine', 'orm:schema-tool:create', '--no-interaction', '-q']);
25
        $process->inheritEnvironmentVariables()
26
                ->mustRun();
27
    }
28
29
    public function seedFixtures(EntityManagerInterface $em, array $config): void
30
    {
31
        $paths = $config['paths'] ?? [];
32
        if (empty($paths)) {
33
            return;
34
        }
35
36
        $loader = new Loader();
37
        foreach ($paths as $path) {
38
            $loader->loadFromDirectory($path);
39
        }
40
41
        $executor = new ORMExecutor($em);
42
        $executor->execute($loader->getFixtures(), true);
43
    }
44
}
45