From 790c3e10c6c9ff33206bde7a9eb3495a1c310ab4 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 15 Oct 2014 19:30:58 +0200 Subject: [PATCH] Added tag implications and suggestions to export --- src/Services/TagService.php | 16 +++++++++++++++ tests/Services/TagServiceTest.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 16ffa45f..9c18301b 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -77,7 +77,23 @@ class TagService 'name' => $tag->getName(), 'usages' => $tag->getUsages(), 'banned' => $tag->isBanned(), + 'implications' => array_values(array_map( + function ($subTag) + { + return $subTag->getName(); + }, + $tag->getImpliedTags())), + 'suggestions' => array_values(array_map( + function ($subTag) + { + return $subTag->getName(); + }, + $tag->getSuggestedTags())), ]; + if (empty($item['implications'])) + unset($item['implications']); + if (empty($item['suggestions'])) + unset($item['suggestions']); $tags[] = $item; } $json = json_encode($tags); diff --git a/tests/Services/TagServiceTest.php b/tests/Services/TagServiceTest.php index 55faa1ce..2ec61a6d 100644 --- a/tests/Services/TagServiceTest.php +++ b/tests/Services/TagServiceTest.php @@ -69,6 +69,39 @@ final class TagServiceTest extends AbstractDatabaseTestCase $this->assertEquals('test3', $result[1]->getName()); } + public function testExportRelations() + { + $fileDao = $this->getPublicFileDao(); + $tagService = $this->getTagService(); + + $tag1 = new Tag(); + $tag1->setName('test'); + $tag1->setCreationTime(date('c')); + + $tag2 = new Tag(); + $tag2->setName('test 2'); + $tag3 = new Tag(); + $tag3->setName('test 3'); + $tag4 = new Tag(); + $tag4->setName('test 4'); + $tag5 = new Tag(); + $tag5->setName('test 5'); + $tagService->createTags([$tag2, $tag3, $tag4, $tag5]); + + $tag1->setImpliedTags([$tag2, $tag3]); + $tag1->setSuggestedTags([$tag4, $tag5]); + + $tagService->createTags([$tag1]); + $tagService->exportJson(); + $this->assertEquals('[' . + '{"name":"test 2","usages":0,"banned":false},' . + '{"name":"test 3","usages":0,"banned":false},' . + '{"name":"test 4","usages":0,"banned":false},' . + '{"name":"test 5","usages":0,"banned":false},' . + '{"name":"test","usages":0,"banned":false,"implications":["test 2","test 3"],"suggestions":["test 4","test 5"]}]', + $fileDao->load('tags.json')); + } + public function testExportSingle() { $tag1 = new Tag();