php - How to test generated id in aggregate root

I wanna to test my entities created from doctrine, but i don`t know how to test id if they are generated themself while object save to database.

class Dish
{
    private ?int $id;

    private Collection $prices;

    public function __construct()
    {
        $this->prices = new ArrayCollection();
    }

    public function identifier(): string
    {
        return $this->id;
    }

    public function isEquals(Dish $dish): bool
    {
        return $this->identifier() === $dish->identifier();
    }

    public function addPrice(float $price)
    {
        $this->prices->add(new DishPrice($this, $price));
    }
}
class DishPrice
{
    use DeletedEntityTrait;

    private ?int $id;
    private float $price;

    private Dish $dish;

    public function __construct(Dish $dish, float $price)
    {
        $this->dish = $dish;
        $this->price = $price;
    }

    public function identifier(): string
    {
        return $this->id;
    }

    public function isEquals(Dish $dish): bool
    {
        return $this->identifier() === $dish->identifier();
    }
}

How to test isEquals methods in both classes?

Answer

Solution:

Basically you have two options.

First option is functional test. You can create two entities and persist them to the database.

Second option is pass ID to the constructor. To be able to do this you can add method to repository that will provide you next ID for your entity in service layer:

interface DishRepository
{
    public function getNextIdentity(): int;
}

$id = $repository->getNextIdentity();
$dish = new Dish($id);

and in unit-tests you can pass to the constructor whatever ID that you want

$testId = 111;
$dish = new Dish($testId);

Answer

Solution:

Try to use uuid instead of int and testing will be easer) also, you will have more benefits while using uuids.

Source