Skip to content

Tests

The aggregates can also be tested very well. You can test whether certain events have been thrown or whether the state is set up correctly when the aggregate is set up again via the events.

use PHPUnit\Framework\TestCase;

final class ProfileTest extends TestCase
{
    public function testCreateProfile(): void
    {
        $id = ProfileId::generate();
        $profile = Profile::createProfile($id, Email::fromString('[email protected]'));

        self::assertEquals(
            $profile->releaseEvents(), 
            [
                new ProfileCreated($id, Email::fromString('[email protected]')),        
            ]
        );

        self::assertEquals('[email protected]', $profile->email()->toString());
    }

    public function testChangeName(): void
    {
        $id = ProfileId::generate();

        $profile = Profile::createFromEvents([
            new ProfileCreated($id, Email::fromString('[email protected]')),
        ]);

        $profile->changeEmail(Email::fromString('[email protected]'));

        self::assertEquals(
            $profile->releaseEvents(), 
            [
                new EmailChanged(Email::fromString('[email protected]')),        
            ]
        );

        self::assertEquals('[email protected]', $profile->email()->toString());
    }
}