Skip to content

Commit

Permalink
Before the break work.
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Jun 25, 2015
1 parent 1c01891 commit c14d25c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 21 deletions.
Expand Up @@ -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);
}
}
95 changes: 86 additions & 9 deletions src/SuperAwesome/Blog/Domain/Model/Post/Post.php
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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]);
}
}
Expand Up @@ -7,10 +7,12 @@

class CreatePostHandlerTest extends AbstractPostHandlerTest
{
/*
public function setUp()
{
$this->markTestIncomplete('Post is not an EventSourcedAggregateRoot.');
}
*/

/** @test */
public function it_can_create()
Expand Down
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
Expand Up @@ -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';
Expand Down
Expand Up @@ -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';

Expand All @@ -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';
Expand All @@ -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';
Expand Down
Expand Up @@ -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),
])
;
}
}

0 comments on commit c14d25c

Please sign in to comment.