|
2 | 2 |
|
3 | 3 | namespace Zenstruck\Foundry\Test\Behat\Exception; |
4 | 4 |
|
| 5 | +use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested; |
| 6 | +use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested; |
| 7 | + |
5 | 8 | final class InvalidResetDbTag extends \LogicException |
6 | 9 | { |
7 | | - public static function bothTagsUsed(): self |
| 10 | + public function __construct(string $message, BeforeFeatureTested|BeforeScenarioTested $event) |
| 11 | + { |
| 12 | + $file = $event->getFeature()->getFile(); |
| 13 | + |
| 14 | + if (!$file) { |
| 15 | + parent::__construct($message); |
| 16 | + |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + $suite = $event->getEnvironment()->getSuite(); |
| 21 | + if ($suite->hasSetting('paths')) { |
| 22 | + foreach ($suite->getSetting('paths') as $path) { |
| 23 | + if (!$path) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + if (str_contains($file, $path)) { |
| 28 | + $file = substr($file, strpos($file, $path)); // @phpstan-ignore argument.type (strpos cannot be false if $path is contained in $file) |
| 29 | + break; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + $errorFileAndLine = match($event::class){ |
| 35 | + BeforeFeatureTested::class => "{$file}:{$event->getFeature()->getLine()}", |
| 36 | + BeforeScenarioTested::class => "{$file}:{$event->getScenario()->getLine()}", |
| 37 | + }; |
| 38 | + |
| 39 | + parent::__construct("$message\nAt $errorFileAndLine"); |
| 40 | + } |
| 41 | + |
| 42 | + public static function bothTagsUsed(BeforeScenarioTested $event): self |
8 | 43 | { |
9 | | - return new self('Cannot use both "@resetDB" and "@noResetDB" tags at the same time.'); |
| 44 | + return new self('Cannot use both "@resetDB" and "@noResetDB" tags at the same time.', $event); |
10 | 45 | } |
11 | 46 |
|
12 | | - public static function resetDbWithScenarioMode(): self |
| 47 | + public static function resetDbWithScenarioMode(BeforeFeatureTested|BeforeScenarioTested $event): self |
13 | 48 | { |
14 | | - return new self('Cannot use "@noResetDB" tag with database_reset_mode set as "manual".'); |
| 49 | + return new self('Cannot use "@noResetDB" tag with database_reset_mode set as "manual".', $event); |
15 | 50 | } |
16 | 51 |
|
17 | | - public static function noResetDbWithManualMode(): self |
| 52 | + public static function noResetDbWithManualMode(BeforeFeatureTested|BeforeScenarioTested $event): self |
18 | 53 | { |
19 | | - return new self('Cannot use "@noResetDB" tag with database_reset_mode set as "manual".'); |
| 54 | + return new self('Cannot use "@noResetDB" tag with database_reset_mode set as "manual".', $event); |
20 | 55 | } |
21 | 56 |
|
22 | | - public static function resetDbOnFeatureWithFeatureMode(): self |
| 57 | + public static function resetDbOnFeatureWithFeatureMode(BeforeFeatureTested $event): self |
23 | 58 | { |
24 | | - return new self('Cannot use "@resetDB" tag on a feature with database_reset_mode set as "feature".'); |
| 59 | + return new self('Cannot use "@resetDB" tag on a feature with database_reset_mode set as "feature".', $event); |
25 | 60 | } |
26 | 61 |
|
27 | | - public static function resetDbOnScenarioWithScenarioMode(): self |
| 62 | + public static function resetDbOnScenarioWithScenarioMode(BeforeScenarioTested $event): self |
28 | 63 | { |
29 | | - return new self('Cannot use "@resetDB" tag on a scenario with database_reset_mode set as "scenario".'); |
| 64 | + return new self('Cannot use "@resetDB" tag on a scenario with database_reset_mode set as "scenario".', $event); |
30 | 65 | } |
31 | 66 |
|
32 | | - public static function noResetDbWithFeatureMode(): self |
| 67 | + public static function noResetDbWithFeatureMode(BeforeFeatureTested|BeforeScenarioTested $event): self |
33 | 68 | { |
34 | | - return new self('Cannot use "@noResetDB" with database_reset_mode set as "feature".'); |
| 69 | + return new self('Cannot use "@noResetDB" with database_reset_mode set as "feature".', $event); |
35 | 70 | } |
36 | 71 | } |
0 commit comments