Skip to content

Commit b1e9ac7

Browse files
IN-1729 - Add TestRunner to separate class from side effects in runner.php
1 parent c6371bb commit b1e9ac7

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

tests/TestRunner.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flyeralarm\Sniffer\Tests;
6+
7+
class TestRunner
8+
{
9+
/**
10+
* @param string $dirPath
11+
*/
12+
public function processDir(string $dirPath)
13+
{
14+
$hasError = $this->recursiveDirProcess($dirPath);
15+
if ($hasError) {
16+
exit(1);
17+
}
18+
exit(0);
19+
}
20+
21+
public function recursiveDirProcess(string $dirPath, bool $hasError = false): bool
22+
{
23+
$dir = opendir($dirPath);
24+
while (($file = readdir($dir)) !== false) {
25+
if (strpos($file, '.') === 0) {
26+
continue;
27+
}
28+
if (is_dir($dirPath . $file)) {
29+
$hasError = $this->recursiveDirProcess($dirPath . $file . '/', $hasError);
30+
continue;
31+
}
32+
33+
$fileContent = file_get_contents($dirPath . $file);
34+
$snifferOutput = shell_exec(
35+
sprintf(
36+
"%s -w -p -s --report-width=120 --standard=%s %s",
37+
escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'),
38+
escapeshellarg(__DIR__ . '/ruleset.xml'),
39+
escapeshellarg($dirPath . $file)
40+
)
41+
);
42+
43+
// expectedPass
44+
if (preg_match('|//\s@expectedPass$|m', $fileContent)) {
45+
if (preg_match('|^FOUND.*AFFECTING.*LINE|m', $snifferOutput) === 0) {
46+
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
47+
continue;
48+
}
49+
50+
$hasError = true;
51+
echo "ERROR - [" . $dirPath . $file . "]:'
52+
. ' Test was expected to fully pass. Result: " . PHP_EOL . $snifferOutput . PHP_EOL;
53+
continue;
54+
}
55+
56+
// expectedError
57+
preg_match('|//\s@expectedError\s(.*)$|m', $fileContent, $expectedMatch);
58+
if (count($expectedMatch) !== 2) {
59+
echo 'WARNING - [' . $dirPath . $file . ']:'
60+
. ' File must contain exactly one "@expectedError <EXPECTATION MESSAGE>"'
61+
. ' or "@expectedPass" comment' . PHP_EOL;
62+
continue;
63+
}
64+
$expected = $expectedMatch[1];
65+
if (preg_match('/ERROR\s\|\s?[\[\]x\s]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) {
66+
$hasError = true;
67+
echo 'ERROR - [' . $dirPath . $file . ']:'
68+
. ' Expectation <<' . $expected . '>>'
69+
. ' not found in result: ' . PHP_EOL . $snifferOutput . PHP_EOL;
70+
} else {
71+
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
72+
}
73+
}
74+
return $hasError;
75+
}
76+
}

tests/runner.php

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,5 @@
11
<?php
22

3-
$hasError = false;
4-
processDir(__DIR__ . '/rules/');
5-
if ($hasError) {
6-
exit(1);
7-
}
8-
exit(0);
3+
require __DIR__ . '/TestRunner.php';
94

10-
function processDir($dirPath)
11-
{
12-
global $hasError;
13-
$dir = opendir($dirPath);
14-
while (($file = readdir($dir)) !== false) {
15-
if (strpos($file, '.') === 0) {
16-
continue;
17-
}
18-
if (is_dir($dirPath . $file)) {
19-
processDir($dirPath . $file . '/');
20-
continue;
21-
}
22-
23-
$fileContent = file_get_contents($dirPath . $file);
24-
$snifferOutput = shell_exec(
25-
sprintf(
26-
"%s -w -p -s --report-width=120 --standard=%s %s",
27-
escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'),
28-
escapeshellarg(__DIR__ . '/ruleset.xml'),
29-
escapeshellarg($dirPath . $file)
30-
)
31-
);
32-
33-
// expectedPass
34-
if (preg_match('|//\s@expectedPass$|m', $fileContent)) {
35-
if (preg_match('|^FOUND.*AFFECTING.*LINE|m', $snifferOutput) === 0) {
36-
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
37-
continue;
38-
}
39-
40-
$hasError = true;
41-
echo 'ERROR - [' . $dirPath . $file . ']: Test was expected to fully pass. Result: ' . PHP_EOL . $snifferOutput . PHP_EOL;
42-
continue;
43-
}
44-
45-
// expectedError
46-
preg_match('|//\s@expectedError\s(.*)$|m', $fileContent, $expectedMatch);
47-
if (count($expectedMatch) !== 2) {
48-
echo 'WARNING - [' . $dirPath . $file . ']: File must contain exactly one "@expectedError <EXPECTATION MESSAGE>" or "@expectedPass" comment' . PHP_EOL;
49-
continue;
50-
}
51-
$expected = $expectedMatch[1];
52-
if (preg_match('/ERROR\s\|\s?[\[\]x\s]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) {
53-
$hasError = true;
54-
echo 'ERROR - [' . $dirPath . $file . ']: Expectation <<' . $expected . '>> not found in result: ' . PHP_EOL . $snifferOutput . PHP_EOL;
55-
} else {
56-
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
57-
}
58-
}
59-
}
5+
(new \Flyeralarm\Sniffer\Tests\TestRunner())->processDir(__DIR__ . '/rules/');

0 commit comments

Comments
 (0)