Completed
Push — master ( d44c20...9ce4e7 )
by Alexis
05:52
created

RunParatestCommand::prepare()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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