From 6e098897305b0aa9a99b2eae4b09e1a1ad5343ee Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 6 Jan 2024 07:24:11 +0100 Subject: [PATCH] Add JUnit report logger --- composer-require-checker.json | 1 + composer.json | 1 + composer.lock | 5 +- psalm-baseline.xml | 73 ++++++++++++ src/Exception/FailedToCreateDirectory.php | 25 ++++ src/Exception/FailedToWriteFile.php | 25 ++++ src/Exception/InvalidFile.php | 25 ++++ src/Exception/InvalidTestFileLine.php | 25 ++++ src/Exception/InvalidTestFilename.php | 25 ++++ src/Extension.php | 50 ++++++-- src/FileWriter/DefaultFileWriter.php | 58 +++++++++ src/FileWriter/File.php | 43 +++++++ src/FileWriter/FileWriter.php | 22 ++++ src/Logger/JUnitLogger.php | 89 ++++++++++++++ src/Logger/Logger.php | 24 ++++ src/Logger/LoggerFactory.php | 63 ++++++++++ src/Logger/NullLogger.php | 26 ++++ src/Reporter/NullReporter.php | 27 +++++ src/SlowTest.php | 10 ++ src/Subscriber/Test/FinishedSubscriber.php | 13 +- .../ExecutionFinishedSubscriber.php | 8 +- src/TestFile.php | 70 +++++++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndNoOption/phpunit.xml | 34 ++++++ .../WithConfigurationAndNoOption/test.phpt | 40 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndOption/phpunit.xml | 34 ++++++ .../WithConfigurationAndOption/test.phpt | 44 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithNoConfigurationAndOption/phpunit.xml | 26 ++++ .../WithNoConfigurationAndOption/test.phpt | 42 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndNoOption/phpunit.xml | 36 ++++++ .../WithConfigurationAndNoOption/test.phpt | 40 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndOption/phpunit.xml | 36 ++++++ .../WithConfigurationAndOption/test.phpt | 44 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithNoConfigurationAndOption/phpunit.xml | 28 +++++ .../WithNoConfigurationAndOption/test.phpt | 42 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndNoOption/phpunit.xml | 36 ++++++ .../WithConfigurationAndNoOption/test.phpt | 40 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithConfigurationAndOption/phpunit.xml | 36 ++++++ .../WithConfigurationAndOption/test.phpt | 44 +++++++ .../SleeperTest.php | 64 ++++++++++ .../WithNoConfigurationAndOption/phpunit.xml | 28 +++++ .../WithNoConfigurationAndOption/test.phpt | 42 +++++++ .../SleeperTest.php | 60 ++++++++++ .../WithConfigurationAndNoOption/phpunit.xml | 35 ++++++ .../WithConfigurationAndNoOption/test.phpt | 18 +++ .../SleeperTest.php | 60 ++++++++++ .../WithConfigurationAndOption/phpunit.xml | 35 ++++++ .../WithConfigurationAndOption/test.phpt | 22 ++++ .../SleeperTest.php | 60 ++++++++++ .../WithNoConfigurationAndOption/phpunit.xml | 33 ++++++ .../WithNoConfigurationAndOption/test.phpt | 20 ++++ .../SleeperTest.php | 60 ++++++++++ .../WithConfigurationAndNoOption/phpunit.xml | 35 ++++++ .../WithConfigurationAndNoOption/test.phpt | 18 +++ .../SleeperTest.php | 60 ++++++++++ .../WithConfigurationAndOption/phpunit.xml | 35 ++++++ .../WithConfigurationAndOption/test.phpt | 22 ++++ .../SleeperTest.php | 60 ++++++++++ .../WithNoConfigurationAndOption/phpunit.xml | 33 ++++++ .../WithNoConfigurationAndOption/test.phpt | 20 ++++ test/Unit/Collector/DefaultCollectorTest.php | 8 ++ .../Exception/FailedToCreateDirectoryTest.php | 30 +++++ test/Unit/Exception/FailedToWriteFileTest.php | 30 +++++ test/Unit/Exception/InvalidFileTest.php | 30 +++++ .../Exception/InvalidTestFileLineTest.php | 30 +++++ .../Exception/InvalidTestFilenameTest.php | 30 +++++ .../Unit/FileWriter/DefaultFileWriterTest.php | 92 ++++++++++++++ test/Unit/FileWriter/FileTest.php | 50 ++++++++ test/Unit/Logger/JUnitLoggerTest.php | 112 ++++++++++++++++++ test/Unit/Reporter/DefaultReporterTest.php | 26 ++++ test/Unit/SlowTestTest.php | 5 + test/Unit/TestFileTest.php | 85 +++++++++++++ 79 files changed, 3155 insertions(+), 15 deletions(-) create mode 100644 src/Exception/FailedToCreateDirectory.php create mode 100644 src/Exception/FailedToWriteFile.php create mode 100644 src/Exception/InvalidFile.php create mode 100644 src/Exception/InvalidTestFileLine.php create mode 100644 src/Exception/InvalidTestFilename.php create mode 100644 src/FileWriter/DefaultFileWriter.php create mode 100644 src/FileWriter/File.php create mode 100644 src/FileWriter/FileWriter.php create mode 100644 src/Logger/JUnitLogger.php create mode 100644 src/Logger/Logger.php create mode 100644 src/Logger/LoggerFactory.php create mode 100644 src/Logger/NullLogger.php create mode 100644 src/Reporter/NullReporter.php create mode 100644 src/TestFile.php create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/SleeperTest.php create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/phpunit.xml create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/test.phpt create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version07/Logger/WithConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/SleeperTest.php create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/phpunit.xml create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/test.phpt create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version08/Logger/WithConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/SleeperTest.php create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/phpunit.xml create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/test.phpt create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version09/Logger/WithConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/SleeperTest.php create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/phpunit.xml create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/test.phpt create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version10/Logger/WithConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/SleeperTest.php create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/phpunit.xml create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/test.phpt create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version11/Logger/WithConfigurationAndOption/test.phpt create mode 100644 test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/SleeperTest.php create mode 100644 test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/phpunit.xml create mode 100644 test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/test.phpt create mode 100644 test/Unit/Exception/FailedToCreateDirectoryTest.php create mode 100644 test/Unit/Exception/FailedToWriteFileTest.php create mode 100644 test/Unit/Exception/InvalidFileTest.php create mode 100644 test/Unit/Exception/InvalidTestFileLineTest.php create mode 100644 test/Unit/Exception/InvalidTestFilenameTest.php create mode 100644 test/Unit/FileWriter/DefaultFileWriterTest.php create mode 100644 test/Unit/FileWriter/FileTest.php create mode 100644 test/Unit/Logger/JUnitLoggerTest.php create mode 100644 test/Unit/TestFileTest.php diff --git a/composer-require-checker.json b/composer-require-checker.json index 0e748b90..a50f5754 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -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", diff --git a/composer.json b/composer.json index 3acf63da..95f3be32 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index 189a061b..bf31806e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e194908cee3cbffa09281595c6f1fc3b", + "content-hash": "5155eb05ce794205cbf710bd173d5aa8", "packages": [ { "name": "doctrine/instantiator", @@ -5496,7 +5496,8 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0" + "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0", + "ext-dom": "*" }, "platform-dev": [], "platform-overrides": { diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 116580e7..fd90a9e8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -11,6 +11,12 @@ $options + + + static function (): void { + } + + seconds() * 1_000 + $duration->nanoseconds() / 1_000_000]]> @@ -21,6 +27,27 @@ ($durationInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1_000 + + + Logger + + + Runner\Extension\ParameterCollection + TextUI\Configuration\Configuration + + + LoggerFactory + + + $configuration + $parameters + + + + + NullReporter + + Event\Test\FinishedSubscriber @@ -57,6 +84,7 @@ $collector $event + $logger $reporter @@ -183,6 +211,21 @@ provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration @@ -323,6 +366,21 @@ provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + + + + provideMillisecondsGreaterThanDefaultMaximumDuration + + provideMillisecondsGreaterThanMaximumDurationFromXmlConfiguration @@ -409,9 +467,24 @@ milliseconds * 1000]]> + + + filePath()]]> + filePath()]]> + filePath()]]> + filePath()]]> + filePath()]]> + filePath()]]> + + iterable + + + filePath()]]> + + diff --git a/src/Exception/FailedToCreateDirectory.php b/src/Exception/FailedToCreateDirectory.php new file mode 100644 index 00000000..9c7a0a58 --- /dev/null +++ b/src/Exception/FailedToCreateDirectory.php @@ -0,0 +1,25 @@ +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 @@ -100,9 +106,11 @@ public function executeAfterTest( } $testIdentifier = TestIdentifier::fromString($test); + $testFile = $this->resolveTestFile($test); $slowTest = SlowTest::create( $testIdentifier, + $testFile, $duration, $maximumDuration, ); @@ -120,6 +128,8 @@ public function executeAfterLastTest(): void $slowTests = $this->collector->collected(); + $this->logger->log(...$slowTests); + if ([] === $slowTests) { return; } @@ -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; @@ -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')) { @@ -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( @@ -228,6 +256,10 @@ public function bootstrap( new Subscriber\TestRunner\ExecutionFinishedSubscriber( $collector, $reporter, + $loggerFactory->forConfiguration( + $configuration, + $parameters, + ), ), ); } diff --git a/src/FileWriter/DefaultFileWriter.php b/src/FileWriter/DefaultFileWriter.php new file mode 100644 index 00000000..7d2caddb --- /dev/null +++ b/src/FileWriter/DefaultFileWriter.php @@ -0,0 +1,58 @@ +file = $file; + } + + public function write(string $contents): void + { + $out = $this->file->toString(); + + \set_error_handler(static function (): void { + }); + + try { + if (0 !== \strpos($out, 'php://')) { + $this->createDirectory($out); + } + + if (false === \file_put_contents($out, $contents)) { + throw Exception\FailedToWriteFile::forFile($out); + } + } finally { + \restore_error_handler(); + } + } + + private function createDirectory(string $out): void + { + $directory = \dirname($out); + + if (!\is_dir($directory) && !\mkdir($directory, 0777, true) && !\is_dir($directory)) { + throw Exception\FailedToCreateDirectory::forDirectory($directory); + } + } +} diff --git a/src/FileWriter/File.php b/src/FileWriter/File.php new file mode 100644 index 00000000..2048b0c9 --- /dev/null +++ b/src/FileWriter/File.php @@ -0,0 +1,43 @@ +value = $value; + } + + public static function fromString(string $value): self + { + if ('' === \trim($value)) { + throw Exception\InvalidFile::blankOrEmpty(); + } + + return new self($value); + } + + public function toString(): string + { + return $this->value; + } +} diff --git a/src/FileWriter/FileWriter.php b/src/FileWriter/FileWriter.php new file mode 100644 index 00000000..23795530 --- /dev/null +++ b/src/FileWriter/FileWriter.php @@ -0,0 +1,22 @@ +durationFormatter = $durationFormatter; + $this->fileWriter = $fileWriter; + } + + public function log(SlowTest ...$slowTests): void + { + $dom = new \DOMDocument('1.0', 'UTF-8'); + $testsuites = $dom->appendChild($dom->createElement('testsuites')); + + /** @var \DOMElement $testsuite */ + $testsuite = $testsuites->appendChild($dom->createElement('testsuite')); + $testsuite->setAttribute('name', 'Slow Tests'); + + $this->createSlowTestCases($dom, $testsuite, $slowTests); + + $this->fileWriter->write($dom->saveXML()); + } + + /** + * @param array $slowTests + */ + private function createSlowTestCases(\DOMDocument $dom, \DOMElement $testsuite, array $slowTests): void + { + foreach ($slowTests as $slowTest) { + $testcase = $this->createSlowTestCase($dom, $slowTest); + $testsuite->appendChild($testcase); + } + + $slowTestsCount = \count($slowTests); + + $testsuite->setAttribute('tests', (string) $slowTestsCount); + $testsuite->setAttribute('failures', (string) $slowTestsCount); + $testsuite->setAttribute('errors', '0'); + } + + private function createSlowTestCase(\DOMDocument $dom, SlowTest $slowTest): \DOMElement + { + $testcase = $dom->createElement('testcase'); + $testcase->setAttribute('name', $slowTest->testIdentifier()->toString()); + $testcase->setAttribute('file', $slowTest->testFile()->filename()); + + if (null !== $line = $slowTest->testFile()->line()) { + $testcase->setAttribute('line', (string) $line); + } + + $failure = $dom->createElement('failure'); + $failure->setAttribute('type', 'slow_test'); + $testcase->appendChild($failure); + + $failure->appendChild($dom->createCDATASection(\sprintf( + 'The actual duration of %s exceeds the maximum allowed duration of %s.', + $this->durationFormatter->format($slowTest->duration()), + $this->durationFormatter->format($slowTest->maximumDuration()), + ))); + + return $testcase; + } +} diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php new file mode 100644 index 00000000..3cab89ac --- /dev/null +++ b/src/Logger/Logger.php @@ -0,0 +1,24 @@ +durationFormatter = $durationFormatter; + } + + public function forConfiguration( + TextUI\Configuration\Configuration $configuration, + Runner\Extension\ParameterCollection $parameters + ): Logger { + if ($configuration->hasLogfileJunit() && $parameters->has('junit-file')) { + return $this->createJUnitLogger(File::fromString($parameters->get('junit-file'))); + } + + return $this->createNullLogger(); + } + + public function forArguments(array $arguments, array $options): Logger + { + if (isset($options['junit-file']) && \in_array('--log-junit', $arguments, true)) { + return $this->createJUnitLogger(File::fromString((string) $options['junit-file'])); + } + + return $this->createNullLogger(); + } + + private function createJUnitLogger(File $file): Logger + { + return new JUnitLogger($this->durationFormatter, new DefaultFileWriter($file)); + } + + private function createNullLogger(): Logger + { + return new NullLogger(); + } +} diff --git a/src/Logger/NullLogger.php b/src/Logger/NullLogger.php new file mode 100644 index 00000000..12c7c49f --- /dev/null +++ b/src/Logger/NullLogger.php @@ -0,0 +1,26 @@ +testIdentifier = $testIdentifier; + $this->testFile = $testFile; $this->duration = $duration; $this->maximumDuration = $maximumDuration; } public static function create( TestIdentifier $testIdentifier, + TestFile $testFile, Duration $duration, Duration $maximumDuration ): self { return new self( $testIdentifier, + $testFile, $duration, $maximumDuration, ); @@ -49,6 +54,11 @@ public function testIdentifier(): TestIdentifier return $this->testIdentifier; } + public function testFile(): TestFile + { + return $this->testFile; + } + public function duration(): Duration { return $this->duration; diff --git a/src/Subscriber/Test/FinishedSubscriber.php b/src/Subscriber/Test/FinishedSubscriber.php index ba4bae0b..25c0b0e9 100644 --- a/src/Subscriber/Test/FinishedSubscriber.php +++ b/src/Subscriber/Test/FinishedSubscriber.php @@ -18,6 +18,7 @@ use Ergebnis\PHPUnit\SlowTestDetector\Duration; use Ergebnis\PHPUnit\SlowTestDetector\PhaseIdentifier; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; +use Ergebnis\PHPUnit\SlowTestDetector\TestFile; use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier; use Ergebnis\PHPUnit\SlowTestDetector\Time; use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper; @@ -49,7 +50,8 @@ public function __construct( */ public function notify(Event\Test\Finished $event): void { - $phaseIdentifier = PhaseIdentifier::fromString($event->test()->id()); + $test = $event->test(); + $phaseIdentifier = PhaseIdentifier::fromString($test->id()); $time = $event->telemetryInfo()->time(); @@ -63,14 +65,19 @@ public function notify(Event\Test\Finished $event): void $duration = $phase->duration(); - $maximumDuration = $this->resolveMaximumDuration($event->test()); + $maximumDuration = $this->resolveMaximumDuration($test); if (!$duration->isGreaterThan($maximumDuration)) { return; } + $testFile = $test instanceof Event\Code\TestMethod + ? TestFile::fromFilenameAndLine($test->file(), $test->line()) + : TestFile::fromFilename($test->file()); + $slowTest = SlowTest::create( - TestIdentifier::fromString($event->test()->id()), + TestIdentifier::fromString($test->id()), + $testFile, $duration, $maximumDuration, ); diff --git a/src/Subscriber/TestRunner/ExecutionFinishedSubscriber.php b/src/Subscriber/TestRunner/ExecutionFinishedSubscriber.php index 30808d88..dbaa8c66 100644 --- a/src/Subscriber/TestRunner/ExecutionFinishedSubscriber.php +++ b/src/Subscriber/TestRunner/ExecutionFinishedSubscriber.php @@ -14,6 +14,7 @@ namespace Ergebnis\PHPUnit\SlowTestDetector\Subscriber\TestRunner; use Ergebnis\PHPUnit\SlowTestDetector\Collector; +use Ergebnis\PHPUnit\SlowTestDetector\Logger; use Ergebnis\PHPUnit\SlowTestDetector\Reporter; use PHPUnit\Event; @@ -24,13 +25,16 @@ final class ExecutionFinishedSubscriber implements Event\TestRunner\ExecutionFin { private Reporter\Reporter $reporter; private Collector\Collector $collector; + private Logger\Logger $logger; public function __construct( Collector\Collector $collector, - Reporter\Reporter $reporter + Reporter\Reporter $reporter, + Logger\Logger $logger ) { $this->collector = $collector; $this->reporter = $reporter; + $this->logger = $logger; } /** @@ -40,6 +44,8 @@ public function notify(Event\TestRunner\ExecutionFinished $event): void { $slowTests = $this->collector->collected(); + $this->logger->log(...$slowTests); + if ([] === $slowTests) { return; } diff --git a/src/TestFile.php b/src/TestFile.php new file mode 100644 index 00000000..bd571d91 --- /dev/null +++ b/src/TestFile.php @@ -0,0 +1,70 @@ +filename = $filename; + $this->line = $line; + } + + /** + * @throws Exception\InvalidTestFilename + */ + public static function fromFilename(string $filename): self + { + if ('' === \trim($filename)) { + throw Exception\InvalidTestFilename::blankOrEmpty(); + } + + return new self($filename); + } + + /** + * @throws Exception\InvalidTestFileLine + * @throws Exception\InvalidTestFilename + */ + public static function fromFilenameAndLine(string $filename, int $line): self + { + if ('' === \trim($filename)) { + throw Exception\InvalidTestFilename::blankOrEmpty(); + } + + if (1 > $line) { + throw Exception\InvalidTestFileLine::lesserThenOne(); + } + + return new self($filename, $line); + } + + public function filename(): string + { + return $this->filename; + } + + public function line(): ?int + { + return $this->line; + } +} diff --git a/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/SleeperTest.php b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/SleeperTest.php new file mode 100644 index 00000000..126e98ca --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/SleeperTest.php @@ -0,0 +1,64 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/phpunit.xml b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/phpunit.xml new file mode 100644 index 00000000..cdeeb169 --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/phpunit.xml @@ -0,0 +1,34 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/test.phpt b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/test.phpt new file mode 100644 index 00000000..f6c81dbc --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithConfigurationAndNoOption/test.phpt @@ -0,0 +1,40 @@ +--TEST-- +Logger with configuration and no option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..cdeeb169 --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/phpunit.xml @@ -0,0 +1,34 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/test.phpt b/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/test.phpt new file mode 100644 index 00000000..1d1e50ec --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithConfigurationAndOption/test.phpt @@ -0,0 +1,44 @@ +--TEST-- +Logger with configuration and option +--FILE-- + + + + +Detected 6 tests where the duration exceeded the maximum duration. + +1. 1.0%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #4 (1000) +2. 0.9%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #3 (900) +3. 0.8%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #2 (800) +4. 0.7%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #1 (700) +5. 0.6%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #0 (600) +6. 0.5%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version07\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDuration + +Time: %s, Memory: %s + +OK (6 tests, 6 assertions) diff --git a/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..dc3e3a4f --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/SleeperTest.php @@ -0,0 +1,64 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..b484eec0 --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/phpunit.xml @@ -0,0 +1,26 @@ + + + + + + + . + + + diff --git a/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/test.phpt b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/test.phpt new file mode 100644 index 00000000..e0564843 --- /dev/null +++ b/test/EndToEnd/Version07/Logger/WithNoConfigurationAndOption/test.phpt @@ -0,0 +1,42 @@ +--TEST-- +Logger with no configuration and option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/phpunit.xml b/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/phpunit.xml new file mode 100644 index 00000000..e85eb833 --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/phpunit.xml @@ -0,0 +1,36 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/test.phpt b/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/test.phpt new file mode 100644 index 00000000..d5bf7ae1 --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithConfigurationAndNoOption/test.phpt @@ -0,0 +1,40 @@ +--TEST-- +Logger with configuration and no option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..e85eb833 --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/phpunit.xml @@ -0,0 +1,36 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/test.phpt b/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/test.phpt new file mode 100644 index 00000000..209d557a --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithConfigurationAndOption/test.phpt @@ -0,0 +1,44 @@ +--TEST-- +Logger with configuration and option +--FILE-- + + + + +Detected 6 tests where the duration exceeded the maximum duration. + +1. 1.0%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #4 (1000) +2. 0.9%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #3 (900) +3. 0.8%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #2 (800) +4. 0.7%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #1 (700) +5. 0.6%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #0 (600) +6. 0.5%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version08\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDuration + +Time: %s, Memory: %s + +OK (6 tests, 6 assertions) diff --git a/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..9756c2ba --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/SleeperTest.php @@ -0,0 +1,64 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..d6e1d5b1 --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/phpunit.xml @@ -0,0 +1,28 @@ + + + + + + + . + + + diff --git a/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/test.phpt b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/test.phpt new file mode 100644 index 00000000..d2a22037 --- /dev/null +++ b/test/EndToEnd/Version08/Logger/WithNoConfigurationAndOption/test.phpt @@ -0,0 +1,42 @@ +--TEST-- +Logger with no configuration and option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/phpunit.xml b/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/phpunit.xml new file mode 100644 index 00000000..a38faa01 --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/phpunit.xml @@ -0,0 +1,36 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/test.phpt b/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/test.phpt new file mode 100644 index 00000000..f30ffffa --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithConfigurationAndNoOption/test.phpt @@ -0,0 +1,40 @@ +--TEST-- +Logger with configuration and no option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..a38faa01 --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/phpunit.xml @@ -0,0 +1,36 @@ + + + + + + + php://stdout + + + + + + + + . + + + diff --git a/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/test.phpt b/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/test.phpt new file mode 100644 index 00000000..d47980dc --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithConfigurationAndOption/test.phpt @@ -0,0 +1,44 @@ +--TEST-- +Logger with configuration and option +--FILE-- + + + + +Detected 6 tests where the duration exceeded the maximum duration. + +1. 1.0%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #4 (1000) +2. 0.9%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #3 (900) +3. 0.8%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #2 (800) +4. 0.7%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #1 (700) +5. 0.6%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider with data set #0 (600) +6. 0.5%s (0.500) Ergebnis\PHPUnit\SlowTestDetector\Test\EndToEnd\Version09\Logger\WithConfigurationAndOption\SleeperTest::testSleeperSleepsLongerThanDefaultMaximumDuration + +Time: %s, Memory: %s + +OK (6 tests, 6 assertions) diff --git a/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..03682eb7 --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/SleeperTest.php @@ -0,0 +1,64 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @dataProvider provideMillisecondsGreaterThanDefaultMaximumDuration + */ + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..03aeaec3 --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/phpunit.xml @@ -0,0 +1,28 @@ + + + + + + + . + + + diff --git a/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/test.phpt b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/test.phpt new file mode 100644 index 00000000..a0fe779b --- /dev/null +++ b/test/EndToEnd/Version09/Logger/WithNoConfigurationAndOption/test.phpt @@ -0,0 +1,42 @@ +--TEST-- +Logger with no configuration and option +--FILE-- +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/phpunit.xml b/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/phpunit.xml new file mode 100644 index 00000000..3b52518d --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/phpunit.xml @@ -0,0 +1,35 @@ + + + + + + + + + . + + + diff --git a/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/test.phpt b/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/test.phpt new file mode 100644 index 00000000..75eb4b3b --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithConfigurationAndNoOption/test.phpt @@ -0,0 +1,18 @@ +--TEST-- +Logger with configuration and no option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- diff --git a/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..d918cfb3 --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/SleeperTest.php @@ -0,0 +1,60 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..3b52518d --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/phpunit.xml @@ -0,0 +1,35 @@ + + + + + + + + + . + + + diff --git a/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/test.phpt b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/test.phpt new file mode 100644 index 00000000..5124b303 --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithConfigurationAndOption/test.phpt @@ -0,0 +1,22 @@ +--TEST-- +Logger with configuration and option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- + + diff --git a/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..cce3d9e6 --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/SleeperTest.php @@ -0,0 +1,60 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..154fe728 --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/phpunit.xml @@ -0,0 +1,33 @@ + + + + + + + . + + + diff --git a/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/test.phpt b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/test.phpt new file mode 100644 index 00000000..347df88d --- /dev/null +++ b/test/EndToEnd/Version10/Logger/WithNoConfigurationAndOption/test.phpt @@ -0,0 +1,20 @@ +--TEST-- +Logger with no configuration and option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/SleeperTest.php b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/SleeperTest.php new file mode 100644 index 00000000..b166ca08 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/SleeperTest.php @@ -0,0 +1,60 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/phpunit.xml b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/phpunit.xml new file mode 100644 index 00000000..daf12967 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/phpunit.xml @@ -0,0 +1,35 @@ + + + + + + + + + . + + + diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/test.phpt b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/test.phpt new file mode 100644 index 00000000..2b715024 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndNoOption/test.phpt @@ -0,0 +1,18 @@ +--TEST-- +Logger with configuration and no option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..adf34e6a --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/SleeperTest.php @@ -0,0 +1,60 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..daf12967 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/phpunit.xml @@ -0,0 +1,35 @@ + + + + + + + + + . + + + diff --git a/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/test.phpt b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/test.phpt new file mode 100644 index 00000000..39405d85 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithConfigurationAndOption/test.phpt @@ -0,0 +1,22 @@ +--TEST-- +Logger with configuration and option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- + + diff --git a/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/SleeperTest.php b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/SleeperTest.php new file mode 100644 index 00000000..046e6723 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/SleeperTest.php @@ -0,0 +1,60 @@ +sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + #[Framework\Attributes\DataProvider('provideMillisecondsGreaterThanDefaultMaximumDuration')] + public function testSleeperSleepsLongerThanDefaultMaximumDurationWithDataProvider(int $milliseconds): void + { + $sleeper = Test\Fixture\Sleeper::fromMilliseconds($milliseconds); + + $sleeper->sleep(); + + self::assertSame($milliseconds, $sleeper->milliseconds()); + } + + /** + * @return \Generator + */ + public static function provideMillisecondsGreaterThanDefaultMaximumDuration(): iterable + { + $values = \range( + 600, + 1000, + 100, + ); + + foreach ($values as $value) { + yield $value => [ + $value, + ]; + } + } +} diff --git a/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/phpunit.xml b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/phpunit.xml new file mode 100644 index 00000000..73bfe906 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/phpunit.xml @@ -0,0 +1,33 @@ + + + + + + + . + + + diff --git a/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/test.phpt b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/test.phpt new file mode 100644 index 00000000..d3c9b129 --- /dev/null +++ b/test/EndToEnd/Version11/Logger/WithNoConfigurationAndOption/test.phpt @@ -0,0 +1,20 @@ +--TEST-- +Logger with no configuration and option +--FILE-- +run($_SERVER['argv']); +--EXPECTF-- diff --git a/test/Unit/Collector/DefaultCollectorTest.php b/test/Unit/Collector/DefaultCollectorTest.php index 64cc3bd4..b6b111be 100644 --- a/test/Unit/Collector/DefaultCollectorTest.php +++ b/test/Unit/Collector/DefaultCollectorTest.php @@ -17,6 +17,7 @@ use Ergebnis\PHPUnit\SlowTestDetector\Duration; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; use Ergebnis\PHPUnit\SlowTestDetector\Test; +use Ergebnis\PHPUnit\SlowTestDetector\TestFile; use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier; use PHPUnit\Framework; @@ -25,6 +26,7 @@ * * @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration * @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest + * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestFile * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier */ final class DefaultCollectorTest extends Framework\TestCase @@ -37,12 +39,14 @@ public function testCollectCollectsSlowTests(): void $one = SlowTest::create( TestIdentifier::fromString($faker->word()), + TestFile::fromFilename($faker->filePath()), Duration::fromMilliseconds($faker->numberBetween(0)), Duration::fromMilliseconds($faker->numberBetween(0)), ); $two = SlowTest::create( TestIdentifier::fromString($faker->word()), + TestFile::fromFilename($faker->filePath()), Duration::fromMilliseconds($faker->numberBetween(0)), Duration::fromMilliseconds($faker->numberBetween(0)), ); @@ -66,12 +70,14 @@ public function testCollectCollectsSlowerTestWithSameTestIdentifier(): void $one = SlowTest::create( TestIdentifier::fromString($faker->word()), + TestFile::fromFilename($faker->filePath()), Duration::fromMilliseconds($faker->numberBetween(0)), Duration::fromMilliseconds($faker->numberBetween(0, 999_999_999 - 1)), ); $two = SlowTest::create( $one->testIdentifier(), + TestFile::fromFilename($faker->filePath()), Duration::fromSecondsAndNanoseconds( $one->duration()->seconds(), $one->duration()->nanoseconds() + 1, @@ -97,12 +103,14 @@ public function testCollectDoesNotCollectFasterTestWithSameTestIdentifier(): voi $one = SlowTest::create( TestIdentifier::fromString($faker->word()), + TestFile::fromFilename($faker->filePath()), Duration::fromMilliseconds($faker->numberBetween(0)), Duration::fromMilliseconds($faker->numberBetween(1, 999_999_999)), ); $two = SlowTest::create( $one->testIdentifier(), + TestFile::fromFilename($faker->filePath()), Duration::fromSecondsAndNanoseconds( $one->duration()->seconds(), $one->duration()->nanoseconds() - 1, diff --git a/test/Unit/Exception/FailedToCreateDirectoryTest.php b/test/Unit/Exception/FailedToCreateDirectoryTest.php new file mode 100644 index 00000000..74d79abf --- /dev/null +++ b/test/Unit/Exception/FailedToCreateDirectoryTest.php @@ -0,0 +1,30 @@ +getMessage()); + } +} diff --git a/test/Unit/Exception/FailedToWriteFileTest.php b/test/Unit/Exception/FailedToWriteFileTest.php new file mode 100644 index 00000000..77a74f0d --- /dev/null +++ b/test/Unit/Exception/FailedToWriteFileTest.php @@ -0,0 +1,30 @@ +getMessage()); + } +} diff --git a/test/Unit/Exception/InvalidFileTest.php b/test/Unit/Exception/InvalidFileTest.php new file mode 100644 index 00000000..dc2ca113 --- /dev/null +++ b/test/Unit/Exception/InvalidFileTest.php @@ -0,0 +1,30 @@ +getMessage()); + } +} diff --git a/test/Unit/Exception/InvalidTestFileLineTest.php b/test/Unit/Exception/InvalidTestFileLineTest.php new file mode 100644 index 00000000..cb62bfaf --- /dev/null +++ b/test/Unit/Exception/InvalidTestFileLineTest.php @@ -0,0 +1,30 @@ +getMessage()); + } +} diff --git a/test/Unit/Exception/InvalidTestFilenameTest.php b/test/Unit/Exception/InvalidTestFilenameTest.php new file mode 100644 index 00000000..3f9216f3 --- /dev/null +++ b/test/Unit/Exception/InvalidTestFilenameTest.php @@ -0,0 +1,30 @@ +getMessage()); + } +} diff --git a/test/Unit/FileWriter/DefaultFileWriterTest.php b/test/Unit/FileWriter/DefaultFileWriterTest.php new file mode 100644 index 00000000..0757e538 --- /dev/null +++ b/test/Unit/FileWriter/DefaultFileWriterTest.php @@ -0,0 +1,92 @@ +tempDir = \sys_get_temp_dir() . '/slow-tests/foo'; + $this->tempFile = $this->tempDir . '/bar.xml'; + } + + protected function tearDown(): void + { + if (\is_file($this->tempFile)) { + \unlink($this->tempFile); + } + + if (\is_dir($this->tempDir)) { + \rmdir($this->tempDir); + } + + if (\is_dir($rootDir = \dirname($this->tempDir))) { + \rmdir($rootDir); + } + } + + public function testContentsIsWrittenSuccessfullyToFile(): void + { + $fileWriter = new FileWriter\DefaultFileWriter( + FileWriter\File::fromString($this->tempFile), + ); + + $fileWriter->write('foo'); + + self::assertFileExists($this->tempFile); + self::assertStringEqualsFile($this->tempFile, 'foo'); + } + + public function testExceptionIsThrownWhenDirectoryCannotBeCreated(): void + { + \mkdir(\dirname($this->tempDir), 000); + + $fileWriter = new FileWriter\DefaultFileWriter( + FileWriter\File::fromString($this->tempFile), + ); + + $this->expectException(Exception\FailedToCreateDirectory::class); + $this->expectExceptionMessage(\sprintf('Directory "%s" was not created.', $this->tempDir)); + + $fileWriter->write('foo'); + } + + public function testExceptionIsThrownWhenFileCannotBeWritten(): void + { + \mkdir($this->tempDir, 0777, true); + \touch($this->tempFile); + \chmod($this->tempFile, 000); + + $fileWriter = new FileWriter\DefaultFileWriter( + FileWriter\File::fromString($this->tempFile), + ); + + $this->expectException(Exception\FailedToWriteFile::class); + $this->expectExceptionMessage(\sprintf('Failed to write to file "%s".', $this->tempFile)); + + $fileWriter->write('foo'); + } +} diff --git a/test/Unit/FileWriter/FileTest.php b/test/Unit/FileWriter/FileTest.php new file mode 100644 index 00000000..53031983 --- /dev/null +++ b/test/Unit/FileWriter/FileTest.php @@ -0,0 +1,50 @@ +expectException(Exception\InvalidFile::class); + + FileWriter\File::fromString($value); + } + + public function testFromStringReturnsFile(): void + { + $value = self::faker()->word(); + + $file = FileWriter\File::fromString($value); + + self::assertSame($value, $file->toString()); + } +} diff --git a/test/Unit/Logger/JUnitLoggerTest.php b/test/Unit/Logger/JUnitLoggerTest.php new file mode 100644 index 00000000..95827d55 --- /dev/null +++ b/test/Unit/Logger/JUnitLoggerTest.php @@ -0,0 +1,112 @@ +expects(self::once()) + ->method('write') + ->with(self::callback(static function (string $contents) use ($expectedContents): bool { + return $contents === $expectedContents; + })); + + $logger = new Logger\JUnitLogger( + new Formatter\DefaultDurationFormatter(), + $fileWriter, + ); + + $logger->log(...$slowTests); + } + + /** + * @return \Generator + */ + public static function provideSlowTestsCases(): iterable + { + yield 'with-lines' => [ + <<<'TXT' + + + +TXT, + SlowTest::create( + TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilenameAndLine('/some/filename.php', 3), + Duration::fromMilliseconds(300), + Duration::fromMilliseconds(100), + ), + SlowTest::create( + TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilenameAndLine('/some/other/filename.php', 45), + Duration::fromMilliseconds(275), + Duration::fromMilliseconds(100), + ), + ]; + + yield 'without-lines' => [ + <<<'TXT' + + + +TXT, + SlowTest::create( + TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/some/filename.php'), + Duration::fromMilliseconds(300), + Duration::fromMilliseconds(100), + ), + SlowTest::create( + TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/some/other/filename.php'), + Duration::fromMilliseconds(275), + Duration::fromMilliseconds(100), + ), + ]; + + yield 'without-slow-tests' => [ + <<<'TXT' + + + +TXT, + ]; + } +} diff --git a/test/Unit/Reporter/DefaultReporterTest.php b/test/Unit/Reporter/DefaultReporterTest.php index 4d398ece..977a9a8f 100644 --- a/test/Unit/Reporter/DefaultReporterTest.php +++ b/test/Unit/Reporter/DefaultReporterTest.php @@ -19,6 +19,7 @@ use Ergebnis\PHPUnit\SlowTestDetector\Reporter; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; use Ergebnis\PHPUnit\SlowTestDetector\Test; +use Ergebnis\PHPUnit\SlowTestDetector\TestFile; use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier; use PHPUnit\Framework; @@ -30,6 +31,7 @@ * @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration * @uses \Ergebnis\PHPUnit\SlowTestDetector\Formatter\DefaultDurationFormatter * @uses \Ergebnis\PHPUnit\SlowTestDetector\SlowTest + * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestFile * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier */ final class DefaultReporterTest extends Framework\TestCase @@ -85,6 +87,7 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), @@ -102,11 +105,13 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(275), Duration::fromMilliseconds(100), ), @@ -125,16 +130,19 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(275), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BazTest::test'), + TestFile::fromFilename('/src/BazTest.php'), Duration::fromMilliseconds(250), Duration::fromMilliseconds(100), ), @@ -153,16 +161,19 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('BazTest::test'), + TestFile::fromFilename('/src/BazTest.php'), Duration::fromMilliseconds(250), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(275), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), @@ -188,51 +199,61 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(1_250_000), Duration::fromMilliseconds(1_000_000), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(575_000), Duration::fromMilliseconds(500_000), ), SlowTest::create( TestIdentifier::fromString('BazTest::test'), + TestFile::fromFilename('/src/BazTest.php'), Duration::fromMilliseconds(250), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('QuxTest::test'), + TestFile::fromFilename('/src/QuxTest.php'), Duration::fromMilliseconds(200), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('QuuxTest::test'), + TestFile::fromFilename('/src/QuuxTest.php'), Duration::fromMilliseconds(160), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('CorgeTest::test'), + TestFile::fromFilename('/src/CorgeTest.php'), Duration::fromMilliseconds(150), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('GraultTest::test'), + TestFile::fromFilename('/src/GraultTest.php'), Duration::fromMilliseconds(140), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('GarplyTest::test'), + TestFile::fromFilename('/src/GarplyTest.php'), Duration::fromMilliseconds(130), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('WaldoTest::test'), + TestFile::fromFilename('/src/WaldoTest.php'), Duration::fromMilliseconds(120), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('FredTest::test'), + TestFile::fromFilename('/src/FredTest.php'), Duration::fromMilliseconds(110), Duration::fromMilliseconds(100), ), @@ -251,11 +272,13 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(275), Duration::fromMilliseconds(100), ), @@ -274,16 +297,19 @@ public static function provideExpectedReportMaximumDurationMaximumCountAndSlowTe [ SlowTest::create( TestIdentifier::fromString('FooTest::test'), + TestFile::fromFilename('/src/FooTest.php'), Duration::fromMilliseconds(300), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BarTest::test'), + TestFile::fromFilename('/src/BarTest.php'), Duration::fromMilliseconds(275), Duration::fromMilliseconds(100), ), SlowTest::create( TestIdentifier::fromString('BazTest::test'), + TestFile::fromFilename('/src/BazTest.php'), Duration::fromMilliseconds(250), Duration::fromMilliseconds(100), ), diff --git a/test/Unit/SlowTestTest.php b/test/Unit/SlowTestTest.php index d0c35b8e..19ec23c2 100644 --- a/test/Unit/SlowTestTest.php +++ b/test/Unit/SlowTestTest.php @@ -16,6 +16,7 @@ use Ergebnis\PHPUnit\SlowTestDetector\Duration; use Ergebnis\PHPUnit\SlowTestDetector\SlowTest; use Ergebnis\PHPUnit\SlowTestDetector\Test; +use Ergebnis\PHPUnit\SlowTestDetector\TestFile; use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier; use PHPUnit\Framework; @@ -23,6 +24,7 @@ * @covers \Ergebnis\PHPUnit\SlowTestDetector\SlowTest * * @uses \Ergebnis\PHPUnit\SlowTestDetector\Duration + * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestFile * @uses \Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier */ final class SlowTestTest extends Framework\TestCase @@ -34,16 +36,19 @@ public function testCreateReturnsSlowTest(): void $faker = self::faker(); $testIdentifier = TestIdentifier::fromString($faker->word()); + $testFile = TestFile::fromFilename($faker->filePath()); $duration = Duration::fromMilliseconds($faker->numberBetween(0)); $maximumDuration = Duration::fromMilliseconds($faker->numberBetween(0)); $slowTest = SlowTest::create( $testIdentifier, + $testFile, $duration, $maximumDuration, ); self::assertSame($testIdentifier, $slowTest->testIdentifier()); + self::assertSame($testFile, $slowTest->testFile()); self::assertSame($duration, $slowTest->duration()); self::assertSame($maximumDuration, $slowTest->maximumDuration()); } diff --git a/test/Unit/TestFileTest.php b/test/Unit/TestFileTest.php new file mode 100644 index 00000000..646c87f8 --- /dev/null +++ b/test/Unit/TestFileTest.php @@ -0,0 +1,85 @@ +expectException(Exception\InvalidTestFilename::class); + + TestFile::fromFilename($value); + } + + public function testFromFilenameReturnsTestFile(): void + { + $value = self::faker()->word(); + + $testFile = TestFile::fromFilename($value); + + self::assertSame($value, $testFile->filename()); + self::assertNull($testFile->line()); + } + + /** + * @dataProvider \Ergebnis\DataProvider\StringProvider::blank + * @dataProvider \Ergebnis\DataProvider\StringProvider::empty + */ + public function testFromFilenameAndLineRejectsInvalidFilename(string $value): void + { + $this->expectException(Exception\InvalidTestFilename::class); + + TestFile::fromFilenameAndLine($value, 1); + } + + /** + * @dataProvider \Ergebnis\DataProvider\IntProvider::lessThanZero() + * @dataProvider \Ergebnis\DataProvider\IntProvider::zero() + */ + public function testFromFilenameAndLineRejectsInvalidLine(int $value): void + { + $this->expectException(Exception\InvalidTestFileLine::class); + + TestFile::fromFilenameAndLine('foo', $value); + } + + public function testFromFilenameAndLineReturnsTestFile(): void + { + $filename = self::faker()->word(); + $line = self::faker()->numberBetween(1, 1000); + + $testFile = TestFile::fromFilenameAndLine($filename, $line); + + self::assertSame($filename, $testFile->filename()); + self::assertSame($line, $testFile->line()); + } +}