Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenario should allow for setting arbitrary ID for given events. #18

Conversation

simensen
Copy link
Contributor

Here is my use case:

    /**
     * @test
     */
    public function it_should_suspend()
    {
        $userId = UserId::generate();

        $this->scenario
            ->given([UserWasRegistered::with($userId, new Person)], $userId)
            ->when(SuspendUser::with($userId))
            ->then([
                UserWasSuspended::with($userId),
            ]);
    }

Without being able to specify an ID for the given() events, I was getting an error from the event store saying that no stream could be found for the given ID.

There was already notes in the class saying that maybe hardcoding the ID to 1 wasn't all that good of an idea. :)

// todo: the hardcoded ID is probably wrong....

It might be more consistent to not have a default ID and to require the aggregate root ID to be the first argument instead. I implemented it this way first as it would be minimal impact on existing code and as a proof of concept.

@sstok
Copy link

sstok commented Aug 27, 2014

👍

@simensen
Copy link
Contributor Author

Merged simensen#1 from @wjzijderveld. It helped make this whole thing more explicit and still leaves it BC.

My only concern about this is that it means someone can call this and might expect that it works but it won't:

$this->scenario
  ->given($eventsBefore)
  ->withAggregateId($userId)
  ->when($command)
  ->then($eventsAfter);

}

/**
* @param array $events
* @param string $id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed again :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@fritsjanb
Copy link
Contributor

This pull made us realize an inconsistency in our test setup.

According to AggregateRoot getId() should return a string, yet in this scenario we use the integer 1 as the default value. Since PHP is PHP everything still works, but I guess this would be a good time to change the default 1 to '1'.

Though this is a minor BC break in the Scenario all tests still run - if you run assertEquals() on an Event with id 1 and an Event with id '1' phpunit counts them as equals.

What is your opinion on this?

@simensen
Copy link
Contributor Author

simensen commented Sep 3, 2014

@fritsjanb I'm wondering if we can remove the requirement for getId on AggregateRoot altogether. I haven't tried implementing this yet, but this is what I have in mind:

https://gist.github.com/simensen/d982fac5550d3b5f8698

Basically, adding something like getIdForAggregateRoot to the RepositoryInterface and removing getId from the AggregateRoot interface. The repository (that already needs to be extended? this might not be entirely true...) would be responsible for asking its aggregate root for its ID in a way that the aggregate root expects.

@fritsjanb
Copy link
Contributor

@simensen Short answer: could you create a separate issue for this?

Long answer: The fact remains that your aggregate needs a function to expose its identity. There is only one reason for this: it allows us to store the events.

Of course you could move the responsibility for this to the RepositoryInterface (I do think this is an elegant solution), but you would still have to implement a function in your AggregateRoot with the sole purpose of exposing an id for storage.

True, it allows you to expose a getFooId() instead of a getId(), but is there really that much added value to this?

Regarding the extending of the Repository: this is not entirely true. In our application we do in fact extend it (for typehinting purposes and it makes testing easier), but at this point it is not yet required.

At the moment our repositories look like this:

<?php

use Broadway\EventHandling\EventBusInterface;
use Broadway\EventSourcing\EventSourcingRepository;
use Broadway\EventStore\EventStoreInterface;

class FooRepository extends EventSourcingRepository
{
    public function __construct(EventStoreInterface $eventStore, EventBusInterface $eventBus, array $eventStreamDecorators = array())
    {   
        parent::__construct($eventStore, $eventBus, '\Qandidate\Project\Foo\Foo', $eventStreamDecorators);
    }   
}

but you could just instantiate an EventSourcingRepository directly.

Anyways, it would be nice if you could create a separate issue for this :)

@kelvinj
Copy link
Contributor

kelvinj commented Sep 4, 2014

Moved the discussion about the aggregate root here: #34

@asm89
Copy link
Member

asm89 commented Sep 4, 2014

👍, nice API :)

@fritsjanb
Copy link
Contributor

@kelvinj could you still change the default int 1 to a '1'? :)

@kelvinj
Copy link
Contributor

kelvinj commented Sep 5, 2014

@kelvinj could you still change the default int 1 to a '1'? :)

I think that one's intended for you @simensen

@simensen
Copy link
Contributor Author

simensen commented Sep 5, 2014

@fritsjanb The most recent change to #19 makes it so that ID can be an int, a string, or an object that implements __toString. I think that means that the default value in this case can be left at 1 (int)? Let me know if you disagree.

FWIW, I think it is a fine assumption that someone might want to use an int for their ID. As far as I know, (string) 5 === '5' so I think this is a safe assumption.

@kelvinj
Copy link
Contributor

kelvinj commented Sep 7, 2014

It'd be good to see #18 and #19 getting merged soon.

@E1379
Copy link
Contributor

E1379 commented Sep 8, 2014

👍

@fritsjanb
Copy link
Contributor

@simensen agreed with leaving 1 as an int

Merging!

fritsjanb added a commit that referenced this pull request Sep 8, 2014
…-testing-scenario

Scenario should allow for setting arbitrary ID for given events.
@fritsjanb fritsjanb merged commit 2006586 into broadway:master Sep 8, 2014
@simensen
Copy link
Contributor Author

simensen commented Sep 8, 2014

@fritsjanb Yay! Awesome. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants