-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventSourcing] Quality of life updates (#108)
- Loading branch information
1 parent
7382e12
commit 983ece4
Showing
9 changed files
with
260 additions
and
1 deletion.
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
27 changes: 27 additions & 0 deletions
27
src/SonsOfPHP/Component/EventSourcing/Test/AggregateAssertionsTrait.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\EventSourcing\Test; | ||
|
||
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface; | ||
|
||
/** | ||
* @author [email protected] | ||
*/ | ||
trait AggregateAssertionsTrait | ||
{ | ||
final public function assertEventRaised(string $eventClass, AggregateInterface $aggregate, string $message): void | ||
{ | ||
$constraint = new EventRaised($eventClass); | ||
|
||
\PHPUnit\Framework\Assert::assertThat($aggregate, $constraint, $message); | ||
} | ||
|
||
final public function assertCountEventsRaised(int $count, AggregateInterface $aggregate, string $message): void | ||
{ | ||
$constraint = new CountEventsRaised($count); | ||
|
||
\PHPUnit\Framework\Assert::assertThat($aggregate, $constraint, $message); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/SonsOfPHP/Component/EventSourcing/Test/CountEventsRaised.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\EventSourcing\Test; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface; | ||
|
||
/** | ||
* Usage: | ||
* $this->assertThat($aggregate, new CountEventsRaised(0)); | ||
* | ||
* @author [email protected] | ||
*/ | ||
final class CountEventsRaised extends Constraint | ||
{ | ||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function __construct(private int $count) {} | ||
|
||
protected function matches(mixed $other): bool | ||
{ | ||
if (!$other instanceof AggregateInterface) { | ||
throw \LogicException(sprintf('Object must implement "%s"', AggregateInterface::class)); | ||
} | ||
|
||
return $this->count === count($other->peekPendingEvents()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf('has raised "%d" events', $this->count); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/SonsOfPHP/Component/EventSourcing/Test/EventRaised.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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\EventSourcing\Test; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface; | ||
|
||
/** | ||
* Usage: | ||
* $this->assertThat($aggregate, new EventRaised(Event::class)); | ||
* | ||
* @author [email protected] | ||
*/ | ||
final class EventRaised extends Constraint | ||
{ | ||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function __construct(private string $eventClass) {} | ||
|
||
/** | ||
* AggregateInterface $other | ||
*/ | ||
protected function matches(mixed $other): bool | ||
{ | ||
if (!$other instanceof AggregateInterface) { | ||
throw \LogicException(sprintf('Object must implement "%s"', AggregateInterface::class)); | ||
} | ||
|
||
foreach ($other->peekPendingEvents() as $event) { | ||
if ($event instanceof $this->eventClass) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return sprintf('has raised the event "%s"', $this->eventClass); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/SonsOfPHP/Component/EventSourcing/Tests/Test/CountEventsRaisedTest.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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\EventSourcing\Tests\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SonsOfPHP\Component\EventSourcing\Test\CountEventsRaised; | ||
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface; | ||
|
||
/** | ||
* @coversDefaultClass \SonsOfPHP\Component\EventSourcing\Test\CountEventsRaised | ||
*/ | ||
final class CountEventsRaisedTest extends TestCase | ||
{ | ||
private $aggregate; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->aggregate = $this->createMock(AggregateInterface::class); | ||
} | ||
|
||
/** | ||
* @covers ::matches | ||
*/ | ||
public function testMatchesWorksAsExpectedWhenNoEvents(): void | ||
{ | ||
$this->aggregate->method('peekPendingEvents')->willReturn([]); | ||
|
||
$constraint = new CountEventsRaised(1); | ||
$method = new \ReflectionMethod($constraint, 'matches'); | ||
|
||
$this->assertFalse($method->invoke($constraint, $this->aggregate)); | ||
} | ||
|
||
/** | ||
* @covers ::matches | ||
*/ | ||
public function testMatchesWorksAsExpectedWhenEventsWereRaised(): void | ||
{ | ||
$this->aggregate->method('peekPendingEvents')->willReturn([ | ||
new \stdClass(), | ||
]); | ||
|
||
$constraint = new CountEventsRaised(1); | ||
$method = new \ReflectionMethod($constraint, 'matches'); | ||
|
||
$this->assertTrue($method->invoke($constraint, $this->aggregate)); | ||
} | ||
|
||
/** | ||
* @covers ::toString | ||
*/ | ||
public function testToStringReturnsCorrectMessage(): void | ||
{ | ||
$constraint = new CountEventsRaised(123); | ||
|
||
$this->assertSame('has raised "123" events', $constraint->toString()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/SonsOfPHP/Component/EventSourcing/Tests/Test/EventRaisedTest.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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\EventSourcing\Tests\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SonsOfPHP\Component\EventSourcing\Test\EventRaised; | ||
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateInterface; | ||
|
||
/** | ||
* @coversDefaultClass \SonsOfPHP\Component\EventSourcing\Test\EventRaised | ||
*/ | ||
final class EventRaisedTest extends TestCase | ||
{ | ||
private $aggregate; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->aggregate = $this->createMock(AggregateInterface::class); | ||
} | ||
|
||
/** | ||
* @covers ::matches | ||
*/ | ||
public function testMatchesWorksAsExpectedWhenEventWasNotRaised(): void | ||
{ | ||
$this->aggregate->method('peekPendingEvents')->willReturn([]); | ||
|
||
$constraint = new EventRaised('stdClass'); | ||
$method = new \ReflectionMethod($constraint, 'matches'); | ||
|
||
$this->assertFalse($method->invoke($constraint, $this->aggregate)); | ||
} | ||
|
||
/** | ||
* @covers ::matches | ||
*/ | ||
public function testMatchesWorksAsExpectedWhenEventWasRaised(): void | ||
{ | ||
$this->aggregate->method('peekPendingEvents')->willReturn([ | ||
new \stdClass(), | ||
]); | ||
|
||
$constraint = new EventRaised('stdClass'); | ||
$method = new \ReflectionMethod($constraint, 'matches'); | ||
|
||
$this->assertTrue($method->invoke($constraint, $this->aggregate)); | ||
} | ||
|
||
/** | ||
* @covers ::toString | ||
*/ | ||
public function testToStringReturnsCorrectMessage(): void | ||
{ | ||
$constraint = new EventRaised('stdClass'); | ||
|
||
$this->assertSame('has raised the event "stdClass"', $constraint->toString()); | ||
} | ||
} |
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