Skip to content

Commit 46f9202

Browse files
authored
Release 0.4.5
2 parents 9d98565 + 7af3777 commit 46f9202

File tree

8 files changed

+193
-2
lines changed

8 files changed

+193
-2
lines changed

src/Domain/Traits/HasInvariants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ final public static function invariants(): array
8989
*
9090
* @return void
9191
*/
92-
final private function check(callable $filter = null, callable $onFail = null): void
92+
private function check(callable $filter = null, callable $onFail = null): void
9393
{
9494
$violations = [];
9595

src/Domain/Traits/IsAggregate.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,23 @@
1717
*/
1818
trait IsAggregate
1919
{
20-
use IsEntity, HasDomainEvents, HasServiceBus;
20+
use HasDomainEvents, HasServiceBus, IsEntity {
21+
withOverrides as entityWithOverrides;
22+
}
23+
24+
/**
25+
* Makes a full copy of the instance with all registered domain events and overrides attributes provided by the client
26+
* @param array $overrides
27+
* @return static
28+
*/
29+
protected function withOverrides(array $overrides)
30+
{
31+
$copy = $this->entityWithOverrides($overrides);
32+
33+
foreach ($this->_domainEvents as $event) {
34+
$copy->registerDomainEvent($event);
35+
}
36+
37+
return $copy;
38+
}
2139
}

src/Domain/Traits/IsModel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public static function wakeup()
5252
}
5353
}
5454

55+
/**
56+
* Makes a full copy of the instance and overrides attributes provided by the client
57+
* @param array $overrides
58+
* @return static
59+
*/
60+
protected function withOverrides(array $overrides)
61+
{
62+
return self::wakeup(
63+
...array_values(array_merge($this->values(), $overrides))
64+
);
65+
}
66+
5567
/**
5668
* Map the given source with the actual attributes by position, if
5769
* the provided array is already mapped (assoc) return it directly.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Domain\Traits;
6+
7+
8+
use Mockery\Adapter\Phpunit\MockeryTestCase;
9+
use OtherCode\ComplexHeart\Domain\ValueObjects\UUIDValue;
10+
use OtherCode\ComplexHeart\Tests\Sample\Aggregates\User;
11+
12+
/**
13+
* Class IsAggregateTrait
14+
* @author Dmytro <[email protected]>
15+
* @package OtherCode\ComplexHeart\Tests\Domain\Traits
16+
*/
17+
class IsAggregateTraitTest extends MockeryTestCase
18+
{
19+
public function testAggregateCanCloneItselfWithOverridesAndDomainEvents(): void
20+
{
21+
$user = new User(UUIDValue::random(), '[email protected]', 'John Dow');
22+
$userCopy = $user->withEmail('[email protected]');
23+
24+
$this->assertEquals($user->id(), $userCopy->id());
25+
$this->assertEquals('[email protected]', $userCopy->email());
26+
$this->assertEquals($user->name(), $userCopy->name());
27+
28+
$this->assertEquals($user->getDomainEvents(), $userCopy->getDomainEvents());
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Domain\Traits;
6+
7+
8+
use Mockery\Adapter\Phpunit\MockeryTestCase;
9+
use OtherCode\ComplexHeart\Tests\Sample\ValueObjects\FullName;
10+
11+
/**
12+
* Class IsModelTest
13+
* @author Dmytro <[email protected]>
14+
* @package OtherCode\ComplexHeart\Tests\Domain\Traits
15+
*/
16+
class IsModelTest extends MockeryTestCase
17+
{
18+
public function testModelCanCloneItselfWithOverrides(): void
19+
{
20+
$name = new FullName('John', 'Dow');
21+
$nameCopy = $name->withLastName('Smith');
22+
23+
$this->assertEquals($name->firstName(), $nameCopy->firstName());
24+
$this->assertEquals('Smith', $nameCopy->lastName());
25+
}
26+
}

tests/Sample/Aggregates/User.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Sample\Aggregates;
6+
7+
8+
use OtherCode\ComplexHeart\Domain\Contracts\Aggregate;
9+
use OtherCode\ComplexHeart\Domain\Traits\IsAggregate;
10+
use OtherCode\ComplexHeart\Domain\ValueObjects\UUIDValue;
11+
12+
/**
13+
* Class User
14+
* @author Dmytro <[email protected]>
15+
* @package OtherCode\ComplexHeart\Tests\Sample\Aggregates
16+
* @method UUIDValue id()
17+
* @method string email()
18+
* @method string name()
19+
*/
20+
class User implements Aggregate
21+
{
22+
use IsAggregate;
23+
24+
private UUIDValue $id;
25+
private string $email;
26+
private string $name;
27+
28+
public function __construct(UUIDValue $id, string $email, string $name)
29+
{
30+
$this->initialize([$id, $email, $name]);
31+
$this->registerDomainEvent(new UserCreated($id->value()));
32+
}
33+
34+
public function withEmail(string $email): self
35+
{
36+
return $this->withOverrides(
37+
[
38+
'email' => $email
39+
]
40+
);
41+
}
42+
43+
public function getDomainEvents(): array
44+
{
45+
return $this->_domainEvents;
46+
}
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Sample\Aggregates;
6+
7+
8+
use OtherCode\ComplexHeart\Domain\Bus\Event;
9+
10+
/**
11+
* Class UserCreated
12+
* @author Dmytro <[email protected]>
13+
* @package OtherCode\ComplexHeart\Tests\Sample\Aggregates
14+
*/
15+
class UserCreated extends Event
16+
{
17+
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OtherCode\ComplexHeart\Tests\Sample\ValueObjects;
6+
7+
8+
use OtherCode\ComplexHeart\Domain\Contracts\ValueObject;
9+
use OtherCode\ComplexHeart\Domain\Traits\HasEquality;
10+
use OtherCode\ComplexHeart\Domain\Traits\IsModel;
11+
12+
/**
13+
* Class FullName
14+
* @author Dmytro <[email protected]>
15+
* @package OtherCode\ComplexHeart\Tests\Sample\ValueObjects
16+
* @method string firstName()
17+
* @method string lastName()
18+
*/
19+
class FullName implements ValueObject
20+
{
21+
use IsModel, HasEquality;
22+
23+
private string $firstName;
24+
25+
private string $lastName;
26+
27+
public function __construct(string $firstName, string $lastName)
28+
{
29+
$this->initialize([$firstName, $lastName]);
30+
}
31+
32+
public function withLastName(string $lastName): self
33+
{
34+
return $this->withOverrides(
35+
[
36+
'lastName' => $lastName
37+
]
38+
);
39+
}
40+
}

0 commit comments

Comments
 (0)