peridot-php / peridot

Event driven BDD test framework for PHP
http://peridot-php.github.io/
MIT License
328 stars 27 forks source link

How can I send config to reporter from console #187

Closed davidsoderberg closed 6 years ago

davidsoderberg commented 7 years ago

Hi, is there a way to send custom config to my custom reporter from console?

ezzatron commented 7 years ago

Take a look at the documentation for the peridot.reporters event. The console input is passed as the first argument, which should allow you to access console options and arguments. Here's a snippet showing real-world usage:

    /**
     * Demonstrate registering a custom reporter via peridot config
     */
    $emitter->on('peridot.reporters', function($input, $reporters) use (&$counts) {
        $banner = $input->getOption('banner');
        $reporters->register('basic', 'a simple summary', function(ReporterInterface $reporter) use (&$counts, $banner) {
            $output = $reporter->getOutput();
            $reporter->getEventEmitter()->on('runner.start', function() use ($banner, $output) {
                $output->writeln($banner);
            });
            $reporter->getEventEmitter()->on('runner.end', function() use ($output, &$counts) {
                $output->writeln(sprintf(
                    '%d run, %d failed, %d pending',
                    $counts['pass'],
                    $counts['fail'],
                    $counts['pending']
                ));
            });
        });
    });
davidsoderberg commented 7 years ago

I have the repoter as a class. How do I do then?

ezzatron commented 7 years ago

As far as I can tell, there's no way to have the console input supplied to your custom reporter's constructor. And $reporters->register doesn't accept a reporter instance, so you can't do it manually either.

That's probably an oversight, by which I mean that $reporters->register should probably accept reporter instances, in addition to class names and callbacks. But I'm new around here, so I don't know for sure. @brianium?

For now, you could have your reporter implement setter methods that you can use to configure the reporter from within the peridot.reporters event handler. Something like:

$reporter = new CustomReporter();

$emitter->on('peridot.reporters', function($input) use ($reporter) {
    $reporter->configure($input);
});
ragboyjr commented 6 years ago

Closing due to inactivity.