1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Profiler;
use Piwik\Plugin\ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Executes PHP tests.
*/
class TestsRun extends ConsoleCommand
{
protected function configure()
{
$this->setName('tests:run');
$this->setDescription('Run Piwik PHPUnit tests one group after the other');
$this->addArgument('group', InputArgument::OPTIONAL, 'Run only a specific test group. Separate multiple groups by comma, for instance core,plugins', '');
$this->addOption('options', 'o', InputOption::VALUE_OPTIONAL, 'All options will be forwarded to phpunit', '');
$this->addOption('xhprof', null, InputOption::VALUE_NONE, 'Profile using xhprof.');
$this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Execute tests within this file. Should be a path relative to the tests/PHPUnit directory.');
$this->addOption('suite', null, InputOption::VALUE_REQUIRED, 'Execute tests of a specific test suite, for instance UnitTests, IntegrationTests or SystemTests.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $input->getOption('options');
$groups = $input->getArgument('group');
$groups = explode(",", $groups);
$groups = array_map('ucfirst', $groups);
$groups = array_filter($groups, 'strlen');
$command = '../../vendor/phpunit/phpunit/phpunit';
// force xdebug usage for coverage options
if (false !== strpos($options, '--coverage') && !extension_loaded('xdebug')) {
$output->writeln('<info>xdebug extension required for code coverage.</info>');
$output->writeln('<info>searching for xdebug extension...</info>');
$extensionDir = shell_exec('php-config --extension-dir');
$xdebugFile = trim($extensionDir) . DIRECTORY_SEPARATOR . 'xdebug.so';
if (!file_exists($xdebugFile)) {
$dialog = $this->getHelperSet()->get('dialog');
$xdebugFile = $dialog->askAndValidate($output, 'xdebug not found. Please provide path to xdebug.so', function($xdebugFile) {
return file_exists($xdebugFile);
});
} else {
$output->writeln('<info>xdebug extension found in extension path.</info>');
}
$output->writeln("<info>using $xdebugFile as xdebug extension.</info>");
$phpunitPath = trim(shell_exec('which phpunit'));
$command = sprintf('php -d zend_extension=%s %s', $xdebugFile, $phpunitPath);
}
if ($input->getOption('xhprof')) {
Profiler::setupProfilerXHProf($isMainRun = true);
putenv('PIWIK_USE_XHPROF=1');
}
$testFile = $input->getOption('file');
if (!empty($testFile)) {
$this->executeTestFile($testFile, $options, $command, $output);
} else {
$suite = $input->getOption('suite');
$this->executeTestGroups($suite, $groups, $options, $command, $output);
}
}
private function executeTestFile($testFile, $options, $command, OutputInterface $output)
{
$params = $options . " " . $testFile;
$cmd = sprintf("cd %s/tests/PHPUnit && %s %s", PIWIK_DOCUMENT_ROOT, $command, $params);
$output->writeln('Executing command: <info>' . $cmd . '</info>');
passthru($cmd);
$output->writeln("");
}
private function executeTestGroups($suite, $groups, $options, $command, OutputInterface $output)
{
if (empty($groups)) {
$groups = $this->getTestsGroups();
}
foreach ($groups as $group) {
$params = '--group ' . $group . ' ' . str_replace('%group%', $group, $options);
if (!empty($suite)) {
$params .= ' --testsuite ' . $suite;
}
$cmd = sprintf('cd %s/tests/PHPUnit && %s %s', PIWIK_DOCUMENT_ROOT, $command, $params);
$output->writeln('Executing command: <info>' . $cmd . '</info>');
passthru($cmd);
$output->writeln("");
}
}
private function getTestsGroups()
{
return array('Core', 'Plugins', 'UI');
}
}
|