Skip to content

Commit

Permalink
Merge pull request #111 from morrislaptop/aggregate-construct-ioc
Browse files Browse the repository at this point in the history
Allow aggregate roots to have dependencies in constructor
  • Loading branch information
freekmurze committed Jun 18, 2020
2 parents c0dbd5f + db3378b commit cbd1796
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AggregateRoot.php
Expand Up @@ -29,7 +29,7 @@ abstract class AggregateRoot
*/
public static function retrieve(string $uuid): self
{
$aggregateRoot = (new static());
$aggregateRoot = resolve(static::class);

$aggregateRoot->uuid = $uuid;

Expand Down
12 changes: 12 additions & 0 deletions tests/AggregateRootTest.php
Expand Up @@ -31,6 +31,18 @@ public function setUp(): void
$this->aggregateUuid = FakeUuid::generate();
}

/** @test */
public function aggregate_root_resolves_dependencies_from_the_container()
{
$this->app->bind(AccountAggregateRoot::class, function () {
return new AccountAggregateRoot(42);
});

$root = AccountAggregateRoot::retrieve($this->aggregateUuid);

$this->assertEquals(42, $root->dependency);
}

/** @test */
public function persisting_an_aggregate_root_will_persist_all_events_it_recorded()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/TestClasses/AggregateRoots/AccountAggregateRoot.php
Expand Up @@ -14,6 +14,13 @@ class AccountAggregateRoot extends AggregateRoot

public int $aggregateVersionAfterReconstitution = 0;

public $dependency;

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

public function addMoney(int $amount): self
{
$this->recordThat(new MoneyAdded($amount));
Expand Down

0 comments on commit cbd1796

Please sign in to comment.