diff options
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
3 files changed, 145 insertions, 131 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 4f70ed0c..77dfd5bf 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php | |||
@@ -7,6 +7,8 @@ use Wallabag\CoreBundle\Helper\ContentProxy; | |||
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | 8 | use Wallabag\CoreBundle\Entity\Tag; |
9 | use Wallabag\UserBundle\Entity\User; | 9 | use Wallabag\UserBundle\Entity\User; |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
11 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | ||
10 | 12 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | 13 | class ContentProxyTest extends \PHPUnit_Framework_TestCase |
12 | { | 14 | { |
@@ -33,7 +35,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
33 | 'language' => '', | 35 | 'language' => '', |
34 | ]); | 36 | ]); |
35 | 37 | ||
36 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 38 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
37 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); | 39 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80'); |
38 | 40 | ||
39 | $this->assertEquals('http://user@:80', $entry->getUrl()); | 41 | $this->assertEquals('http://user@:80', $entry->getUrl()); |
@@ -67,7 +69,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
67 | 'language' => '', | 69 | 'language' => '', |
68 | ]); | 70 | ]); |
69 | 71 | ||
70 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 72 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
71 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 73 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
72 | 74 | ||
73 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); | 75 | $this->assertEquals('http://0.0.0.0', $entry->getUrl()); |
@@ -106,12 +108,12 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
106 | ], | 108 | ], |
107 | ]); | 109 | ]); |
108 | 110 | ||
109 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 111 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
110 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | 112 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); |
111 | 113 | ||
112 | $this->assertEquals('http://domain.io', $entry->getUrl()); | 114 | $this->assertEquals('http://domain.io', $entry->getUrl()); |
113 | $this->assertEquals('my title', $entry->getTitle()); | 115 | $this->assertEquals('my title', $entry->getTitle()); |
114 | $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent()); | 116 | $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent()); |
115 | $this->assertEmpty($entry->getPreviewPicture()); | 117 | $this->assertEmpty($entry->getPreviewPicture()); |
116 | $this->assertEmpty($entry->getLanguage()); | 118 | $this->assertEmpty($entry->getLanguage()); |
117 | $this->assertEmpty($entry->getHttpStatus()); | 119 | $this->assertEmpty($entry->getHttpStatus()); |
@@ -147,7 +149,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
147 | ], | 149 | ], |
148 | ]); | 150 | ]); |
149 | 151 | ||
150 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 152 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
151 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 153 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
152 | 154 | ||
153 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 155 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
@@ -188,7 +190,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
188 | ], | 190 | ], |
189 | ]); | 191 | ]); |
190 | 192 | ||
191 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 193 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
192 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | 194 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); |
193 | 195 | ||
194 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | 196 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); |
@@ -210,7 +212,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
210 | 212 | ||
211 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); | 213 | $graby = $this->getMockBuilder('Graby\Graby')->getMock(); |
212 | 214 | ||
213 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage); | 215 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
214 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 216 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ |
215 | 'html' => str_repeat('this is my content', 325), | 217 | 'html' => str_repeat('this is my content', 325), |
216 | 'title' => 'this is my title', | 218 | 'title' => 'this is my title', |
@@ -239,8 +241,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
239 | ->method('tag') | 241 | ->method('tag') |
240 | ->will($this->throwException(new \Exception())); | 242 | ->will($this->throwException(new \Exception())); |
241 | 243 | ||
242 | $tagRepo = $this->getTagRepositoryMock(); | 244 | $proxy = new ContentProxy($graby, $tagger, $this->getLogger(), $this->fetchingErrorMessage); |
243 | $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
244 | 245 | ||
245 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ | 246 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [ |
246 | 'html' => str_repeat('this is my content', 325), | 247 | 'html' => str_repeat('this is my content', 325), |
@@ -253,134 +254,14 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase | |||
253 | $this->assertCount(0, $entry->getTags()); | 254 | $this->assertCount(0, $entry->getTags()); |
254 | } | 255 | } |
255 | 256 | ||
256 | public function testAssignTagsWithArrayAndExtraSpaces() | ||
257 | { | ||
258 | $graby = $this->getMockBuilder('Graby\Graby') | ||
259 | ->disableOriginalConstructor() | ||
260 | ->getMock(); | ||
261 | |||
262 | $tagRepo = $this->getTagRepositoryMock(); | ||
263 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
264 | |||
265 | $entry = new Entry(new User()); | ||
266 | |||
267 | $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | ||
268 | |||
269 | $this->assertCount(2, $entry->getTags()); | ||
270 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
271 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
272 | } | ||
273 | |||
274 | public function testAssignTagsWithString() | ||
275 | { | ||
276 | $graby = $this->getMockBuilder('Graby\Graby') | ||
277 | ->disableOriginalConstructor() | ||
278 | ->getMock(); | ||
279 | |||
280 | $tagRepo = $this->getTagRepositoryMock(); | ||
281 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
282 | |||
283 | $entry = new Entry(new User()); | ||
284 | |||
285 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | ||
286 | |||
287 | $this->assertCount(2, $entry->getTags()); | ||
288 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
289 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
290 | } | ||
291 | |||
292 | public function testAssignTagsWithEmptyArray() | ||
293 | { | ||
294 | $graby = $this->getMockBuilder('Graby\Graby') | ||
295 | ->disableOriginalConstructor() | ||
296 | ->getMock(); | ||
297 | |||
298 | $tagRepo = $this->getTagRepositoryMock(); | ||
299 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
300 | |||
301 | $entry = new Entry(new User()); | ||
302 | |||
303 | $proxy->assignTagsToEntry($entry, []); | ||
304 | |||
305 | $this->assertCount(0, $entry->getTags()); | ||
306 | } | ||
307 | |||
308 | public function testAssignTagsWithEmptyString() | ||
309 | { | ||
310 | $graby = $this->getMockBuilder('Graby\Graby') | ||
311 | ->disableOriginalConstructor() | ||
312 | ->getMock(); | ||
313 | |||
314 | $tagRepo = $this->getTagRepositoryMock(); | ||
315 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
316 | |||
317 | $entry = new Entry(new User()); | ||
318 | |||
319 | $proxy->assignTagsToEntry($entry, ''); | ||
320 | |||
321 | $this->assertCount(0, $entry->getTags()); | ||
322 | } | ||
323 | |||
324 | public function testAssignTagsAlreadyAssigned() | ||
325 | { | ||
326 | $graby = $this->getMockBuilder('Graby\Graby') | ||
327 | ->disableOriginalConstructor() | ||
328 | ->getMock(); | ||
329 | |||
330 | $tagRepo = $this->getTagRepositoryMock(); | ||
331 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
332 | |||
333 | $tagEntity = new Tag(); | ||
334 | $tagEntity->setLabel('tag1'); | ||
335 | |||
336 | $entry = new Entry(new User()); | ||
337 | $entry->addTag($tagEntity); | ||
338 | |||
339 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | ||
340 | |||
341 | $this->assertCount(2, $entry->getTags()); | ||
342 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
343 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
344 | } | ||
345 | |||
346 | public function testAssignTagsNotFlushed() | ||
347 | { | ||
348 | $graby = $this->getMockBuilder('Graby\Graby') | ||
349 | ->disableOriginalConstructor() | ||
350 | ->getMock(); | ||
351 | |||
352 | $tagRepo = $this->getTagRepositoryMock(); | ||
353 | $tagRepo->expects($this->never()) | ||
354 | ->method('__call'); | ||
355 | |||
356 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage); | ||
357 | |||
358 | $tagEntity = new Tag(); | ||
359 | $tagEntity->setLabel('tag1'); | ||
360 | |||
361 | $entry = new Entry(new User()); | ||
362 | |||
363 | $proxy->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | ||
364 | |||
365 | $this->assertCount(1, $entry->getTags()); | ||
366 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
367 | } | ||
368 | |||
369 | private function getTaggerMock() | 257 | private function getTaggerMock() |
370 | { | 258 | { |
371 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | 259 | return $this->getMockBuilder(RuleBasedTagger::class) |
372 | ->setMethods(['tag']) | 260 | ->setMethods(['tag']) |
373 | ->disableOriginalConstructor() | 261 | ->disableOriginalConstructor() |
374 | ->getMock(); | 262 | ->getMock(); |
375 | } | 263 | } |
376 | 264 | ||
377 | private function getTagRepositoryMock() | ||
378 | { | ||
379 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | ||
380 | ->disableOriginalConstructor() | ||
381 | ->getMock(); | ||
382 | } | ||
383 | |||
384 | private function getLogger() | 265 | private function getLogger() |
385 | { | 266 | { |
386 | return new NullLogger(); | 267 | return new NullLogger(); |
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php index 17b08c2a..1e21f400 100644 --- a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php +++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php | |||
@@ -2,6 +2,8 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | 3 | namespace Tests\Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use Monolog\Handler\TestHandler; | ||
6 | use Monolog\Logger; | ||
5 | use Wallabag\CoreBundle\Entity\Config; | 7 | use Wallabag\CoreBundle\Entity\Config; |
6 | use Wallabag\CoreBundle\Entity\Entry; | 8 | use Wallabag\CoreBundle\Entity\Entry; |
7 | use Wallabag\CoreBundle\Entity\Tag; | 9 | use Wallabag\CoreBundle\Entity\Tag; |
@@ -15,14 +17,19 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
15 | private $tagRepository; | 17 | private $tagRepository; |
16 | private $entryRepository; | 18 | private $entryRepository; |
17 | private $tagger; | 19 | private $tagger; |
20 | private $logger; | ||
21 | private $handler; | ||
18 | 22 | ||
19 | public function setUp() | 23 | public function setUp() |
20 | { | 24 | { |
21 | $this->rulerz = $this->getRulerZMock(); | 25 | $this->rulerz = $this->getRulerZMock(); |
22 | $this->tagRepository = $this->getTagRepositoryMock(); | 26 | $this->tagRepository = $this->getTagRepositoryMock(); |
23 | $this->entryRepository = $this->getEntryRepositoryMock(); | 27 | $this->entryRepository = $this->getEntryRepositoryMock(); |
28 | $this->logger = $this->getLogger(); | ||
29 | $this->handler = new TestHandler(); | ||
30 | $this->logger->pushHandler($this->handler); | ||
24 | 31 | ||
25 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository); | 32 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger); |
26 | } | 33 | } |
27 | 34 | ||
28 | public function testTagWithNoRule() | 35 | public function testTagWithNoRule() |
@@ -32,6 +39,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
32 | $this->tagger->tag($entry); | 39 | $this->tagger->tag($entry); |
33 | 40 | ||
34 | $this->assertTrue($entry->getTags()->isEmpty()); | 41 | $this->assertTrue($entry->getTags()->isEmpty()); |
42 | $records = $this->handler->getRecords(); | ||
43 | $this->assertCount(0, $records); | ||
35 | } | 44 | } |
36 | 45 | ||
37 | public function testTagWithNoMatchingRule() | 46 | public function testTagWithNoMatchingRule() |
@@ -49,6 +58,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
49 | $this->tagger->tag($entry); | 58 | $this->tagger->tag($entry); |
50 | 59 | ||
51 | $this->assertTrue($entry->getTags()->isEmpty()); | 60 | $this->assertTrue($entry->getTags()->isEmpty()); |
61 | $records = $this->handler->getRecords(); | ||
62 | $this->assertCount(0, $records); | ||
52 | } | 63 | } |
53 | 64 | ||
54 | public function testTagWithAMatchingRule() | 65 | public function testTagWithAMatchingRule() |
@@ -70,6 +81,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
70 | $tags = $entry->getTags(); | 81 | $tags = $entry->getTags(); |
71 | $this->assertSame('foo', $tags[0]->getLabel()); | 82 | $this->assertSame('foo', $tags[0]->getLabel()); |
72 | $this->assertSame('bar', $tags[1]->getLabel()); | 83 | $this->assertSame('bar', $tags[1]->getLabel()); |
84 | |||
85 | $records = $this->handler->getRecords(); | ||
86 | $this->assertCount(1, $records); | ||
73 | } | 87 | } |
74 | 88 | ||
75 | public function testTagWithAMixOfMatchingRules() | 89 | public function testTagWithAMixOfMatchingRules() |
@@ -90,6 +104,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
90 | 104 | ||
91 | $tags = $entry->getTags(); | 105 | $tags = $entry->getTags(); |
92 | $this->assertSame('foo', $tags[0]->getLabel()); | 106 | $this->assertSame('foo', $tags[0]->getLabel()); |
107 | $records = $this->handler->getRecords(); | ||
108 | $this->assertCount(1, $records); | ||
93 | } | 109 | } |
94 | 110 | ||
95 | public function testWhenTheTagExists() | 111 | public function testWhenTheTagExists() |
@@ -118,6 +134,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
118 | 134 | ||
119 | $tags = $entry->getTags(); | 135 | $tags = $entry->getTags(); |
120 | $this->assertSame($tag, $tags[0]); | 136 | $this->assertSame($tag, $tags[0]); |
137 | $records = $this->handler->getRecords(); | ||
138 | $this->assertCount(1, $records); | ||
121 | } | 139 | } |
122 | 140 | ||
123 | public function testSameTagWithDifferentfMatchingRules() | 141 | public function testSameTagWithDifferentfMatchingRules() |
@@ -138,6 +156,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
138 | 156 | ||
139 | $tags = $entry->getTags(); | 157 | $tags = $entry->getTags(); |
140 | $this->assertCount(1, $tags); | 158 | $this->assertCount(1, $tags); |
159 | $records = $this->handler->getRecords(); | ||
160 | $this->assertCount(2, $records); | ||
141 | } | 161 | } |
142 | 162 | ||
143 | public function testTagAllEntriesForAUser() | 163 | public function testTagAllEntriesForAUser() |
@@ -209,4 +229,9 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | |||
209 | ->disableOriginalConstructor() | 229 | ->disableOriginalConstructor() |
210 | ->getMock(); | 230 | ->getMock(); |
211 | } | 231 | } |
232 | |||
233 | private function getLogger() | ||
234 | { | ||
235 | return new Logger('foo'); | ||
236 | } | ||
212 | } | 237 | } |
diff --git a/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php new file mode 100644 index 00000000..6d6d6484 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php | |||
@@ -0,0 +1,108 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | ||
7 | use Wallabag\CoreBundle\Helper\TagsAssigner; | ||
8 | use Wallabag\UserBundle\Entity\User; | ||
9 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
10 | |||
11 | class TagsAssignerTest extends \PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | public function testAssignTagsWithArrayAndExtraSpaces() | ||
14 | { | ||
15 | $tagRepo = $this->getTagRepositoryMock(); | ||
16 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
17 | |||
18 | $entry = new Entry(new User()); | ||
19 | |||
20 | $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']); | ||
21 | |||
22 | $this->assertCount(2, $entry->getTags()); | ||
23 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
24 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
25 | } | ||
26 | |||
27 | public function testAssignTagsWithString() | ||
28 | { | ||
29 | $tagRepo = $this->getTagRepositoryMock(); | ||
30 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
31 | |||
32 | $entry = new Entry(new User()); | ||
33 | |||
34 | $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2'); | ||
35 | |||
36 | $this->assertCount(2, $entry->getTags()); | ||
37 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
38 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
39 | } | ||
40 | |||
41 | public function testAssignTagsWithEmptyArray() | ||
42 | { | ||
43 | $tagRepo = $this->getTagRepositoryMock(); | ||
44 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
45 | |||
46 | $entry = new Entry(new User()); | ||
47 | |||
48 | $tagsAssigner->assignTagsToEntry($entry, []); | ||
49 | |||
50 | $this->assertCount(0, $entry->getTags()); | ||
51 | } | ||
52 | |||
53 | public function testAssignTagsWithEmptyString() | ||
54 | { | ||
55 | $tagRepo = $this->getTagRepositoryMock(); | ||
56 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
57 | |||
58 | $entry = new Entry(new User()); | ||
59 | |||
60 | $tagsAssigner->assignTagsToEntry($entry, ''); | ||
61 | |||
62 | $this->assertCount(0, $entry->getTags()); | ||
63 | } | ||
64 | |||
65 | public function testAssignTagsAlreadyAssigned() | ||
66 | { | ||
67 | $tagRepo = $this->getTagRepositoryMock(); | ||
68 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
69 | |||
70 | $tagEntity = new Tag(); | ||
71 | $tagEntity->setLabel('tag1'); | ||
72 | |||
73 | $entry = new Entry(new User()); | ||
74 | $entry->addTag($tagEntity); | ||
75 | |||
76 | $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2'); | ||
77 | |||
78 | $this->assertCount(2, $entry->getTags()); | ||
79 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
80 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | ||
81 | } | ||
82 | |||
83 | public function testAssignTagsNotFlushed() | ||
84 | { | ||
85 | $tagRepo = $this->getTagRepositoryMock(); | ||
86 | $tagRepo->expects($this->never()) | ||
87 | ->method('__call'); | ||
88 | |||
89 | $tagsAssigner = new TagsAssigner($tagRepo); | ||
90 | |||
91 | $tagEntity = new Tag(); | ||
92 | $tagEntity->setLabel('tag1'); | ||
93 | |||
94 | $entry = new Entry(new User()); | ||
95 | |||
96 | $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]); | ||
97 | |||
98 | $this->assertCount(1, $entry->getTags()); | ||
99 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | ||
100 | } | ||
101 | |||
102 | private function getTagRepositoryMock() | ||
103 | { | ||
104 | return $this->getMockBuilder(TagRepository::class) | ||
105 | ->disableOriginalConstructor() | ||
106 | ->getMock(); | ||
107 | } | ||
108 | } | ||