-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Gambling\Memory\Domain\Model\Game\Event; | ||
|
||
use Gambling\Common\Clock\Clock; | ||
use Gambling\Common\Domain\DomainEvent; | ||
use Gambling\Memory\Domain\Model\Game\GameId; | ||
use Gambling\Memory\Domain\Model\Game\Player; | ||
|
||
final class PlayerJoined implements DomainEvent | ||
{ | ||
/** | ||
* @var GameId | ||
*/ | ||
private $gameId; | ||
|
||
/** | ||
* @var Player | ||
*/ | ||
private $player; | ||
|
||
/** | ||
* @var \DateTimeImmutable | ||
*/ | ||
private $occurredOn; | ||
|
||
/** | ||
* PlayerJoined constructor. | ||
* | ||
* @param GameId $gameId | ||
* @param Player $player | ||
*/ | ||
public function __construct(GameId $gameId, Player $player) | ||
{ | ||
$this->gameId = $gameId; | ||
$this->player = $player; | ||
$this->occurredOn = Clock::instance()->now(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function aggregateId(): string | ||
{ | ||
return $this->gameId->toString(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function payload(): array | ||
{ | ||
return [ | ||
'gameId' => $this->gameId->toString(), | ||
'playerId' => $this->player->id() | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function occurredOn(): \DateTimeImmutable | ||
{ | ||
return $this->occurredOn; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function name(): string | ||
{ | ||
return 'PlayerJoined'; | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
code/tests/unit/Memory/Domain/Model/Game/Event/PlayerJoinedTest.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,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Gambling\Memory\Domain\Model\Game\Event; | ||
|
||
use Gambling\Common\Clock\Clock; | ||
use Gambling\Memory\Domain\Model\Game\GameId; | ||
use Gambling\Memory\Domain\Model\Game\Player; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class PlayerJoinedTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function itShouldBeCreatedWithItsValues(): void | ||
{ | ||
Clock::instance()->freeze(); | ||
|
||
$gameId = GameId::generate(); | ||
$playerId = 'playerId'; | ||
$payload = [ | ||
'gameId' => $gameId->toString(), | ||
'playerId' => $playerId | ||
]; | ||
|
||
$playerJoined = new PlayerJoined( | ||
$gameId, | ||
new Player($playerId) | ||
); | ||
|
||
$this->assertSame('PlayerJoined', $playerJoined->name()); | ||
$this->assertSame($gameId->toString(), $playerJoined->aggregateId()); | ||
$this->assertSame(Clock::instance()->now(), $playerJoined->occurredOn()); | ||
$this->assertSame($payload, $playerJoined->payload()); | ||
|
||
Clock::instance()->resume(); | ||
} | ||
} |
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