Completed
Pull Request — master (#258)
by Alexis
07:26
created

RunParatestCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0078

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 21
ccs 14
cts 16
cp 0.875
rs 9.3142
cc 2
eloc 14
nc 2
nop 2
crap 2.0078
1
<?php
2
3
namespace Liip\FunctionalTestBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * Command used to update the project.
12
 */
13
class RunParatestCommand extends ContainerAwareCommand
14
{
15
    private $configuration;
16
    private $output;
17
    private $process = 5;
18
    private $testDbPath;
19
    private $phpunit = './bin/phpunit';
20
21
    /**
22
     * Configuration of the command.
23
     */
24 12
    protected function configure()
25
    {
26 12
        $this
27 12
            ->setName('test:run')
28 12
            ->setDescription('Run phpunit tests with multiple process')
29
        ;
30 12
    }
31
32 1
    protected function prepare()
33
    {
34 1
        $this->configuration = $this->getContainer()->hasParameter('liip_functional_test');
35 1
        $paratestCfg = (!isset($this->configuration['paratest'])) ? array('process' => $this->process, 'phpunit' => $this->phpunit) : $this->configuration['paratest'];
36
37 1
        $this->process = (!empty($this->configuration['process'])) ? $paratestCfg['process'] : $this->process;
38 1
        $this->phpunit = (!empty($this->configuration['phpunit'])) ? $paratestCfg['phpunit'] : $this->phpunit;
39 1
        $this->testDbPath = $this->getContainer()->getParameter('kernel.cache_dir').'/test/';
40 1
        $createDirProcess = new Process('mkdir -p '.$this->testDbPath);
41 1
        $createDirProcess->run();
42 1
        $this->output->writeln('Cleaning old dbs in '.$this->testDbPath.' ...');
43 1
        $cleanProcess = new Process('rm -fr '.$this->testDbPath.'/dbTest.db '.$this->testDbPath.'/dbTest*.db*');
44 1
        $cleanProcess->run();
45 1
        $this->output->writeln('Creating Schema in '.$this->testDbPath.' ...');
46 1
        $createProcess = new Process('php app/console doctrine:schema:create --env=test');
47 1
        $createProcess->run();
48
49 1
        $this->output->writeln('Initial schema created');
50 1
        $populateProcess = new Process('php app/console doctrine:fixtures:load -n --env=test');
51 1
        $populateProcess->run();
52
53 1
        $this->output->writeln('Initial schema populated, duplicating....');
54 1
        for ($a = 0; $a < $this->process; ++$a) {
55 1
            $test = new Process('cp '.$this->testDbPath.'/dbTest.db '.$this->testDbPath.'/dbTest'.$a.'.db');
56 1
            $test->run();
57 1
        }
58 1
    }
59
60
    /**
61
     * Content of the command.
62
     *
63
     * @param InputInterface  $input
64
     * @param OutputInterface $output
65
     */
66 1
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68 1
        $this->output = $output;
69 1
        $this->prepare();
70
71 1
        if (is_file('vendor/bin/paratest') !== true) {
72
            $this->output->writeln('Error : Install paratest first');
73
        } else {
74 1
            $this->output->writeln('Done...Running test.');
75 1
            $runProcess = new Process('vendor/bin/paratest '.
76 1
                '-c '.__DIR__.'/../phpunit.xml.dist '.
77 1
                '--phpunit '.__DIR__.'/'.$this->phpunit.' '.
78 1
                '--runner WrapRunner  -p '.$this->process.' '.
79
                // Don't launch all the tests, that may create an infinite
80
                // loop if this current file is tested.
81 1
                __DIR__.'/../Tests/Test/');
82 1
            $runProcess->run(function ($type, $buffer) use ($output) {
83 1
                $output->write($buffer);
84 1
            });
85
        }
86 1
    }
87
}
88