diff --git a/src/MPWAR/Module/Economy/Test/EconomyModuleUnitTestCase.php b/src/MPWAR/Module/Economy/Test/EconomyModuleUnitTestCase.php index 90732f3..5154bce 100644 --- a/src/MPWAR/Module/Economy/Test/EconomyModuleUnitTestCase.php +++ b/src/MPWAR/Module/Economy/Test/EconomyModuleUnitTestCase.php @@ -28,6 +28,15 @@ protected function shouldPersistAccount(Account $account) ->andReturnNull(); } + protected function shouldSaveAccount(Account $account) + { + $this->accountRepository() + ->shouldReceive('save') + ->once() + ->with($this->assertEqualAggregatedRoot($account)) + ->andReturnNull(); + } + protected function shouldSearchAccount(AccountOwner $owner, Account $account = null) { $this->accountRepository() diff --git a/src/MPWAR/Module/Economy/Test/Stub/AccountBalanceChangedStub.php b/src/MPWAR/Module/Economy/Test/Stub/AccountBalanceChangedStub.php new file mode 100644 index 0000000..df1002f --- /dev/null +++ b/src/MPWAR/Module/Economy/Test/Stub/AccountBalanceChangedStub.php @@ -0,0 +1,20 @@ +value(), $money->amount(), $money->currency()->value()); + } +} diff --git a/src/MPWAR/Module/Economy/Test/Stub/AccountOpenedStub.php b/src/MPWAR/Module/Economy/Test/Stub/AccountOpenedStub.php index d01983b..117ce3b 100644 --- a/src/MPWAR/Module/Economy/Test/Stub/AccountOpenedStub.php +++ b/src/MPWAR/Module/Economy/Test/Stub/AccountOpenedStub.php @@ -4,6 +4,7 @@ use DateTimeImmutable; use MPWAR\Module\Economy\Contract\DomainEvent\AccountOpened; +use MPWAR\Test\Stub\DateTimeStub; final class AccountOpenedStub { @@ -11,4 +12,14 @@ public static function create($owner, DateTimeImmutable $occurredOn) { return new AccountOpened($owner, $occurredOn); } + + public static function random() + { + return self::create(AccountOwnerStub::random()->value(), DateTimeStub::random()); + } + + public static function owned($owner) + { + return self::create($owner, DateTimeStub::random()); + } } diff --git a/src/MPWAR/Module/Economy/Test/Stub/AccountStub.php b/src/MPWAR/Module/Economy/Test/Stub/AccountStub.php index fbc4629..cdcf4db 100644 --- a/src/MPWAR/Module/Economy/Test/Stub/AccountStub.php +++ b/src/MPWAR/Module/Economy/Test/Stub/AccountStub.php @@ -18,6 +18,11 @@ public static function owned(AccountOwner $owner) return self::create($owner, VirtualMoneyStub::randomCoins()); } + public static function zeroCoins() + { + return self::create(AccountOwnerStub::random(), VirtualMoneyStub::zeroCoins()); + } + public static function random() { return self::create(AccountOwnerStub::random(), VirtualMoneyStub::randomCoins()); diff --git a/src/MPWAR/Module/Economy/Tests/Behaviour/GiveWelcomeBonusOnAccountOpenedTest.php b/src/MPWAR/Module/Economy/Tests/Behaviour/GiveWelcomeBonusOnAccountOpenedTest.php new file mode 100644 index 0000000..c94e5a0 --- /dev/null +++ b/src/MPWAR/Module/Economy/Tests/Behaviour/GiveWelcomeBonusOnAccountOpenedTest.php @@ -0,0 +1,67 @@ +accountRepository(), $this->eventBus()); + $this->subscriber = new GiveWelcomeBonusOnAccountOpened($opener); + } + + /** @test */ + public function it_should_open_an_account_when_a_player_has_been_registered() + { + $event = AccountOpenedStub::random(); + + $owner = AccountOwnerStub::create($event->aggregateId()); + $account = AccountStub::create($owner, VirtualMoneyStub::zeroCoins()); + $accountUpdated = AccountStub::create($owner, VirtualMoney::coins(100)); + $accountBalanceChanged = AccountBalanceChangedStub::from($owner, VirtualMoney::coins(100)); + + $this->shouldSearchAccount($owner, $account); + $this->shouldSaveAccount($accountUpdated); + $this->shouldHandleEvent($accountBalanceChanged); + + $this->subscriber->notify($event); + } + + /** + * @test + * @dataProvider invalidAccountOwners + */ + public function it_should_throw_an_exception_registering_a_player_with_an_invalid_identifier($owner) + { + $this->setExpectedException(AccountOwnerNotValidException::class); + + $event = AccountOpenedStub::owned($owner); + + $this->subscriber->notify($event); + } + + public function invalidAccountOwners() + { + return [ + 'null' => ['owner' => null], + 'integer' => ['owner' => 1], + 'string' => ['owner' => 'asdsa'], + ]; + } +} diff --git a/src/MPWAR/Module/Economy/Tests/Integration/Persistence/AccountRepositoryTestCase.php b/src/MPWAR/Module/Economy/Tests/Integration/Persistence/AccountRepositoryTestCase.php index f2ae391..e91ef2d 100644 --- a/src/MPWAR/Module/Economy/Tests/Integration/Persistence/AccountRepositoryTestCase.php +++ b/src/MPWAR/Module/Economy/Tests/Integration/Persistence/AccountRepositoryTestCase.php @@ -2,9 +2,12 @@ namespace MPWAR\Module\Economy\Tests\Integration\Persistence; +use MPWAR\Module\Economy\Domain\Account; use MPWAR\Module\Economy\Domain\AccountRepository; +use MPWAR\Module\Economy\Domain\VirtualMoney; use MPWAR\Module\Economy\Test\EconomyModuleFunctionalTestCase; use MPWAR\Module\Economy\Test\Stub\AccountStub; +use PHPUnit_Framework_Assert as Assertions; abstract class AccountRepositoryTestCase extends EconomyModuleFunctionalTestCase { @@ -34,4 +37,31 @@ public function it_should_return_null_finding_a_player_that_does_not_exists() $this->assertNull($this->repository()->search($account->owner())); } + + /** @test */ + public function it_should_allow_save_a_modified_account() + { + $account = AccountStub::zeroCoins(); + $owner = $account->owner(); + $amountToAdd = VirtualMoney::coins(50); + $expectedAccount = AccountStub::create($owner, $amountToAdd); + + $this->repository()->add($account); + + $originalAccount = $this->repository()->search($owner); + + $originalAccount->add($amountToAdd); + + $this->repository()->save($originalAccount); + + $this->assertEqualAccounts($expectedAccount, $this->repository()->search($owner)); + } + + private function assertEqualAccounts(Account $expected, Account $actualCloned) + { + $actualCloned = clone($actualCloned); + $actualCloned->eraseMessages(); + + return Assertions::equalTo($expected)->evaluate($actualCloned, '', true); + } }