Failed Conditions
Push — master ( d044dc...461c48 )
by Florent
06:53
created

DataFixtureTestCase::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Tests\Functional;
15
16
use Doctrine\ORM\EntityManager;
17
use Symfony\Bundle\FrameworkBundle\Client;
18
use Symfony\Bundle\FrameworkBundle\Console\Application;
19
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
20
use Symfony\Component\Console\Input\StringInput;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * @internal
25
 */
26
class DataFixtureTestCase extends WebTestCase
27
{
28
    /** @var Application $application */
29
    protected static $application;
30
31
    /** @var Client $client */
32
    protected $client;
33
34
    /** @var ContainerInterface $container */
35
    protected $container;
36
37
    /** @var EntityManager $entityManager */
38
    protected $entityManager;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function setUp(): void
44
    {
45
        self::runCommand('doctrine:database:drop --force');
46
        self::runCommand('doctrine:database:create');
47
        self::runCommand('doctrine:schema:create');
48
        self::runCommand('doctrine:fixtures:load --append --no-interaction');
49
50
        $this->client = static::createClient();
51
        $this->container = $this->client->getContainer();
52
        $this->entityManager = $this->container->get('doctrine.orm.entity_manager');
53
54
        parent::setUp();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function tearDown()
61
    {
62
        self::runCommand('doctrine:database:drop --force');
63
64
        parent::tearDown();
65
66
        $this->entityManager->close();
67
        $this->entityManager = null; // avoid memory leaks
68
    }
69
70
    protected static function runCommand($command)
71
    {
72
        $command = sprintf('%s --quiet', $command);
73
74
        return self::getApplication()->run(new StringInput($command));
75
    }
76
77
    protected static function getApplication()
78
    {
79
        if (null === self::$application) {
80
            $client = static::createClient();
81
82
            self::$application = new Application($client->getKernel());
83
            self::$application->setAutoExit(false);
84
        }
85
86
        return self::$application;
87
    }
88
}
89