From 31c4584e942bf0d06119476ec86eedd0152392e8 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 26 Oct 2014 01:16:14 +0200 Subject: [PATCH] Added post note information to post snapshots --- TODO | 3 --- .../PostNoteEntityConverter.php | 8 +++--- src/Services/PostSnapshotProvider.php | 26 +++++++++++++++--- tests/Services/PostSnapshotProviderTest.php | 27 ++++++++++++++++++- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 85926b04..4a712a26 100644 --- a/TODO +++ b/TODO @@ -10,9 +10,6 @@ first major release. - tags: add tag descriptions - tags: add tag edit snapshots (backed-only) -- post notes - - post notes history - refactors: - add enum validation in IValidatables (needs refactors of enums and possible disposal of EnumHelper in favor of something more subtle) diff --git a/src/Dao/EntityConverters/PostNoteEntityConverter.php b/src/Dao/EntityConverters/PostNoteEntityConverter.php index 63f70f93..a0116cbf 100644 --- a/src/Dao/EntityConverters/PostNoteEntityConverter.php +++ b/src/Dao/EntityConverters/PostNoteEntityConverter.php @@ -23,10 +23,10 @@ class PostNoteEntityConverter extends AbstractEntityConverter implements IEntity { $entity = new PostNote($array['id']); $entity->setPostId($array['postId']); - $entity->setLeft($array['x']); - $entity->setTop($array['y']); - $entity->setWidth($array['width']); - $entity->setHeight($array['height']); + $entity->setLeft(intval($array['x'])); + $entity->setTop(intval($array['y'])); + $entity->setWidth(intval($array['width'])); + $entity->setHeight(intval($array['height'])); $entity->setText($array['text']); return $entity; } diff --git a/src/Services/PostSnapshotProvider.php b/src/Services/PostSnapshotProvider.php index b791e844..7e2df45c 100644 --- a/src/Services/PostSnapshotProvider.php +++ b/src/Services/PostSnapshotProvider.php @@ -1,18 +1,25 @@ globalParamDao = $globalParamDao; + $this->postNoteDao = $postNoteDao; } public function getPostChangeSnapshot(Post $post) @@ -33,9 +40,21 @@ class PostSnapshotProvider 'contentChecksum' => $post->getContentChecksum(), 'featured' => $isFeatured, + 'notes' => array_values(array_map(function (PostNote $note) + { + return [ + 'x' => intval($note->getLeft()), + 'y' => intval($note->getTop()), + 'w' => intval($note->getWidth()), + 'h' => intval($note->getHeight()), + 'text' => trim($note->getText()), + ]; + }, + $this->postNoteDao->findByPostId($post->getId()))), + 'tags' => array_values(array_map( - function ($tag) + function (Tag $tag) { return $tag->getName(); }, @@ -43,7 +62,7 @@ class PostSnapshotProvider 'relations' => array_values(array_map( - function ($post) + function (Post $post) { return $post->getId(); }, @@ -54,6 +73,7 @@ class PostSnapshotProvider sort($data['tags']); sort($data['relations']); + usort($data['notes'], function ($note1, $note2) { return $note1['x'] - $note2['x']; }); $snapshot = $this->getPostSnapshot($post); $snapshot->setOperation(Snapshot::OPERATION_CHANGE); diff --git a/tests/Services/PostSnapshotProviderTest.php b/tests/Services/PostSnapshotProviderTest.php index 62af16b5..b0cdb98a 100644 --- a/tests/Services/PostSnapshotProviderTest.php +++ b/tests/Services/PostSnapshotProviderTest.php @@ -1,8 +1,10 @@ globalParamDaoMock = $this->mock(GlobalParamDao::class); + $this->postNoteDaoMock = $this->mock(PostNoteDao::class); } public function testPostChangeSnapshot() @@ -35,6 +38,11 @@ class PostSnapshotProviderTest extends AbstractTestCase $post->setSource('amazing source'); $post->setFlags(Post::FLAG_LOOP); + $this->postNoteDaoMock + ->method('findByPostId') + ->with($post->getId()) + ->willReturn([$this->getTestPostNote()]); + $postSnapshotProvider = $this->getPostSnapshotProvider(); $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); @@ -46,6 +54,7 @@ class PostSnapshotProviderTest extends AbstractTestCase 'tags' => ['tag1', 'tag2'], 'relations' => [1, 2], 'flags' => ['loop'], + 'notes' => [['x' => 5, 'y' => 6, 'w' => 7, 'h' => 8, 'text' => 'text']], ], $snapshot->getData()); $this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType()); @@ -67,6 +76,11 @@ class PostSnapshotProviderTest extends AbstractTestCase ->with(GlobalParam::KEY_FEATURED_POST) ->willReturn($param); + $this->postNoteDaoMock + ->method('findByPostId') + ->with($post->getId()) + ->willReturn([]); + $postSnapshotProvider = $this->getPostSnapshotProvider(); $snapshot = $postSnapshotProvider->getPostChangeSnapshot($post); @@ -75,6 +89,17 @@ class PostSnapshotProviderTest extends AbstractTestCase private function getPostSnapshotProvider() { - return new PostSnapshotProvider($this->globalParamDaoMock); + return new PostSnapshotProvider($this->globalParamDaoMock, $this->postNoteDaoMock); + } + + private function getTestPostNote() + { + $postNote = new PostNote(); + $postNote->setLeft(5); + $postNote->setTop(6); + $postNote->setWidth(7); + $postNote->setHeight(8); + $postNote->setText('text'); + return $postNote; } }