-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for multiple reporters. (#202)
- Loading branch information
Showing
10 changed files
with
323 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
use Evenement\EventEmitter; | ||
use Peridot\Configuration; | ||
use Peridot\Reporter\AbstractBaseReporter; | ||
use Peridot\Reporter\CompositeReporter; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\NullOutput; | ||
|
||
describe('CompositeReporter', function() { | ||
|
||
beforeEach(function() { | ||
$this->configuration = new Configuration(); | ||
$this->output = new BufferedOutput(); | ||
$this->emitter = new EventEmitter(); | ||
$this->reporterA = new FakeReporter($this->configuration, new BufferedOutput(), $this->emitter); | ||
$this->reporterB = new FakeReporter($this->configuration, new NullOutput(), $this->emitter); | ||
$this->reporterC = new FakeReporter($this->configuration, new BufferedOutput(), $this->emitter); | ||
$this->reporters = [$this->reporterA, $this->reporterB, $this->reporterC]; | ||
$this->reporter = new CompositeReporter($this->reporters, $this->configuration, $this->output, $this->emitter); | ||
}); | ||
|
||
context('->setEventEmitter()', function() { | ||
beforeEach(function () { | ||
$this->emitter2 = new EventEmitter(); | ||
$this->reporter->setEventEmitter($this->emitter2); | ||
}); | ||
|
||
it('should set the event emitter', function() { | ||
assert($this->reporter->getEventEmitter() === $this->emitter2, 'should be the same event emitter'); | ||
}); | ||
|
||
it('should set the event emitter for each wrapped reporter', function() { | ||
assert($this->reporterA->getEventEmitter() === $this->emitter2, 'should be the same event emitter'); | ||
assert($this->reporterB->getEventEmitter() === $this->emitter2, 'should be the same event emitter'); | ||
assert($this->reporterC->getEventEmitter() === $this->emitter2, 'should be the same event emitter'); | ||
}); | ||
}); | ||
|
||
context('when runner.end is emitted', function() { | ||
it('should include an error number and the test description', function() { | ||
$this->emitter->emit('runner.end', [1.0]); | ||
$content = $this->output->fetch(); | ||
$expected = implode([ | ||
PHP_EOL, | ||
spl_object_hash($this->reporterA), | ||
PHP_EOL, | ||
PHP_EOL, | ||
spl_object_hash($this->reporterC), | ||
PHP_EOL | ||
]); | ||
assert($content === $expected, 'output should contain wrapped reporter output'); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
class FakeReporter extends AbstractBaseReporter | ||
{ | ||
public function init() | ||
{ | ||
$this->getOutput()->writeln(spl_object_hash($this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
namespace Peridot\Reporter; | ||
|
||
use Evenement\EventEmitterInterface; | ||
use Peridot\Configuration; | ||
use Peridot\Core\HasEventEmitterTrait; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* Combines multiple reporters. | ||
* | ||
* @package Peridot\Reporter | ||
*/ | ||
class CompositeReporter extends AbstractBaseReporter | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $reporters; | ||
|
||
/** | ||
* @param array $reporters | ||
* @param Configuration $configuration | ||
* @param OutputInterface $output | ||
* @param EventEmitterInterface $eventEmitter | ||
*/ | ||
public function __construct( | ||
array $reporters, | ||
Configuration $configuration, | ||
OutputInterface $output, | ||
EventEmitterInterface $eventEmitter | ||
) { | ||
$this->reporters = $reporters; | ||
|
||
parent::__construct($configuration, $output, $eventEmitter); | ||
} | ||
|
||
/** | ||
* Return the wrapped reporters. | ||
* | ||
* @return array | ||
*/ | ||
public function getReporters() | ||
{ | ||
return $this->reporters; | ||
} | ||
|
||
/** | ||
* Initialize reporter. Setup and listen for runner events. | ||
* | ||
* @return void | ||
*/ | ||
public function init() | ||
{ | ||
$this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']); | ||
} | ||
|
||
/** | ||
* @param \Evenement\EventEmitterInterface $eventEmitter | ||
*/ | ||
public function setEventEmitter(EventEmitterInterface $eventEmitter) | ||
{ | ||
parent::setEventEmitter($eventEmitter); | ||
|
||
array_map(function (ReporterInterface $reporter) use ($eventEmitter) { | ||
$reporter->setEventEmitter($eventEmitter); | ||
}, $this->reporters); | ||
|
||
return $this; | ||
} | ||
|
||
public function onRunnerEnd() | ||
{ | ||
$stdout = $this->getOutput(); | ||
|
||
array_map(function (ReporterInterface $reporter) use ($stdout) { | ||
$output = $reporter->getOutput(); | ||
|
||
if ($output instanceof BufferedOutput && $content = $output->fetch()) { | ||
$stdout->writeln(''); | ||
$stdout->write($content); | ||
} | ||
}, $this->reporters); | ||
} | ||
} |
Oops, something went wrong.