Skip to content
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 @@ final class Extension implements
private Duration $maximumDuration;
private Collector\Collector $collector;
private Reporter\Reporter $reporter;
private Logger\Logger $logger;

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

$this->maximumDuration = $maximumDuration;
$this->collector = new Collector\DefaultCollector();
$durationFormatter = new Formatter\DefaultDurationFormatter();
$this->reporter = new Reporter\DefaultReporter(
new Formatter\DefaultDurationFormatter(),
$durationFormatter,
$maximumDuration,
$maximumCount,
);
$loggerFactory = new Logger\LoggerFactory(
$durationFormatter,
);
$this->logger = $loggerFactory->forArguments($_SERVER['argv'], $options);
}

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

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

$slowTest = SlowTest::create(
$testIdentifier,
$testFile,
$duration,
$maximumDuration,
);
Expand All @@ -120,6 +128,8 @@ public function executeAfterLastTest(): void

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

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

if ([] === $slowTests) {
return;
}
Expand Down Expand Up @@ -178,6 +188,22 @@ private function resolveMaximumDuration(string $test): Duration

return $this->maximumDuration;
}

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

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

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

return;
Expand All @@ -194,10 +220,6 @@ public function bootstrap(
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 @@ public function bootstrap(

$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,
);

$facade->registerSubscribers(
Expand All @@ -228,6 +256,10 @@ public function bootstrap(
new Subscriber\TestRunner\ExecutionFinishedSubscriber(
$collector,
$reporter,
$loggerFactory->forConfiguration(
$configuration,
$parameters,
),
),
);
}
Expand Down
Loading