Skip to content

Commit 5fd5fcf

Browse files
committed
minor(behat): add behat file+line in exceptions
1 parent 1d217ef commit 5fd5fcf

File tree

10 files changed

+114
-38
lines changed

10 files changed

+114
-38
lines changed

.github/workflows/behat.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
deps: [ highest, lowest ]
3131
env:
3232
DATABASE_URL: 'sqlite:///%kernel.project_dir%/var/data.db'
33+
USE_DAMA_DOCTRINE_TEST_BUNDLE: 1
34+
USE_PHP_84_LAZY_OBJECTS: 1
35+
MONGO_URL: ''
3336
steps:
3437
- name: Checkout code
3538
uses: actions/checkout@v3
@@ -59,25 +62,25 @@ jobs:
5962
SYMFONY_REQUIRE: ${{ matrix.symfony }}
6063

6164
- name: "Main test suite: reset DB at scenario level, with Dama support"
62-
run: vendor/bin/behat
65+
run: vendor/bin/behat --colors -vvv
6366

6467
- name: "Main test suite: reset DB at scenario level, without Dama support"
65-
run: vendor/bin/behat --profile=main-no-dama
68+
run: vendor/bin/behat --colors -vvv --profile=main-no-dama
6669

6770
- name: "Main test suite: reset DB at scenario level, with native Dama extension"
68-
run: vendor/bin/behat --profile=main-native-dama --tags='~@skip-with-native-dama'
71+
run: vendor/bin/behat --colors -vvv --profile=main-native-dama --tags='~@skip-with-native-dama'
6972

7073
- name: Manual reset DB
71-
run: vendor/bin/behat --profile=reset-manual
74+
run: vendor/bin/behat --colors -vvv --profile=reset-manual
7275

7376
- name: Manual reset DB and Dama support
74-
run: vendor/bin/behat --profile=reset-manual-dama
77+
run: vendor/bin/behat --colors -vvv --profile=reset-manual-dama
7578

7679
- name: Reset DB at feature level
77-
run: vendor/bin/behat --profile=reset-feature
80+
run: vendor/bin/behat --colors -vvv --profile=reset-feature
7881

7982
- name: Reset DB at feature level with Dama support
80-
run: vendor/bin/behat --profile=reset-feature-dama
83+
run: vendor/bin/behat --colors -vvv --profile=reset-feature-dama
8184

8285
- name: Reset DB disabled
83-
run: vendor/bin/behat --profile=reset-disabled
86+
run: vendor/bin/behat --colors -vvv --profile=reset-disabled

.github/workflows/phpunit.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ jobs:
130130
if: contains(matrix.database, 'mysql')
131131
run: sudo /etc/init.d/mysql start
132132

133+
- name: Install PHPBench & Behat
134+
run: |
135+
composer bin phpbench install
136+
composer bin behat install
137+
133138
- name: Test
134139
run: ./phpunit
135140
shell: bash
@@ -204,6 +209,11 @@ jobs:
204209
if: contains(matrix.database, 'mysql')
205210
run: sudo /etc/init.d/mysql start
206211

212+
- name: Install PHPBench & Behat
213+
run: |
214+
composer bin phpbench install
215+
composer bin behat install
216+
207217
- name: Test
208218
run: |
209219
./phpunit --testsuite reset-database --bootstrap tests/bootstrap-reset-database.php
@@ -250,6 +260,11 @@ jobs:
250260
symfony/phpunit-bridge \
251261
symfony/framework-bundle
252262
263+
- name: Install PHPBench & Behat
264+
run: |
265+
composer bin phpbench install
266+
composer bin behat install
267+
253268
- name: Test
254269
run: |
255270
vendor/bin/phpunit tests/Unit
@@ -291,6 +306,11 @@ jobs:
291306
run: composer require brianium/paratest --dev
292307
shell: bash
293308

309+
- name: Install PHPBench & Behat
310+
run: |
311+
composer bin phpbench install
312+
composer bin behat install
313+
294314
- name: Test
295315
run: vendor/bin/paratest --processes 1 --configuration phpunit-paratest.xml.dist
296316
shell: bash
@@ -343,6 +363,11 @@ jobs:
343363
env:
344364
SYMFONY_REQUIRE: 8.0.x
345365

366+
- name: Install PHPBench & Behat
367+
run: |
368+
composer bin phpbench install
369+
composer bin behat install
370+
346371
- name: Test with coverage
347372
run: ./phpunit --coverage-text --coverage-clover coverage.xml
348373
shell: bash

bin/tools/behat/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"friends-of-behat/mink-extension": "^2.0",
66
"friends-of-behat/symfony-extension": "^2.0",
77
"symfony/flex": "^2.10",
8-
"yceruto/behat-extension": "^1.0"
8+
"yceruto/behat-extension": "^1.0.2",
9+
"symfony/polyfill-php84": "^1.33",
10+
"symfony/polyfill-php85": "^1.33"
911
},
1012
"config": {
1113
"allow-plugins": {

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"symfony/framework-bundle": "^6.4|^7.0|^8.0",
4848
"symfony/maker-bundle": "^1.55",
4949
"symfony/phpunit-bridge": "^6.4.26|^7.0|^8.0",
50+
"symfony/polyfill-php80": "^1.16",
5051
"symfony/routing": "^6.4|^7.0|^8.0",
5152
"symfony/runtime": "^6.4|^7.0|^8.0",
5253
"symfony/translation-contracts": "^3.4",

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2702,7 +2702,7 @@ Installation
27022702
27032703
$ composer require --dev behat/behat friends-of-behat/symfony-extension
27042704
2705-
2. Enable the Foundry extension in your ``behat.yml``:
2705+
2. Enable the Foundry extension in your ``behat.yaml``:
27062706

27072707
.. code-block:: yaml
27082708

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/FoundryTableNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function create(
5353
return $tableNode;
5454
}
5555

56-
public function getRowAsString(int $rowNum): string
56+
public function getRowAsString($rowNum): string
5757
{
5858
$values = [];
5959
foreach ($this->getRow($rowNum) as $column => $value) {
@@ -63,7 +63,7 @@ public function getRowAsString(int $rowNum): string
6363
return sprintf('|%s|', implode('|', $values));
6464
}
6565

66-
public function getRowAsStringWithWrappedValues(int $rowNum, callable $wrapper): string
66+
public function getRowAsStringWithWrappedValues($rowNum, $wrapper): string
6767
{
6868
$values = [];
6969
foreach ($this->getRow($rowNum) as $column => $value) {

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
}

symfony.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
"ref": "dfc51177476fb39d014ed89944cde53dc3326d23"
99
}
1010
},
11+
"doctrine/annotations": {
12+
"version": "1.14",
13+
"recipe": {
14+
"repo": "github.com/symfony/recipes",
15+
"branch": "main",
16+
"version": "1.10",
17+
"ref": "64d8583af5ea57b7afa4aba4b159907f3a148b05"
18+
}
19+
},
1120
"doctrine/deprecations": {
1221
"version": "1.1",
1322
"recipe": {

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)