Skip to content

Commit

Permalink
Working on the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Jun 27, 2024
1 parent 8d167b9 commit 014310c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 94 deletions.
7 changes: 4 additions & 3 deletions src/Repository/AggregateFactory/ReflectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public function __construct(
protected string $methodName = 'applyEventsFromHistory',
protected array $classMap = []
protected array $classMap = []
) {
}

Expand Down Expand Up @@ -64,7 +64,8 @@ protected function fromSnapshot(SnapshotInterface $snapshot, Iterator $events)
* @throws EventSourcedRepositoryException
* @throws ReflectionException
*/
protected function fromString(string $aggregate, Iterator $events): object{
protected function fromString(string $aggregate, Iterator $events): object
{
if (isset($this->classMap[$aggregate])) {
$aggregate = $this->classMap[$aggregate];
}
Expand All @@ -82,7 +83,7 @@ protected function fromString(string $aggregate, Iterator $events): object{
protected function assertAggregateHasMethod(object $aggregate): void
{
if (!method_exists($aggregate, $this->methodName)) {
throw new EventSourcedRepositoryException (sprintf(
throw new EventSourcedRepositoryException(sprintf(
'Aggregate class `%s` does not have a method `%s` to reconstruct the aggregate state.',
get_class($aggregate),
$this->methodName
Expand Down
79 changes: 2 additions & 77 deletions tests/Aggregate/AbstractEventSourcedAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,13 @@
use Generator;
use Phauthentic\EventStore\Event;
use PHPUnit\Framework\TestCase;
use Phauthentic\EventSourcing\Aggregate\AbstractEventSourcedAggregate;
use Phauthentic\EventSourcing\Aggregate\Attribute\DomainEvents;
use Phauthentic\EventSourcing\Aggregate\Exception\AggregateEventVersionMismatchException;
use Phauthentic\EventSourcing\Aggregate\Exception\EventMismatchException;
use Phauthentic\EventSourcing\Aggregate\Exception\MissingEventHandlerException;
use Phauthentic\EventSourcing\DomainEvent\AggregateIdentityProvidingEventInterface;

/**
*
*/
class ConcreteAggregate extends AbstractEventSourcedAggregate
{
#[DomainEvents]
protected array $aggregateEvents = [];

public string $testProperty = '';

public function __construct()
{
$this->aggregateId = 'test-id';
}

protected function whenTestEvent(TestEvent $event): void
{
$this->testProperty = $event->getText();
}

public function whenIdentityProvidingTestEvent(IdentityProvidingTestEvent $event)
{
$this->testProperty = $event->getText();
}

public function doSomething(string $data): void
{
$this->recordThat(new TestEvent($data));
}

public function getAggregateEvents(): array
{
return $this->aggregateEvents;
}

public function getAggregateVersion(): int
{
return $this->aggregateVersion;
}
}

/**
*
*/
class TestEvent
{
public function __construct(private readonly string $text = '') {}

public function getText()
{
return $this->text;
}
}

class TestEvent2 extends TestEvent
{
}

class IdentityProvidingTestEvent extends TestEvent implements AggregateIdentityProvidingEventInterface
{
public string $aggregateId = '';

public int $aggregateVersion = 1;

public function getAggregateId(): string
{
return $this->aggregateId;
}

public function getAggregateVersion(): int
{
return $this->aggregateVersion;
}
}

class AbstractEventSourcedAggregateTest extends TestCase
{
private ConcreteAggregate $aggregate;
Expand Down Expand Up @@ -127,7 +52,7 @@ public function testApplyEvent(): void

public function testApplyEventsFromHistory(): void
{
$eventsGenerator = function(): Generator {
$eventsGenerator = function (): Generator {
yield new Event(
aggregateId: 'test-id',
aggregateVersion: 1,
Expand Down Expand Up @@ -159,7 +84,7 @@ public function testApplyEventsFromHistory(): void

public function testApplyEventsFromHistoryWithGenerator(): void
{
$eventsGenerator = function(): Generator {
$eventsGenerator = function (): Generator {
yield new Event(
aggregateId: 'test-id',
aggregateVersion: 1,
Expand Down
49 changes: 49 additions & 0 deletions tests/Aggregate/ConcreteAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Phauthentic\EventSourcing\Test\Aggregate;

use Phauthentic\EventSourcing\Aggregate\AbstractEventSourcedAggregate;
use Phauthentic\EventSourcing\Aggregate\Attribute\DomainEvents;

/**
*
*/
class ConcreteAggregate extends AbstractEventSourcedAggregate
{
#[DomainEvents]
protected array $aggregateEvents = [];

public string $testProperty = '';

public function __construct()
{
$this->aggregateId = 'test-id';
}

protected function whenTestEvent(TestEvent $event): void
{
$this->testProperty = $event->getText();
}

public function whenIdentityProvidingTestEvent(IdentityProvidingTestEvent $event)
{
$this->testProperty = $event->getText();
}

public function doSomething(string $data): void
{
$this->recordThat(new TestEvent($data));
}

public function getAggregateEvents(): array
{
return $this->aggregateEvents;
}

public function getAggregateVersion(): int
{
return $this->aggregateVersion;
}
}
15 changes: 1 addition & 14 deletions tests/Aggregate/DomainEvent/AbstractDomainEventTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// phpcs:ignoreFile

declare(strict_types=1);

Expand All @@ -8,20 +9,6 @@
use Phauthentic\EventSourcing\DomainEvent\AggregateVersionProvidingEvent;
use Phauthentic\EventSourcing\DomainEvent\TypeProvidingDomainEventInterface;
use PHPUnit\Framework\TestCase;
use Phauthentic\EventSourcing\DomainEvent\AbstractDomainEvent;

/**
* A concrete implementation of AbstractDomainEvent for testing
*/
class ConcreteDomainEvent extends AbstractDomainEvent
{
public function __construct(string $aggregateId, int $aggregateVersion, string $domainEventType)
{
$this->aggregateId = $aggregateId;
$this->aggregateVersion = $aggregateVersion;
$this->domainEventType = $domainEventType;
}
}

/**
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Aggregate/DomainEvent/ConcreteDomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Phauthentic\EventSourcing\Test\Aggregate\DomainEvent;

use Phauthentic\EventSourcing\DomainEvent\AbstractDomainEvent;

/**
* A concrete implementation of AbstractDomainEvent for testing
*/
class ConcreteDomainEvent extends AbstractDomainEvent
{
public function __construct(string $aggregateId, int $aggregateVersion, string $domainEventType)
{
$this->aggregateId = $aggregateId;
$this->aggregateVersion = $aggregateVersion;
$this->domainEventType = $domainEventType;
}
}
27 changes: 27 additions & 0 deletions tests/Aggregate/IdentityProvidingTestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Phauthentic\EventSourcing\Test\Aggregate;

use Phauthentic\EventSourcing\DomainEvent\AggregateIdentityProvidingEventInterface;

/**
*
*/
class IdentityProvidingTestEvent extends TestEvent implements AggregateIdentityProvidingEventInterface
{
public string $aggregateId = '';

public int $aggregateVersion = 1;

public function getAggregateId(): string
{
return $this->aggregateId;
}

public function getAggregateVersion(): int
{
return $this->aggregateVersion;
}
}
20 changes: 20 additions & 0 deletions tests/Aggregate/TestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Phauthentic\EventSourcing\Test\Aggregate;

/**
*
*/
class TestEvent
{
public function __construct(private readonly string $text = '')
{
}

public function getText()
{
return $this->text;
}
}
9 changes: 9 additions & 0 deletions tests/Aggregate/TestEvent2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Phauthentic\EventSourcing\Test\Aggregate;

class TestEvent2 extends TestEvent
{
}

0 comments on commit 014310c

Please sign in to comment.