Completed
Push — master ( 8c06ff...a2f0f3 )
by Julian
02:34
created

TestMethod   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 87
ccs 8
cts 16
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

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