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 89214f4
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 15 deletions.
10 changes: 9 additions & 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 Expand Up @@ -278,6 +278,14 @@
<code><![CDATA[$id]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php">
<MissingParamType>
<code><![CDATA[$message]]></code>
</MissingParamType>
<ReservedWord>
<code><![CDATA[ProfileVisited&ProfileCreated $event]]></code>
</ReservedWord>
</file>
<file src="tests/Unit/Subscription/Engine/DefaultSubscriptionEngineTest.php">
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$update1]]></code>
Expand Down
36 changes: 36 additions & 0 deletions src/Metadata/Subscriber/ArgumentTypeNotSupported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Subscriber;

use Patchlevel\EventSourcing\Metadata\MetadataException;

use function sprintf;

final class ArgumentTypeNotSupported extends MetadataException
{
public static function missingType(string $class, string $method, string $argumentName): self
{
return new self(
sprintf(
'Argument type for method "%s" in class "%s" is not supported. Argument "%s" must have a type.',
$method,
$class,
$argumentName,
),
);
}

public static function onlyNamedTypeSupported(string $class, string $method, string $argumentName): self
{
return new self(
sprintf(
'Argument type for method "%s" in class "%s" is not supported. Argument "%s" must not have a union or intersection type.',
$method,
$class,
$argumentName,
),
);
}
}
15 changes: 13 additions & 2 deletions src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use RuntimeException;

use function array_key_exists;

Expand Down Expand Up @@ -102,8 +101,20 @@ private function subscribeMethod(ReflectionMethod $method): SubscribeMethodMetad
foreach ($method->getParameters() as $parameter) {
$type = $parameter->getType();

if ($type === null) {
throw ArgumentTypeNotSupported::missingType(

Check warning on line 105 in src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.3, ubuntu-latest)

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ foreach ($method->getParameters() as $parameter) { $type = $parameter->getType(); if ($type === null) { - throw ArgumentTypeNotSupported::missingType($method->getDeclaringClass()->getName(), $method->getName(), $parameter->getName()); + ArgumentTypeNotSupported::missingType($method->getDeclaringClass()->getName(), $method->getName(), $parameter->getName()); } if (!$type instanceof ReflectionNamedType) { throw ArgumentTypeNotSupported::onlyNamedTypeSupported($method->getDeclaringClass()->getName(), $method->getName(), $parameter->getName());
$method->getDeclaringClass()->getName(),
$method->getName(),
$parameter->getName(),
);
}

if (!$type instanceof ReflectionNamedType) {
throw new RuntimeException('parameter type is required');
throw ArgumentTypeNotSupported::onlyNamedTypeSupported(
$method->getDeclaringClass()->getName(),
$method->getName(),
$parameter->getName(),
);
}

$arguments[] = new ArgumentMetadata(
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,9 @@
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\ArgumentTypeNotSupported;
use Patchlevel\EventSourcing\Metadata\Subscriber\AttributeSubscriberMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Subscriber\ClassIsNotASubscriber;
use Patchlevel\EventSourcing\Metadata\Subscriber\DuplicateSetupMethod;
Expand Down Expand Up @@ -163,6 +166,92 @@ 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 testMissingArgumentType(): void
{
$this->expectException(ArgumentTypeNotSupported::class);

$subscriber = new #[Subscriber('foo', RunMode::FromBeginning)]
class {
// phpcs:disable
#[Subscribe(ProfileVisited::class)]
public function profileVisited($message): void
{
}
// phpcs:enable
};

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

public function testUnionTypeNotSupported(): void
{
$this->expectException(ArgumentTypeNotSupported::class);

$subscriber = new #[Subscriber('foo', RunMode::FromBeginning)]
class {
#[Subscribe(ProfileVisited::class)]
public function profileVisited(ProfileVisited|ProfileCreated $event): void
{
}
};

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

public function testIntersectionTypeNotSupported(): void
{
$this->expectException(ArgumentTypeNotSupported::class);

$subscriber = new #[Subscriber('foo', RunMode::FromBeginning)]
class {
#[Subscribe(ProfileVisited::class)]
public function profileVisited(ProfileVisited&ProfileCreated $event): void
{
}
};

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

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,
),
);
}
}
Loading

0 comments on commit 89214f4

Please sign in to comment.