Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura committed Feb 5, 2024
1 parent d80f244 commit 8ab1c60
Show file tree
Hide file tree
Showing 22 changed files with 452 additions and 10 deletions.
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@default": true
},
"minMsi": 57,
"minCoveredMsi": 90,
"minCoveredMsi": 87,
"testFrameworkOptions": "--testsuite=unit"
}
5 changes: 5 additions & 0 deletions src/Pipeline/Middleware/RecalculatePlayheadMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __invoke(Message $message): array
];
}

public function reset(): void
{
$this->index = [];
}

/**
* @param class-string<AggregateRoot> $aggregateClass
*
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Aggregate/AggregateRootIdNotSupportedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public function testCreate(): void
'aggregate root id in class "Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile" must be instance of "Patchlevel\EventSourcing\Aggregate\AggregateRootId", got "int"',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Aggregate/ApplyMethodNotFoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public function testCreate(): void
'Apply method in "Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile" could not be found for the event "Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated"',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Aggregate/MetadataNotPossibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public function testCreate(): void
'Metadata method must be called on the concrete implementation',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Console/InvalidArgumentGivenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public function testException(): void

self::assertSame('Invalid argument given: need type "int" got "string"', $exception->getMessage());
self::assertSame($expectedValue, $exception->value());
self::assertSame(0, $exception->getCode());
}
}
58 changes: 51 additions & 7 deletions tests/Unit/Outbox/DoctrineOutboxStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,64 @@ public function testConfigureSchema(): void
);

$table = $this->prophesize(Table::class);
$column = $this->prophesize(Column::class);
$table->addColumn('aggregate', Types::STRING)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->setLength(255)->shouldBeCalledOnce()->willReturn($column->reveal())
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('aggregate_id', Types::STRING)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->setLength(32)->shouldBeCalledOnce()->willReturn($column->reveal())
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('playhead', Types::INTEGER)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('event', Types::STRING)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->setLength(255)->shouldBeCalledOnce()->willReturn($column->reveal())
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('payload', Types::JSON)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('recorded_on', Types::DATETIMETZ_IMMUTABLE)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(false)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(false)->shouldBeCalledOnce()
->getObjectProphecy()->reveal()
);

$column = $this->prophesize(Column::class);
$table->addColumn('custom_headers', Types::JSON)->shouldBeCalledOnce()
->willReturn($this->prophesize(Column::class)->setNotnull(true)->shouldBeCalledOnce()->getObjectProphecy()->reveal());
->willReturn(
$column
->setNotnull(true)->shouldBeCalledOnce()
->getObjectProphecy()->reveal()
);

$table->setPrimaryKey(['aggregate', 'aggregate_id', 'playhead'])->shouldBeCalledOnce();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/** @covers \Patchlevel\EventSourcing\Pipeline\Middleware\RecalculatePlayheadMiddleware */
final class RecalculatePlayheadMiddlewareTest extends TestCase
{
public function testReculatePlayhead(): void
public function testRecalculatePlayhead(): void
{
$middleware = new RecalculatePlayheadMiddleware();

Expand All @@ -36,7 +36,7 @@ public function testReculatePlayhead(): void
self::assertSame(1, $result[0]->playhead());
}

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

Expand All @@ -54,4 +54,65 @@ public function testReculatePlayheadWithSamePlayhead(): void

self::assertSame([$message], $result);
}
public function testRecalculateMultipleMessages(): void
{
$middleware = new RecalculatePlayheadMiddleware();

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$message = Message::create($event)
->withAggregateClass(Profile::class)
->withAggregateId('1')
->withPlayhead(5);
$result = $middleware($message);

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

$message = Message::create($event)
->withAggregateClass(Profile::class)
->withAggregateId('1')
->withPlayhead(8);

$result = $middleware($message);

self::assertCount(1, $result);
self::assertSame(Profile::class, $result[0]->aggregateClass());
self::assertSame(2, $result[0]->playhead());
}
public function testReset(): void
{
$middleware = new RecalculatePlayheadMiddleware();

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$message = Message::create($event)
->withAggregateClass(Profile::class)
->withAggregateId('1')
->withPlayhead(5);
$result = $middleware($message);

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

$message = Message::create($event)
->withAggregateClass(Profile::class)
->withAggregateId('1')
->withPlayhead(8);

$middleware->reset();
$result = $middleware($message);

self::assertCount(1, $result);
self::assertSame(Profile::class, $result[0]->aggregateClass());
self::assertSame(1, $result[0]->playhead());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Pipeline/Source/InMemorySourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ public function testCount(): void

self::assertSame(1, $source->count());
}

public function testCountWithIterator(): void
{
$message = new Message(
new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')),
);

$source = new InMemorySource(new \ArrayIterator([$message]));

self::assertSame(1, $source->count());
}
}
78 changes: 78 additions & 0 deletions tests/Unit/Pipeline/Target/ProjectorRepositoryTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

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

use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectorRepositoryTarget;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectorTarget;
use Patchlevel\EventSourcing\Pipeline\Target\StoreTarget;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Patchlevel\EventSourcing\Store\Store;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/** @covers \Patchlevel\EventSourcing\Pipeline\Target\ProjectorRepositoryTarget */
final class ProjectorRepositoryTargetTest extends TestCase
{
use ProphecyTrait;

public function testSave(): void
{
$message = new Message(
new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')),
);

$projector = new class {
public Message|null $message = null;

public function __invoke(Message $message)
{
$this->message = $message;
}
};

$projectorRepository = $this->prophesize(ProjectorRepository::class);
$projectorRepository->projectors()->shouldBeCalledOnce()->willReturn([$projector]);

$projectorResolver = $this->prophesize(ProjectorResolver::class);
$projectorResolver->resolveSubscribeMethod($projector, $message)->shouldBeCalledOnce()->willReturn($projector(...));

$projectorRepositoryTarget = new ProjectorRepositoryTarget($projectorRepository->reveal(), $projectorResolver->reveal());
$projectorRepositoryTarget->save($message);

self::assertSame($message, $projector->message);
}

public function testSaveNoHit(): void
{
$message = new Message(
new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')),
);

$projector = new class {
public Message|null $message = null;

public function __invoke(Message $message)
{
$this->message = $message;
}
};

$projectorRepository = $this->prophesize(ProjectorRepository::class);
$projectorRepository->projectors()->shouldBeCalledOnce()->willReturn([$projector]);

$projectorResolver = $this->prophesize(ProjectorResolver::class);
$projectorResolver->resolveSubscribeMethod($projector, $message)->shouldBeCalledOnce()->willReturn(null);

$projectorRepositoryTarget = new ProjectorRepositoryTarget($projectorRepository->reveal(), $projectorResolver->reveal());
$projectorRepositoryTarget->save($message);

self::assertNull($projector->message);
}
}
72 changes: 72 additions & 0 deletions tests/Unit/Pipeline/Target/ProjectorTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

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

use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Pipeline\Target\ProjectorTarget;
use Patchlevel\EventSourcing\Pipeline\Target\StoreTarget;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Patchlevel\EventSourcing\Store\Store;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/** @covers \Patchlevel\EventSourcing\Pipeline\Target\ProjectorTarget */
final class ProjectorTargetTest extends TestCase
{
use ProphecyTrait;

public function testSave(): void
{
$message = new Message(
new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')),
);

$projector = new class {
public Message|null $message = null;

public function __invoke(Message $message)
{
$this->message = $message;
}
};


$projectorResolver = $this->prophesize(ProjectorResolver::class);
$projectorResolver->resolveSubscribeMethod($projector, $message)->shouldBeCalledOnce()->willReturn($projector(...));

$projectorTarget = new ProjectorTarget($projector, $projectorResolver->reveal());
$projectorTarget->save($message);

self::assertSame($message, $projector->message);
}

public function testSaveNoHit(): void
{
$message = new Message(
new ProfileCreated(ProfileId::fromString('1'), Email::fromString('[email protected]')),
);

$projector = new class {
public Message|null $message = null;

public function __invoke(Message $message)
{
$this->message = $message;
}
};


$projectorResolver = $this->prophesize(ProjectorResolver::class);
$projectorResolver->resolveSubscribeMethod($projector, $message)->shouldBeCalledOnce()->willReturn(null);

$projectorTarget = new ProjectorTarget($projector, $projectorResolver->reveal());
$projectorTarget->save($message);

self::assertNull($projector->message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public function testCreate(): void
'projection with the id "foo-1" exist already',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public function testCreate(): void
'projection with the id "foo-1" not found',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Serializer/Encoder/DecodeNotPossibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public function testCreate(): void
$exception->getMessage(),
);
self::assertSame('foo', $exception->data());
self::assertSame(0, $exception->getCode());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Serializer/Encoder/EncodeNotPossibleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public function testCreate(): void
$exception->getMessage(),
);
self::assertSame([], $exception->data());
self::assertSame(0, $exception->getCode());
}
}
Loading

0 comments on commit 8ab1c60

Please sign in to comment.