Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding more tests, integration tests doesnt cover now #48

Merged
merged 4 commits into from
Feb 7, 2021
Merged
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
4 changes: 2 additions & 2 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"mutators": {
"@default": true
},
"minMsi": 59,
"minCoveredMsi": 83
"minMsi": 51,
"minCoveredMsi": 90
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use function file_exists;
use function unlink;

/**
* @coversNothing
*/
final class BasicIntegrationTest extends TestCase
{
private Connection $connection;
Expand Down
3 changes: 3 additions & 0 deletions tests/Integration/Pipeline/PipelineChangeStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use function file_exists;
use function unlink;

/**
* @coversNothing
*/
final class PipelineChangeStoreTest extends TestCase
{
private Connection $connectionOld;
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Aggregate/AggregateChangedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use Error;
use Patchlevel\EventSourcing\Aggregate\AggregateException;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
Expand Down Expand Up @@ -95,6 +96,18 @@ public function testSerialize(): void
);
}

public function testSerializeNotRecorded(): void
{
$id = ProfileId::fromString('1');
$email = Email::fromString('d.a.badura@gmail.com');

$event = ProfileCreated::raise($id, $email);

$this->expectException(AggregateException::class);
$this->expectExceptionMessage('The change was not recorded.');
$event->serialize();
}

public function testDeserialize(): void
{
$id = ProfileId::fromString('1');
Expand Down
153 changes: 153 additions & 0 deletions tests/Unit/Aggregate/SnapshotableAggregateRootTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Snapshot\Snapshot;
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\MessagePublished;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithSnapshot;
use PHPUnit\Framework\TestCase;

class SnapshotableAggregateRootTest extends TestCase
{
public function testCreateAggregate(): void
{
$id = ProfileId::fromString('1');
$email = Email::fromString('d.a.badura@gmail.com');

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

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

$events = $profile->releaseEvents();

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

public function testExecuteMethod(): void
{
$profileId = ProfileId::fromString('1');
$email = Email::fromString('d.a.badura@gmail.com');

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

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

$events = $profile->releaseEvents();

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

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

self::assertEquals('1', $profile->aggregateRootId());
self::assertEquals(1, $profile->playhead());
self::assertEquals($profileId, $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
{
$visitorProfile = ProfileWithSnapshot::createProfile(
ProfileId::fromString('1'),
Email::fromString('visitor@test.com')
);

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

$visitedProfile = ProfileWithSnapshot::createProfile(
ProfileId::fromString('2'),
Email::fromString('visited@test.com')
);

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

$visitorProfile->visitProfile($visitedProfile->id());

$events = $visitedProfile->releaseEvents();
self::assertCount(0, $events);
self::assertEquals(0, $visitedProfile->playhead());
}

public function testInitliazingState(): void
{
$eventStream = [
ProfileCreated::raise(
ProfileId::fromString('1'),
Email::fromString('profile@test.com')
),
MessagePublished::raise(
ProfileId::fromString('1'),
Message::create(
MessageId::fromString('2'),
'message value'
)
),
];

$profile = ProfileWithSnapshot::createFromEventStream($eventStream);

self::assertEquals('1', $profile->id()->toString());
self::assertCount(1, $profile->messages());
}

public function testCreateFromSnapshot(): void
{
$eventStream = [
MessagePublished::raise(
ProfileId::fromString('1'),
Message::create(
MessageId::fromString('2'),
'message value'
)
),
];

$snapshot = new Snapshot(
ProfileWithSnapshot::class,
'1',
1,
[
'id' => '1',
'email' => 'profile@test.com',
]
);

$profile = ProfileWithSnapshot::createFromSnapshot($snapshot, $eventStream);

self::assertEquals('1', $profile->id()->toString());
self::assertCount(1, $profile->messages());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,26 @@ public function testReculatePlayhead(): void

self::assertEquals(0, $event->playhead());
}

public function testReculatePlayheadWithSamePlayhead(): void
{
$middleware = new RecalculatePlayheadMiddleware();

$bucket = new EventBucket(
Profile::class,
ProfileCreated::raise(
ProfileId::fromString('1'),
Email::fromString('d.a.badura@gmail.com')
)->recordNow(0)
);

$result = $middleware($bucket);

self::assertCount(1, $result);
self::assertEquals(Profile::class, $result[0]->aggregateClass());

$event = $result[0]->event();

self::assertEquals(0, $event->playhead());
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Pipeline/Middleware/ReplaceEventMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Patchlevel\EventSourcing\Pipeline\EventBucket;
use Patchlevel\EventSourcing\Pipeline\Middleware\ReplaceEventMiddleware;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\MessagePublished;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
Expand Down Expand Up @@ -45,4 +46,35 @@ static function (ProfileCreated $event) {
self::assertInstanceOf(ProfileVisited::class, $event);
self::assertEquals(5, $event->playhead());
}

public function testReplaceInvalidClass(): void
{
$middleware = new ReplaceEventMiddleware(
MessagePublished::class,
static function (ProfileCreated $event) {
return ProfileVisited::raise(
$event->profileId(),
$event->profileId()
);
}
);

$bucket = new EventBucket(
Profile::class,
ProfileCreated::raise(
ProfileId::fromString('1'),
Email::fromString('d.a.badura@gmail.com')
)->recordNow(5)
);

$result = $middleware($bucket);

self::assertCount(1, $result);
self::assertEquals(Profile::class, $result[0]->aggregateClass());

$event = $result[0]->event();

self::assertInstanceOf(ProfileCreated::class, $event);
self::assertEquals(5, $event->playhead());
}
}
31 changes: 31 additions & 0 deletions tests/Unit/Pipeline/Target/InMemoryTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline\Target;

use Patchlevel\EventSourcing\Pipeline\EventBucket;
use Patchlevel\EventSourcing\Pipeline\Target\InMemoryTarget;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;

class InMemoryTargetTest extends TestCase
{
public function testSave(): void
{
$inMemoryTarget = new InMemoryTarget();

$bucket = new EventBucket(
Profile::class,
ProfileCreated::raise(ProfileId::fromString('1'), Email::fromString('foo@test.com'))
);
$inMemoryTarget->save($bucket);

$buckets = $inMemoryTarget->buckets();
self::assertCount(1, $buckets);
self::assertEquals($bucket, $buckets[0]);
}
}
35 changes: 35 additions & 0 deletions tests/Unit/Pipeline/Target/ProjectionRepositoryTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline\Target;

use Patchlevel\EventSourcing\Pipeline\EventBucket;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectionRepositoryTarget;
use Patchlevel\EventSourcing\Projection\ProjectionRepository;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class ProjectionRepositoryTargetTest extends TestCase
{
use ProphecyTrait;

public function testSave(): void
{
$bucket = new EventBucket(
Profile::class,
ProfileCreated::raise(ProfileId::fromString('1'), Email::fromString('foo@test.com'))
);

$projectionRepository = $this->prophesize(ProjectionRepository::class);
$projectionRepository->handle($bucket->event())->shouldBeCalledOnce();

$projectionRepositoryTarget = new ProjectionRepositoryTarget($projectionRepository->reveal());

$projectionRepositoryTarget->save($bucket);
}
}
37 changes: 37 additions & 0 deletions tests/Unit/Pipeline/Target/ProjectionTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline\Target;

use Patchlevel\EventSourcing\Pipeline\EventBucket;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectionTarget;
use Patchlevel\EventSourcing\Projection\Projection;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class ProjectionTargetTest extends TestCase
{
use ProphecyTrait;

public function testSave(): void
{
$this->markTestIncomplete('Testing not finished, needs discussion');

$bucket = new EventBucket(
Profile::class,
ProfileCreated::raise(ProfileId::fromString('1'), Email::fromString('foo@test.com'))
);

$projectionRepository = $this->prophesize(Projection::class);
$projectionRepository->handledEvents()->will(static fn () => yield ProfileCreated::class => 'applyProfileCreated');

$projectionTarget = new ProjectionTarget($projectionRepository->reveal());

$projectionTarget->save($bucket);
}
}
Loading