|
13 | 13 |
|
14 | 14 | namespace Symfony\Component\Panther; |
15 | 15 |
|
| 16 | +use PHPUnit\Event\Test\Errored; |
| 17 | +use PHPUnit\Event\Test\ErroredSubscriber; |
| 18 | +use PHPUnit\Event\Test\Failed; |
| 19 | +use PHPUnit\Event\Test\FailedSubscriber; |
| 20 | +use PHPUnit\Event\Test\Finished as TestFinishedEvent; |
| 21 | +use PHPUnit\Event\Test\FinishedSubscriber as TestFinishedSubscriber; |
| 22 | +use PHPUnit\Event\Test\PreparationStarted as TestStartedEvent; |
| 23 | +use PHPUnit\Event\Test\PreparationStartedSubscriber as TestStartedSubscriber; |
| 24 | +use PHPUnit\Event\TestRunner\Finished as TestRunnerFinishedEvent; |
| 25 | +use PHPUnit\Event\TestRunner\FinishedSubscriber as TestRunnerFinishedSubscriber; |
| 26 | +use PHPUnit\Event\TestRunner\Started as TestRunnerStartedEvent; |
| 27 | +use PHPUnit\Event\TestRunner\StartedSubscriber as TestRunnerStartedSubscriber; |
16 | 28 | use PHPUnit\Runner\AfterLastTestHook; |
17 | 29 | use PHPUnit\Runner\AfterTestErrorHook; |
18 | 30 | use PHPUnit\Runner\AfterTestFailureHook; |
19 | 31 | use PHPUnit\Runner\AfterTestHook; |
20 | 32 | use PHPUnit\Runner\BeforeFirstTestHook; |
21 | 33 | use PHPUnit\Runner\BeforeTestHook; |
| 34 | +use PHPUnit\Runner\Extension\Extension; |
| 35 | +use PHPUnit\Runner\Extension\Facade; |
| 36 | +use PHPUnit\Runner\Extension\ParameterCollection; |
| 37 | +use PHPUnit\TextUI\Configuration\Configuration; |
22 | 38 |
|
23 | | -/** |
| 39 | +/* |
24 | 40 | * @author Dany Maillard <[email protected]> |
25 | 41 | */ |
26 | | -final class ServerExtension implements BeforeFirstTestHook, AfterLastTestHook, BeforeTestHook, AfterTestHook, AfterTestErrorHook, AfterTestFailureHook |
27 | | -{ |
28 | | - use ServerTrait; |
29 | | - |
30 | | - private static bool $enabled = false; |
31 | | - |
32 | | - /** @var Client[] */ |
33 | | - private static array $registeredClients = []; |
34 | | - |
35 | | - public static function registerClient(Client $client): void |
| 42 | +if (interface_exists(Extension::class)) { |
| 43 | + /** |
| 44 | + * PHPUnit >= 10. |
| 45 | + */ |
| 46 | + final class ServerExtension implements Extension |
36 | 47 | { |
37 | | - if (self::$enabled && !\in_array($client, self::$registeredClients, true)) { |
38 | | - self::$registeredClients[] = $client; |
| 48 | + public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void |
| 49 | + { |
| 50 | + $extension = new ServerExtensionLegacy(); |
| 51 | + |
| 52 | + $facade->registerSubscriber(new class($extension) implements TestRunnerStartedSubscriber { |
| 53 | + public function __construct(private $extension) |
| 54 | + { |
| 55 | + } |
| 56 | + |
| 57 | + public function notify(TestRunnerStartedEvent $event): void |
| 58 | + { |
| 59 | + $this->extension->executeBeforeFirstTest(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + $facade->registerSubscriber(new class($extension) implements TestRunnerFinishedSubscriber { |
| 64 | + public function __construct(private $extension) |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | + public function notify(TestRunnerFinishedEvent $event): void |
| 69 | + { |
| 70 | + $this->extension->executeAfterLastTest(); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + $facade->registerSubscriber(new class($extension) implements TestStartedSubscriber { |
| 75 | + public function __construct(private $extension) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + public function notify(TestStartedEvent $event): void |
| 80 | + { |
| 81 | + $this->extension->executeBeforeTest(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + $facade->registerSubscriber(new class($extension) implements TestFinishedSubscriber { |
| 86 | + public function __construct(private $extension) |
| 87 | + { |
| 88 | + } |
| 89 | + |
| 90 | + public function notify(TestFinishedEvent $event): void |
| 91 | + { |
| 92 | + $this->extension->executeAfterTest(); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + $facade->registerSubscriber(new class($extension) implements ErroredSubscriber { |
| 97 | + public function __construct(private $extension) |
| 98 | + { |
| 99 | + } |
| 100 | + |
| 101 | + public function notify(Errored $event): void |
| 102 | + { |
| 103 | + $this->extension->executeAfterTestError(); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + $facade->registerSubscriber(new class($extension) implements FailedSubscriber { |
| 108 | + public function __construct(private $extension) |
| 109 | + { |
| 110 | + } |
| 111 | + |
| 112 | + public function notify(Failed $event): void |
| 113 | + { |
| 114 | + $this->extension->executeAfterTestFailure(); |
| 115 | + } |
| 116 | + }); |
39 | 117 | } |
40 | | - } |
41 | | - |
42 | | - public function executeBeforeFirstTest(): void |
43 | | - { |
44 | | - self::$enabled = true; |
45 | | - $this->keepServerOnTeardown(); |
46 | | - } |
47 | 118 |
|
48 | | - public function executeAfterLastTest(): void |
49 | | - { |
50 | | - $this->stopWebServer(); |
51 | | - } |
52 | | - |
53 | | - public function executeBeforeTest(string $test): void |
54 | | - { |
55 | | - self::reset(); |
56 | | - } |
57 | | - |
58 | | - public function executeAfterTest(string $test, float $time): void |
59 | | - { |
60 | | - self::reset(); |
61 | | - } |
62 | | - |
63 | | - public function executeAfterTestError(string $test, string $message, float $time): void |
64 | | - { |
65 | | - $this->pause(sprintf('Error: %s', $message)); |
66 | | - } |
67 | | - |
68 | | - public function executeAfterTestFailure(string $test, string $message, float $time): void |
69 | | - { |
70 | | - $this->pause(sprintf('Failure: %s', $message)); |
71 | | - } |
72 | | - |
73 | | - private static function reset(): void |
74 | | - { |
75 | | - self::$registeredClients = []; |
| 119 | + public static function registerClient(Client $client): void |
| 120 | + { |
| 121 | + ServerExtensionLegacy::registerClient($client); |
| 122 | + } |
76 | 123 | } |
77 | | - |
78 | | - public static function takeScreenshots(string $type, string $test): void |
| 124 | +} elseif (interface_exists(BeforeFirstTestHook::class)) { |
| 125 | + /** |
| 126 | + * PHPUnit < 10. |
| 127 | + */ |
| 128 | + final class ServerExtension extends ServerExtensionLegacy implements BeforeFirstTestHook, BeforeTestHook, AfterTestHook, AfterLastTestHook, AfterTestErrorHook, AfterTestFailureHook |
79 | 129 | { |
80 | | - if (!self::$enabled || !($_SERVER['PANTHER_ERROR_SCREENSHOT_DIR'] ?? false)) { |
81 | | - return; |
82 | | - } |
83 | | - |
84 | | - foreach (self::$registeredClients as $i => $client) { |
85 | | - $screenshotPath = sprintf('%s/%s_%s_%s-%d.png', |
86 | | - $_SERVER['PANTHER_ERROR_SCREENSHOT_DIR'], |
87 | | - date('Y-m-d_H-i-s'), |
88 | | - $type, |
89 | | - strtr($test, ['\\' => '-', ':' => '_']), |
90 | | - $i |
91 | | - ); |
92 | | - $client->takeScreenshot($screenshotPath); |
93 | | - if ($_SERVER['PANTHER_ERROR_SCREENSHOT_ATTACH'] ?? false) { |
94 | | - printf('[[ATTACHMENT|%s]]', $screenshotPath); |
95 | | - } |
96 | | - } |
97 | 130 | } |
| 131 | +} else { |
| 132 | + exit("Failed to initialize Symfony\Component\Panther\ServerExtension, undetectable or unsupported phpunit version."); |
98 | 133 | } |
0 commit comments