Skip to content

Commit dd1de42

Browse files
Implement TestCase::provideAdditionalInformation()
1 parent bb6509e commit dd1de42

File tree

11 files changed

+275
-0
lines changed

11 files changed

+275
-0
lines changed

ChangeLog-12.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes of the PHPUnit 12.2 release series are documented in this fi
77
### Added
88

99
* `--with-telemetry` CLI option that can be used together with `--debug` to print debugging information that includes telemetry information
10+
* The `TestCase::provideAdditionalInformation()` method can now be used to emit a `Test\AdditionalInformationProvided` event
1011
* The new `Test\AfterLastTestMethodFailed`, `Test\AfterTestMethodFailed`, `Test\BeforeFirstTestMethodFailed`, `Test\BeforeTestMethodFailed`, `Test\PostConditionFailed`, `Test\PreConditionFailed` events are now emitted instead of `Test\AfterLastTestMethodErrored`, `Test\AfterTestMethodErrored`, `Test\BeforeFirstTestMethodErrored`, `Test\BeforeTestMethodErrored`, `Test\PostConditionErrored`, `Test\PreConditionErrored` when the `Throwable` extends `AssertionFailedError` to distinguish between errors and failures triggered in hook methods
1112
* The new `Test\PreparationErrored` event is now emitted instead of `Test\PreparationFailed` when the `Throwable` does not extend `AssertionFailedError` to distinguish between errors and failures triggered during test preparation
1213
* `Test\PreparationFailed::throwable()`

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,23 @@ public function testPrintedUnexpectedOutput(string $output): void
10241024
);
10251025
}
10261026

1027+
/**
1028+
* @param non-empty-string $additionalInformation
1029+
*
1030+
* @throws InvalidArgumentException
1031+
* @throws UnknownEventTypeException
1032+
*/
1033+
public function testProvidedAdditionalInformation(TestMethod $test, string $additionalInformation): void
1034+
{
1035+
$this->dispatcher->dispatch(
1036+
new Test\AdditionalInformationProvided(
1037+
$this->telemetryInfo(),
1038+
$test,
1039+
$additionalInformation,
1040+
),
1041+
);
1042+
}
1043+
10271044
/**
10281045
* @param non-negative-int $numberOfAssertionsPerformed
10291046
*

src/Event/Emitter/Emitter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ public function testTriggeredPhpunitWarning(Code\Test $test, string $message): v
240240
*/
241241
public function testPrintedUnexpectedOutput(string $output): void;
242242

243+
/**
244+
* @param non-empty-string $additionalInformation
245+
*/
246+
public function testProvidedAdditionalInformation(TestMethod $test, string $additionalInformation): void;
247+
243248
/**
244249
* @param non-negative-int $numberOfAssertionsPerformed
245250
*/
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Event\Test;
11+
12+
use const PHP_EOL;
13+
use function sprintf;
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\Event;
16+
use PHPUnit\Event\Telemetry;
17+
18+
/**
19+
* @immutable
20+
*
21+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
22+
*/
23+
final readonly class AdditionalInformationProvided implements Event
24+
{
25+
private Telemetry\Info $telemetryInfo;
26+
private TestMethod $test;
27+
28+
/**
29+
* @var non-empty-string
30+
*/
31+
private string $additionalInformation;
32+
33+
/**
34+
* @param non-empty-string $additionalInformation
35+
*/
36+
public function __construct(Telemetry\Info $telemetryInfo, TestMethod $test, string $additionalInformation)
37+
{
38+
$this->telemetryInfo = $telemetryInfo;
39+
$this->test = $test;
40+
$this->additionalInformation = $additionalInformation;
41+
}
42+
43+
public function telemetryInfo(): Telemetry\Info
44+
{
45+
return $this->telemetryInfo;
46+
}
47+
48+
public function test(): TestMethod
49+
{
50+
return $this->test;
51+
}
52+
53+
/**
54+
* @return non-empty-string
55+
*/
56+
public function additionalInformation(): string
57+
{
58+
return $this->additionalInformation;
59+
}
60+
61+
/**
62+
* @return non-empty-string
63+
*/
64+
public function asString(): string
65+
{
66+
return sprintf(
67+
'Test Provided Additional Information%s%s',
68+
PHP_EOL,
69+
$this->additionalInformation,
70+
);
71+
}
72+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Event\Test;
11+
12+
use PHPUnit\Event\Subscriber;
13+
14+
/**
15+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16+
*/
17+
interface AdditionalInformationProvidedSubscriber extends Subscriber
18+
{
19+
public function notify(AdditionalInformationProvided $event): void;
20+
}

src/Event/Facade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ private function registerDefaultTypes(TypeMap $typeMap): void
189189
Test\BeforeTestMethodErrored::class,
190190
Test\BeforeTestMethodFailed::class,
191191
Test\BeforeTestMethodFinished::class,
192+
Test\AdditionalInformationProvided::class,
192193
Test\ComparatorRegistered::class,
193194
Test\ConsideredRisky::class,
194195
Test\DeprecationTriggered::class,

src/Framework/TestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,17 @@ final protected function createPartialMock(string $type, array $methods): MockOb
12421242
return $partialMock;
12431243
}
12441244

1245+
/**
1246+
* @param non-empty-string $additionalInformation
1247+
*/
1248+
final protected function provideAdditionalInformation(string $additionalInformation): void
1249+
{
1250+
Event\Facade::emitter()->testProvidedAdditionalInformation(
1251+
$this->valueObjectForEvents(),
1252+
$additionalInformation,
1253+
);
1254+
}
1255+
12451256
protected function transformException(Throwable $t): Throwable
12461257
{
12471258
return $t;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\Event;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class AdditionalInformationTest extends TestCase
15+
{
16+
public function testSuccess(): void
17+
{
18+
$this->assertTrue(true);
19+
20+
$this->provideAdditionalInformation('additional information');
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
The right events are emitted in the right order for a successful test that provides additional information
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--debug';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/AdditionalInformationTest.php';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (PHPUnit %s using %s)
15+
Test Runner Configured
16+
Event Facade Sealed
17+
Test Suite Loaded (1 test)
18+
Test Runner Started
19+
Test Suite Sorted
20+
Test Runner Execution Started (1 test)
21+
Test Suite Started (PHPUnit\TestFixture\Event\AdditionalInformationTest, 1 test)
22+
Test Preparation Started (PHPUnit\TestFixture\Event\AdditionalInformationTest::testSuccess)
23+
Test Prepared (PHPUnit\TestFixture\Event\AdditionalInformationTest::testSuccess)
24+
Test Provided Additional Information
25+
additional information
26+
Test Passed (PHPUnit\TestFixture\Event\AdditionalInformationTest::testSuccess)
27+
Test Finished (PHPUnit\TestFixture\Event\AdditionalInformationTest::testSuccess)
28+
Test Suite Finished (PHPUnit\TestFixture\Event\AdditionalInformationTest, 1 test)
29+
Test Runner Execution Finished
30+
Test Runner Finished
31+
PHPUnit Finished (Shell Exit Code: 0)

tests/unit/Event/Emitter/DispatchingEmitterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,47 @@ public function notify(Test\PrintedUnexpectedOutput $event): void
25312531
$this->assertSame($output, $event->output());
25322532
}
25332533

2534+
#[TestDox('testProvidedAdditionalInformation() emits Test\AdditionalInformationProvided event')]
2535+
public function testTestProvidedAdditionalInformationEmitsAdditionalInformationProvidedEvent(): void
2536+
{
2537+
$subscriber = new class extends RecordingSubscriber implements Test\AdditionalInformationProvidedSubscriber
2538+
{
2539+
public function notify(Test\AdditionalInformationProvided $event): void
2540+
{
2541+
$this->record($event);
2542+
}
2543+
};
2544+
2545+
$dispatcher = $this->dispatcherWithRegisteredSubscriber(
2546+
Test\AdditionalInformationProvidedSubscriber::class,
2547+
Test\AdditionalInformationProvided::class,
2548+
$subscriber,
2549+
);
2550+
2551+
$telemetrySystem = $this->telemetrySystem();
2552+
$test = $this->testValueObject();
2553+
2554+
$emitter = new DispatchingEmitter(
2555+
$dispatcher,
2556+
$telemetrySystem,
2557+
);
2558+
2559+
$additionalInformation = 'addtional information';
2560+
2561+
$emitter->testProvidedAdditionalInformation(
2562+
$test,
2563+
$additionalInformation,
2564+
);
2565+
2566+
$this->assertSame(1, $subscriber->recordedEventCount());
2567+
2568+
$event = $subscriber->lastRecordedEvent();
2569+
2570+
$this->assertInstanceOf(Test\AdditionalInformationProvided::class, $event);
2571+
$this->assertSame($test, $event->test());
2572+
$this->assertSame($additionalInformation, $event->additionalInformation());
2573+
}
2574+
25342575
#[TestDox('testFinished() emits Test\Finished event')]
25352576
public function testTestFinishedEmitsTestFinishedEvent(): void
25362577
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Event\Test;
11+
12+
use PHPUnit\Event\AbstractEventTestCase;
13+
use PHPUnit\Framework\Attributes\CoversClass;
14+
use PHPUnit\Framework\Attributes\Small;
15+
16+
#[CoversClass(AdditionalInformationProvided::class)]
17+
#[Small]
18+
final class AdditionalInformationProvidedTest extends AbstractEventTestCase
19+
{
20+
public function testConstructorSetsValues(): void
21+
{
22+
$telemetryInfo = $this->telemetryInfo();
23+
$test = $this->testValueObject();
24+
$additionalInformation = 'additional information';
25+
26+
$event = new AdditionalInformationProvided(
27+
$telemetryInfo,
28+
$test,
29+
$additionalInformation,
30+
);
31+
32+
$this->assertSame($telemetryInfo, $event->telemetryInfo());
33+
$this->assertSame($test, $event->test());
34+
$this->assertSame($additionalInformation, $event->additionalInformation());
35+
}
36+
37+
public function testCanBeRepresentedAsString(): void
38+
{
39+
$event = new AdditionalInformationProvided(
40+
$this->telemetryInfo(),
41+
$this->testValueObject(),
42+
'additional information',
43+
);
44+
45+
$this->assertStringEqualsStringIgnoringLineEndings(
46+
<<<'EOT'
47+
Test Provided Additional Information
48+
additional information
49+
EOT
50+
,
51+
$event->asString(),
52+
);
53+
}
54+
}

0 commit comments

Comments
 (0)