diff --git a/src/Logging/TeamCity/Converter.php b/src/Logging/Converter.php similarity index 96% rename from src/Logging/TeamCity/Converter.php rename to src/Logging/Converter.php index 2a2cce4f..7cbf8f12 100644 --- a/src/Logging/TeamCity/Converter.php +++ b/src/Logging/Converter.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pest\Logging\TeamCity; +namespace Pest\Logging; use NunoMaduro\Collision\Adapters\Phpunit\State; use Pest\Exceptions\ShouldNotHappen; @@ -150,6 +150,14 @@ final class Converter return Str::after($name, self::PREFIX); } + /** + * Gets the trimmed test class name. + */ + public function getTrimmedTestClassName(TestMethod $test): string + { + return Str::after($test->className(), self::PREFIX); + } + /** * Gets the test suite location. */ diff --git a/src/Logging/JUnit/Converter.php b/src/Logging/JUnit/Converter.php deleted file mode 100644 index e31fb1ff..00000000 --- a/src/Logging/JUnit/Converter.php +++ /dev/null @@ -1,183 +0,0 @@ -testDox()->prettifiedMethodName(); - } - - /** - * Gets the test case location. - */ - public function getTestCaseLocation(Test $test, bool $withDescription = false): string - { - if (! $test instanceof TestMethod) { - throw ShouldNotHappen::fromMessage('Not an instance of TestMethod'); - } - - $path = $test->testDox()->prettifiedClassName(); - $relativePath = $this->toRelativePath($path); - - // TODO: Get the description without the dataset. - $description = $test->testDox()->prettifiedMethodName(); - - if (! $withDescription) { - return $relativePath; - } - - return "$relativePath::$description"; - } - - /** - * Gets the trimmed test class name. - */ - public function getTrimmedTestClassName(TestMethod $test): string - { - return Str::after($test->className(), self::PREFIX); - } - - /** - * Gets the exception message. - */ - public function getExceptionMessage(Throwable $throwable): string - { - if (is_a($throwable->className(), FrameworkException::class, true)) { - return $throwable->message(); - } - - $buffer = $throwable->className(); - $throwableMessage = $throwable->message(); - - if ($throwableMessage !== '') { - $buffer .= ": $throwableMessage"; - } - - return $buffer; - } - - /** - * Gets the exception details. - */ - public function getExceptionDetails(Throwable $throwable): string - { - $buffer = $this->getStackTrace($throwable); - - while ($throwable->hasPrevious()) { - $throwable = $throwable->previous(); - - $buffer .= sprintf( - "\nCaused by\n%s\n%s", - $throwable->description(), - $this->getStackTrace($throwable) - ); - } - - return $buffer; - } - - /** - * Gets the stack trace. - */ - public function getStackTrace(Throwable $throwable): string - { - $stackTrace = $throwable->stackTrace(); - - // Split stacktrace per frame. - $frames = explode("\n", $stackTrace); - - // Remove empty lines - $frames = array_filter($frames); - - // clean the paths of each frame. - $frames = array_map( - fn (string $frame): string => $this->toRelativePath($frame), - $frames - ); - - // Format stacktrace as `at ` - $frames = array_map( - fn (string $frame) => "at $frame", - $frames - ); - - return implode("\n", $frames); - } - - /** - * Gets the test suite name. - */ - public function getTestSuiteName(TestSuite $testSuite): string - { - $name = $testSuite->name(); - - if (str_starts_with($name, self::PREFIX)) { - return Str::after($name, self::PREFIX); - } - - return Str::after($name, $this->rootPath); - } - - /** - * Gets the test suite location. - */ - public function getTestSuiteLocation(TestSuite $testSuite): ?string - { - $tests = $testSuite->tests()->asArray(); - - // TODO: figure out how to get the file path without a test being there. - if ($tests === []) { - return null; - } - - $firstTest = $tests[0]; - if (! $firstTest instanceof TestMethod) { - throw ShouldNotHappen::fromMessage('Not an instance of TestMethod'); - } - - $path = $firstTest->testDox()->prettifiedClassName(); - - return $this->toRelativePath($path); - } - - /** - * Transforms the given path in relative path. - */ - private function toRelativePath(string $path): string - { - // Remove cwd from the path. - return str_replace("$this->rootPath".DIRECTORY_SEPARATOR, '', $path); - } -} diff --git a/src/Logging/JUnit/JUnitLogger.php b/src/Logging/JUnit/JUnitLogger.php index 68ce1756..497fe8ef 100644 --- a/src/Logging/JUnit/JUnitLogger.php +++ b/src/Logging/JUnit/JUnitLogger.php @@ -6,6 +6,7 @@ namespace Pest\Logging\JUnit; use DOMDocument; use DOMElement; +use Pest\Logging\Converter; use Pest\Logging\JUnit\Subscriber\TestErroredSubscriber; use Pest\Logging\JUnit\Subscriber\TestFailedSubscriber; use Pest\Logging\JUnit\Subscriber\TestFinishedSubscriber; diff --git a/src/Logging/TeamCity/TeamCityLogger.php b/src/Logging/TeamCity/TeamCityLogger.php index 92d35daf..072bd608 100644 --- a/src/Logging/TeamCity/TeamCityLogger.php +++ b/src/Logging/TeamCity/TeamCityLogger.php @@ -6,6 +6,7 @@ namespace Pest\Logging\TeamCity; use NunoMaduro\Collision\Adapters\Phpunit\Style; use Pest\Exceptions\ShouldNotHappen; +use Pest\Logging\Converter; use Pest\Logging\TeamCity\Subscriber\TestConsideredRiskySubscriber; use Pest\Logging\TeamCity\Subscriber\TestErroredSubscriber; use Pest\Logging\TeamCity\Subscriber\TestExecutionFinishedSubscriber; diff --git a/src/Subscribers/EnsureJunitEnabled.php b/src/Subscribers/EnsureJunitEnabled.php index 79c78eee..b6f6a339 100644 --- a/src/Subscribers/EnsureJunitEnabled.php +++ b/src/Subscribers/EnsureJunitEnabled.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Pest\Subscribers; +use Pest\Logging\Converter; use Pest\Logging\JUnit\JUnitLogger; -use Pest\Logging\JUnit\Converter; use Pest\Support\Container; use Pest\TestSuite; use PHPUnit\Event\TestRunner\Configured; diff --git a/src/Subscribers/EnsureTeamCityEnabled.php b/src/Subscribers/EnsureTeamCityEnabled.php index bd6b1bf2..264800c0 100644 --- a/src/Subscribers/EnsureTeamCityEnabled.php +++ b/src/Subscribers/EnsureTeamCityEnabled.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Pest\Subscribers; -use Pest\Logging\TeamCity\Converter; +use Pest\Logging\Converter; use Pest\Logging\TeamCity\TeamCityLogger; use Pest\TestSuite; use PHPUnit\Event\TestRunner\Configured;