CommandTestCase::createTestDB()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Velikonja\LabbyBundle\Test\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Tester\ApplicationTester;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
abstract class CommandTestCase extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var ApplicationTester
14
     */
15
    protected $tester;
16
17
    /**
18
     * Temporary test dir, set also in config.yml
19
     *
20
     * @var string
21
     */
22
    protected $tmpDir;
23
24
    /**
25
     * @var Filesystem
26
     */
27
    protected $fs;
28
29
    /**
30
     * Prepare environment.
31
     */
32
    public function setUp()
33
    {
34
        $application = new Application(
35
            new \VelikonjaLabbyBundleTestAppKernel('test', true)
36
        );
37
        $application->setAutoExit(false);
38
39
        $this->tester = new ApplicationTester($application);
40
        $this->tmpDir = '/tmp/labby-bundle-tests';
41
        $this->fs     = new Filesystem();
42
43
        $this->dropTestDB();
44
        $this->createTestDB();
45
        $this->createTempDir();
46
    }
47
48
    /**
49
     * Clean up.
50
     */
51
    public function tearDown()
52
    {
53
        $this->dropTestDB();
54
        $this->removeTempDir();
55
    }
56
57
    /**
58
     * Drops the test database.
59
     */
60
    protected function dropTestDB()
61
    {
62
        $exitCode = $this->tester->run(array(
63
            'command' => 'doctrine:database:drop',
64
            '--force' => true
65
        ));
66
67
        # 0 for successful exit
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
        # 1 for exiting if database does not exists
69
        if (! in_array($exitCode, array(0, 1))) {
70
            throw new \RuntimeException(trim($this->tester->getDisplay()));
71
        }
72
73
    }
74
75
    /**
76
     * Create test database.
77
     */
78
    protected function createTestDB()
79
    {
80
        $exitCode = $this->tester->run(array(
81
            'command' => 'doctrine:database:create'
82
        ), array(
83
            'verbosity' => OutputInterface::VERBOSITY_DEBUG
84
        ));
85
86
        if ($exitCode) {
87
            throw new \RuntimeException(trim($this->tester->getDisplay()));
88
        }
89
    }
90
91
    /**
92
     * Creates temporary folder.
93
     */
94
    protected function createTempDir()
95
    {
96
        $this->fs->remove($this->tmpDir);
97
        $this->fs->mkdir(
98
            array(
99
                $this->tmpDir,
100
            )
101
        );
102
    }
103
104
    /**
105
     * Remove temporary folder.
106
     */
107
    protected function removeTempDir()
108
    {
109
        $this->fs->remove($this->tmpDir);
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    protected function getFixturesDir()
116
    {
117
        return __DIR__ . '/../fixtures';
118
    }
119
}
120