Completed
Pull Request — master (#258)
by Alexis
06:47
created

RunParatestCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0065

Importance

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