Skip to content

Commit

Permalink
Merge pull request #98 from patchlevel/rename-trait-and-cleanup-tests
Browse files Browse the repository at this point in the history
rename DefaultApplyMethod to NonStrictApplyMethod & cleanup tests
  • Loading branch information
DavidBadura authored Dec 2, 2021
2 parents 3b4129a + c0143e8 commit 6fdc270
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 30 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace App\Domain\Profile;

use App\Domain\Profile\Event\MessagePublished;
use App\Domain\Profile\Event\ProfileCreated;
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;

final class Profile extends AggregateRoot
Expand Down Expand Up @@ -59,17 +60,22 @@ final class Profile extends AggregateRoot
$message,
));
}

protected function applyProfileCreated(ProfileCreated $event): void
{
$this->id = $event->profileId();
$this->email = $event->email();
$this->messages = [];
}

protected function applyMessagePublished(MessagePublished $event): void

protected function apply(AggregateChanged $event): void
{
$this->messages[] = $event->message();
if ($event instanceof ProfileCreated) {
$this->id = $event->profileId();
$this->email = $event->email();
$this->messages = [];

return;
}

if ($event instanceof MessagePublished) {
$this->messages[] = $event->message();

return;
}
}

public function aggregateRootId(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-require-extends AggregateRoot
*/
trait DefaultApplyMethod
trait NonStrictApplyMethod
{
protected function apply(AggregateChanged $event): void
{
Expand Down
1 change: 1 addition & 0 deletions src/Console/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct($value, string $need)
gettype($value)
)
);

$this->value = $value;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/BasicImplementation/Aggregate/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Aggregate;

use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod;
use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod;
use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated;

final class Profile extends SnapshotableAggregateRoot
{
use DefaultApplyMethod;
use NonStrictApplyMethod;

private string $id;

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Pipeline/Aggregate/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Patchlevel\EventSourcing\Tests\Integration\Pipeline\Aggregate;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod;
use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod;
use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\NewVisited;
use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\OldVisited;
use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\PrivacyAdded;
use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\ProfileCreated;

final class Profile extends AggregateRoot
{
use DefaultApplyMethod;
use NonStrictApplyMethod;

private string $id;
private bool $privacy;
Expand Down
64 changes: 64 additions & 0 deletions tests/Unit/Aggregate/AggregateRootWithNonStrictApplyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Message;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\MessageId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithNonStrictApply;
use PHPUnit\Framework\TestCase;

class AggregateRootWithNonStrictApplyTest extends TestCase
{
public function testApplyMethod(): void
{
$id = ProfileId::fromString('1');
$email = Email::fromString('david.badura@patchlevel.de');

$profile = ProfileWithNonStrictApply::createProfile($id, $email);

self::assertEquals('1', $profile->aggregateRootId());
self::assertEquals(1, $profile->playhead());
self::assertEquals($id, $profile->id());
self::assertEquals($email, $profile->email());

$events = $profile->releaseEvents();

self::assertCount(1, $events);
$event = $events[0];
self::assertEquals(1, $event->playhead());
}

public function testEventWithoutApplyMethod(): void
{
$profileId = ProfileId::fromString('1');
$email = Email::fromString('david.badura@patchlevel.de');

$messageId = MessageId::fromString('2');

$profile = ProfileWithNonStrictApply::createProfile($profileId, $email);

$events = $profile->releaseEvents();

self::assertCount(1, $events);
self::assertEquals(1, $profile->playhead());
$event = $events[0];
self::assertEquals(1, $event->playhead());

$profile->publishMessage(
Message::create(
$messageId,
'foo'
)
);

$events = $profile->releaseEvents();

self::assertCount(1, $events);
$event = $events[0];
self::assertEquals(2, $event->playhead());
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Console/InvalidArgumentExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Console;

use Patchlevel\EventSourcing\Console\InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class InvalidArgumentExceptionTest extends TestCase
{
public function testException(): void
{
$expectedValue = 'foo';
$exception = new InvalidArgumentException($expectedValue, 'int');

self::assertEquals('Invalid argument given: need type "int" got "string"', $exception->getMessage());
self::assertEquals($expectedValue, $exception->value());
}
}
29 changes: 16 additions & 13 deletions tests/Unit/Fixture/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture;

use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod;

final class Profile extends AggregateRoot
{
use DefaultApplyMethod;

private ProfileId $id;
private Email $email;
/** @var array<Message> */
Expand Down Expand Up @@ -55,20 +53,25 @@ public function visitProfile(ProfileId $profileId): void
$this->record(ProfileVisited::raise($this->id, $profileId));
}

protected function applyProfileCreated(ProfileCreated $event): void
public function aggregateRootId(): string
{
$this->id = $event->profileId();
$this->email = $event->email();
$this->messages = [];
return $this->id->toString();
}

protected function applyMessagePublished(MessagePublished $event): void
protected function apply(AggregateChanged $event): void
{
$this->messages[] = $event->message();
}
if ($event instanceof ProfileCreated) {
$this->id = $event->profileId();
$this->email = $event->email();
$this->messages = [];

public function aggregateRootId(): string
{
return $this->id->toString();
return;
}

if ($event instanceof MessagePublished) {
$this->messages[] = $event->message();

return;
}
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Fixture/ProfileWithNonStrictApply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod;

final class ProfileWithNonStrictApply extends AggregateRoot
{
use NonStrictApplyMethod;

private ProfileId $id;
private Email $email;

public function id(): ProfileId
{
return $this->id;
}

public function email(): Email
{
return $this->email;
}

public static function createProfile(ProfileId $id, Email $email): self
{
$self = new self();
$self->record(ProfileCreated::raise($id, $email));

return $self;
}

public function publishMessage(Message $message): void
{
$this->record(MessagePublished::raise(
$this->id,
$message,
));
}

public function visitProfile(ProfileId $profileId): void
{
$this->record(ProfileVisited::raise($this->id, $profileId));
}

protected function applyProfileCreated(ProfileCreated $event): void
{
$this->id = $event->profileId();
$this->email = $event->email();
}

public function aggregateRootId(): string
{
return $this->id->toString();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Fixture/ProfileWithSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture;

use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod;
use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod;
use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot;

final class ProfileWithSnapshot extends SnapshotableAggregateRoot
{
use DefaultApplyMethod;
use NonStrictApplyMethod;

private ProfileId $id;
private Email $email;
Expand Down

0 comments on commit 6fdc270

Please sign in to comment.