Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
TestFinder | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
5.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
find | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
4.00 |
1 | <?php |
2 | /** |
3 | * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org> |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | namespace MediaWiki\Tool\PatchCoverage; |
20 | |
21 | use Symfony\Component\Finder\Finder; |
22 | |
23 | /** |
24 | * Finds tests that cover provided classes |
25 | */ |
26 | class TestFinder { |
27 | |
28 | /** |
29 | * @var string |
30 | */ |
31 | private $testDir; |
32 | |
33 | /** |
34 | * @param string $testDir |
35 | */ |
36 | public function __construct( $testDir ) { |
37 | $this->testDir = $testDir; |
38 | } |
39 | |
40 | /** |
41 | * @param string[] $classes |
42 | * |
43 | * @return string[] Absolute filenames to tests |
44 | */ |
45 | public function find( array $classes ) { |
46 | if ( !$classes ) { |
47 | return []; |
48 | } |
49 | $regex = implode( '|', array_map( static function ( $class ) { |
50 | return preg_quote( $class ); |
51 | }, $classes ) ); |
52 | // Look for @covers, @coversDefaultClass |
53 | // There might be a leading \ or not |
54 | // After the class there can be ::method or end of line |
55 | $regex = "/@covers(DefaultClass)? (\\\\)?($regex)(::|$)/m"; |
56 | $finder = new Finder(); |
57 | $finder->files()->in( $this->testDir )->name( '*Test.php' ); |
58 | $found = []; |
59 | foreach ( $finder as $fileInfo ) { |
60 | $contents = $fileInfo->getContents(); |
61 | if ( preg_match( $regex, $contents ) === 1 ) { |
62 | $found[] = $fileInfo->getRealPath(); |
63 | } |
64 | } |
65 | |
66 | sort( $found ); |
67 | return $found; |
68 | } |
69 | } |