From c14d25c7bdc3adb9390c0dc3d36f87a88e322bdc Mon Sep 17 00:00:00 2001 From: Beau Simensen Date: Thu, 25 Jun 2015 15:43:44 +0200 Subject: [PATCH] Before the break work. --- .../Post/Command/Handler/UntagPostHandler.php | 6 +- .../Blog/Domain/Model/Post/Post.php | 95 +++++++++++++++++-- .../Command/Handler/CreatePostHandlerTest.php | 2 + .../Handler/PublishPostHandlerTest.php | 10 +- .../Command/Handler/TagPostHandlerTest.php | 6 +- .../Command/Handler/UntagPostHandlerTest.php | 4 +- .../Model/Post/Adapter/Broadway/PostTest.php | 6 +- .../PostCategoryCountProjectorTest.php | 33 +++++++ 8 files changed, 141 insertions(+), 21 deletions(-) diff --git a/src/SuperAwesome/Blog/Domain/Model/Post/Command/Handler/UntagPostHandler.php b/src/SuperAwesome/Blog/Domain/Model/Post/Command/Handler/UntagPostHandler.php index 579078f..3745048 100644 --- a/src/SuperAwesome/Blog/Domain/Model/Post/Command/Handler/UntagPostHandler.php +++ b/src/SuperAwesome/Blog/Domain/Model/Post/Command/Handler/UntagPostHandler.php @@ -4,10 +4,12 @@ use SuperAwesome\Blog\Domain\Model\Post\Command\UntagPost; -class UntagPostHandler +class UntagPostHandler extends PostHandler { public function handle(UntagPost $command) { - // TODO: Implement handle() method. + $post = $this->getPostRepository()->find($command->id); + $post->removeTag($command->tag); + $this->getPostRepository()->save($post); } } diff --git a/src/SuperAwesome/Blog/Domain/Model/Post/Post.php b/src/SuperAwesome/Blog/Domain/Model/Post/Post.php index 5576a2f..6da8ece 100644 --- a/src/SuperAwesome/Blog/Domain/Model/Post/Post.php +++ b/src/SuperAwesome/Blog/Domain/Model/Post/Post.php @@ -2,10 +2,15 @@ namespace SuperAwesome\Blog\Domain\Model\Post; +use Broadway\EventSourcing\EventSourcedAggregateRoot; use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasCategorized; +use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasCreated; use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasPublished; +use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasTagged; +use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasUncategorized; +use SuperAwesome\Blog\Domain\Model\Post\Event\PostWasUntagged; -class Post +class Post extends EventSourcedAggregateRoot { /** @var string */ private $id; @@ -28,9 +33,31 @@ class Post /** * @param string $id */ - public function __construct($id) + private function __construct() { - $this->id = $id; + } + + public static function instantiateForReconstitution() + { + return new static(); + } + + public static function create($id) + { + $instance = new static(); + $instance->apply(new PostWasCreated($id)); + + return $instance; + } + + protected function applyPostWasCreated(PostWasCreated $event) + { + $this->id = $event->id; + } + + public function getAggregateRootId() + { + return (string) $this->id; } /** @@ -82,9 +109,37 @@ public function getTags() */ public function publish($title, $content, $category) { - $this->title = $title; - $this->content = $content; - $this->category = $category; + if ($title == $this->title && $content == $this->content && $this->category == $category) { + return; + } + + if ($this->category && $category != $this->category) { + $this->apply(new PostWasUncategorized( + $this->id, + $this->category + )); + } + + if ($this->category != $category) { + $this->apply(new PostWasCategorized( + $this->id, + $category + )); + } + + $this->apply(new PostWasPublished( + $this->id, + $title, + $content, + $category + )); + } + + protected function applyPostWasPublished(PostWasPublished $event) + { + $this->title = $event->title; + $this->content = $event->content; + $this->category = $event->category; } /** @@ -94,7 +149,19 @@ public function publish($title, $content, $category) */ public function addTag($tag) { - $this->tags[$tag] = true; + if (array_key_exists($tag, $this->tags)) { + return; + } + + $this->apply(new PostWasTagged( + $this->id, + $tag + )); + } + + protected function applyPostWasTagged(PostWasTagged $event) + { + $this->tags[$event->tag] = true; } /** @@ -104,8 +171,18 @@ public function addTag($tag) */ public function removeTag($tag) { - if (isset($this->tags[$tag])) { - unset($this->tags[$tag]); + if (! array_key_exists($tag, $this->tags)) { + return; } + + $this->apply(new PostWasUntagged( + $this->id, + $tag + )); + } + + protected function applyPostWasUntagged(PostWasUntagged $event) + { + unset($this->tags[$event->tag]); } } diff --git a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/CreatePostHandlerTest.php b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/CreatePostHandlerTest.php index eb0d681..bfe2883 100644 --- a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/CreatePostHandlerTest.php +++ b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/CreatePostHandlerTest.php @@ -7,10 +7,12 @@ class CreatePostHandlerTest extends AbstractPostHandlerTest { + /* public function setUp() { $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); } + */ /** @test */ public function it_can_create() diff --git a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/PublishPostHandlerTest.php b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/PublishPostHandlerTest.php index 2e704fe..45c7f51 100644 --- a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/PublishPostHandlerTest.php +++ b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/PublishPostHandlerTest.php @@ -10,15 +10,17 @@ class PublishPostHandlerTest extends AbstractPostHandlerTest { + /* public function setUp() { $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); } + */ /** @test */ public function it_can_publish() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; @@ -41,7 +43,7 @@ public function it_can_publish() /** @test */ public function it_uncategorizes_when_publishing_with_a_different_category() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; @@ -71,7 +73,7 @@ public function it_uncategorizes_when_publishing_with_a_different_category() /** @test */ public function it_does_not_uncategorize_when_publishing_with_same_category() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; @@ -99,7 +101,7 @@ public function it_does_not_uncategorize_when_publishing_with_same_category() /** @test */ public function it_does_not_publish_if_nothing_changed() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; diff --git a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/TagPostHandlerTest.php b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/TagPostHandlerTest.php index 952ee4c..888974e 100644 --- a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/TagPostHandlerTest.php +++ b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/TagPostHandlerTest.php @@ -10,15 +10,17 @@ class TagPostHandlerTest extends AbstractPostHandlerTest { + /* public function setUp() { $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); } + */ /** @test */ public function it_can_tag() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; @@ -54,7 +56,7 @@ public function it_can_tag() /** @test */ public function it_does_not_tag_again() { - $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); + //$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); $id = 'my-id'; $title = 'the title'; diff --git a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/UntagPostHandlerTest.php b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/UntagPostHandlerTest.php index 42d51fa..0a750ca 100644 --- a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/UntagPostHandlerTest.php +++ b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/Command/Handler/UntagPostHandlerTest.php @@ -11,15 +11,17 @@ class UntagPostHandlerTest extends AbstractPostHandlerTest { + /* public function setUp() { $this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.'); } + */ /** @test */ public function it_can_untag() { - $this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); + //$this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); $id = 'my-id'; $title = 'the title'; diff --git a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/PostTest.php b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/PostTest.php index 4ef6cd3..26baf87 100644 --- a/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/PostTest.php +++ b/tests/SuperAwesome/Blog/Domain/Model/Post/Adapter/Broadway/PostTest.php @@ -38,7 +38,7 @@ protected function getAggregateRootFactory() /** @test */ public function it_can_create() { - $this->markTestIncomplete('Post::create() does not exist.'); + //$this->markTestIncomplete('Post::create() does not exist.'); $id = 'my-id'; @@ -55,7 +55,7 @@ public function it_can_create() /** @test */ public function it_can_publish() { - $this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); + //$this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); $id = 'my-id'; $title = 'the title'; @@ -80,7 +80,7 @@ public function it_can_publish() /** @test */ public function it_uncategorizes_when_publishing_with_a_different_category() { - $this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); + //$this->markTestIncomplete('Post::instantiateForReconstitution does not exist.'); $id = 'my-id'; $title = 'the title'; diff --git a/tests/SuperAwesome/Blog/Domain/ReadModel/PostCategoryCount/Adapter/Broadway/PostCategoryCountProjectorTest.php b/tests/SuperAwesome/Blog/Domain/ReadModel/PostCategoryCount/Adapter/Broadway/PostCategoryCountProjectorTest.php index 7c95c1c..824747d 100644 --- a/tests/SuperAwesome/Blog/Domain/ReadModel/PostCategoryCount/Adapter/Broadway/PostCategoryCountProjectorTest.php +++ b/tests/SuperAwesome/Blog/Domain/ReadModel/PostCategoryCount/Adapter/Broadway/PostCategoryCountProjectorTest.php @@ -64,4 +64,37 @@ public function it_sums_correctly() ]) ; } + + /** @test */ + public function it_sums_correctly_with_different_ids() + { + $this->scenario + ->given([ + new PostWasCategorized('my-id', 'drafts'), + new PostWasCategorized('foo', 'drafts'), + new PostWasCategorized('my-id', 'drafts'), + ]) + ->when(new PostWasCategorized('my-id', 'drafts')) + ->then([ + new PostCategoryCount('drafts', 4), + ]) + ; + } + + /** @test */ + public function it_sums_correctly_with_different_tags() + { + $this->scenario + ->given([ + new PostWasCategorized('my-id', 'drafts'), + new PostWasCategorized('my-id', 'trash'), + new PostWasCategorized('my-id', 'drafts'), + ]) + ->when(new PostWasCategorized('my-id', 'drafts')) + ->then([ + new PostCategoryCount('drafts', 3), + new PostCategoryCount('trash', 1), + ]) + ; + } }