|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the zenstruck/browser package. |
| 5 | + * |
| 6 | + * (c) Kevin Bond <kevinbond@gmail.com> |
| 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\Artifact; |
| 13 | + |
| 14 | +use Zenstruck\Browser\BrowserRegistry; |
| 15 | + |
| 16 | +/** |
| 17 | + * Drives the test-or-scenario lifecycle that captures browser state on failure |
| 18 | + * and accumulates the "Saved Browser Artifacts" summary printed at end of suite. |
| 19 | + * Test-runner-agnostic: PHPUnit and Behat each wire their own events to these |
| 20 | + * methods. |
| 21 | + * |
| 22 | + * @author Hugo Hamon <hello@kodero.fr> |
| 23 | + */ |
| 24 | +final class ArtifactCollector |
| 25 | +{ |
| 26 | + /** @var array<string, array<string, string[]>> */ |
| 27 | + private array $savedArtifacts = []; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly BrowserRegistry $registry, |
| 31 | + private readonly ArtifactSink $sink, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function onSuiteStart(): void |
| 36 | + { |
| 37 | + $this->registry->start(); |
| 38 | + } |
| 39 | + |
| 40 | + public function onScenarioStart(string $name): void |
| 41 | + { |
| 42 | + $this->registry->clear(); |
| 43 | + } |
| 44 | + |
| 45 | + public function onScenarioFailed(string $name, FailureType $type): void |
| 46 | + { |
| 47 | + if ($this->registry->isEmpty()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $filename = \sprintf('%s_%s', $type->value, self::normalizeName($name)); |
| 52 | + |
| 53 | + foreach ($this->registry->all() as $i => $browser) { |
| 54 | + try { |
| 55 | + $browser->saveCurrentState("{$filename}__{$i}"); |
| 56 | + } catch (\Throwable) { |
| 57 | + // swallow exceptions related to dumping the current state so as to not |
| 58 | + // lose the actual error/failure being reported by the test runner |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function onScenarioFinish(string $name): void |
| 64 | + { |
| 65 | + foreach ($this->registry->all() as $browser) { |
| 66 | + foreach ($browser->savedArtifacts() as $category => $artifacts) { |
| 67 | + if (\count($artifacts) === 0) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + $this->savedArtifacts[$name][$category] = $artifacts; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $this->registry->clear(); |
| 76 | + } |
| 77 | + |
| 78 | + public function onSuiteFinish(): void |
| 79 | + { |
| 80 | + $this->sink->writeSummary($this->savedArtifacts); |
| 81 | + $this->registry->stop(); |
| 82 | + } |
| 83 | + |
| 84 | + private static function normalizeName(string $name): string |
| 85 | + { |
| 86 | + if (!\mb_strstr($name, 'with data set')) { |
| 87 | + return \strtr($name, '\\:', '-_'); |
| 88 | + } |
| 89 | + |
| 90 | + // Try to match for a numeric data set index. If it didn't, match for a string one. |
| 91 | + if (!\preg_match('#^(?<test>[\w:\\\]+) with data set \#(?<dataset>\d+)#', $name, $matches)) { |
| 92 | + \preg_match('#^(?<test>[\w:\\\]+) with data set "(?<dataset>.*)"#', $name, $matches); |
| 93 | + } |
| 94 | + |
| 95 | + $normalized = \strtr($matches['test'], '\\:', '-_'); |
| 96 | + |
| 97 | + if (isset($matches['dataset'])) { |
| 98 | + $normalized .= '__data-set-'.\preg_replace('/\W+/', '-', $matches['dataset']); |
| 99 | + } |
| 100 | + |
| 101 | + return $normalized; |
| 102 | + } |
| 103 | +} |
0 commit comments