Test Failed
Pull Request — master (#349)
by Alexis
05:22
created

RunParatestCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 16
cts 18
cp 0.8889
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
crap 2.0054
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
use Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Bundle\FrameworkBundle\Console\Application;
12
13
/**
14
 * Command used to update the project.
15
 */
16
class RunParatestCommand extends ContainerAwareCommand
17
{
18
    private $output;
19
20
    private $process;
21
22
    private $testDbPath;
23
24
    private $phpunit;
25
26
    /**
27
     * Configuration of the command.
28
     */
29 12
    protected function configure()
30
    {
31 12
        $this
32 12
            ->setName('paratest:run')
33 12
            ->setDescription('Run phpunit tests with multiple processes')
34
            // Pass arguments from this command "paratest:run" to the paratest command.
35 12
            ->addArgument('options', InputArgument::OPTIONAL, 'Options')
36
        ;
37 12
    }
38
39 1
    protected function prepare()
40
    {
41 1
        $this->phpunit = $this->getContainer()->getParameter('liip_functional_test.paratest.phpunit');
42 1
        $this->process = $this->getContainer()->getParameter('liip_functional_test.paratest.process');
43
44 1
        $this->testDbPath = $this->getContainer()->get('kernel')->getCacheDir();
45 1
        $this->output->writeln("Cleaning old dbs in $this->testDbPath ...");
46 1
        $createDirProcess = new Process('mkdir -p '.$this->testDbPath);
47 1
        $createDirProcess->run();
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
        $application = new Application($this->getContainer()->get('kernel'));
52 1
        $application->setAutoExit(false);
53 1
        $input = new ArrayInput(array('doctrine:schema:create', '--env' => 'test'));
54 1
        $application->run($input, $this->output);
55
56 1
        $this->output->writeln('Initial schema created');
57 1
        $input = new ArrayInput(array(
58 1
            'doctrine:fixtures:load',
59 1
            '-n' => '',
60 1
            '--env' => 'test',
61 1
        ));
62 1
        $application->run($input, $this->output);
63
64 1
        $this->output->writeln('Initial schema populated, duplicating....');
65 1
        for ($a = 0; $a < $this->process; ++$a) {
66 1
            $test = new Process("cp $this->testDbPath/dbTest.db ".$this->testDbPath."/dbTest$a.db");
67 1
            $test->run();
68 1
        }
69 1
    }
70
71
    /**
72
     * Content of the command.
73
     *
74
     * @param InputInterface  $input
75
     * @param OutputInterface $output
76
     */
77 1
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79 1
        $this->output = $output;
80 1
        $this->prepare();
81 1
        if (true !== is_file('vendor/bin/paratest')) {
82
            $this->output->writeln('Error : Install paratest first');
83
        } else {
84 1
            $this->output->writeln('Done...Running test.');
85 1
            $runProcess = new Process('vendor/bin/paratest '.
86 1
                '-c phpunit.xml.dist '.
87 1
                '--phpunit '.$this->phpunit.' '.
88 1
                '--runner WrapRunner '.
89 1
                '-p '.$this->process.' '.
90 1
                $input->getArgument('options')
91 1
            );
92 1
            $runProcess->run(function ($type, $buffer) use ($output) {
93 1
                $output->write($buffer);
94 1
            });
95
        }
96 1
    }
97
}
98