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
4 changes: 4 additions & 0 deletions src/Drupal/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function emailBeforeScenario(BeforeScenarioScope $scope): void {
return;
}

// Force the lazy 6.x driver to boot Drupal so '\Drupal::config()' below is
// safe regardless of hook ordering between traits.
$this->getDriver();

if ($scope->getScenario()->hasTag('debug')) {
$this->emailDebug = TRUE;
}
Expand Down
44 changes: 32 additions & 12 deletions tests/behat/bootstrap/BehatCliTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ public function behatCliBeforeScenario(BeforeScenarioScope $scope): void {
return;
}

$this->behatCliWriteFeatureContextFile($traits);
// The generated 'FeatureContext.php' normally includes a 'bootstrapDrupal'
// workaround that primes the lazy 6.x driver before any trait hook runs.
// Scenarios tagged '@behat-cli-no-bootstrap' opt out of that workaround so
// a trait under test can be exercised in true isolation - useful for
// proving a trait bootstraps Drupal itself when its hooks call '\Drupal::'.
$bootstrap_workaround = !$scope->getScenario()->hasTag('behat-cli-no-bootstrap');

$this->behatCliWriteFeatureContextFile($traits, $bootstrap_workaround);
}

#[BeforeStep]
Expand All @@ -71,14 +78,21 @@ public function behatCliBeforeStep(): void {
*
* @param array $traits
* Optional array of trait classes.
* @param bool $bootstrap_workaround
* When TRUE (default), the generated context includes a 'bootstrapDrupal'
* '@BeforeScenario @api' hook that primes the lazy 6.x driver. Pass FALSE
* to omit that hook so a trait under test can be exercised in true
* isolation - useful for proving a trait bootstraps Drupal itself when
* its hooks call '\Drupal::' before any step runs.
*
* @return string
* Path to written file.
*/
public function behatCliWriteFeatureContextFile(array $traits = []): string {
public function behatCliWriteFeatureContextFile(array $traits = [], bool $bootstrap_workaround = TRUE): string {
$tokens = [
'{{USE_DECLARATION}}' => '',
'{{USE_IN_CLASS}}' => '',
'{{BOOTSTRAP_METHOD}}' => '',
];
foreach ($traits as $trait) {
// Check if trait contains slash to determine if it's in a subdirectory.
Expand Down Expand Up @@ -138,16 +152,8 @@ public function behatCliWriteFeatureContextFile(array $traits = []): string {
$tokens['{{USE_IN_CLASS}}'] .= sprintf('use %s;' . PHP_EOL, $trait_name);
}

$content = <<<'EOL'
<?php

use Drupal\DrupalExtension\Context\DrupalContext;
{{USE_DECLARATION}}

class FeatureContext extends DrupalContext {
{{USE_IN_CLASS}}

use FeatureContextTrait;
if ($bootstrap_workaround) {
$tokens['{{BOOTSTRAP_METHOD}}'] = <<<'EOL'

/**
* Force Drupal bootstrap before any @api scenario step runs.
Expand All @@ -163,6 +169,20 @@ public function bootstrapDrupal(): void {
$this->getDriver();
}

EOL;
}

$content = <<<'EOL'
<?php

use Drupal\DrupalExtension\Context\DrupalContext;
{{USE_DECLARATION}}

class FeatureContext extends DrupalContext {
{{USE_IN_CLASS}}

use FeatureContextTrait;
{{BOOTSTRAP_METHOD}}
/**
* @Given I throw test exception with message :message
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/behat/features/drupal_email.feature
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,20 @@ Feature: Check that EmailTrait works
When I run "behat --no-colors"
Then it should pass

@trait:Drupal\EmailTrait @behat-cli-no-bootstrap
Scenario: Assert that EmailTrait boots the Drupal kernel itself when used in isolation
Given some behat configuration
And scenario steps tagged with "@api @email":
"""
When I send test email to "test@example.com" with:
'''
Test content
'''
Then an email should be sent to the "test@example.com"
"""
When I run "behat --no-colors"
Then it should pass

@api @email @email:default
Scenario: As a developer, I want to verify custom email handler type tag is processed
When I send test email to "test@example.com" with:
Expand Down