1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParaTest\Runners\PHPUnit; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class TestMethod. |
7
|
|
|
* |
8
|
|
|
* Represents a set of tests grouped in batch which can be passed to a single phpunit process. |
9
|
|
|
* Batch limited to run tests only from one php test case file. |
10
|
|
|
* Used for running ParaTest in functional mode. |
11
|
|
|
* |
12
|
|
|
* @todo Rename to Batch |
13
|
|
|
*/ |
14
|
|
|
class TestMethod extends ExecutableTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The path to the test case file. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $path; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A set of filters for test, they are merged into phpunit's --filter option. |
25
|
|
|
* |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
protected $filters; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* Passed filters must be unescaped and must represent test name, optionally including |
34
|
|
|
* dataset name (numeric or named). |
35
|
|
|
* |
36
|
|
|
* @param string $testPath path to phpunit test case file |
37
|
|
|
* @param string|string[] $filters array of filters or single filter |
38
|
|
|
*/ |
39
|
21 |
|
public function __construct($testPath, $filters) |
40
|
|
|
{ |
41
|
21 |
|
$this->path = $testPath; |
42
|
|
|
// for compatibility with other code (tests), which can pass string (one filter) |
43
|
|
|
// instead of array of filters |
44
|
21 |
|
$this->filters = (array) $filters; |
45
|
21 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns the test method's filters. |
49
|
|
|
* |
50
|
|
|
* @return string[] |
51
|
|
|
*/ |
52
|
|
|
public function getFilters() |
53
|
|
|
{ |
54
|
|
|
return $this->filters; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the test method's name. |
59
|
|
|
* |
60
|
|
|
* This method will join all filters via pipe character and return as string. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
3 |
|
public function getName() |
65
|
|
|
{ |
66
|
3 |
|
return implode('|', $this->filters); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Additional processing for options being passed to PHPUnit. |
71
|
|
|
* |
72
|
|
|
* This sets up the --filter switch used to run a single PHPUnit test method. |
73
|
|
|
* This method also provide escaping for method name to be used as filter regexp. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function prepareOptions($options) |
80
|
|
|
{ |
81
|
|
|
$re = array_reduce($this->filters, function ($r, $v) { |
82
|
|
|
$isDataSet = strpos($v, ' with data set ') !== false; |
83
|
|
|
|
84
|
|
|
return ($r ? $r . '|' : '') . preg_quote($v, '/') . ($isDataSet ? '$' : "(?:\s|\$)"); |
85
|
|
|
}); |
86
|
|
|
$options['filter'] = '/' . $re . '/'; |
87
|
|
|
|
88
|
|
|
return $options; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the expected count of tests to be executed. |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
1 |
|
public function getTestCount() |
97
|
|
|
{ |
98
|
1 |
|
return count($this->filters); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|