aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-05-27 22:08:14 +0200
committerThomas Citharel <tcit@tcit.fr>2017-05-27 22:08:14 +0200
commit6bc6fb1f60e7b81a21f844dca025671a2f4a4564 (patch)
treefde672650c6a2ef2ccb611a6a29989a7c944ea00 /tests/Wallabag/CoreBundle
parent35941d57ee4d06ec3557d4b126d5f6fd263bcf3a (diff)
downloadwallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.tar.gz
wallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.tar.zst
wallabag-6bc6fb1f60e7b81a21f844dca025671a2f4a4564.zip
Move Tags assigner to a separate file
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'tests/Wallabag/CoreBundle')
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php139
-rw-r--r--tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php114
2 files changed, 124 insertions, 129 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
index 8abb1bbb..6494f348 100644
--- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -7,6 +7,8 @@ use Wallabag\CoreBundle\Helper\ContentProxy;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag; 8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Repository\TagRepository;
11use Wallabag\CoreBundle\Helper\RuleBasedTagger;
10 12
11class ContentProxyTest extends \PHPUnit_Framework_TestCase 13class 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,7 +108,7 @@ 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());
@@ -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/TagsAssignerTest.php b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
new file mode 100644
index 00000000..bc59eeab
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/TagsAssignerTest.php
@@ -0,0 +1,114 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Psr\Log\NullLogger;
6use Wallabag\CoreBundle\Helper\ContentProxy;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Helper\TagsAssigner;
10use Wallabag\UserBundle\Entity\User;
11use Wallabag\CoreBundle\Repository\TagRepository;
12use Wallabag\CoreBundle\Helper\RuleBasedTagger;
13
14class TagsAssignerTest extends \PHPUnit_Framework_TestCase
15{
16
17 public function testAssignTagsWithArrayAndExtraSpaces()
18 {
19
20 $tagRepo = $this->getTagRepositoryMock();
21 $tagsAssigner = new TagsAssigner($tagRepo);
22
23 $entry = new Entry(new User());
24
25 $tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
26
27 $this->assertCount(2, $entry->getTags());
28 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
29 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
30 }
31
32 public function testAssignTagsWithString()
33 {
34 $tagRepo = $this->getTagRepositoryMock();
35 $tagsAssigner = new TagsAssigner($tagRepo);
36
37 $entry = new Entry(new User());
38
39 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
40
41 $this->assertCount(2, $entry->getTags());
42 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
43 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
44 }
45
46 public function testAssignTagsWithEmptyArray()
47 {
48 $tagRepo = $this->getTagRepositoryMock();
49 $tagsAssigner = new TagsAssigner($tagRepo);
50
51 $entry = new Entry(new User());
52
53 $tagsAssigner->assignTagsToEntry($entry, []);
54
55 $this->assertCount(0, $entry->getTags());
56 }
57
58 public function testAssignTagsWithEmptyString()
59 {
60 $tagRepo = $this->getTagRepositoryMock();
61 $tagsAssigner = new TagsAssigner($tagRepo);
62
63 $entry = new Entry(new User());
64
65 $tagsAssigner->assignTagsToEntry($entry, '');
66
67 $this->assertCount(0, $entry->getTags());
68 }
69
70 public function testAssignTagsAlreadyAssigned()
71 {
72 $tagRepo = $this->getTagRepositoryMock();
73 $tagsAssigner = new TagsAssigner($tagRepo);
74
75 $tagEntity = new Tag();
76 $tagEntity->setLabel('tag1');
77
78 $entry = new Entry(new User());
79 $entry->addTag($tagEntity);
80
81 $tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
82
83 $this->assertCount(2, $entry->getTags());
84 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
85 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
86 }
87
88 public function testAssignTagsNotFlushed()
89 {
90
91 $tagRepo = $this->getTagRepositoryMock();
92 $tagRepo->expects($this->never())
93 ->method('__call');
94
95 $tagsAssigner = new TagsAssigner($tagRepo);
96
97 $tagEntity = new Tag();
98 $tagEntity->setLabel('tag1');
99
100 $entry = new Entry(new User());
101
102 $tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
103
104 $this->assertCount(1, $entry->getTags());
105 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
106 }
107
108 private function getTagRepositoryMock()
109 {
110 return $this->getMockBuilder(TagRepository::class)
111 ->disableOriginalConstructor()
112 ->getMock();
113 }
114}