Skip to content
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

Open
OndraM opened this issue Jun 6, 2017 · 2 comments
Open

Custom EventListener which adds custom CLI option #13

OndraM opened this issue Jun 6, 2017 · 2 comments

Comments

@OndraM
Copy link
Member

OndraM commented Jun 6, 2017

Add custom option to Steward run command.

@Qlwentt
Copy link

Qlwentt commented Sep 6, 2018

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).

@OndraM OndraM changed the title Custom EventDispatcher which adds custom CLI option Custom EventListener which adds custom CLI option Jan 7, 2019
@OndraM
Copy link
Member Author

OndraM commented Jan 7, 2019

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:

  • place the file inside your src/Console/EventListener directory (this directories should be placed on the same level as your composer.json file)
  • add this directory to composer.json autoload section:
"psr-4": {
            "My\\Steward\\": "src/",
            "My\\": "tests/",
}
  • run composer dumpautoload
  • the file name must match pattern *Listener.php
  • the class must implement Symfony\Component\EventDispatcher\EventSubscriberInterface.

Example of such file (src/Console/EventListener/FooListener.php)

<?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);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants