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
79 changes: 77 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,41 @@ parameters:
count: 1
path: src/Collector/DefaultCollector.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsolePrinter\\:\\:printLine\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsolePrinter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsoleReporter\\:\\:printFooter\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsoleReporter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsoleReporter\\:\\:printLegend\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsoleReporter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsoleReporter\\:\\:report\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsoleReporter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsoleReporter\\:\\:reportWithCustomAndGlobalMaximumDuration\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsoleReporter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsoleReporter\\:\\:reportWithGlobalMaximumDuration\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Console/ConsoleReporter.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Reporter\\:\\:report\\(\\) has no return type specified\\.$#"
count: 1
path: src/Reporter/Reporter.php

-
message: "#^Offset 'major' does not exist on array\\{0\\?\\: string, major\\?\\: numeric\\-string, 1\\?\\: numeric\\-string, 2\\?\\: '0'\\|\\(non\\-falsy\\-string&numeric\\-string\\), minor\\?\\: ''\\|numeric\\-string, 3\\?\\: ''\\|numeric\\-string, 4\\?\\: '0'\\|\\(non\\-falsy\\-string&numeric\\-string\\)\\}\\.$#"
count: 1
Expand Down Expand Up @@ -240,6 +275,11 @@ parameters:
count: 1
path: test/Unit/Exception/InvalidNanosecondsTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidOutputTest\\:\\:testNotResourceReturnsException\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Exception/InvalidOutputTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Exception\\\\InvalidPhaseIdentifierTest\\:\\:testBlankOrEmptyReturnsException\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -336,15 +376,50 @@ parameters:
path: test/Unit/Reporter/Console/ColorTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\ConsoleReporterTest\\:\\:testReportReturnsEmptyStringWhenSlowTestListIsEmpty\\(\\) has no return type specified\\.$#"
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\ConsolePrinterTest\\:\\:testPrintLineWritesLineFollowedByNewlineToResource\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsolePrinterTest.php

-
message: "#^Parameter \\#1 \\$fp of function rewind expects resource, resource\\|false given\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsolePrinterTest.php

-
message: "#^Parameter \\#1 \\$output of class Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsolePrinter constructor expects resource, resource\\|false given\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsolePrinterTest.php

-
message: "#^Parameter \\#1 \\$source of function stream_get_contents expects resource, resource\\|false given\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsolePrinterTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\ConsoleReporterTest\\:\\:testReportPrintsNothingWhenSlowTestListIsEmpty\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsoleReporterTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\ConsoleReporterTest\\:\\:testReportReturnsReportWhenSlowTestListHasFewerSlowTestsThanMaximumCount\\(\\) has no return type specified\\.$#"
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\ConsoleReporterTest\\:\\:testReportPrintsReportWhenSlowTestListHasFewerSlowTestsThanMaximumCount\\(\\) has no return type specified\\.$#"
count: 1
path: test/Unit/Reporter/Console/ConsoleReporterTest.php

-
message: "#^Parameter \\#1 \\$fp of function rewind expects resource, resource\\|false given\\.$#"
count: 2
path: test/Unit/Reporter/Console/ConsoleReporterTest.php

-
message: "#^Parameter \\#1 \\$output of class Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Reporter\\\\Console\\\\ConsolePrinter constructor expects resource, resource\\|false given\\.$#"
count: 2
path: test/Unit/Reporter/Console/ConsoleReporterTest.php

-
message: "#^Parameter \\#1 \\$source of function stream_get_contents expects resource, resource\\|false given\\.$#"
count: 2
path: test/Unit/Reporter/Console/ConsoleReporterTest.php

-
message: "#^Method Ergebnis\\\\PHPUnit\\\\SlowTestDetector\\\\Test\\\\Unit\\\\Reporter\\\\Console\\\\DurationFormatterTest\\:\\:testFormatFormats\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
31 changes: 31 additions & 0 deletions src/Exception/InvalidOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2026 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 InvalidOutput extends \InvalidArgumentException
{
/**
* @param mixed $output
*/
public static function notResource($output): self
{
return new self(\sprintf(
'Output needs to be a resource, got %s instead.',
\is_object($output) ? \get_class($output) : \gettype($output)
));
}
}
28 changes: 12 additions & 16 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function __construct(array $options = [])
$this->maximumDuration = $maximumDuration;
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\Console\ConsoleReporter(
new Reporter\Console\ConsolePrinter(\fopen(
'php://stdout',
'wb'
)),
new Reporter\Console\DurationFormatter(),
$maximumDuration,
$maximumCount
Expand Down Expand Up @@ -135,13 +139,7 @@ public function endTestSuite(Framework\TestSuite $suite)
return;
}

$report = $this->reporter->report($slowTestList);

if ('' === $report) {
return;
}

echo $report;
$this->reporter->report($slowTestList);
}

public function startTest(Framework\Test $test)
Expand Down Expand Up @@ -272,6 +270,10 @@ public function __construct(array $options = [])
$this->maximumDuration = $maximumDuration;
$this->collector = new Collector\DefaultCollector();
$this->reporter = new Reporter\Console\ConsoleReporter(
new Reporter\Console\ConsolePrinter(\fopen(
'php://stdout',
'wb'
)),
new Reporter\Console\DurationFormatter(),
$maximumDuration,
$maximumCount
Expand Down Expand Up @@ -337,13 +339,7 @@ public function executeAfterLastTest(): void
return;
}

$report = $this->reporter->report($slowTestList);

if ('' === $report) {
return;
}

echo $report;
$this->reporter->report($slowTestList);
}

private function resolveMaximumDuration(string $test): MaximumDuration
Expand Down Expand Up @@ -465,11 +461,11 @@ public function bootstrap(
new Subscriber\TestRunner\ExecutionFinishedSubscriber(
$collector,
new Reporter\Console\ConsoleReporter(
new Reporter\Console\ConsolePrinter($output),
new Reporter\Console\DurationFormatter(),
$maximumDuration,
$maximumCount
),
$output
)
)
);
}
Expand Down
49 changes: 49 additions & 0 deletions src/Reporter/Console/ConsolePrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2021-2026 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\Reporter\Console;

use Ergebnis\PHPUnit\SlowTestDetector\Exception;

/**
* @internal
*/
final class ConsolePrinter
{
/**
* @var resource
*/
private $output;

/**
* @param resource $output
*
* @throws Exception\InvalidOutput
*/
public function __construct($output)
{
if (!\is_resource($output)) {
throw Exception\InvalidOutput::notResource($output);
}

$this->output = $output;
}

public function printLine(string $line)
{
\fwrite(
$this->output,
$line . "\n"
);
}
}
Loading
Loading