Skip to content

Commit

Permalink
stricter entity constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 25, 2019
1 parent 3e38722 commit ef05cf8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prefer-stable": true,
"require": {
"php": "^7.1",
"phpgears/event": "~0.2",
"phpgears/event": "~0.3.2",
"phpgears/identity": "~0.2"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractEntity implements Entity
*
* @param Identity $identity
*/
public function __construct(Identity $identity)
final protected function __construct(Identity $identity)
{
$this->identity = $identity;
}
Expand Down
10 changes: 6 additions & 4 deletions src/EventBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ final protected function recordEvent(Event $event): void
*/
final public function getRecordedEvents(): EventCollection
{
$recordedEvents = $this->recordedEvents ?? new \ArrayObject();
return new EventIteratorCollection($recordedEvents->getIterator());
return new EventIteratorCollection(
$this->recordedEvents !== null ? $this->recordedEvents->getIterator() : new \EmptyIterator()
);
}

/**
Expand All @@ -63,8 +64,9 @@ final public function clearRecordedEvents(): void
*/
final public function collectRecordedEvents(): EventCollection
{
$recordedEvents = $this->recordedEvents ?? new \ArrayObject();
$events = new EventIteratorCollection($recordedEvents->getIterator());
$events = new EventIteratorCollection(
$this->recordedEvents !== null ? $this->recordedEvents->getIterator() : new \EmptyIterator()
);

$this->recordedEvents = new \ArrayObject();

Expand Down
13 changes: 7 additions & 6 deletions tests/Aggregate/AbstractAggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ class AbstractAggregateRootTest extends TestCase
{
public function testApply(): void
{
/** @var Event $event */
$event = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->getMock();
$aggregateIdentity = UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f');

$aggregateRoot = AbstractAggregateRootStub::instantiateWithEvent($aggregateIdentity, $event);
$aggregateRoot = AbstractAggregateRootStub::instantiate($aggregateIdentity);

static::assertSame($aggregateIdentity, $aggregateRoot->getIdentity());
}
Expand All @@ -44,6 +40,10 @@ public function testRecordedEvents(): void
->getMock();
$aggregateIdentity = UuidIdentity::fromString('3247cb6e-e9c7-4f3a-9c6c-0dec26a0353f');

$aggregateRoot = AbstractAggregateRootStub::instantiate($aggregateIdentity);

static::assertCount(0, $aggregateRoot->getRecordedEvents());

$aggregateRoot = AbstractAggregateRootStub::instantiateWithEvent($aggregateIdentity, $event);

static::assertCount(1, $aggregateRoot->getRecordedEvents());
Expand All @@ -54,8 +54,9 @@ public function testRecordedEvents(): void

static::assertCount(1, $aggregateRoot->getRecordedEvents());
$recordedEvents = $aggregateRoot->collectRecordedEvents();
static::assertCount(0, $aggregateRoot->collectRecordedEvents());
static::assertCount(1, $recordedEvents);
static::assertCount(0, $aggregateRoot->getRecordedEvents());
static::assertCount(0, $aggregateRoot->collectRecordedEvents());

static::assertEquals([$event], \iterator_to_array($recordedEvents));
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Aggregate/AbstractEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Gears\Aggregate\Tests;

use Gears\Aggregate\AbstractEntity;
use Gears\Aggregate\Tests\Stub\AbstractEntityStub;
use Gears\Identity\UuidIdentity;
use PHPUnit\Framework\TestCase;

Expand All @@ -26,8 +26,7 @@ public function testCreation(): void
{
$identity = UuidIdentity::fromString('30a447f7-72c0-4b43-a0ab-a22fb8ce651d');

$entity = new class($identity) extends AbstractEntity {
};
$entity = AbstractEntityStub::instantiate($identity);

static::assertSame($identity, $entity->getIdentity());
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Aggregate/Stub/AbstractAggregateRootStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
*/
class AbstractAggregateRootStub extends AbstractAggregateRoot
{
/**
* @param Identity $identity
*
* @return self
*/
public static function instantiate(Identity $identity): self
{
return new self($identity);
}

/**
* @param Identity $identity
* @param Event $event
Expand Down
33 changes: 33 additions & 0 deletions tests/Aggregate/Stub/AbstractEntityStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* aggregate (https://github.com/phpgears/aggregate).
* Aggregate base.
*
* @license MIT
* @link https://github.com/phpgears/aggregate
* @author Julián Gutiérrez <juliangut@gmail.com>
*/

declare(strict_types=1);

namespace Gears\Aggregate\Tests\Stub;

use Gears\Aggregate\AbstractEntity;
use Gears\Identity\Identity;

/**
* Abstract entity stub class.
*/
class AbstractEntityStub extends AbstractEntity
{
/**
* @param Identity $identity
*
* @return self
*/
public static function instantiate(Identity $identity): self
{
return new self($identity);
}
}

0 comments on commit ef05cf8

Please sign in to comment.