Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielBadura committed Feb 5, 2024
1 parent 9b461fd commit d80f244
Show file tree
Hide file tree
Showing 49 changed files with 1,271 additions and 94 deletions.
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;
}
}
3 changes: 1 addition & 2 deletions src/EventBus/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;

use function array_key_exists;
use function array_keys;

/**
* @template-covariant T of object
Expand Down Expand Up @@ -179,7 +178,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
23 changes: 23 additions & 0 deletions tests/Unit/Aggregate/AggregateRootIdNotSupportedTest.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\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(),
);
}
}
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();
}
}
1 change: 1 addition & 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 Down
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());
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Aggregate/MetadataNotPossibleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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(),
);
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Aggregate/UuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Aggregate;

use Patchlevel\EventSourcing\Aggregate\Uuid;
use PHPUnit\Framework\TestCase;
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($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($node = null, int|null $clockSeq = 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);
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Attribute/IdTest.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\Id;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Id */
final class IdTest extends TestCase
{
#[DoesNotPerformAssertions]
public function testInstantiate(): void
{
$attribute = new Id();
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Attribute/ProjectorTest.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\Projector;
use PHPUnit\Framework\TestCase;

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

self::assertSame('foo', $attribute->name);
self::assertSame(0, $attribute->version);
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Attribute/SetupTest.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\Setup;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Attribute\Setup */
final class SetupTest extends TestCase
{
#[DoesNotPerformAssertions]
public function testInstantiate(): void
{
$attribute = new Setup();
}
}
Loading

0 comments on commit d80f244

Please sign in to comment.