Skip to content

Commit

Permalink
Rely on static analysis for MapAggregateTypeResolver (#585)
Browse files Browse the repository at this point in the history
This will explode with invalid values anyway (though just not ever hit
on invalid keys), the static analysis typings mean there is already a
safety net for invalid input.
  • Loading branch information
ben-challis authored Oct 12, 2023
1 parent 258b84d commit 16a585f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 27 deletions.
4 changes: 0 additions & 4 deletions src/MapAggregateTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Lendable\Aggregate;

use Assert\Assertion;

/**
* @phpstan-template TAggregateRoot of object
*
Expand All @@ -18,8 +16,6 @@ final class MapAggregateTypeResolver implements AggregateTypeResolver
*/
public function __construct(private readonly array $map)
{
Assertion::allClassExists(\array_keys($map), 'All map keys must be class names that exist, %s does not exist.');
Assertion::allIsInstanceOf($map, AggregateType::class, 'All map values must be instances of '.AggregateType::class.', %s is not.');
}

public function resolve(object $aggregate): AggregateType
Expand Down
23 changes: 0 additions & 23 deletions tests/unit/MapAggregateTypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,6 @@ public function it_can_resolve_an_aggregate_type_for_a_mapped_aggregate_root():
$this->assertSame('foo', $fixture->resolve(new \stdClass())->toString());
}

#[Test]
public function throws_if_a_mapped_class_does_not_exist(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('All map keys must be class names that exist, FooBarBaz does not exist.');

// @phpstan-ignore-next-line intentional undefined class.
new MapAggregateTypeResolver([\FooBarBaz::class => AggregateType::fromString('FooBarBaz')]);
}

#[Test]
public function throws_if_a_mapped_value_is_not_an_aggregate_type(): void
{
$badValue = new class () {
};

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf('All map values must be instances of %s, %s is not', AggregateType::class, $badValue::class));

// @phpstan-ignore-next-line intentionally non-compliant value passed.
new MapAggregateTypeResolver([\stdClass::class => $badValue]);
}

#[Test]
public function throws_if_cannot_resolve_an_aggregate_as_not_mapped(): void
{
Expand Down

0 comments on commit 16a585f

Please sign in to comment.