Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Checkout source code
uses: actions/checkout@v2

- name: Install PHP
- name: Install PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
Expand All @@ -36,7 +36,6 @@ jobs:
run: composer validate

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Execute tests (Unit and Feature tests) via PHPUnit
Expand Down
9 changes: 2 additions & 7 deletions src/Domain/Bus/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ abstract class Event extends Message
*/
public function __construct(string $aggregateId, array $data = [])
{
parent::__construct(
[
'aggregateId' => $aggregateId,
'data' => $data,
]
);
parent::__construct(array_merge(['aggregateId' => $aggregateId], $data));
}

/**
* Return the Aggregate Id.
*
* @return string
*/
public function aggregateId()
public function aggregateId(): string
{
return $this->fromPayload('aggregateId');
}
Expand Down
13 changes: 9 additions & 4 deletions src/Domain/Contracts/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OtherCode\ComplexHeart\Domain\Contracts;

use OtherCode\ComplexHeart\Domain\Bus\Event;
use OtherCode\ComplexHeart\Domain\Contracts\Bus\EventBus;

/**
* Interface Aggregate
Expand All @@ -15,9 +15,14 @@
interface Aggregate extends Entity
{
/**
* Pull the registered Domain Events.
* Publish the registered Domain Events.
*
* @return Event[]
* $aggregate = new Aggregate();
* // do things and generate events
* $aggregate->publishDomainEvents($eventBus);
*
* @param EventBus $eventBus
* @return void
*/
public function pullDomainEvents(): array;
public function publishDomainEvents(EventBus $eventBus): void;
}
14 changes: 7 additions & 7 deletions src/Domain/Traits/HasDomainEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace OtherCode\ComplexHeart\Domain\Traits;

use OtherCode\ComplexHeart\Domain\Bus\Event;
use OtherCode\ComplexHeart\Domain\Contracts\Bus\EventBus;

/**
* Trait HasDomainEvents
Expand All @@ -18,21 +19,20 @@ trait HasDomainEvents
/**
* List of registered domain events.
*
* @var array<Event>
* @var Event[]
*/
private array $_domainEvents = [];

/**
* Pull out all the registered domain events.
* Publish the registered Domain Events.
*
* @return array<Event>
* @param EventBus $eventBus
* @return void
*/
final public function pullDomainEvents(): array
final public function publishDomainEvents(EventBus $eventBus): void
{
$domainEvents = $this->_domainEvents;
$eventBus->publish(...$this->_domainEvents);
$this->_domainEvents = [];

return $domainEvents;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/Traits/HasServiceBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ final public function bind(ServiceBus $bus): void
$this->_serviceBus = $bus;
}

/**
* Return true if the Service Bus is bound to the object, false otherwise.
*
* @return bool
*/
final public function isBound(): bool
{
return isset($this->_serviceBus);
}

/**
* Return the Command Bus implementation.
*
Expand Down
10 changes: 9 additions & 1 deletion tests/Domain/Traits/HasDomainEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace OtherCode\ComplexHeart\Tests\Domain\Traits;

use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use OtherCode\ComplexHeart\Domain\Bus\Event;
use OtherCode\ComplexHeart\Domain\Contracts\Bus\EventBus;
use OtherCode\ComplexHeart\Domain\ValueObjects\UUIDValue;
use OtherCode\ComplexHeart\Tests\Sample\Order;
use OtherCode\ComplexHeart\Tests\Sample\OrderLine;
Expand All @@ -20,14 +23,19 @@ class HasDomainEventTest extends MockeryTestCase
{
public function testShouldAddAndPullDomainEvent(): void
{
$eventBus = Mockery::mock(EventBus::class);
$eventBus->shouldReceive('publish')
->once()
->with(Mockery::type(Event::class));

$o = Order::create(
UUIDValue::random(),
new OrderLine(UUIDValue::random(), new ProductName('PR 1')),
new OrderLine(UUIDValue::random(), new ProductName('PR 2')),
);

$this->assertCount(1, $o->getDomainEvents());
$this->assertCount(1, $o->pullDomainEvents());
$o->publishDomainEvents($eventBus);
$this->assertCount(0, $o->getDomainEvents());
}
}