Skip to content

Commit 4f4ef2f

Browse files
committed
minor(behat): add behat file+line in exceptions
1 parent 43007c7 commit 4f4ef2f

File tree

3 files changed

+62
-26
lines changed

3 files changed

+62
-26
lines changed

src/Test/Behat/Exception/InvalidResetDbTag.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,70 @@
22

33
namespace Zenstruck\Foundry\Test\Behat\Exception;
44

5+
use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
6+
use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
7+
58
final class InvalidResetDbTag extends \LogicException
69
{
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
843
{
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);
1045
}
1146

12-
public static function resetDbWithScenarioMode(): self
47+
public static function resetDbWithScenarioMode(BeforeFeatureTested|BeforeScenarioTested $event): self
1348
{
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);
1550
}
1651

17-
public static function noResetDbWithManualMode(): self
52+
public static function noResetDbWithManualMode(BeforeFeatureTested|BeforeScenarioTested $event): self
1853
{
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);
2055
}
2156

22-
public static function resetDbOnFeatureWithFeatureMode(): self
57+
public static function resetDbOnFeatureWithFeatureMode(BeforeFeatureTested $event): self
2358
{
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);
2560
}
2661

27-
public static function resetDbOnScenarioWithScenarioMode(): self
62+
public static function resetDbOnScenarioWithScenarioMode(BeforeScenarioTested $event): self
2863
{
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);
3065
}
3166

32-
public static function noResetDbWithFeatureMode(): self
67+
public static function noResetDbWithFeatureMode(BeforeFeatureTested|BeforeScenarioTested $event): self
3368
{
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);
3570
}
3671
}

src/Test/Behat/Listener/DatabaseResetListener.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ public function disableStaticConnection(): void
9696
public function validateFeature(BeforeFeatureTested $event): void
9797
{
9898
if ($this->hasResetDbTag($event) && $this->resetMode === DatabaseResetMode::FEATURE) {
99-
throw InvalidResetDbTag::resetDbOnFeatureWithFeatureMode();
99+
throw InvalidResetDbTag::resetDbOnFeatureWithFeatureMode($event);
100100
}
101101
}
102102

103103
public function validateScenario(BeforeScenarioTested $event): void
104104
{
105105
if ($this->hasResetDbTag($event) && $this->resetMode === DatabaseResetMode::SCENARIO) {
106-
throw InvalidResetDbTag::resetDbOnScenarioWithScenarioMode();
106+
throw InvalidResetDbTag::resetDbOnScenarioWithScenarioMode($event);
107107
}
108108

109109
if ($this->hasResetDbTag($event) && $this->hasNoResetDbTag($event)) {
110-
throw InvalidResetDbTag::bothTagsUsed();
110+
throw InvalidResetDbTag::bothTagsUsed($event);
111111
}
112112
}
113113

@@ -175,8 +175,7 @@ private function hasResetDbTag(BeforeFeatureTested|BeforeScenarioTested $event):
175175
}
176176

177177
if ($this->resetMode === DatabaseResetMode::SCENARIO) {
178-
// todo: ajouter des infos concernant le fichier de features
179-
throw InvalidResetDbTag::resetDbWithScenarioMode();
178+
throw InvalidResetDbTag::resetDbWithScenarioMode($event);
180179
}
181180

182181
return true;
@@ -201,8 +200,8 @@ private function hasNoResetDbTag(BeforeFeatureTested|BeforeScenarioTested $event
201200
}
202201

203202
return match($this->resetMode) {
204-
DatabaseResetMode::MANUAL => throw InvalidResetDbTag::noResetDbWithManualMode(),
205-
DatabaseResetMode::FEATURE => throw InvalidResetDbTag::noResetDbWithFeatureMode(),
203+
DatabaseResetMode::MANUAL => throw InvalidResetDbTag::noResetDbWithManualMode($event),
204+
DatabaseResetMode::FEATURE => throw InvalidResetDbTag::noResetDbWithFeatureMode($event),
206205
default => true,
207206
};
208207
}

tests/Integration/Behat/Listener/DatabaseResetListenerTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
1818
use Behat\Gherkin\Node\FeatureNode;
1919
use Behat\Gherkin\Node\ScenarioNode;
20-
use Behat\Testwork\Environment\Environment;
20+
use Behat\Testwork\Environment\StaticEnvironment;
21+
use Behat\Testwork\Suite\GenericSuite;
2122
use PHPUnit\Framework\Attributes\DataProvider;
2223
use PHPUnit\Framework\Attributes\Test;
2324
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -314,6 +315,11 @@ private function objectRegistry(): ObjectRegistry
314315
return self::getContainer()->get('.zenstruck_foundry.behat.object_registry'); // @phpstan-ignore return.type
315316
}
316317

318+
private function createEnvironment(): StaticEnvironment
319+
{
320+
return new StaticEnvironment(new GenericSuite('default', ['paths' => ['/path/to']]));
321+
}
322+
317323
/**
318324
* @param list<string> $tags
319325
*/
@@ -331,9 +337,7 @@ private function createFeatureEvent(array $tags): BeforeFeatureTested
331337
1
332338
);
333339

334-
$environment = $this->createStub(Environment::class);
335-
336-
return new BeforeFeatureTested($environment, $feature);
340+
return new BeforeFeatureTested($this->createEnvironment(), $feature);
337341
}
338342

339343
/**
@@ -355,8 +359,6 @@ private function createScenarioEvent(array $tags): BeforeScenarioTested
355359
1
356360
);
357361

358-
$environment = $this->createStub(Environment::class);
359-
360-
return new BeforeScenarioTested($environment, $feature, $scenario);
362+
return new BeforeScenarioTested($this->createEnvironment(), $feature, $scenario);
361363
}
362364
}

0 commit comments

Comments
 (0)