Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 26, 2024
1 parent 00d280c commit 0a369ac
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 13 deletions.
2 changes: 1 addition & 1 deletion baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<MixedMethodCall>
<code><![CDATA[$method]]></code>
<code><![CDATA[$method]]></code>
<code><![CDATA[$method]]></code>
<code><![CDATA[$methodName]]></code>
</MixedMethodCall>
</file>
<file src="tests/Benchmark/BasicImplementation/Profile.php">
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription/Subscriber/NoSuitableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use function sprintf;

class NoSuitableResolver extends RuntimeException
final class NoSuitableResolver extends RuntimeException
{
public function __construct(string $class, string $methodName, string $argumentName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Metadata\Subscriber\AttributeSubscriberMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Subscriber\ClassIsNotASubscriber;
use Patchlevel\EventSourcing\Metadata\Subscriber\DuplicateSetupMethod;
Expand Down Expand Up @@ -163,6 +165,42 @@ public function handle(): void
);
}

public function testSubscribeAttributes(): void
{
$subscriber = new #[Subscriber('foo', RunMode::FromBeginning)]
class {
#[Subscribe(ProfileVisited::class)]
public function profileVisited(Message $message): void
{
}

#[Subscribe(ProfileCreated::class)]
public function profileCreated(ProfileCreated $profileCreated, string $aggregateId): void
{
}
};

$metadataFactory = new AttributeSubscriberMetadataFactory();
$metadata = $metadataFactory->metadata($subscriber::class);

self::assertEquals(
[
ProfileVisited::class => [
new SubscribeMethodMetadata('profileVisited', [
new ArgumentMetadata('message', Message::class),
]),
],
ProfileCreated::class => [
new SubscribeMethodMetadata('profileCreated', [
new ArgumentMetadata('profileCreated', ProfileCreated::class),
new ArgumentMetadata('aggregateId', 'string'),
]),
],
],
$metadata->subscribeMethods,
);
}

public function testDuplicateSetupAttributeException(): void
{
$this->expectException(DuplicateSetupMethod::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Subscriber\ArgumentResolver;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\AggregateIdArgumentResolver;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\AggregateIdArgumentResolver */
final class AggregateIdArgumentResolverTest extends TestCase
{
public function testSupport(): void
{
$resolver = new AggregateIdArgumentResolver();

self::assertTrue(
$resolver->support(
new ArgumentMetadata('aggregateId', 'string'),
ProfileCreated::class,
),
);

self::assertTrue(
$resolver->support(
new ArgumentMetadata('aggregateRootId', 'string'),
ProfileCreated::class,
),
);

self::assertFalse(
$resolver->support(
new ArgumentMetadata('foo', 'string'),
ProfileCreated::class,
),
);
}

public function testResolve(): void
{
$event = new ProfileVisited(ProfileId::fromString('1'));

$resolver = new AggregateIdArgumentResolver();
$message = (new Message($event))->withHeader(
new AggregateHeader('foo', 'bar', 1, new DateTimeImmutable()),
);

self::assertSame(
'bar',
$resolver->resolve(
new ArgumentMetadata('foo', 'string'),
$message,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Subscriber\ArgumentResolver;

use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\EventArgumentResolver;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\EventArgumentResolver */
final class EventArgumentResolverTest extends TestCase
{
public function testSupport(): void
{
$resolver = new EventArgumentResolver();

self::assertTrue(
$resolver->support(
new ArgumentMetadata('foo', ProfileCreated::class),
ProfileCreated::class,
),
);

self::assertFalse(
$resolver->support(
new ArgumentMetadata('foo', ProfileVisited::class),
ProfileCreated::class,
),
);
}

public function testResolve(): void
{
$event = new ProfileVisited(ProfileId::fromString('1'));

$resolver = new EventArgumentResolver();
$message = new Message($event);

self::assertSame(
$event,
$resolver->resolve(
new ArgumentMetadata('foo', ProfileVisited::class),
$message,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Subscriber\ArgumentResolver;

use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\MessageArgumentResolver;
use PHPUnit\Framework\TestCase;
use stdClass;

/** @covers \Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\MessageArgumentResolver */
final class MessageArgumentResolverTest extends TestCase
{
public function testSupport(): void
{
$resolver = new MessageArgumentResolver();

self::assertTrue(
$resolver->support(
new ArgumentMetadata('foo', Message::class),
'qux',
),
);

self::assertFalse(
$resolver->support(
new ArgumentMetadata('foo', 'bar'),
'qux',
),
);
}

public function testResolve(): void
{
$resolver = new MessageArgumentResolver();
$message = new Message(new stdClass());

self::assertSame(
$message,
$resolver->resolve(
new ArgumentMetadata('foo', Message::class),
$message,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Subscriber\ArgumentResolver;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Aggregate\AggregateHeader;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\RecordedOnArgumentResolver;
use PHPUnit\Framework\TestCase;
use stdClass;

/** @covers \Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver\RecordedOnArgumentResolver */
final class RecordedOnArgumentResolverTest extends TestCase
{
public function testSupport(): void
{
$resolver = new RecordedOnArgumentResolver();

self::assertTrue(
$resolver->support(
new ArgumentMetadata('foo', DateTimeImmutable::class),
'qux',
),
);

self::assertFalse(
$resolver->support(
new ArgumentMetadata('foo', 'bar'),
'qux',
),
);
}

public function testResolve(): void
{
$date = new DateTimeImmutable();

$resolver = new RecordedOnArgumentResolver();
$message = (new Message(new stdClass()))->withHeader(
new AggregateHeader(
'foo',
'bar',
1,
$date,
),
);

self::assertSame(
$date,
$resolver->resolve(
new ArgumentMetadata('foo', DateTimeImmutable::class),
$message,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Patchlevel\EventSourcing\Tests\Unit\Subscription\Subscriber;

use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\Subscriber\ArgumentMetadata;
use Patchlevel\EventSourcing\Metadata\Subscriber\AttributeSubscriberMetadataFactory;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\Subscription\Subscriber\ArgumentResolver;
Expand Down Expand Up @@ -32,15 +34,29 @@ class {
};
$metadataFactory = new AttributeSubscriberMetadataFactory();

$customResolver = new class implements ArgumentResolver\ArgumentResolver {
public function resolve(ArgumentMetadata $argument, Message $message): mixed
{
return null;
}

public function support(ArgumentMetadata $argument, string $eventClass): bool
{
return false;
}
};

$repository = new MetadataSubscriberAccessorRepository(
[$subscriber],
$metadataFactory,
[$customResolver],
);

$accessor = new MetadataSubscriberAccessor(
$subscriber,
$metadataFactory->metadata($subscriber::class),
[
$customResolver,
new ArgumentResolver\MessageArgumentResolver(),
new ArgumentResolver\EventArgumentResolver(),
new ArgumentResolver\AggregateIdArgumentResolver(),
Expand Down
Loading

0 comments on commit 0a369ac

Please sign in to comment.