Skip to content

Commit df8fe06

Browse files
committed
Fix the implementation of the default dialect for the keywords provider
The legacy KeywordsInterface was stateful, so we cannot assume that the _current_ language is the default one. We need to read the _initial_ language instead.
1 parent ec034ab commit df8fe06

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Dialect/KeywordsDialectProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
*/
2222
final class KeywordsDialectProvider implements DialectProviderInterface
2323
{
24+
private readonly string $defaultLanguage;
25+
2426
public function __construct(
2527
private readonly KeywordsInterface $keywords,
2628
) {
29+
// Assume a default dialect of `en` as the KeywordsInterface does not allow reading its language but returns the current data
30+
$this->defaultLanguage = $this->keywords instanceof ArrayKeywords ? $this->keywords->getLanguage() : 'en';
2731
}
2832

2933
public function getDialect(string $language): GherkinDialect
@@ -40,10 +44,9 @@ public function getDialect(string $language): GherkinDialect
4044

4145
public function getDefaultDialect(): GherkinDialect
4246
{
43-
// Assume a default dialect of `en` as the KeywordsInterface does not allow reading its language but returns the current data
44-
$language = $this->keywords instanceof ArrayKeywords ? $this->keywords->getLanguage() : 'en';
47+
$this->keywords->setLanguage($this->defaultLanguage);
4548

46-
return $this->buildDialect($language);
49+
return $this->buildDialect($this->defaultLanguage);
4750
}
4851

4952
private function buildDialect(string $language): GherkinDialect

tests/Dialect/KeywordsDialectProviderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Behat\Gherkin\Dialect\KeywordsDialectProvider;
1414
use Behat\Gherkin\Keywords\ArrayKeywords;
15+
use Behat\Gherkin\Keywords\CachedArrayKeywords;
1516
use PHPUnit\Framework\TestCase;
1617

1718
class KeywordsDialectProviderTest extends TestCase
@@ -40,4 +41,15 @@ public function testFailsForEmptyKeywordString(): void
4041
$this->expectExceptionMessage('A keyword string must contain at least one keyword.');
4142
$dialectProvider->getDialect('en');
4243
}
44+
45+
public function testDefaultDialectAfterExplicitDialect(): void
46+
{
47+
$dialectProvider = new KeywordsDialectProvider(CachedArrayKeywords::withDefaultKeywords());
48+
49+
$ruDialect = $dialectProvider->getDialect('ru');
50+
$defaultDialect = $dialectProvider->getDefaultDialect();
51+
52+
$this->assertSame('ru', $ruDialect->getLanguage());
53+
$this->assertSame('en', $defaultDialect->getLanguage());
54+
}
4355
}

0 commit comments

Comments
 (0)