-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom EventListener which adds custom CLI option #13
Comments
does anyone have an example of registering a custom event which performs some setup before all tests are run? For example, defining the APPLICATION_PATH, connecting to a database, adding to the include path (for a Zend application). |
We should added this as an standalone example and properly document, but for the time being, what you need to do to register custom event listener:
"psr-4": {
"My\\Steward\\": "src/",
"My\\": "tests/",
}
Example of such file ( <?php declare(strict_types=1);
namespace My\Steward\Console\EventListener;
use Lmc\Steward\Console\CommandEvents;
use Lmc\Steward\Console\Event\BasicConsoleEvent;
use Lmc\Steward\Console\Event\ExtendedConsoleEvent;
use Lmc\Steward\Console\Event\RunTestsProcessEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FooListener implements EventSubscriberInterface
{
/** @var string */
protected $customOption;
public static function getSubscribedEvents(): array
{
return [
CommandEvents::CONFIGURE => 'onCommandConfigure',
CommandEvents::RUN_TESTS_INIT => 'onCommandRunTestsInit',
CommandEvents::RUN_TESTS_PROCESS => 'onCommandRunTestsProcess',
];
}
/**
* Add option to run command configuration.
*/
public function onCommandConfigure(BasicConsoleEvent $event): void
{
if ($event->getCommand()->getName() !== 'run') {
return;
}
$event->getCommand()->addOption('custom-option', /* ... */); // See Symfony console docs
}
/**
* Get input option on command initialization
*/
public function onCommandRunTestsInit(ExtendedConsoleEvent $event): void
{
$input = $event->getInput();
$output = $event->getOutput();
$this->customOption = $input->getOption('custom-option');
$output->writeln(sprintf('Custom option: %s', $this->customOption));
}
/**
* Inject option as environment value of every phpunit process
*/
public function onCommandRunTestsProcess(RunTestsProcessEvent $event): void
{
$env = $event->getEnvironmentVars();
$env['CUSTOM_OPTION'] = $this->customOption;
$event->setEnvironmentVars($env);
}
} |
Add custom option to Steward run command.
The text was updated successfully, but these errors were encountered: