-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #406 from sonja-turo/main
Fix snapshots in aggregate partials
- Loading branch information
Showing
5 changed files
with
177 additions
and
2 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
46 changes: 46 additions & 0 deletions
46
tests/TestClasses/AggregateRoots/AccountAggregateRootWithPartial.php
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,46 @@ | ||
<?php | ||
|
||
namespace Spatie\EventSourcing\Tests\TestClasses\AggregateRoots; | ||
|
||
use Spatie\EventSourcing\AggregateRoots\AggregateRoot; | ||
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Partials\MoneyPartial; | ||
|
||
class AccountAggregateRootWithPartial extends AggregateRoot | ||
{ | ||
public int $aggregateVersion = 0; | ||
|
||
public int $aggregateVersionAfterReconstitution = 0; | ||
|
||
public $dependency; | ||
|
||
protected Math $math; | ||
|
||
protected MoneyPartial $moneyPartial; | ||
|
||
public function __construct(Math $math, $dependency = null) | ||
{ | ||
$this->dependency = $dependency; | ||
$this->math = $math; | ||
|
||
$this->moneyPartial = new MoneyPartial($this, $math); | ||
} | ||
|
||
public function addMoney(int $amount): self | ||
{ | ||
$this->moneyPartial->addMoney($amount); | ||
|
||
return $this; | ||
} | ||
|
||
public function multiplyMoney(int $amount): self | ||
{ | ||
$this->moneyPartial->multiplyMoney($amount); | ||
|
||
return $this; | ||
} | ||
|
||
public function getBalance() | ||
{ | ||
return $this->moneyPartial->balance; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/TestClasses/AggregateRoots/Partials/MoneyPartial.php
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,47 @@ | ||
<?php | ||
|
||
namespace Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Partials; | ||
|
||
use Spatie\EventSourcing\AggregateRoots\AggregatePartial; | ||
use Spatie\EventSourcing\AggregateRoots\AggregateRoot; | ||
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\Math; | ||
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyAdded; | ||
use Spatie\EventSourcing\Tests\TestClasses\AggregateRoots\StorableEvents\MoneyMultiplied; | ||
|
||
class MoneyPartial extends AggregatePartial | ||
{ | ||
public int $balance = 0; | ||
|
||
protected Math $math; | ||
|
||
public function __construct(AggregateRoot $aggregateRoot, Math $math) | ||
{ | ||
parent::__construct($aggregateRoot); | ||
$this->math = $math; | ||
} | ||
|
||
public function addMoney(int $amount): self | ||
{ | ||
$this->recordThat(new MoneyAdded($amount)); | ||
|
||
return $this; | ||
} | ||
|
||
public function multiplyMoney(int $amount): self | ||
{ | ||
$this->recordThat(new MoneyMultiplied($amount)); | ||
|
||
return $this; | ||
} | ||
|
||
protected function applyMoneyAdded(MoneyAdded $event) | ||
{ | ||
$this->balance += $event->amount; | ||
} | ||
|
||
public function applyMoneyMultiplied(MoneyMultiplied $event) | ||
{ | ||
$this->balance = $this->math->multiply($this->balance, $event->amount); | ||
} | ||
|
||
} |