|
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
|
|
|
protected function configure() |
|
25
|
|
|
{ |
|
26
|
|
|
$this |
|
27
|
|
|
->setName('test:run') |
|
28
|
|
|
->setDescription('Run phpunit tests with multiple process') |
|
29
|
|
|
; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function prepare() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->configuration = $this->getContainer()->hasParameter('liip_functional_test'); |
|
35
|
|
|
$paratestCfg = (!isset($this->configuration['paratest'])) ? array('process' => $this->process, 'phpunit' => $this->phpunit) : $this->configuration['paratest']; |
|
36
|
|
|
|
|
37
|
|
|
$this->process = (!empty($this->configuration['process'])) ? $paratestCfg['process'] : $this->process; |
|
38
|
|
|
$this->phpunit = (!empty($this->configuration['phpunit'])) ? $paratestCfg['phpunit'] : $this->phpunit; |
|
39
|
|
|
$this->testDbPath = $this->getContainer()->getParameter('kernel.cache_dir').'/test/'; |
|
40
|
|
|
$createDirProcess = new Process('mkdir -p '.$this->testDbPath); |
|
41
|
|
|
$createDirProcess->run(); |
|
42
|
|
|
$this->output->writeln("Cleaning old dbs in $this->testDbPath ..."); |
|
43
|
|
|
$cleanProcess = new Process("rm -fr ".$this->testDbPath."/dbTest.db ".$this->testDbPath."/dbTest*.db*"); |
|
44
|
|
|
$cleanProcess->run(); |
|
45
|
|
|
$this->output->writeln("Creating Schema in ".$this->testDbPath." ..."); |
|
46
|
|
|
$createProcess = new Process('php app/console doctrine:schema:create --env=test'); |
|
47
|
|
|
$createProcess->run(); |
|
48
|
|
|
|
|
49
|
|
|
$this->output->writeln('Initial schema created'); |
|
50
|
|
|
$populateProcess = new Process("php app/console doctrine:fixtures:load -n --env=test"); |
|
51
|
|
|
$populateProcess->run(); |
|
52
|
|
|
|
|
53
|
|
|
$this->output->writeln('Initial schema populated, duplicating....'); |
|
54
|
|
|
for ($a = 0; $a < $this->process; ++$a) { |
|
55
|
|
|
$test = new Process("cp ".$this->testDbPath/dbTest.db." ".$this->testDbPath."/dbTest".$a.".db"); |
|
56
|
|
|
$test->run(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Content of the command. |
|
62
|
|
|
* |
|
63
|
|
|
* @param InputInterface $input |
|
64
|
|
|
* @param OutputInterface $output |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->output = $output; |
|
69
|
|
|
$this->prepare(); |
|
70
|
|
|
|
|
71
|
|
|
if (is_file('vendor/bin/paratest') === true) { |
|
72
|
|
|
$paratest = 'vendor/bin/paratest'; |
|
73
|
|
|
} elseif (is_file(__DIR__.'/../vendor/bin/paratest') === true) { |
|
74
|
|
|
$paratest = __DIR__.'/../vendor/bin/paratest'; |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->output->writeln('Error : Install paratest first'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->output->writeln('Done...Running test.'); |
|
80
|
|
|
$runProcess = new Process($paratest . ' ' . |
|
|
|
|
|
|
81
|
|
|
'-c ' . __DIR__ . '/../phpunit.xml.dist '. |
|
82
|
|
|
'--phpunit ' . __DIR__ . '/' . $this->phpunit. ' ' . |
|
83
|
|
|
'--runner WrapRunner -p ' . $this->process . ' ' . |
|
84
|
|
|
// Don't launch all the tests, that may create an infinite |
|
85
|
|
|
// loop if this current file is tested. |
|
86
|
|
|
__DIR__ . '/../Tests/Test/'); |
|
87
|
|
|
$runProcess->run(function ($type, $buffer) use ($output) { |
|
88
|
|
|
$output->write($buffer); |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: