]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Helper; | |
4 | ||
5 | use Psr\Log\NullLogger; | |
6 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
8 | use Wallabag\CoreBundle\Entity\Tag; | |
9 | use Wallabag\UserBundle\Entity\User; | |
10 | ||
11 | class ContentProxyTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
13 | public function testWithEmptyContent() | |
14 | { | |
15 | $tagger = $this->getTaggerMock(); | |
16 | $tagger->expects($this->once()) | |
17 | ->method('tag'); | |
18 | ||
19 | $graby = $this->getMockBuilder('Graby\Graby') | |
20 | ->setMethods(array('fetchContent')) | |
21 | ->disableOriginalConstructor() | |
22 | ->getMock(); | |
23 | ||
24 | $graby->expects($this->any()) | |
25 | ->method('fetchContent') | |
26 | ->willReturn(array( | |
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://0.0.0.0'); | |
36 | ||
37 | $this->assertEquals('http://0.0.0.0', $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('0.0.0.0', $entry->getDomainName()); | |
45 | } | |
46 | ||
47 | public function testWithEmptyContentButOG() | |
48 | { | |
49 | $tagger = $this->getTaggerMock(); | |
50 | $tagger->expects($this->once()) | |
51 | ->method('tag'); | |
52 | ||
53 | $graby = $this->getMockBuilder('Graby\Graby') | |
54 | ->setMethods(array('fetchContent')) | |
55 | ->disableOriginalConstructor() | |
56 | ->getMock(); | |
57 | ||
58 | $graby->expects($this->any()) | |
59 | ->method('fetchContent') | |
60 | ->willReturn(array( | |
61 | 'html' => false, | |
62 | 'title' => '', | |
63 | 'url' => '', | |
64 | 'content_type' => '', | |
65 | 'language' => '', | |
66 | 'open_graph' => array( | |
67 | 'og_title' => 'my title', | |
68 | 'og_description' => 'desc', | |
69 | ), | |
70 | )); | |
71 | ||
72 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); | |
73 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); | |
74 | ||
75 | $this->assertEquals('http://domain.io', $entry->getUrl()); | |
76 | $this->assertEquals('my title', $entry->getTitle()); | |
77 | $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent()); | |
78 | $this->assertEmpty($entry->getPreviewPicture()); | |
79 | $this->assertEmpty($entry->getLanguage()); | |
80 | $this->assertEmpty($entry->getMimetype()); | |
81 | $this->assertEquals(0.0, $entry->getReadingTime()); | |
82 | $this->assertEquals('domain.io', $entry->getDomainName()); | |
83 | } | |
84 | ||
85 | public function testWithContent() | |
86 | { | |
87 | $tagger = $this->getTaggerMock(); | |
88 | $tagger->expects($this->once()) | |
89 | ->method('tag'); | |
90 | ||
91 | $graby = $this->getMockBuilder('Graby\Graby') | |
92 | ->setMethods(array('fetchContent')) | |
93 | ->disableOriginalConstructor() | |
94 | ->getMock(); | |
95 | ||
96 | $graby->expects($this->any()) | |
97 | ->method('fetchContent') | |
98 | ->willReturn(array( | |
99 | 'html' => str_repeat('this is my content', 325), | |
100 | 'title' => 'this is my title', | |
101 | 'url' => 'http://1.1.1.1', | |
102 | 'content_type' => 'text/html', | |
103 | 'language' => 'fr', | |
104 | 'open_graph' => array( | |
105 | 'og_title' => 'my OG title', | |
106 | 'og_description' => 'OG desc', | |
107 | 'og_image' => 'http://3.3.3.3/cover.jpg', | |
108 | ), | |
109 | )); | |
110 | ||
111 | $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger()); | |
112 | $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); | |
113 | ||
114 | $this->assertEquals('http://1.1.1.1', $entry->getUrl()); | |
115 | $this->assertEquals('this is my title', $entry->getTitle()); | |
116 | $this->assertContains('this is my content', $entry->getContent()); | |
117 | $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture()); | |
118 | $this->assertEquals('text/html', $entry->getMimetype()); | |
119 | $this->assertEquals('fr', $entry->getLanguage()); | |
120 | $this->assertEquals(4.0, $entry->getReadingTime()); | |
121 | $this->assertEquals('1.1.1.1', $entry->getDomainName()); | |
122 | } | |
123 | ||
124 | public function testAssignTagsWithArrayAndExtraSpaces() | |
125 | { | |
126 | $graby = $this->getMockBuilder('Graby\Graby') | |
127 | ->disableOriginalConstructor() | |
128 | ->getMock(); | |
129 | ||
130 | $tagRepo = $this->getTagRepositoryMock(); | |
131 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | |
132 | ||
133 | $entry = new Entry(new User()); | |
134 | ||
135 | $proxy->assignTagsToEntry($entry, array(' tag1', 'tag2 ')); | |
136 | ||
137 | $this->assertCount(2, $entry->getTags()); | |
138 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
139 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
140 | } | |
141 | ||
142 | public function testAssignTagsWithString() | |
143 | { | |
144 | $graby = $this->getMockBuilder('Graby\Graby') | |
145 | ->disableOriginalConstructor() | |
146 | ->getMock(); | |
147 | ||
148 | $tagRepo = $this->getTagRepositoryMock(); | |
149 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | |
150 | ||
151 | $entry = new Entry(new User()); | |
152 | ||
153 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | |
154 | ||
155 | $this->assertCount(2, $entry->getTags()); | |
156 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
157 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
158 | } | |
159 | ||
160 | public function testAssignTagsWithEmptyArray() | |
161 | { | |
162 | $graby = $this->getMockBuilder('Graby\Graby') | |
163 | ->disableOriginalConstructor() | |
164 | ->getMock(); | |
165 | ||
166 | $tagRepo = $this->getTagRepositoryMock(); | |
167 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | |
168 | ||
169 | $entry = new Entry(new User()); | |
170 | ||
171 | $proxy->assignTagsToEntry($entry, array()); | |
172 | ||
173 | $this->assertCount(0, $entry->getTags()); | |
174 | } | |
175 | ||
176 | public function testAssignTagsWithEmptyString() | |
177 | { | |
178 | $graby = $this->getMockBuilder('Graby\Graby') | |
179 | ->disableOriginalConstructor() | |
180 | ->getMock(); | |
181 | ||
182 | $tagRepo = $this->getTagRepositoryMock(); | |
183 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | |
184 | ||
185 | $entry = new Entry(new User()); | |
186 | ||
187 | $proxy->assignTagsToEntry($entry, ''); | |
188 | ||
189 | $this->assertCount(0, $entry->getTags()); | |
190 | } | |
191 | ||
192 | public function testAssignTagsAlreadyAssigned() | |
193 | { | |
194 | $graby = $this->getMockBuilder('Graby\Graby') | |
195 | ->disableOriginalConstructor() | |
196 | ->getMock(); | |
197 | ||
198 | $tagRepo = $this->getTagRepositoryMock(); | |
199 | $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger()); | |
200 | ||
201 | $tagEntity = new Tag(); | |
202 | $tagEntity->setLabel('tag1'); | |
203 | ||
204 | $entry = new Entry(new User()); | |
205 | $entry->addTag($tagEntity); | |
206 | ||
207 | $proxy->assignTagsToEntry($entry, 'tag1, tag2'); | |
208 | ||
209 | $this->assertCount(2, $entry->getTags()); | |
210 | $this->assertEquals('tag1', $entry->getTags()[0]->getLabel()); | |
211 | $this->assertEquals('tag2', $entry->getTags()[1]->getLabel()); | |
212 | } | |
213 | ||
214 | private function getTaggerMock() | |
215 | { | |
216 | return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') | |
217 | ->setMethods(array('tag')) | |
218 | ->disableOriginalConstructor() | |
219 | ->getMock(); | |
220 | } | |
221 | ||
222 | private function getTagRepositoryMock() | |
223 | { | |
224 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | |
225 | ->disableOriginalConstructor() | |
226 | ->getMock(); | |
227 | } | |
228 | ||
229 | private function getLogger() | |
230 | { | |
231 | return new NullLogger(); | |
232 | } | |
233 | } |