Skip to content

Commit 9d16bf1

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

File tree

3 files changed

+78
-56
lines changed

3 files changed

+78
-56
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"require": {
1818
"squizlabs/php_codesniffer": "^3.5"
1919
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Flyeralarm\\Sniffer\\Tests\\": "tests/"
23+
}
24+
},
2025
"bin": [
2126
"bin/php-code-validator"
2227
]

tests/TestRunner.php

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

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_once __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\TestRunner())->processDir(__DIR__ . '/rules/');

0 commit comments

Comments
 (0)