Skip to content

DrushCommandsTestTrait

Helpers for calling Drush command methods directly in kernel tests.

Calling command methods is much faster than running drush in a functional test with Drush's DrushTestTrait, but skips what Drush does around the method: argument parsing, validators, hooks and prompts. Commands get non-interactive input with --yes, so confirmations are accepted, and their output and log messages are buffered for assertions.

The test is skipped when Drush isn't installed.


  • Full name: \Drupal\Tests\helper\Traits\DrushCommandsTestTrait

Properties

drushInput

The input given to the commands, with Drush's --yes and --no options.

protected ?\Symfony\Component\Console\Input\ArrayInput $drushInput

drushOutput

The output written by the commands.

protected ?\Symfony\Component\Console\Output\BufferedOutput $drushOutput

drushLogger

The messages logged by the commands.

protected ?\Symfony\Component\ErrorHandler\BufferingLogger $drushLogger

Methods

createDrushCommands

Creates a Drush commands object, ready to call its command methods.

protected createDrushCommands(class-string<\Drupal\Tests\helper\Traits\T> $class): \Drupal\Tests\helper\Traits\T

Parameters:

Parameter Type Description
$class class-string<\Drupal\Tests\helper\Traits\T>
The commands class. It must have a static create() method taking the
container, such as the one from Drush's AutowireTrait. That one creates
"new self", so subclasses must use the trait again. Otherwise build it
yourself and pass it to prepareDrushCommands().

Return Value:

The commands object.


prepareDrushCommands

Gives a Drush commands object non-interactive input, output and a logger.

protected prepareDrushCommands(\Drupal\Tests\helper\Traits\T $commands): \Drupal\Tests\helper\Traits\T

Parameters:

Parameter Type Description
$commands \Drupal\Tests\helper\Traits\T
The commands object.

Return Value:

The same commands object.


getDrushLogMessages

Gets and clears the messages logged by the commands.

protected getDrushLogMessages(): string[]

Return Value:

The messages, as logged, in order. Messages built with dt() already have their placeholders replaced.


getDrushOutput

Gets and clears the output written by the commands.

protected getDrushOutput(): string

tearDownDrush

Clears unchecked log messages and Drush's container after each test.

protected tearDownDrush(): void

BufferingLogger prints the messages left in it when it's destroyed, which PHPUnit reports as an error in tests run in separate processes.


setUpDrush

Loads Drush's functions, and creates the buffered output and logger.

private setUpDrush(): void