Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUnit report logger #458

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"int",
"null",
"PHPUnit\\Event\\Code\\Test",
"PHPUnit\\Event\\Code\\TestMethod",
"PHPUnit\\Event\\Test\\Finished",
"PHPUnit\\Event\\Test\\FinishedSubscriber",
"PHPUnit\\Event\\Test\\PreparationStarted",
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-dom": "*",
"phpunit/phpunit": "^7.5.0 || ^8.5.19 || ^9.0.0 || ^10.0.0"
},
"require-dev": {
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<code>$options</code>
</UnusedParam>
</file>
<file src="src/FileWriter/DefaultFileWriter.php">
<InvalidArgument>
<code>static function (): void {
}</code>
</InvalidArgument>
</file>
<file src="src/Formatter/DefaultDurationFormatter.php">
<InvalidOperand>
<code><![CDATA[$duration->seconds() * 1_000 + $duration->nanoseconds() / 1_000_000]]></code>
Expand All @@ -21,6 +27,27 @@
<code>($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1_000</code>
</InvalidOperand>
</file>
<file src="src/Logger/LoggerFactory.php">
<MixedInferredReturnType>
<code>Logger</code>
</MixedInferredReturnType>
<UndefinedClass>
<code>Runner\Extension\ParameterCollection</code>
<code>TextUI\Configuration\Configuration</code>
</UndefinedClass>
<UnusedClass>
<code>LoggerFactory</code>
</UnusedClass>
<UnusedParam>
<code>$configuration</code>
<code>$parameters</code>
</UnusedParam>
</file>
<file src="src/Reporter/NullReporter.php">
<UnusedClass>
<code>NullReporter</code>
</UnusedClass>
</file>
<file src="src/Subscriber/Test/FinishedSubscriber.php">
<UndefinedClass>
<code>Event\Test\FinishedSubscriber</code>
Expand Down Expand Up @@ -57,6 +84,7 @@
<UnusedParam>
<code>$collector</code>
<code>$event</code>
<code>$logger</code>
<code>$reporter</code>
</UnusedParam>
</file>
Expand Down Expand Up @@ -183,6 +211,21 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/Logger/WithConfigurationAndOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version10/TestCase/Bare/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -323,6 +366,21 @@
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/Logger/WithConfigurationAndOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanDefaultMaximumDuration</code>
</PossiblyUnusedMethod>
</file>
<file src="test/EndToEnd/Version11/TestCase/Bare/SleeperTest.php">
<PossiblyUnusedMethod>
<code>provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration</code>
Expand Down Expand Up @@ -409,9 +467,24 @@
<code><![CDATA[$this->milliseconds * 1000]]></code>
</ArgumentTypeCoercion>
</file>
<file src="test/Unit/Collector/DefaultCollectorTest.php">
<MixedArgument>
<code><![CDATA[$faker->filePath()]]></code>
<code><![CDATA[$faker->filePath()]]></code>
<code><![CDATA[$faker->filePath()]]></code>
<code><![CDATA[$faker->filePath()]]></code>
<code><![CDATA[$faker->filePath()]]></code>
<code><![CDATA[$faker->filePath()]]></code>
</MixedArgument>
</file>
<file src="test/Unit/Reporter/DefaultReporterTest.php">
<MixedInferredReturnType>
<code>iterable</code>
</MixedInferredReturnType>
</file>
<file src="test/Unit/SlowTestTest.php">
<MixedArgument>
<code><![CDATA[$faker->filePath()]]></code>
</MixedArgument>
</file>
</files>
25 changes: 25 additions & 0 deletions src/Exception/FailedToCreateDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class FailedToCreateDirectory extends \RuntimeException
{
public static function forDirectory(string $directory): self
{
return new self(\sprintf('Directory "%s" was not created.', $directory));
}
}
25 changes: 25 additions & 0 deletions src/Exception/FailedToWriteFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class FailedToWriteFile extends \RuntimeException
{
public static function forFile(string $file): self
{
return new self(\sprintf('Failed to write to file "%s".', $file));
}
}
25 changes: 25 additions & 0 deletions src/Exception/InvalidFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class InvalidFile extends \InvalidArgumentException
{
public static function blankOrEmpty(): self
{
return new self('Value cannot be blank or empty.');
}
}
25 changes: 25 additions & 0 deletions src/Exception/InvalidTestFileLine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class InvalidTestFileLine extends \InvalidArgumentException
{
public static function lesserThenOne(): self
{
return new self('Value cannot be lesser than one.');
}
}
25 changes: 25 additions & 0 deletions src/Exception/InvalidTestFilename.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2024 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/

namespace Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class InvalidTestFilename extends \InvalidArgumentException
{
public static function blankOrEmpty(): self
{
return new self('Value cannot be blank or empty.');
}
}
50 changes: 41 additions & 9 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
private Duration $maximumDuration;
private Collector\Collector $collector;
private Reporter\Reporter $reporter;
private Logger\Logger $logger;

Check warning on line 43 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L43

Added line #L43 was not covered by tests

public function __construct(array $options = [])
{
Expand All @@ -57,11 +58,16 @@

$this->maximumDuration = $maximumDuration;
$this->collector = new Collector\DefaultCollector();
$durationFormatter = new Formatter\DefaultDurationFormatter();

Check warning on line 61 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L61

Added line #L61 was not covered by tests
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$durationFormatter,

Check warning on line 63 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L63

Added line #L63 was not covered by tests
$maximumDuration,
$maximumCount,
);
$loggerFactory = new Logger\LoggerFactory(
$durationFormatter,
);
$this->logger = $loggerFactory->forArguments($_SERVER['argv'], $options);

Check warning on line 70 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L67-L70

Added lines #L67 - L70 were not covered by tests
}

public function executeBeforeFirstTest(): void
Expand Down Expand Up @@ -100,9 +106,11 @@
}

$testIdentifier = TestIdentifier::fromString($test);
$testFile = $this->resolveTestFile($test);

Check warning on line 109 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L109

Added line #L109 was not covered by tests

$slowTest = SlowTest::create(
$testIdentifier,
$testFile,

Check warning on line 113 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L113

Added line #L113 was not covered by tests
$duration,
$maximumDuration,
);
Expand All @@ -120,6 +128,8 @@

$slowTests = $this->collector->collected();

$this->logger->log(...$slowTests);

Check warning on line 131 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L131

Added line #L131 was not covered by tests

if ([] === $slowTests) {
return;
}
Expand Down Expand Up @@ -178,6 +188,22 @@

return $this->maximumDuration;
}

private function resolveTestFile(string $test): TestFile
{
$test = \explode(' ', $test, 2)[0];

Check warning on line 194 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L193-L194

Added lines #L193 - L194 were not covered by tests

try {
$methodReflection = new \ReflectionMethod($test);
} catch (\ReflectionException $e) {
return TestFile::fromFilename($test);
}

Check warning on line 200 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L196-L200

Added lines #L196 - L200 were not covered by tests

return TestFile::fromFilenameAndLine(
$methodReflection->getFileName(),
$methodReflection->getStartLine(),
);
}

Check warning on line 206 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L202-L206

Added lines #L202 - L206 were not covered by tests
}

return;
Expand All @@ -194,10 +220,6 @@
Runner\Extension\Facade $facade,
Runner\Extension\ParameterCollection $parameters
): void {
if ($configuration->noOutput()) {
return;
}

$maximumCount = Count::fromInt(10);

if ($parameters->has('maximum-count')) {
Expand All @@ -212,10 +234,16 @@

$timeKeeper = new TimeKeeper();
$collector = new Collector\DefaultCollector();
$reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$maximumDuration,
$maximumCount,
$durationFormatter = new Formatter\DefaultDurationFormatter();
$reporter = $configuration->noOutput()
? new Reporter\NullReporter()
: new Reporter\DefaultReporter(
$durationFormatter,
$maximumDuration,
$maximumCount,
);
$loggerFactory = new Logger\LoggerFactory(
$durationFormatter,

Check warning on line 246 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L237-L246

Added lines #L237 - L246 were not covered by tests
);

$facade->registerSubscribers(
Expand All @@ -228,6 +256,10 @@
new Subscriber\TestRunner\ExecutionFinishedSubscriber(
$collector,
$reporter,
$loggerFactory->forConfiguration(
$configuration,
$parameters,
),

Check warning on line 262 in src/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Extension.php#L259-L262

Added lines #L259 - L262 were not covered by tests
),
);
}
Expand Down
Loading