Skip to content

Commit c212c65

Browse files
committed
Remove setFixtureContent
Replace with option to mock the fixture path instead.
1 parent 4a519fe commit c212c65

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

moodle/Tests/MoodleCSBaseTestCase.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ abstract class MoodleCSBaseTestCase extends \PHPUnit\Framework\TestCase
5858
*/
5959
protected ?string $fixture = null;
6060

61-
/** @var string|null Fixture file content */
62-
protected ?string $fixtureContent = null;
61+
/** @var string|null A path name to mock for the fixture */
62+
protected ?string $fixtureFileName = null;
6363

6464
/**
6565
* @var array custom config elements to setup before running phpcs. name => value.
@@ -88,6 +88,8 @@ protected function tearDown(): void {
8888
$this->sniff = null;
8989
$this->errors = null;
9090
$this->warnings = null;
91+
$this->fixture = null;
92+
$this->fixtureFileName = null;
9193
// Reset any mocked component mappings.
9294
\MoodleHQ\MoodleCS\moodle\Util\MoodleUtil::setMockedComponentMappings([]);
9395
// If there is any custom config setup, remove it.
@@ -144,22 +146,17 @@ protected function setSniff($sniff) {
144146
* Set the full path to the file used as input.
145147
*
146148
* @param string $fixture full path to the file used as input (fixture).
149+
* @param string|null $fileName A path name to mock for the fixture. If not specified, the fixture filepath is used.
147150
*/
148-
protected function setFixture($fixture) {
149-
if ($this->fixtureContent !== null) {
150-
$this->fail('Fixture file content already set, cannot set it again.');
151-
}
151+
protected function setFixture(
152+
string $fixture,
153+
?string $fileName = null
154+
) {
152155
if (!is_readable($fixture)) {
153156
$this->fail('Unreadable fixture passed: ' . $fixture);
154157
}
155158
$this->fixture = $fixture;
156-
}
157-
158-
protected function setFixtureFileContent(string $content): void {
159-
if ($this->fixture !== null) {
160-
$this->fail('Fixture file content already set, cannot set it again.');
161-
}
162-
$this->fixtureContent = $content;
159+
$this->fixtureFileName = $fileName;
163160
}
164161

165162
/**
@@ -223,8 +220,13 @@ protected function verifyCsResults() {
223220

224221
// Let's process the fixture.
225222
try {
226-
if ($this->fixtureContent !== null) {
227-
$phpcsfile = new \PHP_CodeSniffer\Files\DummyFile($this->fixtureContent, $ruleset, $config);
223+
if ($this->fixtureFileName !== null) {
224+
$fixtureSource = file_get_contents($this->fixture);
225+
$fixtureContent = <<<EOF
226+
phpcs_input_file: {$this->fixtureFileName}
227+
{$fixtureSource}
228+
EOF;
229+
$phpcsfile = new \PHP_CodeSniffer\Files\DummyFile($fixtureContent, $ruleset, $config);
228230
} else {
229231
$phpcsfile = new \PHP_CodeSniffer\Files\LocalFile($this->fixture, $ruleset, $config);
230232
}

moodle/Tests/Sniffs/Commenting/MissingDocblockSniffTest.php

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ class MissingDocblockSniffTest extends MoodleCSBaseTestCase
3737
*/
3838
public function testMissingDocblockSniff(
3939
string $fixture,
40+
?string $fixtureFilename,
4041
array $errors,
4142
array $warnings
4243
): void {
4344
$this->setStandard('moodle');
4445
$this->setSniff('moodle.Commenting.MissingDocblock');
45-
$this->setFixture(sprintf("%s/fixtures/%s.php", __DIR__, $fixture));
46+
$this->setFixture(sprintf("%s/fixtures/%s.php", __DIR__, $fixture), $fixtureFilename);
4647
$this->setWarnings($warnings);
4748
$this->setErrors($errors);
4849
$this->setComponentMapping([
@@ -56,6 +57,7 @@ public static function docblockCorrectnessProvider(): array {
5657
$cases = [
5758
'Multiple artifacts in a file' => [
5859
'fixture' => 'missing_docblock_multiple_artifacts',
60+
'fixtureFilename' => null,
5961
'errors' => [
6062
1 => 'Missing docblock for file missing_docblock_multiple_artifacts.php',
6163
34 => 'Missing docblock for function missing_docblock_in_function',
@@ -75,95 +77,79 @@ public static function docblockCorrectnessProvider(): array {
7577
],
7678
'File level tag, no class' => [
7779
'fixture' => 'missing_docblock_class_without_docblock',
80+
'fixtureFilename' => null,
7881
'errors' => [
7982
11 => 'Missing docblock for class class_without_docblock',
8083
],
8184
'warnings' => [],
8285
],
8386
'Class only (incorrect whitespace)' => [
8487
'fixture' => 'missing_docblock_class_only_with_incorrect_whitespace',
88+
'fixtureFilename' => null,
8589
'errors' => [
8690
11 => 'Missing docblock for class class_only_with_incorrect_whitespace',
8791
],
8892
'warnings' => [],
8993
],
9094
'Class only (correct)' => [
9195
'fixture' => 'missing_docblock_class_only',
96+
'fixtureFilename' => null,
9297
'errors' => [],
9398
'warnings' => [],
9499
],
95100
'Class only with attributes (correct)' => [
96101
'fixture' => 'missing_docblock_class_only_with_attributes',
102+
'fixtureFilename' => null,
97103
'errors' => [],
98104
'warnings' => [],
99105
],
100106
'Class only with attributes and incorrect whitespace' => [
101107
'fixture' => 'missing_docblock_class_only_with_attributes_incorrect_whitespace',
108+
'fixtureFilename' => null,
102109
'errors' => [
103110
13 => 'Missing docblock for class class_only_with_attributes_incorrect_whitespace',
104111
],
105112
'warnings' => [],
106113
],
107114
'Class and file (correct)' => [
108115
'fixture' => 'missing_docblock_class_and_file',
116+
'fixtureFilename' => null,
109117
'errors' => [],
110118
'warnings' => [],
111119
],
112120
'Interface only (correct)' => [
113121
'fixture' => 'missing_docblock_interface_only',
122+
'fixtureFilename' => null,
114123
'errors' => [],
115124
'warnings' => [],
116125
],
117126
'Trait only (correct)' => [
118127
'fixture' => 'missing_docblock_trait_only',
128+
'fixtureFilename' => null,
119129
'errors' => [],
120130
'warnings' => [],
121131
],
132+
'Testcase' => [
133+
'fixture' => 'MissingDocblock/testcase_class',
134+
'fixtureFilename' => '/lib/tests/example_test.php',
135+
'errors' => [
136+
3 => 'Missing docblock for class example_test',
137+
],
138+
'warnings' => [
139+
12 => 'Missing docblock for function test_the_thing',
140+
],
141+
],
122142
];
123143

124144
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
125145
$cases['Enum only (correct)'] = [
126146
'fixture' => 'missing_docblock_enum_only',
147+
'fixtureFilename' => null,
127148
'errors' => [],
128149
'warnings' => [],
129150
];
130151
}
131152

132153
return $cases;
133154
}
134-
135-
public function testMissingDocblockSniffWithTest(): void {
136-
$content = <<<EOF
137-
phpcs_input_file: /lib/tests/example_test.php
138-
<?php
139-
140-
class example_test extends advanced_testcase {
141-
public function setUp(): void {
142-
}
143-
public function tearDown(): void {
144-
}
145-
public static function setUpBeforeClass(): void {
146-
}
147-
public static function tearDownAfterClass(): void {
148-
}
149-
public function test_the_thing(): void {
150-
}
151-
}
152-
EOF;
153-
154-
$this->setStandard('moodle');
155-
$this->setSniff('moodle.Commenting.MissingDocblock');
156-
$this->setFixtureFileContent($content);
157-
$this->setWarnings([
158-
12 => 'Missing docblock for function test_the_thing',
159-
]);
160-
$this->setErrors([
161-
3 => 'Missing docblock for class example_test',
162-
]);
163-
$this->setComponentMapping([
164-
'local_codechecker' => dirname(__DIR__),
165-
]);
166-
167-
$this->verifyCsResults();
168-
}
169155
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
class example_test extends advanced_testcase {
4+
public function setUp(): void {
5+
}
6+
public function tearDown(): void {
7+
}
8+
public static function setUpBeforeClass(): void {
9+
}
10+
public static function tearDownAfterClass(): void {
11+
}
12+
public function test_the_thing(): void {
13+
}
14+
}

0 commit comments

Comments
 (0)