Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 9, 2024
1 parent 35d6f69 commit 1b7f34b
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 10 deletions.
9 changes: 9 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ deptrac:
collectors:
- type: directory
value: src/Clock/.*
- name: CommandBus
collectors:
- type: directory
value: src/CommandBus/.*
- name: Console
collectors:
- type: directory
Expand Down Expand Up @@ -102,7 +106,12 @@ deptrac:
- Attribute
- MetadataAggregate
Attribute:
- Aggregate
Clock:
CommandBus:
- Aggregate
- Attribute
- Repository
Console:
- Aggregate
- Message
Expand Down
7 changes: 5 additions & 2 deletions src/CommandBus/AggregateHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ private function aggregateClass(string $commandClass): string
return $handledBy->aggregateClass;
}

private function handleClass(Handle $handle, ReflectionMethod $reflectionMethod): string|null
/**
* @return class-string

Check failure on line 76 in src/CommandBus/AggregateHandlerProvider.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MoreSpecificReturnType

src/CommandBus/AggregateHandlerProvider.php:76:16: MoreSpecificReturnType: The declared return type 'class-string' for Patchlevel\EventSourcing\CommandBus\AggregateHandlerProvider::handleClass is more specific than the inferred return type 'class-string|non-empty-string' (see https://psalm.dev/070)
*/
private function handleClass(Handle $handle, ReflectionMethod $reflectionMethod): string
{
$parameters = $reflectionMethod->getParameters();

Expand All @@ -93,7 +96,7 @@ private function handleClass(Handle $handle, ReflectionMethod $reflectionMethod)
return $type->getName();

Check failure on line 96 in src/CommandBus/AggregateHandlerProvider.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.3, ubuntu-latest)

Method Patchlevel\EventSourcing\CommandBus\AggregateHandlerProvider::handleClass() should return class-string but returns string.

Check failure on line 96 in src/CommandBus/AggregateHandlerProvider.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

LessSpecificReturnStatement

src/CommandBus/AggregateHandlerProvider.php:96:20: LessSpecificReturnStatement: The type 'non-empty-string' is more general than the declared return type 'class-string' for Patchlevel\EventSourcing\CommandBus\AggregateHandlerProvider::handleClass (see https://psalm.dev/129)
}

throw InvalidHandleMethod::noType(
throw InvalidHandleMethod::incompatibleType(
$reflectionMethod->getDeclaringClass()->getName(),
$reflectionMethod->getName(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/CommandBus/InvalidHandleMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function noParameters(string $aggregateClass, string $method): sel
return new self(sprintf('Method "%s" in aggregate "%s" has no parameters', $method, $aggregateClass));
}

public static function noType(string $aggregateClass, string $method): self
public static function incompatibleType(string $aggregateClass, string $method): self
{
return new self(sprintf('Method "%s" in aggregate "%s" has no compatible type', $method, $aggregateClass));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CommandBus/AggregateHandlerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use stdClass;

/** @covers \Patchlevel\EventSourcing\CommandBus\AggregateHandlerProvider */
class AggregateHandlerProviderTest extends TestCase
final class AggregateHandlerProviderTest extends TestCase
{
use ProphecyTrait;

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CommandBus/DefaultCommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Prophecy\PhpUnit\ProphecyTrait;

/** @covers \Patchlevel\EventSourcing\CommandBus\DefaultCommandBus */
class DefaultCommandBusTest extends TestCase
final class DefaultCommandBusTest extends TestCase
{
use ProphecyTrait;

Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/CommandBus/Handler/DefaultHandlerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\CommandBus\Handler;

use Patchlevel\EventSourcing\CommandBus\Handler\CreateAggregateHandler;
use Patchlevel\EventSourcing\CommandBus\Handler\DefaultHandlerFactory;
use Patchlevel\EventSourcing\CommandBus\Handler\UpdateAggregateHandler;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Container\ContainerInterface;

/** @covers \Patchlevel\EventSourcing\CommandBus\Handler\DefaultHandlerFactory */
final class DefaultHandlerFactoryTest extends TestCase
{
use ProphecyTrait;

public function testCreateHandler(): void
{
$repositoryManager = $this->prophesize(RepositoryManager::class);
$container = $this->prophesize(ContainerInterface::class);

$handlerFactory = new DefaultHandlerFactory(
$repositoryManager->reveal(),
$container->reveal(),
);

$handler = $handlerFactory->createHandler(
'aggregateClass',
'method',
);

$this->assertInstanceOf(CreateAggregateHandler::class, $handler);
}

public function testUpdateHandler(): void
{
$repositoryManager = $this->prophesize(RepositoryManager::class);
$container = $this->prophesize(ContainerInterface::class);

$handlerFactory = new DefaultHandlerFactory(
$repositoryManager->reveal(),
$container->reveal(),
);

$handler = $handlerFactory->updateHandler(
'aggregateClass',
'method',
);

$this->assertInstanceOf(UpdateAggregateHandler::class, $handler);
}
}
59 changes: 59 additions & 0 deletions tests/Unit/CommandBus/HandlerDescriptorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\CommandBus;

use Patchlevel\EventSourcing\CommandBus\HandlerDescriptor;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithHandlers;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\CommandBus\HandlerDescriptor */
final class HandlerDescriptorTest extends TestCase
{
public function testObjectMethod(): void
{
$aggregate = ProfileWithHandlers::createEmpty();

$descriptor = new HandlerDescriptor($aggregate->changeName(...));

self::assertEquals($aggregate->changeName(...), $descriptor->callable());
self::assertEquals('Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithHandlers::changeName', $descriptor->name());
}

public function testStaticObjectMethod(): void
{
$descriptor = new HandlerDescriptor([ProfileWithHandlers::class, 'create']);

self::assertEquals(ProfileWithHandlers::create(...), $descriptor->callable());
self::assertEquals('Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileWithHandlers::create', $descriptor->name());
}

#[RequiresPhp('>= 8.2')]
public function testAnonymousFunction(): void
{
$handler = static function (): void {
};

$descriptor = new HandlerDescriptor($handler(...));

self::assertEquals($handler(...), $descriptor->callable());
self::assertEquals('Closure', $descriptor->name());
}

public function testAnonymousClass(): void
{
$handler = new class {
public function __invoke(): void
{
}
};

$descriptor = new HandlerDescriptor($handler->__invoke(...));

self::assertEquals($handler->__invoke(...), $descriptor->callable());
self::assertStringContainsString('class@anonymous', $descriptor->name());
self::assertStringContainsString(__FILE__, $descriptor->name());
}
}
2 changes: 1 addition & 1 deletion tests/Unit/CommandBus/ServiceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Psr\Clock\ClockInterface;

/** @covers \Patchlevel\EventSourcing\CommandBus\ServiceLocator */
class ServiceLocatorTest extends TestCase
final class ServiceLocatorTest extends TestCase
{
public function testGetService(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Fixture/ChangeProfileName.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Patchlevel\EventSourcing\Attribute\Id;

#[HandledBy(ProfileWithHandlers::class)]
class ChangeProfileName
final class ChangeProfileName
{
public function __construct(
#[Id]
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Fixture/NoParameterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
use Patchlevel\EventSourcing\Attribute\HandledBy;

#[HandledBy(ProfileWithHandlers::class)]
class NoParameterCommand
final class NoParameterCommand
{
}
2 changes: 1 addition & 1 deletion tests/Unit/Fixture/NoTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
use Patchlevel\EventSourcing\Attribute\HandledBy;

#[HandledBy(ProfileWithHandlers::class)]
class NoTypeCommand
final class NoTypeCommand
{
}
8 changes: 7 additions & 1 deletion tests/Unit/Fixture/ProfileWithHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ final class ProfileWithHandlers extends BasicAggregateRoot
private ProfileId $id;

#[Handle]
public static function create(CreateProfile $command): void
public static function create(CreateProfile $command): self
{
return new self();
}

public static function createEmpty(): self
{
return new self();
}

#[Handle]
Expand Down

0 comments on commit 1b7f34b

Please sign in to comment.