Completed
Pull Request — master (#258)
by Alexis
16:21 queued 08:03
created

RunParatestCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
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 $configuration;
17
    private $output;
18
    private $process = 5;
19
    private $testDbPath;
20
    private $phpunit = './bin/phpunit';
21
22
    /**
23
     * Configuration of the command.
24
     */
25 12
    protected function configure()
26
    {
27 12
        $this
28 12
            ->setName('test:run')
29 12
            ->setDescription('Run phpunit tests with multiple process')
30 12
            ->addArgument('options', InputArgument::OPTIONAL, 'Options')
31
        ;
32 12
    }
33
34 1
    protected function prepare()
35
    {
36 1
        $this->configuration = $this->getContainer()->hasParameter('liip_functional_test');
37 1
        $paratestCfg = (!isset($this->configuration['paratest'])) ? array('process' => $this->process, 'phpunit' => $this->phpunit) : $this->configuration['paratest'];
38
39 1
        $this->process = (!empty($this->configuration['process'])) ? $paratestCfg['process'] : $this->process;
40 1
        $this->phpunit = (!empty($this->configuration['phpunit'])) ? $paratestCfg['phpunit'] : $this->phpunit;
41 1
        $this->testDbPath = $this->getContainer()->getParameter('kernel.cache_dir').'/test/';
42 1
        $createDirProcess = new Process('mkdir -p '.$this->testDbPath);
43 1
        $createDirProcess->run();
44 1
        $this->output->writeln('Cleaning old dbs in '.$this->testDbPath.' ...');
45 1
        $cleanProcess = new Process('rm -fr '.$this->testDbPath.'/dbTest.db '.$this->testDbPath.'/dbTest*.db*');
46 1
        $cleanProcess->run();
47 1
        $this->output->writeln('Creating Schema in '.$this->testDbPath.' ...');
48 1
        $createProcess = new Process('php app/console doctrine:schema:create --env=test');
49 1
        $createProcess->run();
50
51 1
        $this->output->writeln('Initial schema created');
52 1
        $populateProcess = new Process('php app/console doctrine:fixtures:load -n --env=test');
53 1
        $populateProcess->run();
54
55 1
        $this->output->writeln('Initial schema populated, duplicating....');
56 1
        for ($a = 0; $a < $this->process; ++$a) {
57 1
            $test = new Process('cp '.$this->testDbPath.'/dbTest.db '.$this->testDbPath.'/dbTest'.$a.'.db');
58 1
            $test->run();
59 1
        }
60 1
    }
61
62
    /**
63
     * Content of the command.
64
     *
65
     * @param InputInterface  $input
66
     * @param OutputInterface $output
67
     */
68 1
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70 1
        $this->output = $output;
71 1
        $this->prepare();
72
73 1
        if (is_file('vendor/bin/paratest') !== true) {
74
            $this->output->writeln('Error : Install paratest first');
75
        } else {
76 1
            $this->output->writeln('Done...Running test.');
77 1
            $runProcess = new Process('vendor/bin/paratest '.
78 1
                '-c '.__DIR__.'/../phpunit.xml.dist '.
79 1
                '--phpunit '.__DIR__.'/'.$this->phpunit.' '.
80 1
                '--runner WrapRunner  -p '.$this->process.' '.
81 1
                $input->getArgument('options')
82 1
            );
83 1
            $runProcess->run(function ($type, $buffer) use ($output) {
84 1
                $output->write($buffer);
85 1
            });
86
        }
87 1
    }
88
}
89