Skip to content

Commit

Permalink
changed results formatters into event subscribers
Browse files Browse the repository at this point in the history
this closes #42
  • Loading branch information
konecnyjakub committed Dec 27, 2024
1 parent c30fa53 commit c3642f7
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 124 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion RELEASE_NOTES
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
28 changes: 3 additions & 25 deletions src/IResultsFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
61 changes: 0 additions & 61 deletions src/ResultsFormatterEventSubscriber.php

This file was deleted.

33 changes: 28 additions & 5 deletions src/ResultsFormatters/AbstractResultsFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,26 +31,44 @@ 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
$totalTime = (int) Timer::read(self::TIMER_NAME, Timer::FORMAT_PRECISE);
$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
Expand Down
7 changes: 3 additions & 4 deletions src/ResultsFormatters/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 : ""));
Expand Down
11 changes: 4 additions & 7 deletions src/ResultsFormatters/Tap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
13 changes: 8 additions & 5 deletions tests/ResultsFormatters/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions tests/ResultsFormatters/JUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions tests/ResultsFormatters/TAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Expand Down
13 changes: 8 additions & 5 deletions tests/ResultsFormatters/TestDoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Expand Down

0 comments on commit c3642f7

Please sign in to comment.