/** * @test */ public function it_can_be_started_with_a_message_and_provides_next_commands() { $startMessage = new FileWasUploaded(); $story = Story::start(StoryName::fromString('Test Story'), $startMessage, $this->getTestChapterTemplates(), new NoOpMessageConverter()); $events = $this->popRecordedEvents($story); $this->assertEquals(2, count($events)); /** @var $storyWasStarted Story\Event\StoryWasStarted */ $storyWasStarted = $events[0]; $this->assertInstanceOf(Story\Event\StoryWasStarted::class, $storyWasStarted); $this->assertEquals('Test Story', $storyWasStarted->storyName()->toString()); //StoryId should be the uuid of the start message $this->assertEquals($startMessage->uuid()->toString(), $storyWasStarted->storyId()->toString()); /** @var $chapterWasStarted Story\Event\ChapterWasStarted */ $chapterWasStarted = $events[1]; $this->assertInstanceOf(Story\Event\ChapterWasStarted::class, $chapterWasStarted); $this->assertTrue($storyWasStarted->storyId()->equals($chapterWasStarted->chapterStatus()->chapterNumber()->storyId())); $this->assertEquals(Story\ChapterStatus::STATUS_RUNNING, $chapterWasStarted->chapterStatus()->status()); $this->assertEquals(1, $chapterWasStarted->chapterStatus()->chapterNumber()->number()); $nextCommands = $story->getNextCommands(); $this->assertEquals(1, count($nextCommands)); $this->assertInstanceOf(ProcessFile::class, $nextCommands[0]); //Next commands can only be requested once $this->assertEquals(0, count($story->getNextCommands())); $this->applyPendingEvents($story, $events); return [$story, $nextCommands[0]]; }
/** * @param Message $message * @throws Exception\Story */ public function __invoke(Message $message) { if ($this->isStartMessage($message)) { $story = Story::start($this->storyName, $message, $this->chapterTemplates, $this->messageConverter); $this->storyLog->add($story); } else { $chapterNumber = ChapterNumber::fromString($message->metadata()[Metadata::STORY_CHAPTER]); $story = $this->storyLog->get($chapterNumber->storyId()); if (null === $story) { throw Exception\Story::withIdCannotBeFound($chapterNumber->storyId()); } $story->continueWith($message, $this->chapterTemplates, $this->messageConverter, $this->messageFactory); } foreach ($story->getNextCommands() as $command) { $this->commandBus->delayDispatch($command); } }