Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Dialect/KeywordsDialectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
*/
final class KeywordsDialectProvider implements DialectProviderInterface
{
private readonly string $defaultLanguage;

public function __construct(
private readonly KeywordsInterface $keywords,
) {
// Assume a default dialect of `en` as the KeywordsInterface does not allow reading its language but returns the current data
$this->defaultLanguage = $this->keywords instanceof ArrayKeywords ? $this->keywords->getLanguage() : 'en';
}

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

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

return $this->buildDialect($language);
return $this->buildDialect($this->defaultLanguage);
}

private function buildDialect(string $language): GherkinDialect
Expand Down
12 changes: 12 additions & 0 deletions tests/Dialect/KeywordsDialectProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Behat\Gherkin\Dialect\KeywordsDialectProvider;
use Behat\Gherkin\Keywords\ArrayKeywords;
use Behat\Gherkin\Keywords\CachedArrayKeywords;
use PHPUnit\Framework\TestCase;

class KeywordsDialectProviderTest extends TestCase
Expand Down Expand Up @@ -40,4 +41,15 @@ public function testFailsForEmptyKeywordString(): void
$this->expectExceptionMessage('A keyword string must contain at least one keyword.');
$dialectProvider->getDialect('en');
}

public function testDefaultDialectAfterExplicitDialect(): void
{
$dialectProvider = new KeywordsDialectProvider(CachedArrayKeywords::withDefaultKeywords());

$ruDialect = $dialectProvider->getDialect('ru');
$defaultDialect = $dialectProvider->getDefaultDialect();

$this->assertSame('ru', $ruDialect->getLanguage());
$this->assertSame('en', $defaultDialect->getLanguage());
}
}