Skip to content

Running assertions on the aggregate after given/when/then #20

@wikando-ck

Description

@wikando-ck

Context
We use public private(set) $properties on our aggregates.
These are modified via the apply*-methods.
They just expose the aggregates current state instead of having getters.

Using given/when/then, we can only assert that the right events are created on a given aggregate when something is called.
To cover the correctness of the apply-methods, we also need to assert on the exposed state of the aggregate.

I see two ways of having that:

  • Add a new method that can receive a callback, which can run assertions and can use variables from the setup scope.
  • Allow callbacks passed into then(), which will behave in the same way.

Example

$this
    ->given(
        new ProfileCreated(
            ProfileId::fromString('1'),
            Email::fromString('hq@patchlevel.de'),
        ),
    )
    ->when(static fn (Profile $profile) => $profile->visitProfile(ProfileId::fromString('2')))
    ->then(
        new ProfileVisited(ProfileId::fromString('2')),
        // passing a callback, requires a union-type for `then()`
        static fn (Profile $profile) => $this->assertSame(ProfileId::fromString('1'), $profile->id))
    )
    // or with a method, but this would break the "standard" given-when-then
    ->thenCallback(static fn (Profile $profile) => $this->assertSame(ProfileId::fromString('1'), $profile->id)))
;
}

Right now PHPUnit assertions can only be ran in the when(), due to the execution phase using after.
Or am i missing something here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions