Skip to content

Commit 35c7952

Browse files
Added handleCommand() wrapper method.
1 parent b6a7ad1 commit 35c7952

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/CommandBusAwareTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ public function setCommandBus(CommandBusInterface $commandBus): self
6161

6262
return $this;
6363
}
64+
65+
/**
66+
* Wrapper for command bus `handle()` method.
67+
*
68+
* @param mixed $command Command class.
69+
* @return mixed
70+
*/
71+
public function handleCommand($command)
72+
{
73+
return $this->getCommandBus()->handle($command);
74+
}
6475
}

tests/CommandBusAwareTraitTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
use PHPUnit\Framework\TestCase;
2929
use Robotusers\Commander\CommandBusAwareTrait;
30+
use Robotusers\Commander\CommandBusInterface;
31+
use RuntimeException;
32+
use stdClass;
3033

3134
/**
3235
* @author Robert Pustułka <r.pustulka@robotusers.com>
@@ -37,7 +40,7 @@ class CommandBusAwareTraitTest extends TestCase
3740
public function testGetCommandBus()
3841
{
3942
$object = $this->getMockForTrait(CommandBusAwareTrait::class);
40-
$bus = $this->createMock(\Robotusers\Commander\CommandBusInterface::class);
43+
$bus = $this->createMock(CommandBusInterface::class);
4144

4245
$result = $object->setCommandBus($bus);
4346
$this->assertSame($object, $result);
@@ -46,9 +49,26 @@ public function testGetCommandBus()
4649
$this->assertSame($bus, $result);
4750
}
4851

52+
public function testHandleCommand()
53+
{
54+
$object = $this->getMockForTrait(CommandBusAwareTrait::class);
55+
$bus = $this->createMock(CommandBusInterface::class);
56+
$command = new stdClass();
57+
$result = new stdClass();
58+
59+
$bus->expects($this->once())
60+
->method('handle')
61+
->with($command)
62+
->willReturn($result);
63+
64+
$object->setCommandBus($bus);
65+
$return = $object->handleCommand($command);
66+
$this->assertSame($result, $return);
67+
}
68+
4969
/**
5070
* @expectedException RuntimeException
51-
* @expectedException Command bus has not been set.
71+
* @expectedExceptionMessage Command bus has not been set.
5272
*/
5373
public function testGetCommandBusMissing()
5474
{

0 commit comments

Comments
 (0)