|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the zenstruck/browser package. |
| 5 | + * |
| 6 | + * (c) Kevin Bond <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Zenstruck\Browser\Test; |
| 13 | + |
| 14 | +use PHPUnit\Event\Code\Test; |
| 15 | +use PHPUnit\Event\Test\Errored; |
| 16 | +use PHPUnit\Event\Test\ErroredSubscriber; |
| 17 | +use PHPUnit\Event\Test\Failed; |
| 18 | +use PHPUnit\Event\Test\FailedSubscriber; |
| 19 | +use PHPUnit\Event\Test\Finished as TestFinishedEvent; |
| 20 | +use PHPUnit\Event\Test\FinishedSubscriber as TestFinishedSubscriber; |
| 21 | +use PHPUnit\Event\Test\PreparationStarted as TestStartedEvent; |
| 22 | +use PHPUnit\Event\Test\PreparationStartedSubscriber as TestStartedSubscriber; |
| 23 | +use PHPUnit\Event\TestRunner\Finished as TestRunnerFinishedEvent; |
| 24 | +use PHPUnit\Event\TestRunner\FinishedSubscriber as TestRunnerFinishedSubscriber; |
| 25 | +use PHPUnit\Event\TestRunner\Started as TestRunnerStartedEvent; |
| 26 | +use PHPUnit\Event\TestRunner\StartedSubscriber as TestRunnerStartedSubscriber; |
| 27 | +use PHPUnit\Runner\Extension\Facade; |
| 28 | +use PHPUnit\Runner\Extension\ParameterCollection; |
| 29 | +use PHPUnit\TextUI\Configuration\Configuration; |
| 30 | +use Zenstruck\Browser; |
| 31 | + |
| 32 | +class BootstrappedExtension |
| 33 | +{ |
| 34 | + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void |
| 35 | + { |
| 36 | + $extension = new LegacyExtension(); |
| 37 | + |
| 38 | + $facade->registerSubscriber(new class($extension) implements TestRunnerStartedSubscriber { |
| 39 | + public function __construct( |
| 40 | + private LegacyExtension $extension, |
| 41 | + ) { |
| 42 | + } |
| 43 | + |
| 44 | + public function notify(TestRunnerStartedEvent $event): void |
| 45 | + { |
| 46 | + $this->extension->executeBeforeFirstTest(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + $facade->registerSubscriber(new class($extension) implements TestRunnerFinishedSubscriber { |
| 51 | + public function __construct( |
| 52 | + private LegacyExtension $extension, |
| 53 | + ) { |
| 54 | + } |
| 55 | + |
| 56 | + public function notify(TestRunnerFinishedEvent $event): void |
| 57 | + { |
| 58 | + $this->extension->executeAfterLastTest(); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + $facade->registerSubscriber(new class($extension) implements TestStartedSubscriber { |
| 63 | + public function __construct( |
| 64 | + private LegacyExtension $extension, |
| 65 | + ) { |
| 66 | + } |
| 67 | + public function notify(TestStartedEvent $event): void |
| 68 | + { |
| 69 | + $this->extension->executeBeforeTest($event->test()->name()); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + $facade->registerSubscriber(new class($extension) implements TestFinishedSubscriber { |
| 74 | + public function __construct( |
| 75 | + private LegacyExtension $extension, |
| 76 | + ) { |
| 77 | + } |
| 78 | + |
| 79 | + public function notify(TestFinishedEvent $event): void |
| 80 | + { |
| 81 | + $this->extension->executeAfterTest( |
| 82 | + $event->test()->name(), |
| 83 | + (float) $event->telemetryInfo()->time()->seconds() |
| 84 | + ); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + $facade->registerSubscriber(new class($extension) implements ErroredSubscriber { |
| 89 | + public function __construct( |
| 90 | + private LegacyExtension $extension, |
| 91 | + ) { |
| 92 | + } |
| 93 | + |
| 94 | + public function notify(Errored $event): void |
| 95 | + { |
| 96 | + $this->extension->executeAfterTestError( |
| 97 | + BootstrappedExtension::testName($event->test()), |
| 98 | + $event->throwable()->message(), |
| 99 | + (float) $event->telemetryInfo()->time()->seconds() |
| 100 | + ); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + $facade->registerSubscriber(new class($extension) implements FailedSubscriber { |
| 105 | + public function __construct( |
| 106 | + private LegacyExtension $extension, |
| 107 | + ) { |
| 108 | + } |
| 109 | + |
| 110 | + public function notify(Failed $event): void |
| 111 | + { |
| 112 | + $this->extension->executeAfterTestFailure( |
| 113 | + BootstrappedExtension::testName($event->test()), |
| 114 | + $event->throwable()->message(), |
| 115 | + (float) $event->telemetryInfo()->time()->seconds()) |
| 116 | + ; |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @internal |
| 123 | + */ |
| 124 | + public static function testName(Test $test): string |
| 125 | + { |
| 126 | + if ($test->isTestMethod()) { |
| 127 | + return $test->nameWithClass(); |
| 128 | + } |
| 129 | + |
| 130 | + return $test->name(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @internal |
| 135 | + */ |
| 136 | + public static function registerBrowser(Browser $browser): void |
| 137 | + { |
| 138 | + LegacyExtension::registerBrowser($browser); |
| 139 | + } |
| 140 | +} |
0 commit comments