aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle/Helper
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-06-01 21:27:35 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-06-22 17:59:35 +0200
commit23634d5d842dabcf5d7475e2becb7e127824239e (patch)
treeb91688722a996c46f27e8fe0542c356424483da3 /tests/Wallabag/CoreBundle/Helper
parent891a026e31ad54ca90b70f6026f23260cfadb7fd (diff)
downloadwallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.gz
wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.zst
wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.zip
Jump to Symfony 3.1
Diffstat (limited to 'tests/Wallabag/CoreBundle/Helper')
-rw-r--r--tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php318
-rw-r--r--tests/Wallabag/CoreBundle/Helper/RedirectTest.php55
-rw-r--r--tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php212
3 files changed, 585 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
new file mode 100644
index 00000000..7abb0737
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php
@@ -0,0 +1,318 @@
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\UserBundle\Entity\User;
10
11class ContentProxyTest extends \PHPUnit_Framework_TestCase
12{
13 public function testWithBadUrl()
14 {
15 $tagger = $this->getTaggerMock();
16 $tagger->expects($this->once())
17 ->method('tag');
18
19 $graby = $this->getMockBuilder('Graby\Graby')
20 ->setMethods(['fetchContent'])
21 ->disableOriginalConstructor()
22 ->getMock();
23
24 $graby->expects($this->any())
25 ->method('fetchContent')
26 ->willReturn([
27 'html' => false,
28 'title' => '',
29 'url' => '',
30 'content_type' => '',
31 'language' => '',
32 ]);
33
34 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
35 $entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
36
37 $this->assertEquals('http://user@:80', $entry->getUrl());
38 $this->assertEmpty($entry->getTitle());
39 $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
40 $this->assertEmpty($entry->getPreviewPicture());
41 $this->assertEmpty($entry->getMimetype());
42 $this->assertEmpty($entry->getLanguage());
43 $this->assertEquals(0.0, $entry->getReadingTime());
44 $this->assertEquals(false, $entry->getDomainName());
45 }
46
47 public function testWithEmptyContent()
48 {
49 $tagger = $this->getTaggerMock();
50 $tagger->expects($this->once())
51 ->method('tag');
52
53 $graby = $this->getMockBuilder('Graby\Graby')
54 ->setMethods(['fetchContent'])
55 ->disableOriginalConstructor()
56 ->getMock();
57
58 $graby->expects($this->any())
59 ->method('fetchContent')
60 ->willReturn([
61 'html' => false,
62 'title' => '',
63 'url' => '',
64 'content_type' => '',
65 'language' => '',
66 ]);
67
68 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
69 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
70
71 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
72 $this->assertEmpty($entry->getTitle());
73 $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
74 $this->assertEmpty($entry->getPreviewPicture());
75 $this->assertEmpty($entry->getMimetype());
76 $this->assertEmpty($entry->getLanguage());
77 $this->assertEquals(0.0, $entry->getReadingTime());
78 $this->assertEquals('0.0.0.0', $entry->getDomainName());
79 }
80
81 public function testWithEmptyContentButOG()
82 {
83 $tagger = $this->getTaggerMock();
84 $tagger->expects($this->once())
85 ->method('tag');
86
87 $graby = $this->getMockBuilder('Graby\Graby')
88 ->setMethods(['fetchContent'])
89 ->disableOriginalConstructor()
90 ->getMock();
91
92 $graby->expects($this->any())
93 ->method('fetchContent')
94 ->willReturn([
95 'html' => false,
96 'title' => '',
97 'url' => '',
98 'content_type' => '',
99 'language' => '',
100 'open_graph' => [
101 'og_title' => 'my title',
102 'og_description' => 'desc',
103 ],
104 ]);
105
106 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
107 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
108
109 $this->assertEquals('http://domain.io', $entry->getUrl());
110 $this->assertEquals('my title', $entry->getTitle());
111 $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent());
112 $this->assertEmpty($entry->getPreviewPicture());
113 $this->assertEmpty($entry->getLanguage());
114 $this->assertEmpty($entry->getMimetype());
115 $this->assertEquals(0.0, $entry->getReadingTime());
116 $this->assertEquals('domain.io', $entry->getDomainName());
117 }
118
119 public function testWithContent()
120 {
121 $tagger = $this->getTaggerMock();
122 $tagger->expects($this->once())
123 ->method('tag');
124
125 $graby = $this->getMockBuilder('Graby\Graby')
126 ->setMethods(['fetchContent'])
127 ->disableOriginalConstructor()
128 ->getMock();
129
130 $graby->expects($this->any())
131 ->method('fetchContent')
132 ->willReturn([
133 'html' => str_repeat('this is my content', 325),
134 'title' => 'this is my title',
135 'url' => 'http://1.1.1.1',
136 'content_type' => 'text/html',
137 'language' => 'fr',
138 'open_graph' => [
139 'og_title' => 'my OG title',
140 'og_description' => 'OG desc',
141 'og_image' => 'http://3.3.3.3/cover.jpg',
142 ],
143 ]);
144
145 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
146 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
147
148 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
149 $this->assertEquals('this is my title', $entry->getTitle());
150 $this->assertContains('this is my content', $entry->getContent());
151 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
152 $this->assertEquals('text/html', $entry->getMimetype());
153 $this->assertEquals('fr', $entry->getLanguage());
154 $this->assertEquals(4.0, $entry->getReadingTime());
155 $this->assertEquals('1.1.1.1', $entry->getDomainName());
156 }
157
158 public function testWithForcedContent()
159 {
160 $tagger = $this->getTaggerMock();
161 $tagger->expects($this->once())
162 ->method('tag');
163
164 $graby = $this->getMockBuilder('Graby\Graby')->getMock();
165
166 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
167 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
168 'html' => str_repeat('this is my content', 325),
169 'title' => 'this is my title',
170 'url' => 'http://1.1.1.1',
171 'content_type' => 'text/html',
172 'language' => 'fr',
173 ]);
174
175 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
176 $this->assertEquals('this is my title', $entry->getTitle());
177 $this->assertContains('this is my content', $entry->getContent());
178 $this->assertEquals('text/html', $entry->getMimetype());
179 $this->assertEquals('fr', $entry->getLanguage());
180 $this->assertEquals(4.0, $entry->getReadingTime());
181 $this->assertEquals('1.1.1.1', $entry->getDomainName());
182 }
183
184 public function testTaggerThrowException()
185 {
186 $graby = $this->getMockBuilder('Graby\Graby')
187 ->disableOriginalConstructor()
188 ->getMock();
189
190 $tagger = $this->getTaggerMock();
191 $tagger->expects($this->once())
192 ->method('tag')
193 ->will($this->throwException(new \Exception()));
194
195 $tagRepo = $this->getTagRepositoryMock();
196 $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger());
197
198 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
199 'html' => str_repeat('this is my content', 325),
200 'title' => 'this is my title',
201 'url' => 'http://1.1.1.1',
202 'content_type' => 'text/html',
203 'language' => 'fr',
204 ]);
205
206 $this->assertCount(0, $entry->getTags());
207 }
208
209 public function testAssignTagsWithArrayAndExtraSpaces()
210 {
211 $graby = $this->getMockBuilder('Graby\Graby')
212 ->disableOriginalConstructor()
213 ->getMock();
214
215 $tagRepo = $this->getTagRepositoryMock();
216 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
217
218 $entry = new Entry(new User());
219
220 $proxy->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
221
222 $this->assertCount(2, $entry->getTags());
223 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
224 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
225 }
226
227 public function testAssignTagsWithString()
228 {
229 $graby = $this->getMockBuilder('Graby\Graby')
230 ->disableOriginalConstructor()
231 ->getMock();
232
233 $tagRepo = $this->getTagRepositoryMock();
234 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
235
236 $entry = new Entry(new User());
237
238 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
239
240 $this->assertCount(2, $entry->getTags());
241 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
242 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
243 }
244
245 public function testAssignTagsWithEmptyArray()
246 {
247 $graby = $this->getMockBuilder('Graby\Graby')
248 ->disableOriginalConstructor()
249 ->getMock();
250
251 $tagRepo = $this->getTagRepositoryMock();
252 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
253
254 $entry = new Entry(new User());
255
256 $proxy->assignTagsToEntry($entry, []);
257
258 $this->assertCount(0, $entry->getTags());
259 }
260
261 public function testAssignTagsWithEmptyString()
262 {
263 $graby = $this->getMockBuilder('Graby\Graby')
264 ->disableOriginalConstructor()
265 ->getMock();
266
267 $tagRepo = $this->getTagRepositoryMock();
268 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
269
270 $entry = new Entry(new User());
271
272 $proxy->assignTagsToEntry($entry, '');
273
274 $this->assertCount(0, $entry->getTags());
275 }
276
277 public function testAssignTagsAlreadyAssigned()
278 {
279 $graby = $this->getMockBuilder('Graby\Graby')
280 ->disableOriginalConstructor()
281 ->getMock();
282
283 $tagRepo = $this->getTagRepositoryMock();
284 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
285
286 $tagEntity = new Tag();
287 $tagEntity->setLabel('tag1');
288
289 $entry = new Entry(new User());
290 $entry->addTag($tagEntity);
291
292 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
293
294 $this->assertCount(2, $entry->getTags());
295 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
296 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
297 }
298
299 private function getTaggerMock()
300 {
301 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
302 ->setMethods(['tag'])
303 ->disableOriginalConstructor()
304 ->getMock();
305 }
306
307 private function getTagRepositoryMock()
308 {
309 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
310 ->disableOriginalConstructor()
311 ->getMock();
312 }
313
314 private function getLogger()
315 {
316 return new NullLogger();
317 }
318}
diff --git a/tests/Wallabag/CoreBundle/Helper/RedirectTest.php b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php
new file mode 100644
index 00000000..f339f75e
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/RedirectTest.php
@@ -0,0 +1,55 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Wallabag\CoreBundle\Helper\Redirect;
6
7class RedirectTest extends \PHPUnit_Framework_TestCase
8{
9 /** @var \PHPUnit_Framework_MockObject_MockObject */
10 private $routerMock;
11
12 /** @var Redirect */
13 private $redirect;
14
15 public function setUp()
16 {
17 $this->routerMock = $this->getRouterMock();
18 $this->redirect = new Redirect($this->routerMock);
19 }
20
21 public function testRedirectToNullWithFallback()
22 {
23 $redirectUrl = $this->redirect->to(null, 'fallback');
24
25 $this->assertEquals('fallback', $redirectUrl);
26 }
27
28 public function testRedirectToNullWithoutFallback()
29 {
30 $redirectUrl = $this->redirect->to(null);
31
32 $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
33 }
34
35 public function testRedirectToValidUrl()
36 {
37 $redirectUrl = $this->redirect->to('/unread/list');
38
39 $this->assertEquals('/unread/list', $redirectUrl);
40 }
41
42 private function getRouterMock()
43 {
44 $mock = $this->getMockBuilder('Symfony\Component\Routing\Router')
45 ->disableOriginalConstructor()
46 ->getMock();
47
48 $mock->expects($this->any())
49 ->method('generate')
50 ->with('homepage')
51 ->willReturn('homepage');
52
53 return $mock;
54 }
55}
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
new file mode 100644
index 00000000..17b08c2a
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedTaggerTest.php
@@ -0,0 +1,212 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Wallabag\CoreBundle\Entity\Config;
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\CoreBundle\Entity\TaggingRule;
9use Wallabag\CoreBundle\Helper\RuleBasedTagger;
10use Wallabag\UserBundle\Entity\User;
11
12class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
13{
14 private $rulerz;
15 private $tagRepository;
16 private $entryRepository;
17 private $tagger;
18
19 public function setUp()
20 {
21 $this->rulerz = $this->getRulerZMock();
22 $this->tagRepository = $this->getTagRepositoryMock();
23 $this->entryRepository = $this->getEntryRepositoryMock();
24
25 $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository);
26 }
27
28 public function testTagWithNoRule()
29 {
30 $entry = new Entry($this->getUser());
31
32 $this->tagger->tag($entry);
33
34 $this->assertTrue($entry->getTags()->isEmpty());
35 }
36
37 public function testTagWithNoMatchingRule()
38 {
39 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
40 $user = $this->getUser([$taggingRule]);
41 $entry = new Entry($user);
42
43 $this->rulerz
44 ->expects($this->once())
45 ->method('satisfies')
46 ->with($entry, 'rule as string')
47 ->willReturn(false);
48
49 $this->tagger->tag($entry);
50
51 $this->assertTrue($entry->getTags()->isEmpty());
52 }
53
54 public function testTagWithAMatchingRule()
55 {
56 $taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
57 $user = $this->getUser([$taggingRule]);
58 $entry = new Entry($user);
59
60 $this->rulerz
61 ->expects($this->once())
62 ->method('satisfies')
63 ->with($entry, 'rule as string')
64 ->willReturn(true);
65
66 $this->tagger->tag($entry);
67
68 $this->assertFalse($entry->getTags()->isEmpty());
69
70 $tags = $entry->getTags();
71 $this->assertSame('foo', $tags[0]->getLabel());
72 $this->assertSame('bar', $tags[1]->getLabel());
73 }
74
75 public function testTagWithAMixOfMatchingRules()
76 {
77 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
78 $otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
79
80 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
81 $entry = new Entry($user);
82
83 $this->rulerz
84 ->method('satisfies')
85 ->will($this->onConsecutiveCalls(false, true));
86
87 $this->tagger->tag($entry);
88
89 $this->assertFalse($entry->getTags()->isEmpty());
90
91 $tags = $entry->getTags();
92 $this->assertSame('foo', $tags[0]->getLabel());
93 }
94
95 public function testWhenTheTagExists()
96 {
97 $taggingRule = $this->getTaggingRule('rule as string', ['foo']);
98 $user = $this->getUser([$taggingRule]);
99 $entry = new Entry($user);
100 $tag = new Tag();
101
102 $this->rulerz
103 ->expects($this->once())
104 ->method('satisfies')
105 ->with($entry, 'rule as string')
106 ->willReturn(true);
107
108 $this->tagRepository
109 ->expects($this->once())
110 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
111 // to magically call the `findOneBy` with ['label' => 'foo']
112 ->method('__call')
113 ->willReturn($tag);
114
115 $this->tagger->tag($entry);
116
117 $this->assertFalse($entry->getTags()->isEmpty());
118
119 $tags = $entry->getTags();
120 $this->assertSame($tag, $tags[0]);
121 }
122
123 public function testSameTagWithDifferentfMatchingRules()
124 {
125 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
126 $otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
127
128 $user = $this->getUser([$taggingRule, $otherTaggingRule]);
129 $entry = new Entry($user);
130
131 $this->rulerz
132 ->method('satisfies')
133 ->willReturn(true);
134
135 $this->tagger->tag($entry);
136
137 $this->assertFalse($entry->getTags()->isEmpty());
138
139 $tags = $entry->getTags();
140 $this->assertCount(1, $tags);
141 }
142
143 public function testTagAllEntriesForAUser()
144 {
145 $taggingRule = $this->getTaggingRule('bla bla', ['hey']);
146
147 $user = $this->getUser([$taggingRule]);
148
149 $this->rulerz
150 ->method('satisfies')
151 ->willReturn(true);
152
153 $this->rulerz
154 ->method('filter')
155 ->willReturn([new Entry($user), new Entry($user)]);
156
157 $entries = $this->tagger->tagAllForUser($user);
158
159 $this->assertCount(2, $entries);
160
161 foreach ($entries as $entry) {
162 $tags = $entry->getTags();
163
164 $this->assertCount(1, $tags);
165 $this->assertEquals('hey', $tags[0]->getLabel());
166 }
167 }
168
169 private function getUser(array $taggingRules = [])
170 {
171 $user = new User();
172 $config = new Config($user);
173
174 $user->setConfig($config);
175
176 foreach ($taggingRules as $rule) {
177 $config->addTaggingRule($rule);
178 }
179
180 return $user;
181 }
182
183 private function getTaggingRule($rule, array $tags)
184 {
185 $taggingRule = new TaggingRule();
186 $taggingRule->setRule($rule);
187 $taggingRule->setTags($tags);
188
189 return $taggingRule;
190 }
191
192 private function getRulerZMock()
193 {
194 return $this->getMockBuilder('RulerZ\RulerZ')
195 ->disableOriginalConstructor()
196 ->getMock();
197 }
198
199 private function getTagRepositoryMock()
200 {
201 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
202 ->disableOriginalConstructor()
203 ->getMock();
204 }
205
206 private function getEntryRepositoryMock()
207 {
208 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
209 ->disableOriginalConstructor()
210 ->getMock();
211 }
212}