Skip to content

Commit c2db829

Browse files
Implement Test\PreparationFailed event
1 parent 91a0a66 commit c2db829

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed

ChangeLog-10.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes of the PHPUnit 10.4 release series are documented in this fi
1010
* [#5462](https://github.com/sebastianbergmann/phpunit/pull/5462): Support for multiple arguments
1111
* [#5471](https://github.com/sebastianbergmann/phpunit/issues/5471): `assertFileMatchesFormat()` and `assertFileMatchesFormatFile()`
1212
* [#5515](https://github.com/sebastianbergmann/phpunit/issues/5515): `PHPUnit\Runner\Extension\Facade::requireExportOfObjects()` so that test runner extensions can indicate that they require the export of objects for events such as `Test\AssertionSucceeded` and `Test\AssertionFailed`
13+
* `Test\PreparationFailed` event
1314
* Attribute `id` attribute for `testCaseMethod` elements in the XML document generated by `--list-tests-xml`
1415

1516
### Changed

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,20 @@ public function testPreparationStarted(Code\Test $test): void
307307
);
308308
}
309309

310+
/**
311+
* @throws InvalidArgumentException
312+
* @throws UnknownEventTypeException
313+
*/
314+
public function testPreparationFailed(Code\Test $test): void
315+
{
316+
$this->dispatcher->dispatch(
317+
new Test\PreparationFailed(
318+
$this->telemetryInfo(),
319+
$test,
320+
),
321+
);
322+
}
323+
310324
/**
311325
* @psalm-param class-string $testClassName
312326
*

src/Event/Emitter/Emitter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function testSuiteStarted(TestSuite $testSuite): void;
6363

6464
public function testPreparationStarted(Code\Test $test): void;
6565

66+
public function testPreparationFailed(Code\Test $test): void;
67+
6668
/**
6769
* @psalm-param class-string $testClassName
6870
*/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 function sprintf;
13+
use PHPUnit\Event\Code;
14+
use PHPUnit\Event\Event;
15+
use PHPUnit\Event\Telemetry;
16+
17+
/**
18+
* @psalm-immutable
19+
*
20+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
21+
*/
22+
final class PreparationFailed implements Event
23+
{
24+
private readonly Telemetry\Info $telemetryInfo;
25+
private readonly Code\Test $test;
26+
27+
public function __construct(Telemetry\Info $telemetryInfo, Code\Test $test)
28+
{
29+
$this->telemetryInfo = $telemetryInfo;
30+
$this->test = $test;
31+
}
32+
33+
public function telemetryInfo(): Telemetry\Info
34+
{
35+
return $this->telemetryInfo;
36+
}
37+
38+
public function test(): Code\Test
39+
{
40+
return $this->test;
41+
}
42+
43+
public function asString(): string
44+
{
45+
return sprintf(
46+
'Test Preparation Failed (%s)',
47+
$this->test->id(),
48+
);
49+
}
50+
}
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 PreparationFailedSubscriber extends Subscriber
18+
{
19+
public function notify(PreparationFailed $event): void;
20+
}

src/Event/Facade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ private function registerDefaultTypes(TypeMap $typeMap): void
204204
Test\PreConditionFinished::class,
205205
Test\PreparationStarted::class,
206206
Test\Prepared::class,
207+
Test\PreparationFailed::class,
207208
Test\PrintedUnexpectedOutput::class,
208209
Test\Skipped::class,
209210
Test\WarningTriggered::class,

src/Framework/TestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ final public function runBare(): void
673673
} catch (AssertionError|AssertionFailedError $e) {
674674
if (!$this->wasPrepared) {
675675
$this->wasPrepared = true;
676+
677+
$emitter->testPreparationFailed(
678+
$this->valueObjectForEvents(),
679+
);
676680
}
677681

678682
$this->status = TestStatus::failure($e->getMessage());
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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(PreparationFailed::class)]
17+
#[Small]
18+
final class PreparationFailedTest extends AbstractEventTestCase
19+
{
20+
public function testConstructorSetsValues(): void
21+
{
22+
$telemetryInfo = $this->telemetryInfo();
23+
$test = $this->testValueObject();
24+
25+
$event = new PreparationFailed(
26+
$telemetryInfo,
27+
$test,
28+
);
29+
30+
$this->assertSame($telemetryInfo, $event->telemetryInfo());
31+
$this->assertSame($test, $event->test());
32+
}
33+
34+
public function testCanBeRepresentedAsString(): void
35+
{
36+
$event = new PreparationFailed(
37+
$this->telemetryInfo(),
38+
$this->testValueObject(),
39+
);
40+
41+
$this->assertSame('Test Preparation Failed (FooTest::testBar)', $event->asString());
42+
}
43+
}

0 commit comments

Comments
 (0)