Completed
Pull Request — master (#339)
by Alejandro
09:39 queued 07:20
created

TestHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createTestDb() 0 10 2
A seedFixtures() 0 14 3
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