From 84b1782b8b8c310e937f46318c5d4cc257c650ce Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Fri, 23 Oct 2026 16:32:45 +0100 Subject: [PATCH] run json output test --- src/Logging/JsonLogger.php | 2 ++ src/Plugin.php | 15 ++++++++++++++- tests/Plugin.php | 31 ++++++++++++++++++------------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Logging/JsonLogger.php b/src/Logging/JsonLogger.php index 017707e..f0a46c1 100644 --- a/src/Logging/JsonLogger.php +++ b/src/Logging/JsonLogger.php @@ -39,6 +39,8 @@ public function append(string $path, array $profanity): void */ public function output(): void { + usort($this->logs, fn (array $a, array $b): int => strcmp($a['file'], $b['file'])); + $json = json_encode([ 'format' => 'pest', 'result' => $this->logs, diff --git a/src/Plugin.php b/src/Plugin.php index 41de7ab..341e6fb 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -140,7 +140,7 @@ public function handleOriginalArguments(array $arguments): void Analyser::analyse( array_keys(iterator_to_array($files)), function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void { - $path = str_replace(TestSuite::getInstance()->rootPath.'/', '', $result->file); + $path = $this->relativePathFromRoot($result->file); $errors = $result->errors; if (empty($errors)) { @@ -198,4 +198,17 @@ public function exit(int $code): never { exit($code); } + + private function relativePathFromRoot(string $file): string + { + $root = str_replace('\\', '/', TestSuite::getInstance()->rootPath); + $file = str_replace('\\', '/', $file); + $root = rtrim($root, '/').'/'; + + if (str_starts_with($file, $root)) { + return substr($file, strlen($root)); + } + + return $file; + } } diff --git a/tests/Plugin.php b/tests/Plugin.php index 8f83a03..a2dbb36 100644 --- a/tests/Plugin.php +++ b/tests/Plugin.php @@ -158,19 +158,13 @@ public function exit(int $code): never ->and(file_get_contents(__DIR__.'/../test.json'))->json()->toMatchArray([ 'format' => 'pest', 'result' => [ - [ - 'file' => 'tests/Fixtures/Properties.php', - 'profanity' => [ - 'pr12(bollocks)', - ], - ], [ 'file' => 'tests/Fixtures/All.php', 'profanity' => [ - 'pr9(shit)', - 'pr11(shit)', - 'pr13(shit)', - 'pr15(shit)', + 'pr12(shit)', + 'pr14(shit)', + 'pr16(shit)', + 'pr18(shit)', ], ], [ @@ -185,11 +179,16 @@ public function exit(int $code): never 'pr12(fuck)', ], ], + [ + 'file' => 'tests/Fixtures/HasProfanityInAttributeAsArrayKeySnakeCase.php', + 'profanity' => [ + 'pr9(shit)', + ], + ], [ 'file' => 'tests/Fixtures/Language.php', 'profanity' => [ - 'pr7(møgso)', - 'pr8(bollocks)', + 'pr13(bollocks)', ], ], [ @@ -198,8 +197,14 @@ public function exit(int $code): never 'pr12(nobhead)', ], ], + [ + 'file' => 'tests/Fixtures/Properties.php', + 'profanity' => [ + 'pr12(bollocks)', + ], + ], ], ]); unlink(__DIR__.'/../test.json'); -})->todo(); +});