Completed
Push — master ( 6cfb9d...8bf893 )
by Marcel
02:47
created

FixtureTestCase   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 97
ccs 9
cts 30
cp 0.3
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A setUpBeforeClass() 0 6 1
A tearDownAfterClass() 0 4 1
A loadFixtures() 0 4 1
A getDataFixtures() 0 4 1
A createSchema() 0 5 1
A runCommand() 0 6 1
A getApplication() 0 11 2
1
<?php
2
3
namespace AppBundle\Tests;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8
use Symfony\Bundle\FrameworkBundle\Console\Application;
9
use Symfony\Component\Console\Input\StringInput;
10
11
/**
12
 * Class FixtureTestCase
13
 *
14
 * @package AppBundle\Tests
15
 */
16
class FixtureTestCase extends WebTestCase
17
{
18
    /**
19
     * @var Application
20
     */
21
    private static $application;
22
23
    /**
24
     * @var Client
25
     */
26
    protected static $client;
27
28
    /**
29
     * @var EntityManager
30
     */
31
    protected static $entityManager;
32
33
    /**
34
     * @return void
35
     */
36 7
    public function setUp()
37
    {
38 7
        parent::setUp();
39 7
        $this->loadFixtures();
40 7
    }
41
42
    /**
43
     * @return void
44
     */
45
    public static function setUpBeforeClass()
46
    {
47
        self::getApplication();
48
        self::createSchema();
49
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public static function  tearDownAfterClass()
56
    {
57
        self::runCommand('doctrine:database:drop --force');
58
    }
59
60
    /**
61
     * @SuppressWarnings(PHPMD.StaticAccess)
62
     * @return void
63
     */
64 7
    protected function loadFixtures()
65
    {
66 7
        \Nelmio\Alice\Fixtures::load($this->getDataFixtures(), self::$entityManager);
67 7
    }
68
69
    /**
70
     * @return array
71
     */
72 7
    protected function getDataFixtures()
73
    {
74 7
        return [];
75
    }
76
77
    /**
78
     * @throws \Doctrine\ORM\Tools\ToolsException
79
     */
80
    private static function createSchema()
81
    {
82
        self::runCommand('doctrine:database:create');
83
        self::runCommand('doctrine:schema:update --force');
84
    }
85
86
    /**
87
     * @param string $command
88
     *
89
     * @return int|mixed
90
     */
91
    private static function runCommand($command)
92
    {
93
        $command = sprintf('%s --quiet', $command);
94
95
        return self::getApplication()->run(new StringInput($command));
96
    }
97
98
    /**
99
     * @return Application
100
     */
101
    private static function getApplication()
102
    {
103
        if (null === self::$application) {
104
            self::$client = static::createClient();
105
            self::$entityManager = self::$client->getContainer()->get('doctrine.orm.default_entity_manager');
106
            self::$application = new Application(self::$client->getKernel());
107
            self::$application->setAutoExit(false);
108
        }
109
110
        return self::$application;
111
    }
112
}
113