Skip to content

Commit 6c74c6f

Browse files
committed
Add some tests to phpcs new test-version option.
1 parent bebf7b0 commit 6c74c6f

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

tests/Command/CodeCheckerCommandTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp()
3434
$this->fs->dumpFile($this->pluginDir.'/.moodle-plugin-ci.yml', Yaml::dump($config));
3535
}
3636

37-
protected function executeCommand($pluginDir = null, $maxWarnings = -1)
37+
protected function executeCommand($pluginDir = null, $maxWarnings = -1, $testVersion = null)
3838
{
3939
if ($pluginDir === null) {
4040
$pluginDir = $this->pluginDir;
@@ -51,6 +51,10 @@ protected function executeCommand($pluginDir = null, $maxWarnings = -1)
5151
$options['--max-warnings'] = $maxWarnings;
5252
}
5353

54+
if (null !== $testVersion) {
55+
$options['--test-version'] = $testVersion;
56+
}
57+
5458
$commandTester = new CommandTester($application->find('codechecker'));
5559
$commandTester->execute($options);
5660

@@ -138,6 +142,68 @@ public function testExecuteWithWarningsAndThreshold()
138142
$this->assertSame(0, $commandTester->getStatusCode());
139143
}
140144

145+
public function testExecuteWithTestVersion()
146+
{
147+
// Let's add a file with some new and deprecated stuff, and verify that the test-version option affects to the outcome.
148+
$content = <<<'EOT'
149+
<?php // phpcs:disable moodle
150+
mb_str_split(); // New in PHP 7.4.
151+
ini_get('allow_url_include'); // Deprecated in PHP 7.4.
152+
ldap_count_references(); // New in PHP 8.0.
153+
pg_errormessage(); // Deprecated in PHP 8.0.
154+
$fb = new ReflectionFiber(); // New in PHP 8.1.
155+
ini_get('auto_detect_line_endings'); // Deprecated in PHP 8.1.
156+
openssl_cipher_key_length(); // New in PHP 8.2.
157+
utf8_encode(); // Deprecated in PHP 8.2.
158+
159+
EOT;
160+
$this->fs->dumpFile($this->pluginDir . '/test_versions.php', $content);
161+
162+
// By default, without specify test-version, only reports deprecation warnings and returns 0.
163+
$commandTester = $this->executeCommand($this->pluginDir, -1, null);
164+
$output = $commandTester->getDisplay();
165+
$this->assertSame(0, $commandTester->getStatusCode());
166+
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES/', $output);
167+
168+
// With test-version 7.4, reports 2 new errors and <= 7.4 specific warnings and returns 1.
169+
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4');
170+
$output = $commandTester->getDisplay();
171+
$this->assertSame(1, $commandTester->getStatusCode());
172+
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 1 WARNING AFFECTING 3 LINES/', $output);
173+
174+
// With test-version 8.0, reports 1 new errors and <= 8.0 specific warnings and returns 1.
175+
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.0');
176+
$output = $commandTester->getDisplay();
177+
$this->assertSame(1, $commandTester->getStatusCode());
178+
$this->assertMatchesRegularExpression('/FOUND 1 ERROR AND 2 WARNINGS AFFECTING 3 LINES/', $output);
179+
180+
// With test-version 8.1, reports 0 new errors and <= 8.1 specific warnings and returns 0.
181+
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.1');
182+
$output = $commandTester->getDisplay();
183+
$this->assertSame(0, $commandTester->getStatusCode());
184+
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES/', $output);
185+
186+
// With test-version 7.4-8.0, reports 2 new errors and <= 8.0 specific warnings and returns 1.
187+
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.0');
188+
$output = $commandTester->getDisplay();
189+
$this->assertSame(1, $commandTester->getStatusCode());
190+
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 2 WARNINGS AFFECTING 4 LINES/', $output);
191+
192+
// With test-version 7.4-8.1, reports 2 new errors and <= 8.1 specific warnings and returns 1.
193+
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.1');
194+
$output = $commandTester->getDisplay();
195+
$this->assertSame(1, $commandTester->getStatusCode());
196+
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 3 WARNINGS AFFECTING 5 LINES/', $output);
197+
198+
// With test-version 7.4- (open range), reports 2 new errors and <= 8.2 specific warnings and returns 1.
199+
// (note that it should be 1 more warning and 1 more error, but the PHPCompatibility sniffs are not
200+
// still ready for many of the PHP 8.2 changes. We'll amend this test when they are ready).
201+
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-');
202+
$output = $commandTester->getDisplay();
203+
$this->assertSame(1, $commandTester->getStatusCode());
204+
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 3 WARNINGS AFFECTING 5 LINES/', $output);
205+
}
206+
141207
public function testExecuteNoFiles()
142208
{
143209
// Just random directory with no PHP files.

0 commit comments

Comments
 (0)