Skip to content

Commit

Permalink
support closure
Browse files Browse the repository at this point in the history
  • Loading branch information
kalibora committed Jul 25, 2018
1 parent b04651a commit e9e27b8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/ConsoleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
class ConsoleProcessor implements EventSubscriberInterface
{
protected $command;
protected $input;
protected $output;
protected $closure;

public function __construct(callable $closure = null)
{
$this->closure = $closure;
}

/**
* @param array $record
Expand All @@ -20,10 +28,30 @@ class ConsoleProcessor implements EventSubscriberInterface
*/
public function __invoke(array $record)
{
$extra = [];

if ($this->command) {
$name = $this->command->getName();
$extra['command_name'] = $name;
}

if ($this->closure) {
$other = call_user_func($this->closure, $this->command, $this->input, $this->output);
if (!is_array($other)) {
throw new \DomainException('Must return array');
}

$extra = array_merge($extra, $other);
}

if (empty($extra)) {
return $record;
}

$record['extra']['command_name'] = $name;
if (isset($record['extra'])) {
$record['extra'] = array_merge($record['extra'], $extra);
} else {
$record['extra'] = $extra;
}

return $record;
Expand All @@ -35,6 +63,8 @@ public function __invoke(array $record)
public function onCommand(ConsoleCommandEvent $event)
{
$this->command = $event->getCommand();
$this->input = $event->getInput();
$this->output = $event->getOutput();
}

/**
Expand Down
71 changes: 68 additions & 3 deletions tests/ConsoleProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Otobank\Monolog\Processor;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

class ConsoleProcessorTest extends TestCase
{
Expand All @@ -14,18 +18,29 @@ public function testInvoke($commandName, $expectedRecord)
$processor = new ConsoleProcessor();

if ($commandName) {
$commandp = $this->prophesize('\Symfony\Component\Console\Command\Command');
// Command
$commandp = $this->prophesize(Command::class);
$commandp->getName()->willReturn($commandName);
$eventp = $this->prophesize('\Symfony\Component\Console\Event\ConsoleCommandEvent');

// InputInterface
$inputp = $this->prophesize(InputInterface::class);

// OutputInterface
$outputp = $this->prophesize(OutputInterface::class);

// Event
$eventp = $this->prophesize(ConsoleCommandEvent::class);
$eventp->getCommand()->willReturn($commandp->reveal());
$eventp->getInput()->willReturn($inputp->reveal());
$eventp->getOutput()->willReturn($outputp->reveal());

$processor->onCommand($eventp->reveal());
}

$record = [];
$record = $processor($record);

$this->assertSame($expectedRecord, $record);
$this->assertEquals($expectedRecord, $record);
}

public function providerForInvoke()
Expand All @@ -35,4 +50,54 @@ public function providerForInvoke()
['foo', ['extra' => ['command_name' => 'foo']]],
];
}

/**
* @dataProvider providerForInvokeWithClosure
*/
public function testInvokeWithClosure($commandName, $expectedRecord)
{
$processor = new ConsoleProcessor(function ($command, $input, $output) {
return [
'command_hoge' => 'fuga',
'input_bar' => $input->getArgument('bar'),
];
});

// Command
$commandp = $this->prophesize(Command::class);
$commandp->getName()->willReturn($commandName);

// InputInterface
$inputp = $this->prophesize(InputInterface::class);
$inputp->getArgument('bar')->willReturn('baz');

// OutputInterface
$outputp = $this->prophesize(OutputInterface::class);

// Event
$eventp = $this->prophesize(ConsoleCommandEvent::class);
$eventp->getCommand()->willReturn($commandp->reveal());
$eventp->getInput()->willReturn($inputp->reveal());
$eventp->getOutput()->willReturn($outputp->reveal());

$processor->onCommand($eventp->reveal());

$record = [];
$record = $processor($record);

$this->assertEquals($expectedRecord, $record);
}

public function providerForInvokeWithClosure()
{
return [
['foo', [
'extra' => [
'input_bar' => 'baz',
'command_name' => 'foo',
'command_hoge' => 'fuga',
],
]],
];
}
}

0 comments on commit e9e27b8

Please sign in to comment.