Skip to content

Commit

Permalink
Release 0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
othercodes authored Dec 6, 2021
2 parents 9d98565 + 7af3777 commit 46f9202
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Domain/Traits/HasInvariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ final public static function invariants(): array
*
* @return void
*/
final private function check(callable $filter = null, callable $onFail = null): void
private function check(callable $filter = null, callable $onFail = null): void
{
$violations = [];

Expand Down
20 changes: 19 additions & 1 deletion src/Domain/Traits/IsAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,23 @@
*/
trait IsAggregate
{
use IsEntity, HasDomainEvents, HasServiceBus;
use HasDomainEvents, HasServiceBus, IsEntity {
withOverrides as entityWithOverrides;
}

/**
* Makes a full copy of the instance with all registered domain events and overrides attributes provided by the client
* @param array $overrides
* @return static
*/
protected function withOverrides(array $overrides)
{
$copy = $this->entityWithOverrides($overrides);

foreach ($this->_domainEvents as $event) {
$copy->registerDomainEvent($event);
}

return $copy;
}
}
12 changes: 12 additions & 0 deletions src/Domain/Traits/IsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public static function wakeup()
}
}

/**
* Makes a full copy of the instance and overrides attributes provided by the client
* @param array $overrides
* @return static
*/
protected function withOverrides(array $overrides)
{
return self::wakeup(
...array_values(array_merge($this->values(), $overrides))
);
}

/**
* Map the given source with the actual attributes by position, if
* the provided array is already mapped (assoc) return it directly.
Expand Down
30 changes: 30 additions & 0 deletions tests/Domain/Traits/IsAggregateTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace OtherCode\ComplexHeart\Tests\Domain\Traits;


use Mockery\Adapter\Phpunit\MockeryTestCase;
use OtherCode\ComplexHeart\Domain\ValueObjects\UUIDValue;
use OtherCode\ComplexHeart\Tests\Sample\Aggregates\User;

/**
* Class IsAggregateTrait
* @author Dmytro <dmytro@2amigos.us>
* @package OtherCode\ComplexHeart\Tests\Domain\Traits
*/
class IsAggregateTraitTest extends MockeryTestCase
{
public function testAggregateCanCloneItselfWithOverridesAndDomainEvents(): void
{
$user = new User(UUIDValue::random(), 'user@email.com', 'John Dow');
$userCopy = $user->withEmail('new@email.com');

$this->assertEquals($user->id(), $userCopy->id());
$this->assertEquals('new@email.com', $userCopy->email());
$this->assertEquals($user->name(), $userCopy->name());

$this->assertEquals($user->getDomainEvents(), $userCopy->getDomainEvents());
}
}
26 changes: 26 additions & 0 deletions tests/Domain/Traits/IsModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace OtherCode\ComplexHeart\Tests\Domain\Traits;


use Mockery\Adapter\Phpunit\MockeryTestCase;
use OtherCode\ComplexHeart\Tests\Sample\ValueObjects\FullName;

/**
* Class IsModelTest
* @author Dmytro <dmytro@2amigos.us>
* @package OtherCode\ComplexHeart\Tests\Domain\Traits
*/
class IsModelTest extends MockeryTestCase
{
public function testModelCanCloneItselfWithOverrides(): void
{
$name = new FullName('John', 'Dow');
$nameCopy = $name->withLastName('Smith');

$this->assertEquals($name->firstName(), $nameCopy->firstName());
$this->assertEquals('Smith', $nameCopy->lastName());
}
}
47 changes: 47 additions & 0 deletions tests/Sample/Aggregates/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace OtherCode\ComplexHeart\Tests\Sample\Aggregates;


use OtherCode\ComplexHeart\Domain\Contracts\Aggregate;
use OtherCode\ComplexHeart\Domain\Traits\IsAggregate;
use OtherCode\ComplexHeart\Domain\ValueObjects\UUIDValue;

/**
* Class User
* @author Dmytro <dmytro@2amigos.us>
* @package OtherCode\ComplexHeart\Tests\Sample\Aggregates
* @method UUIDValue id()
* @method string email()
* @method string name()
*/
class User implements Aggregate
{
use IsAggregate;

private UUIDValue $id;
private string $email;
private string $name;

public function __construct(UUIDValue $id, string $email, string $name)
{
$this->initialize([$id, $email, $name]);
$this->registerDomainEvent(new UserCreated($id->value()));
}

public function withEmail(string $email): self
{
return $this->withOverrides(
[
'email' => $email
]
);
}

public function getDomainEvents(): array
{
return $this->_domainEvents;
}
}
18 changes: 18 additions & 0 deletions tests/Sample/Aggregates/UserCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace OtherCode\ComplexHeart\Tests\Sample\Aggregates;


use OtherCode\ComplexHeart\Domain\Bus\Event;

/**
* Class UserCreated
* @author Dmytro <dmytro@2amigos.us>
* @package OtherCode\ComplexHeart\Tests\Sample\Aggregates
*/
class UserCreated extends Event
{

}
40 changes: 40 additions & 0 deletions tests/Sample/ValueObjects/FullName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace OtherCode\ComplexHeart\Tests\Sample\ValueObjects;


use OtherCode\ComplexHeart\Domain\Contracts\ValueObject;
use OtherCode\ComplexHeart\Domain\Traits\HasEquality;
use OtherCode\ComplexHeart\Domain\Traits\IsModel;

/**
* Class FullName
* @author Dmytro <dmytro@2amigos.us>
* @package OtherCode\ComplexHeart\Tests\Sample\ValueObjects
* @method string firstName()
* @method string lastName()
*/
class FullName implements ValueObject
{
use IsModel, HasEquality;

private string $firstName;

private string $lastName;

public function __construct(string $firstName, string $lastName)
{
$this->initialize([$firstName, $lastName]);
}

public function withLastName(string $lastName): self
{
return $this->withOverrides(
[
'lastName' => $lastName
]
);
}
}

0 comments on commit 46f9202

Please sign in to comment.