Skip to content

Commit

Permalink
Merge pull request #490 from patchlevel/add-more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
DavidBadura authored Feb 6, 2024
2 parents 9b461fd + 90adf13 commit ec16bc7
Show file tree
Hide file tree
Showing 64 changed files with 1,734 additions and 99 deletions.
19 changes: 18 additions & 1 deletion baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.19.0@06b71be009a6bd6d81b9811855d6629b9fe90e1b">
<files psalm-version="5.21.1@8c473e2437be8b6a8fd8f630f0f11a16b114c494">
<file src="src/Aggregate/AggregateRootBehaviour.php">
<UnsafeInstantiation>
<code>new static()</code>
Expand Down Expand Up @@ -188,6 +188,23 @@
<code>$name</code>
</PropertyNotSetInConstructor>
</file>
<file src="tests/Unit/EventBus/MessageTest.php">
<InvalidArgument>
<code><![CDATA[[
'foo' => 'bar',
'aggregateClass' => Profile::class,
'aggregateId' => '1',
'playhead' => 3,
'recordedOn' => $recordedAt,
'newStreamStart' => true,
'archived' => true,
]]]></code>
</InvalidArgument>
<TypeDoesNotContainType>
<code>assertSame</code>
<code>assertSame</code>
</TypeDoesNotContainType>
</file>
<file src="tests/Unit/Fixture/MessageNormalizer.php">
<MixedArgumentTypeCoercion>
<code>$value</code>
Expand Down
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"
}
42 changes: 20 additions & 22 deletions src/EventBus/ListenerDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@ final class ListenerDescriptor

public function __construct(callable $callable)
{
$callable = $callable(...);
$this->callable = $callable(...);
$this->name = self::closureName($this->callable);
}

public function name(): string
{
return $this->name;
}

$this->callable = $callable;
public function callable(): callable
{
return $this->callable;
}

$reflectionFunction = new ReflectionFunction($callable);
private static function closureName(Closure $closure): string
{
$reflectionFunction = new ReflectionFunction($closure);

if (method_exists($reflectionFunction, 'isAnonymous') && $reflectionFunction->isAnonymous()) {
$this->name = 'Closure';

return;
return 'Closure';
}

$callable = $reflectionFunction->getClosureThis();
$closureThis = $reflectionFunction->getClosureThis();

if (!$callable) {
if (!$closureThis) {
$class = $reflectionFunction->getClosureCalledClass();

$this->name = ($class ? $class->name . '::' : '') . $reflectionFunction->name;

return;
return ($class ? $class->name . '::' : '') . $reflectionFunction->name;
}

$this->name = $callable::class . '::' . $reflectionFunction->name;
}

public function name(): string
{
return $this->name;
}

public function callable(): callable
{
return $this->callable;
return $closureThis::class . '::' . $reflectionFunction->name;
}
}
12 changes: 9 additions & 3 deletions src/EventBus/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;

use function array_key_exists;
use function array_keys;

/**
* @template-covariant T of object
* @psalm-immutable
* @psalm-type Headers = array{aggregateClass?: class-string<AggregateRoot>, aggregateId?:string, playhead?:positive-int, recordedOn?: DateTimeImmutable, newStreamStart?: bool, archived?: bool}
* @psalm-type Headers = array{
* aggregateClass?: class-string<AggregateRoot>,
* aggregateId?: string,
* playhead?: positive-int,
* recordedOn?: DateTimeImmutable,
* newStreamStart?: bool,
* archived?: bool
* }
*/
final class Message
{
Expand Down Expand Up @@ -179,7 +185,7 @@ public function withArchived(bool $value): self
/** @throws HeaderNotFound */
public function customHeader(string $name): mixed
{
if (array_keys($this->customHeaders, $name)) {
if (!array_key_exists($name, $this->customHeaders)) {
throw HeaderNotFound::custom($name);
}

Expand Down
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
24 changes: 24 additions & 0 deletions tests/Unit/Aggregate/AggregateRootIdNotSupportedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Aggregate\AggregateRootIdNotSupported;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Aggregate\AggregateRootIdNotSupported */
final class AggregateRootIdNotSupportedTest extends TestCase
{
public function testCreate(): void
{
$exception = new AggregateRootIdNotSupported(Profile::class, 1);

self::assertSame(
'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());
}
}
29 changes: 28 additions & 1 deletion tests/Unit/Aggregate/AggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Aggregate\AggregateRootIdNotSupported;
use Patchlevel\EventSourcing\Aggregate\ApplyMethodNotFound;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\MetadataNotPossible;
Expand All @@ -16,10 +17,16 @@
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileInvalid;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithBrokenId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithSuppressAll;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot */
/**
* @covers \Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot
* @covers \Patchlevel\EventSourcing\Aggregate\AggregateRootBehaviour
* @covers \Patchlevel\EventSourcing\Aggregate\AggregateRootAttributeBehaviour
* @covers \Patchlevel\EventSourcing\Aggregate\AggregateRootMetadataAwareBehaviour
*/
final class AggregateRootTest extends TestCase
{
public function testApplyMethod(): void
Expand Down Expand Up @@ -162,4 +169,24 @@ public function testMetadataNotPossible(): void

BasicAggregateRoot::metadata();
}

public function testCachedAggregateId(): void
{
$profileId = ProfileId::fromString('1');
$email = Email::fromString('[email protected]');

$profile = Profile::createProfile($profileId, $email);
$id = $profile->aggregateRootId();

self::assertSame($profileId, $id);
self::assertSame($id, $profile->aggregateRootId());
}

public function testInvalidAggregateId(): void
{
$aggregate = ProfileWithBrokenId::create();

$this->expectException(AggregateRootIdNotSupported::class);
$aggregate->aggregateRootId();
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Aggregate/ApplyMethodNotFoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Aggregate\ApplyMethodNotFound */
final class ApplyMethodNotFoundTest extends TestCase
{
public function testCreate(): void
Expand All @@ -19,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());
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Aggregate/CustomIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Aggregate\CustomId;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Aggregate\CustomId */
final class CustomIdTest extends TestCase
{
public function testFromString(): void
{
$id = CustomId::fromString('1');

self::assertSame('1', $id->toString());
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Aggregate/MetadataNotPossibleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Aggregate\MetadataNotPossible;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Aggregate\MetadataNotPossible */
final class MetadataNotPossibleTest extends TestCase
{
public function testCreate(): void
{
$exception = new MetadataNotPossible();

self::assertSame(
'Metadata method must be called on the concrete implementation',
$exception->getMessage(),
);
self::assertSame(0, $exception->getCode());
}
}
56 changes: 56 additions & 0 deletions tests/Unit/Aggregate/UuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use DateTimeInterface;
use Patchlevel\EventSourcing\Aggregate\Uuid;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Uuid as RamseyUuid;
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidInterface;

/** @covers \Patchlevel\EventSourcing\Aggregate\Uuid */
final class UuidTest extends TestCase
{
public function testFromString(): void
{
$id = Uuid::fromString('1eec1e5c-e397-6644-9aed-0242ac110002');

self::assertSame('1eec1e5c-e397-6644-9aed-0242ac110002', $id->toString());
}

public function testV6(): void
{
$factory = new class extends UuidFactory
{
public function uuid6(Hexadecimal|null $node = null, int|null $clockSeq = null): UuidInterface
{
return RamseyUuid::fromString('1eec1e5c-e397-6644-9aed-0242ac110002');
}
};

RamseyUuid::setFactory($factory);
$id = Uuid::v6();

self::assertSame('1eec1e5c-e397-6644-9aed-0242ac110002', $id->toString());
}

public function testV7(): void
{
$factory = new class extends UuidFactory
{
public function uuid7(DateTimeInterface|null $dateTime = null): UuidInterface
{
return RamseyUuid::fromString('018d6a97-6aba-7104-825f-67313a77a2a4');
}
};

RamseyUuid::setFactory($factory);
$id = Uuid::v7();

self::assertSame('018d6a97-6aba-7104-825f-67313a77a2a4', $id->toString());
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Attribute/AggregateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Attribute;

use Patchlevel\EventSourcing\Attribute\Aggregate;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Aggregate */
final class AggregateTest extends TestCase
{
public function testInstantiate(): void
{
$attribute = new Aggregate('foo');

self::assertSame('foo', $attribute->name);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Attribute/ApplyTest.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\Attribute;

use Patchlevel\EventSourcing\Attribute\Apply;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Apply */
final class ApplyTest extends TestCase
{
public function testInstantiate(): void
{
$attribute = new Apply(Profile::class);

self::assertSame(Profile::class, $attribute->eventClass);
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Attribute/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Attribute;

use Patchlevel\EventSourcing\Attribute\Event;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Event */
final class EventTest extends TestCase
{
public function testInstantiate(): void
{
$attribute = new Event('foo');

self::assertSame('foo', $attribute->name);
}
}
Loading

0 comments on commit ec16bc7

Please sign in to comment.