diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9a43e7..9dff5b11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Version 8.0.0-dev - added events ExtensionsLoaded, RunnerStarted and RunnerFinished - BC break: added methods getName and getSubscribedEvents to interface ITesterExtension, removed methods on* (it is now an event subscriber) - InfoExtension (when added to automated tests runner) prints active extensions +- BC break: added method getSubscribedEvents to interface IResultsFormatter, removed methods report* (it is now an event subscriber) Version 7.3.1 - allowed installation konecnyjakub/event-dispatcher 2 diff --git a/RELEASE_NOTES b/RELEASE_NOTES index f8cf77dc..56153756 100644 --- a/RELEASE_NOTES +++ b/RELEASE_NOTES @@ -2,7 +2,9 @@ A new version of My Tester is out. It is only for Composer 2.2 or later. It remo Interface ITesterExtension was changed - it has new method getName and was changed into an event subscriber. -There are new event ExtensionsLoaded, RunnerStarted and RunnerFinished which event subscribers (extensions) can now add listeners for. +Interface IResultsFormatter was changed into an event subscriber. You can extend from class MyTester\ResultsFormatters\AbstractResultsFormatter to make updating a bit easier. + +There are new event ExtensionsLoaded, RunnerStarted and RunnerFinished which event subscribers (extensions and result formatters) can now add listeners for. Automated tests runner can now print list of active extensions along My Tester and PHP version (all of that is handled by InfoExtension). diff --git a/src/IResultsFormatter.php b/src/IResultsFormatter.php index 17080995..ffcf9e40 100644 --- a/src/IResultsFormatter.php +++ b/src/IResultsFormatter.php @@ -3,37 +3,15 @@ namespace MyTester; +use Konecnyjakub\EventDispatcher\IEventSubscriber; + /** * Results formatter for {@see Tester} * * @author Jakub Konečný */ -interface IResultsFormatter +interface IResultsFormatter extends IEventSubscriber { - /** - * Report that we started running tests - * - * @param TestCase[] $testCases - */ - public function reportTestsStarted(array $testCases): void; - - /** - * Report that all tests finished - * - * @param TestCase[] $testCases - */ - public function reportTestsFinished(array $testCases): void; - - /** - * Report that a {@see TestCase} was started - */ - public function reportTestCaseStarted(TestCase $testCase): void; - - /** - * Report results of one {@see TestCase} - */ - public function reportTestCaseFinished(TestCase $testCase): void; - /** * Generates results of Tester run and outputs it to set file/console * diff --git a/src/ResultsFormatterEventSubscriber.php b/src/ResultsFormatterEventSubscriber.php deleted file mode 100644 index c9e526e8..00000000 --- a/src/ResultsFormatterEventSubscriber.php +++ /dev/null @@ -1,61 +0,0 @@ - [ - ["reportTestsStarted", ], - ], - Events\TestsFinished::class => [ - ["reportTestsFinished", ], - ], - Events\TestCaseStarted::class => [ - ["reportTestCaseStarted", ], - ], - Events\TestCaseFinished::class => [ - ["reportTestCaseFinished", ], - ], - ]; - } - - #[Listener(priority: AutoListenerProvider::PRIORITY_HIGH)] - public function reportTestsStarted(Events\TestsStarted $event): void - { - $this->resultsFormatter->reportTestsStarted($event->testCases); - } - - #[Listener(priority: AutoListenerProvider::PRIORITY_HIGH)] - public function reportTestsFinished(Events\TestsFinished $event): void - { - $this->resultsFormatter->reportTestsFinished($event->testCases); - } - - #[Listener(priority: AutoListenerProvider::PRIORITY_HIGH)] - public function reportTestCaseStarted(Events\TestCaseStarted $event): void - { - $this->resultsFormatter->reportTestCaseStarted($event->testCase); - } - - #[Listener(priority: AutoListenerProvider::PRIORITY_HIGH)] - public function reportTestCaseFinished(Events\TestCaseFinished $event): void - { - $this->resultsFormatter->reportTestCaseFinished($event->testCase); - } -} diff --git a/src/ResultsFormatters/AbstractResultsFormatter.php b/src/ResultsFormatters/AbstractResultsFormatter.php index 5262e1d5..fb1ff8e3 100644 --- a/src/ResultsFormatters/AbstractResultsFormatter.php +++ b/src/ResultsFormatters/AbstractResultsFormatter.php @@ -4,6 +4,11 @@ namespace MyTester\ResultsFormatters; use Ayesh\PHP_Timer\Timer; +use Konecnyjakub\EventDispatcher\AutoListenerProvider; +use MyTester\Events\TestCaseFinished; +use MyTester\Events\TestCaseStarted; +use MyTester\Events\TestsFinished; +use MyTester\Events\TestsStarted; use MyTester\IResultsFormatter; use MyTester\ResultsFormatters\Helper as ResultsHelper; use MyTester\TestCase; @@ -26,12 +31,30 @@ abstract class AbstractResultsFormatter implements IResultsFormatter /** @var int Total elapsed time in milliseconds */ protected int $totalTime = 0; - public function reportTestsStarted(array $testCases): void + public static function getSubscribedEvents(): iterable + { + return [ + TestsStarted::class => [ + ["reportTestsStarted", AutoListenerProvider::PRIORITY_HIGH, ], + ], + TestsFinished::class => [ + ["reportTestsFinished", AutoListenerProvider::PRIORITY_HIGH, ], + ], + TestCaseStarted::class => [ + ["reportTestCaseStarted", AutoListenerProvider::PRIORITY_HIGH, ], + ], + TestCaseFinished::class => [ + ["reportTestCaseFinished", AutoListenerProvider::PRIORITY_HIGH, ], + ], + ]; + } + + public function reportTestsStarted(TestsStarted $event): void { Timer::start(self::TIMER_NAME); } - public function reportTestsFinished(array $testCases): void + public function reportTestsFinished(TestsFinished $event): void { Timer::stop(self::TIMER_NAME); // @phpstan-ignore argument.type, cast.int @@ -39,13 +62,13 @@ public function reportTestsFinished(array $testCases): void $this->totalTime = $totalTime; } - public function reportTestCaseStarted(TestCase $testCase): void + public function reportTestCaseStarted(TestCaseStarted $event): void { } - public function reportTestCaseFinished(TestCase $testCase): void + public function reportTestCaseFinished(TestCaseFinished $event): void { - $this->testCases[] = $testCase; + $this->testCases[] = $event->testCase; } public function outputResults(string $outputFolder): void diff --git a/src/ResultsFormatters/Console.php b/src/ResultsFormatters/Console.php index fc1c1cd1..bf0f26da 100644 --- a/src/ResultsFormatters/Console.php +++ b/src/ResultsFormatters/Console.php @@ -4,10 +4,10 @@ namespace MyTester\ResultsFormatters; use MyTester\ConsoleColors; +use MyTester\Events\TestCaseFinished; use MyTester\IConsoleAwareResultsFormatter; use MyTester\JobResult; use MyTester\SkippedTest; -use MyTester\TestCase; use MyTester\TestWarning; /** @@ -36,10 +36,9 @@ public function setConsole(ConsoleColors $console): void $this->console = $console; } - public function reportTestCaseFinished(TestCase $testCase): void + public function reportTestCaseFinished(TestCaseFinished $event): void { - $jobs = $testCase->jobs; - foreach ($jobs as $job) { + foreach ($event->testCase->jobs as $job) { switch ($job->result) { case JobResult::SKIPPED: $this->skipped[] = new SkippedTest($job->name, (is_string($job->skip) ? $job->skip : "")); diff --git a/src/ResultsFormatters/Tap.php b/src/ResultsFormatters/Tap.php index 81434671..e25410bd 100644 --- a/src/ResultsFormatters/Tap.php +++ b/src/ResultsFormatters/Tap.php @@ -3,9 +3,9 @@ namespace MyTester\ResultsFormatters; +use MyTester\Events\TestsStarted; use MyTester\IResultsFormatter; use MyTester\JobResult; -use MyTester\TestCase; /** * TAP results formatter for Tester @@ -20,13 +20,10 @@ final class Tap extends AbstractResultsFormatter implements IResultsFormatter private int $totalTests = 0; - /** - * @param TestCase[] $testCases - */ - public function reportTestsStarted(array $testCases): void + public function reportTestsStarted(TestsStarted $event): void { - parent::reportTestsStarted($testCases); - foreach ($testCases as $testCase) { + parent::reportTestsStarted($event); + foreach ($event->testCases as $testCase) { $this->totalTests += count($testCase->jobs); } } diff --git a/src/Tester.php b/src/Tester.php index 777d7a6d..62344101 100644 --- a/src/Tester.php +++ b/src/Tester.php @@ -43,7 +43,7 @@ private function createEventDispatcher(): EventDispatcherInterface $listenerProvider->addSubscriber($extension); } - $listenerProvider->addSubscriber(new ResultsFormatterEventSubscriber($this->resultsFormatter)); + $listenerProvider->addSubscriber($this->resultsFormatter); $listenerProvider->addListener( #[Listener(priority: AutoListenerProvider::PRIORITY_HIGH - 1)] diff --git a/tests/ResultsFormatters/ConsoleTest.php b/tests/ResultsFormatters/ConsoleTest.php index 757e5e5e..2f7fafeb 100644 --- a/tests/ResultsFormatters/ConsoleTest.php +++ b/tests/ResultsFormatters/ConsoleTest.php @@ -5,6 +5,9 @@ use MyTester\Attributes\TestSuite; use MyTester\ConsoleColors; +use MyTester\Events\TestCaseFinished; +use MyTester\Events\TestsFinished; +use MyTester\Events\TestsStarted; use MyTester\TestCase; /** @@ -20,17 +23,17 @@ public function testRender(): void $console = new ConsoleColors(); $outputFormatter = new Console(); $outputFormatter->setConsole($console); - $outputFormatter->reportTestsStarted([]); + $outputFormatter->reportTestsStarted(new TestsStarted([])); $testCase1 = new TestCaseOne(); $testCase1->run(); - $outputFormatter->reportTestCaseFinished($testCase1); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase1)); $testCase2 = new TestCaseTwo(); $testCase2->run(); - $outputFormatter->reportTestCaseFinished($testCase2); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase2)); $testCase3 = new TestCaseThree(); $testCase3->run(); - $outputFormatter->reportTestCaseFinished($testCase3); - $outputFormatter->reportTestsFinished([]); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase3)); + $outputFormatter->reportTestsFinished(new TestsFinished([])); $result = $outputFormatter->render(); $result = (string) preg_replace('/[0-9]+ ms\)/', "1 ms)", $result); $this->assertMatchesFile(__DIR__ . "/console_output.txt", $result); diff --git a/tests/ResultsFormatters/JUnitTest.php b/tests/ResultsFormatters/JUnitTest.php index 4e40c2d5..a802e1f9 100644 --- a/tests/ResultsFormatters/JUnitTest.php +++ b/tests/ResultsFormatters/JUnitTest.php @@ -4,6 +4,9 @@ namespace MyTester\ResultsFormatters; use MyTester\Attributes\TestSuite; +use MyTester\Events\TestCaseFinished; +use MyTester\Events\TestsFinished; +use MyTester\Events\TestsStarted; use MyTester\TestCase; /** @@ -17,17 +20,17 @@ final class JUnitTest extends TestCase public function testRender(): void { $outputFormatter = new JUnit(); - $outputFormatter->reportTestsStarted([]); + $outputFormatter->reportTestsStarted(new TestsStarted([])); $testCase1 = new TestCaseOne(); $testCase1->run(); - $outputFormatter->reportTestCaseFinished($testCase1); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase1)); $testCase2 = new TestCaseTwo(); $testCase2->run(); - $outputFormatter->reportTestCaseFinished($testCase2); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase2)); $testCase3 = new TestCaseThree(); $testCase3->run(); - $outputFormatter->reportTestCaseFinished($testCase3); - $outputFormatter->reportTestsFinished([]); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase3)); + $outputFormatter->reportTestsFinished(new TestsFinished([])); $result = $outputFormatter->render(); $result = str_replace(__DIR__, "/var/project/tests/ResultsFormatters", $result); $result = (string) preg_replace('/time="0\.[0-9]+"/', 'time="0.001"', $result); diff --git a/tests/ResultsFormatters/TAPTest.php b/tests/ResultsFormatters/TAPTest.php index 6fff143a..f16e40fc 100644 --- a/tests/ResultsFormatters/TAPTest.php +++ b/tests/ResultsFormatters/TAPTest.php @@ -4,6 +4,9 @@ namespace MyTester\ResultsFormatters; use MyTester\Attributes\TestSuite; +use MyTester\Events\TestCaseFinished; +use MyTester\Events\TestsFinished; +use MyTester\Events\TestsStarted; use MyTester\TestCase; /** @@ -20,14 +23,14 @@ public function testRender(): void $testCase2 = new TestCaseTwo(); $testCase3 = new TestCaseThree(); $outputFormatter = new Tap(); - $outputFormatter->reportTestsStarted([$testCase1, $testCase2, $testCase3, ]); + $outputFormatter->reportTestsStarted(new TestsStarted([$testCase1, $testCase2, $testCase3, ])); $testCase1->run(); - $outputFormatter->reportTestCaseFinished($testCase1); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase1)); $testCase2->run(); - $outputFormatter->reportTestCaseFinished($testCase2); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase2)); $testCase3->run(); - $outputFormatter->reportTestCaseFinished($testCase3); - $outputFormatter->reportTestsFinished([]); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase3)); + $outputFormatter->reportTestsFinished(new TestsFinished([])); $result = $outputFormatter->render(); $this->assertMatchesFile(__DIR__ . "/tap_output.txt", $result); } diff --git a/tests/ResultsFormatters/TestDoxTest.php b/tests/ResultsFormatters/TestDoxTest.php index f3f4a671..3cefa829 100644 --- a/tests/ResultsFormatters/TestDoxTest.php +++ b/tests/ResultsFormatters/TestDoxTest.php @@ -5,6 +5,9 @@ use MyTester\Attributes\TestSuite; use MyTester\ConsoleColors; +use MyTester\Events\TestCaseFinished; +use MyTester\Events\TestsFinished; +use MyTester\Events\TestsStarted; use MyTester\TestCase; /** @@ -20,17 +23,17 @@ public function testRender(): void $console = new ConsoleColors(); $outputFormatter = new TestDox(); $outputFormatter->setConsole($console); - $outputFormatter->reportTestsStarted([]); + $outputFormatter->reportTestsStarted(new TestsStarted([])); $testCase1 = new TestCaseOne(); $testCase1->run(); - $outputFormatter->reportTestCaseFinished($testCase1); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase1)); $testCase2 = new TestCaseTwo(); $testCase2->run(); - $outputFormatter->reportTestCaseFinished($testCase2); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase2)); $testCase3 = new TestCaseThree(); $testCase3->run(); - $outputFormatter->reportTestCaseFinished($testCase3); - $outputFormatter->reportTestsFinished([]); + $outputFormatter->reportTestCaseFinished(new TestCaseFinished($testCase3)); + $outputFormatter->reportTestsFinished(new TestsFinished([])); $result = $outputFormatter->render(); $this->assertMatchesFile(__DIR__ . "/testdox_output.txt", $result); }