Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. This projec

## Unreleased

### Added

- New `IsEntityWithTypedId` trait for use on entities that strictly type their id.

## [6.0.1] - 2026-06-13

### Fixed
Expand Down
45 changes: 45 additions & 0 deletions src/Domain/IsEntityWithTypedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* Copyright 2026 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace CloudCreativity\Modules\Domain;

use CloudCreativity\Modules\Contracts\Domain\Entity;
use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier;

trait IsEntityWithTypedId
{
public function getId(): Identifier
{
return $this->id;
}

public function getIdOrFail(): Identifier
{
return $this->id;
}

public function is(?Entity $other): bool
{
if ($other instanceof $this) {
return $this->id->is(
$other->getId(),
);
}

return false;
}

public function isNot(?Entity $other): bool
{
return !$this->is($other);
}
}
89 changes: 89 additions & 0 deletions tests/Unit/Domain/EntityWithTypedIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* Copyright 2026 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace CloudCreativity\Modules\Tests\Unit\Domain;

use CloudCreativity\Modules\Contracts\Domain\Entity;
use CloudCreativity\Modules\Domain\IsEntityWithTypedId;
use CloudCreativity\Modules\Toolkit\Identifiers\IntegerId;
use CloudCreativity\Modules\Toolkit\Identifiers\UuidV7;
use PHPUnit\Framework\TestCase;

final class EntityWithTypedIdTest extends TestCase
{
public function test(): void
{
$entity = new TestEntityWithTypedId($id = UuidV7::make());

$this->assertSame($id, $entity->id);
$this->assertSame($id, $entity->getId());
$this->assertSame($id, $entity->getIdOrFail());
$this->assertFalse($entity->is(null));
$this->assertTrue($entity->isNot(null));
}

public function testItIsTheSame(): void
{
$a = new TestEntityWithTypedId(
$id1 = UuidV7::make(),
);

$b = new TestEntityWithTypedId(
UuidV7::from($id1->toString()),
);

$this->assertTrue($a->is($b));
$this->assertFalse($a->isNot($b));
}

public function testItIsNotTheSame(): void
{
$a = new TestEntityWithTypedId(UuidV7::make());
$b = new TestEntityWithTypedId(UuidV7::make());

$this->assertFalse($a->is($b));
$this->assertTrue($a->isNot($b));
}

public function testItIsDifferentClass(): void
{
$a = new TestEntityWithTypedId(UuidV7::make());
$b = $this->createMock(Entity::class);
$b->method('getId')->willReturn(UuidV7::from($a->id->toString()));

$this->assertFalse($a->is($b));
$this->assertTrue($a->isNot($b));
}

public function testItCanUseTraitOnNonReadonlyClass(): void
{
$id = new IntegerId(123);

$entity = new class ($id, 'Bob') implements Entity {
use IsEntityWithTypedId;

public function __construct(public IntegerId $id, public private(set) string $name)
{
}

public function changeName(string $name): void
{
$this->name = $name;
}
};

$this->assertSame($id, $entity->getId());
$this->assertSame('Bob', $entity->name);
$entity->changeName('Chris');
$this->assertSame('Chris', $entity->name);
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Domain/TestEntityWithTypedId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* Copyright 2026 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace CloudCreativity\Modules\Tests\Unit\Domain;

use CloudCreativity\Modules\Contracts\Domain\Entity;
use CloudCreativity\Modules\Domain\IsEntityWithTypedId;
use CloudCreativity\Modules\Toolkit\Identifiers\UuidV7;

final readonly class TestEntityWithTypedId implements Entity
{
use IsEntityWithTypedId;

public function __construct(public UuidV7 $id, public string $name = 'Jane Doe')
{
}
}
Loading