Skip to content

Commit 468155c

Browse files
authored
Merge pull request #141 from stronk7/coverage_php_has_precedence
Coverage php has precedence
2 parents 8eaf716 + e2ecaa7 commit 468155c

File tree

7 files changed

+183
-20
lines changed

7 files changed

+183
-20
lines changed

.php-cs-fixer.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1111
*/
1212

13-
$header = <<<'EOF'
14-
This file is part of the Moodle Plugin CI package.
15-
16-
For the full copyright and license information, please view the LICENSE
17-
file that was distributed with this source code.
18-
19-
Copyright (c) 2018 Blackboard Inc. (http://www.blackboard.com)
20-
License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21-
EOF;
22-
2313
return (new PhpCsFixer\Config())
2414
->setRiskyAllowed(true)
2515
->setRules([
@@ -33,7 +23,6 @@
3323
'expectedExceptionMessage',
3424
'expectedExceptionMessageRegExp',
3525
]],
36-
'header_comment' => ['header' => $header],
3726
'heredoc_to_nowdoc' => true,
3827
'no_extra_blank_lines' => ['tokens' => [
3928
'break', 'continue', 'extra', 'return',

src/Installer/TestSuiteInstaller.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,26 @@ public function injectPHPUnitFilter()
146146
return;
147147
}
148148

149+
// If the plugin already has a tests/coverage.php file, then phpunit.xml filter/coverage
150+
// section is already configured following it. Nothing to do here.
151+
$coverage = $this->plugin->directory.'/tests/coverage.php';
152+
// If the file exists and we are Moodle >= 3.7.
153+
// TODO: Remove the branch condition when 3.6 becomes unsupported by moodle-local-ci.
154+
if ($this->moodle->getBranch() >= 37 && is_readable($coverage)) {
155+
return;
156+
}
157+
149158
$files = $this->getCoverageFiles();
150159
$filterXml = $this->getFilterXml($files);
151160
$subject = file_get_contents($config);
152161
$count = 0;
153162

154163
// Replace existing filter.
155-
$contents = preg_replace('/<filter>(.|\n)*<\/filter>/m', trim($filterXml), $subject, 1, $count);
164+
$contents = preg_replace('/<coverage>(.|\n)*<\/coverage>/m', trim($filterXml), $subject, 1, $count);
165+
// TODO: Remove this when 3.10 becomes unsupported by moodle-local-ci.
166+
if ($this->moodle->getBranch() < 311) {
167+
$contents = preg_replace('/<filter>(.|\n)*<\/filter>/m', trim($filterXml), $subject, 1, $count);
168+
}
156169

157170
// Or if no existing filter, inject the filter.
158171
if ($count === 0) {
@@ -176,6 +189,7 @@ private function getCoverageFiles()
176189
{
177190
$finder = Finder::create()
178191
->name('*.php')
192+
->notName('coverage.php')
179193
->notName('*_test.php')
180194
->notName('version.php')
181195
->notName('settings.php')
@@ -231,19 +245,33 @@ private function removeDbFiles($dbPath, array $files)
231245
*/
232246
private function getFilterXml(array $files)
233247
{
248+
// Default (Moodle 3.11 and above) template (PHPUnit 9.5 and up).
249+
$template = <<<'XML'
250+
<coverage>
251+
<include>
252+
{{includes}}
253+
</include>
254+
</coverage>
255+
XML;
256+
// Before Moodle 3.11 (PHPUnit < 9.5), it was filter & white-list.
257+
// TODO: Remove this when 3.10 becomes unsupported by moodle-local-ci.
258+
if ($this->moodle->getBranch() < 311) {
259+
$template = <<<'XML'
260+
<filter>
261+
<whitelist addUncoveredFilesFromWhitelist="true">
262+
{{includes}}
263+
</whitelist>
264+
</filter>
265+
XML;
266+
}
267+
268+
// Let's add the found php files to become code-coverage analysed.
234269
$includes = [];
235270
foreach ($files as $file) {
236271
$includes[] = sprintf('<file>%s</file>', $file);
237272
}
238273
$includes = implode("\n ", $includes);
239274

240-
return <<<XML
241-
<filter>
242-
<whitelist addUncoveredFilesFromWhitelist="true">
243-
$includes
244-
</whitelist>
245-
</filter>
246-
247-
XML;
275+
return str_replace('{{includes}}', $includes, $template);
248276
}
249277
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
// This file is part of the Moodle Plugin CI package.
4+
//
5+
// Moodle 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+
// Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>.
17+
18+
namespace MoodlePluginCI\Tests\Fake\Bridge;
19+
20+
/**
21+
* Dummy Moodle Fixture class to verify stuff like it's a plugin running for Moodle 3.11.
22+
*
23+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
24+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
class DummyMoodle311 extends DummyMoodle
27+
{
28+
public $branch = 311;
29+
}

tests/Fixture/phpunit/coverage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This is a fake coverage.php file. Their contents aren't important at all, just its existence is.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="../../lib/phpunit/phpunit.xsd"
5+
bootstrap="../../lib/phpunit/bootstrap.php"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
backupGlobals="false"
11+
backupStaticAttributes="false"
12+
stopOnError="false"
13+
stopOnFailure="false"
14+
stopOnIncomplete="false"
15+
stopOnSkipped="false"
16+
printerClass="Hint_ResultPrinter"
17+
testSuiteLoaderClass="phpunit_autoloader"
18+
>
19+
20+
<php>
21+
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
22+
<const name="PHPUNIT_SEQUENCE_START" value="168000"/>
23+
24+
<!--Following constants instruct tests to fetch external test files from alternative location or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
25+
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
26+
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
27+
</php>
28+
29+
30+
<testsuites>
31+
<testsuite name="component_testsuite">
32+
<directory suffix="_test.php">.</directory>
33+
</testsuite>
34+
</testsuites>
35+
<coverage>
36+
<include>
37+
<directory suffix=".php">.</directory>
38+
</include>
39+
<exclude>
40+
<directory suffix="_test.php">.</directory>
41+
</exclude>
42+
</coverage>
43+
44+
</phpunit>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="../../lib/phpunit/phpunit.xsd"
5+
bootstrap="../../lib/phpunit/bootstrap.php"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
backupGlobals="false"
11+
backupStaticAttributes="false"
12+
stopOnError="false"
13+
stopOnFailure="false"
14+
stopOnIncomplete="false"
15+
stopOnSkipped="false"
16+
printerClass="Hint_ResultPrinter"
17+
testSuiteLoaderClass="phpunit_autoloader"
18+
>
19+
20+
<php>
21+
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
22+
<const name="PHPUNIT_SEQUENCE_START" value="168000"/>
23+
24+
<!--Following constants instruct tests to fetch external test files from alternative location or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
25+
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
26+
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/> uncomment and alter to fetch external test files from alternative location-->
27+
</php>
28+
29+
30+
<testsuites>
31+
<testsuite name="component_testsuite">
32+
<directory suffix="_test.php">.</directory>
33+
</testsuite>
34+
</testsuites>
35+
<coverage>
36+
<include>
37+
<file>classes/math.php</file>
38+
<file>lib.php</file>
39+
</include>
40+
</coverage>
41+
42+
</phpunit>

tests/Installer/TestSuiteInstallerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use MoodlePluginCI\Bridge\MoodlePlugin;
1616
use MoodlePluginCI\Installer\TestSuiteInstaller;
1717
use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodle;
18+
use MoodlePluginCI\Tests\Fake\Bridge\DummyMoodle311;
1819
use MoodlePluginCI\Tests\Fake\Process\DummyExecute;
1920
use MoodlePluginCI\Tests\MoodleTestCase;
2021
use Symfony\Component\Yaml\Yaml;
@@ -93,5 +94,34 @@ public function testPHPUnitXMLFile()
9394
$this->fs->copy(__DIR__.'/../Fixture/phpunit/phpunit-33.xml', $xmlFile, true);
9495
$installer->injectPHPUnitFilter();
9596
$this->assertXmlFileEqualsXmlFile(__DIR__.'/../Fixture/phpunit/phpunit-expected.xml', $xmlFile);
97+
98+
// Test Moodle 3.3 PHPUnit when coverage.php is available.
99+
$this->fs->copy(__DIR__.'/../Fixture/phpunit/phpunit-33.xml', $xmlFile, true);
100+
$this->fs->copy(__DIR__.'/../Fixture/phpunit/coverage.php', $this->pluginDir.'/tests/coverage.php', true);
101+
$installer->injectPHPUnitFilter();
102+
// 3.3 did not support tests/coverage.php files, so defaults are applied normally.
103+
$this->assertXmlFileEqualsXmlFile(__DIR__.'/../Fixture/phpunit/phpunit-expected.xml', $xmlFile);
104+
}
105+
106+
public function testPHPUnitXMLFile311()
107+
{
108+
$xmlFile = $this->pluginDir.'/phpunit.xml';
109+
$installer = new TestSuiteInstaller(
110+
new DummyMoodle311(''),
111+
new MoodlePlugin($this->pluginDir),
112+
new DummyExecute()
113+
);
114+
115+
// Test Moodle 3.11 PHPUnit XML file.
116+
$this->fs->copy(__DIR__.'/../Fixture/phpunit/phpunit-311.xml', $xmlFile, true);
117+
$installer->injectPHPUnitFilter();
118+
$this->assertXmlFileEqualsXmlFile(__DIR__.'/../Fixture/phpunit/phpunit-expected-311.xml', $xmlFile);
119+
120+
// Test Moodle 3.11 PHPUnit XML file when coverage.php is available.
121+
$this->fs->copy(__DIR__.'/../Fixture/phpunit/phpunit-311.xml', $xmlFile, true);
122+
$this->fs->copy(__DIR__.'/../Fixture/phpunit/coverage.php', $this->pluginDir.'/tests/coverage.php', true);
123+
$installer->injectPHPUnitFilter();
124+
// 3.11 supports tests/coverage.php files, so nothing is changed, expected file is the original one.
125+
$this->assertXmlFileEqualsXmlFile(__DIR__.'/../Fixture/phpunit/phpunit-311.xml', $xmlFile);
96126
}
97127
}

0 commit comments

Comments
 (0)