Skip to content
This repository was archived by the owner on Mar 29, 2021. It is now read-only.
Open
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
9 changes: 9 additions & 0 deletions src/MPWAR/Module/Economy/Test/EconomyModuleUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions src/MPWAR/Module/Economy/Test/Stub/AccountBalanceChangedStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MPWAR\Module\Economy\Test\Stub;

use MPWAR\Module\Economy\Contract\DomainEvent\AccountBalanceChanged;
use MPWAR\Module\Economy\Domain\AccountOwner;
use MPWAR\Module\Economy\Domain\VirtualMoney;

final class AccountBalanceChangedStub
{
public static function create($owner, $amount, $currency)
{
return new AccountBalanceChanged($owner, $amount, $currency);
}

public static function from(AccountOwner $owner, VirtualMoney $money)
{
return self::create($owner->value(), $money->amount(), $money->currency()->value());
}
}
11 changes: 11 additions & 0 deletions src/MPWAR/Module/Economy/Test/Stub/AccountOpenedStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@

use DateTimeImmutable;
use MPWAR\Module\Economy\Contract\DomainEvent\AccountOpened;
use MPWAR\Test\Stub\DateTimeStub;

final class AccountOpenedStub
{
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());
}
}
5 changes: 5 additions & 0 deletions src/MPWAR/Module/Economy/Test/Stub/AccountStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace MPWAR\Module\Economy\Tests\Behaviour;

use MPWAR\Module\Economy\Application\DomainEventSubscriber\GiveWelcomeBonusOnAccountOpened;
use MPWAR\Module\Economy\Application\Service\TransactionProcessor;
use MPWAR\Module\Economy\Contract\Exception\AccountOwnerNotValidException;
use MPWAR\Module\Economy\Domain\VirtualMoney;
use MPWAR\Module\Economy\Test\EconomyModuleUnitTestCase;
use MPWAR\Module\Economy\Test\Stub\AccountBalanceChangedStub;
use MPWAR\Module\Economy\Test\Stub\AccountOpenedStub;
use MPWAR\Module\Economy\Test\Stub\AccountOwnerStub;
use MPWAR\Module\Economy\Test\Stub\AccountStub;
use MPWAR\Module\Economy\Test\Stub\VirtualMoneyStub;

final class GiveWelcomeBonusOnAccountOpenedTest extends EconomyModuleUnitTestCase
{
/** @var GiveWelcomeBonusOnAccountOpened */
private $subscriber;

protected function setUp()
{
parent::setUp();

$opener = new TransactionProcessor($this->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'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}