aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-03-27 20:35:56 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-03-27 20:54:57 +0200
commit4d0ec0e72108ff47952906e5d968a7c3eb0a76f9 (patch)
treeb8e9cd86010368631296c22a2352f5dbedb429a1 /src/Wallabag/CoreBundle
parent5d6f6f56a2a0f72a67c2d3f96eb61986cf821a1e (diff)
downloadwallabag-4d0ec0e72108ff47952906e5d968a7c3eb0a76f9.tar.gz
wallabag-4d0ec0e72108ff47952906e5d968a7c3eb0a76f9.tar.zst
wallabag-4d0ec0e72108ff47952906e5d968a7c3eb0a76f9.zip
Fix some Scrutinizer issues
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r--src/Wallabag/CoreBundle/Command/InstallCommand.php23
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php4
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php34
-rw-r--r--src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php85
4 files changed, 127 insertions, 19 deletions
diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php
index c9dad0df..2d73a9ad 100644
--- a/src/Wallabag/CoreBundle/Command/InstallCommand.php
+++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php
@@ -83,7 +83,8 @@ class InstallCommand extends ContainerAwareCommand
83 $help = 'Needs one of sqlite, mysql or pgsql PDO drivers'; 83 $help = 'Needs one of sqlite, mysql or pgsql PDO drivers';
84 } 84 }
85 85
86 $rows[] = array($label, $status, $help); 86 $rows = [];
87 $rows[] = [$label, $status, $help];
87 88
88 foreach ($this->functionExists as $functionRequired) { 89 foreach ($this->functionExists as $functionRequired) {
89 $label = '<comment>'.$functionRequired.'</comment>'; 90 $label = '<comment>'.$functionRequired.'</comment>';
@@ -97,12 +98,12 @@ class InstallCommand extends ContainerAwareCommand
97 $help = 'You need the '.$functionRequired.' function activated'; 98 $help = 'You need the '.$functionRequired.' function activated';
98 } 99 }
99 100
100 $rows[] = array($label, $status, $help); 101 $rows[] = [$label, $status, $help];
101 } 102 }
102 103
103 $table = new Table($this->defaultOutput); 104 $table = new Table($this->defaultOutput);
104 $table 105 $table
105 ->setHeaders(array('Checked', 'Status', 'Recommendation')) 106 ->setHeaders(['Checked', 'Status', 'Recommendation'])
106 ->setRows($rows) 107 ->setRows($rows)
107 ->render(); 108 ->render();
108 109
@@ -126,7 +127,7 @@ class InstallCommand extends ContainerAwareCommand
126 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache'); 127 $this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
127 128
128 $this 129 $this
129 ->runCommand('doctrine:database:drop', array('--force' => true)) 130 ->runCommand('doctrine:database:drop', ['--force' => true])
130 ->runCommand('doctrine:database:create') 131 ->runCommand('doctrine:database:create')
131 ->runCommand('doctrine:schema:create') 132 ->runCommand('doctrine:schema:create')
132 ->runCommand('cache:clear') 133 ->runCommand('cache:clear')
@@ -158,7 +159,7 @@ class InstallCommand extends ContainerAwareCommand
158 $this->defaultOutput->writeln('Droping database, creating database and schema'); 159 $this->defaultOutput->writeln('Droping database, creating database and schema');
159 160
160 $this 161 $this
161 ->runCommand('doctrine:database:drop', array('--force' => true)) 162 ->runCommand('doctrine:database:drop', ['--force' => true])
162 ->runCommand('doctrine:database:create') 163 ->runCommand('doctrine:database:create')
163 ->runCommand('doctrine:schema:create') 164 ->runCommand('doctrine:schema:create')
164 ; 165 ;
@@ -168,7 +169,7 @@ class InstallCommand extends ContainerAwareCommand
168 $this->defaultOutput->writeln('Droping schema and creating schema'); 169 $this->defaultOutput->writeln('Droping schema and creating schema');
169 170
170 $this 171 $this
171 ->runCommand('doctrine:schema:drop', array('--force' => true)) 172 ->runCommand('doctrine:schema:drop', ['--force' => true])
172 ->runCommand('doctrine:schema:create') 173 ->runCommand('doctrine:schema:create')
173 ; 174 ;
174 } 175 }
@@ -388,19 +389,19 @@ class InstallCommand extends ContainerAwareCommand
388 * @param string $command 389 * @param string $command
389 * @param array $parameters Parameters to this command (usually 'force' => true) 390 * @param array $parameters Parameters to this command (usually 'force' => true)
390 */ 391 */
391 protected function runCommand($command, $parameters = array()) 392 protected function runCommand($command, $parameters = [])
392 { 393 {
393 $parameters = array_merge( 394 $parameters = array_merge(
394 array('command' => $command), 395 ['command' => $command],
395 $parameters, 396 $parameters,
396 array( 397 [
397 '--no-debug' => true, 398 '--no-debug' => true,
398 '--env' => $this->defaultInput->getOption('env') ?: 'dev', 399 '--env' => $this->defaultInput->getOption('env') ?: 'dev',
399 ) 400 ]
400 ); 401 );
401 402
402 if ($this->defaultInput->getOption('no-interaction')) { 403 if ($this->defaultInput->getOption('no-interaction')) {
403 $parameters = array_merge($parameters, array('--no-interaction' => true)); 404 $parameters = array_merge($parameters, ['--no-interaction' => true]);
404 } 405 }
405 406
406 $this->getApplication()->setAutoExit(false); 407 $this->getApplication()->setAutoExit(false);
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 1a0b80ac..fa633031 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -92,13 +92,11 @@ class EntryController extends Controller
92 } 92 }
93 93
94 /** 94 /**
95 * @param Request $request
96 *
97 * @Route("/new", name="new") 95 * @Route("/new", name="new")
98 * 96 *
99 * @return \Symfony\Component\HttpFoundation\Response 97 * @return \Symfony\Component\HttpFoundation\Response
100 */ 98 */
101 public function addEntryAction(Request $request) 99 public function addEntryAction()
102 { 100 {
103 return $this->render('WallabagCoreBundle:Entry:new.html.twig'); 101 return $this->render('WallabagCoreBundle:Entry:new.html.twig');
104 } 102 }
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index ba90b731..ed4a220d 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -32,14 +32,21 @@ class ContentProxy
32 * Fetch content using graby and hydrate given entry with results information. 32 * Fetch content using graby and hydrate given entry with results information.
33 * In case we couldn't find content, we'll try to use Open Graph data. 33 * In case we couldn't find content, we'll try to use Open Graph data.
34 * 34 *
35 * @param Entry $entry Entry to update 35 * We can also force the content, in case of an import from the v1 for example, so the function won't
36 * @param string $url Url to grab content for 36 * fetch the content from the website but rather use information given with the $content parameter.
37 *
38 * @param Entry $entry Entry to update
39 * @param string $url Url to grab content for
40 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
37 * 41 *
38 * @return Entry 42 * @return Entry
39 */ 43 */
40 public function updateEntry(Entry $entry, $url) 44 public function updateEntry(Entry $entry, $url, array $content = [])
41 { 45 {
42 $content = $this->graby->fetchContent($url); 46 // do we have to fetch the content or the provided one is ok?
47 if (empty($content) || false === $this->validateContent($content)) {
48 $content = $this->graby->fetchContent($url);
49 }
43 50
44 $title = $content['title']; 51 $title = $content['title'];
45 if (!$title && isset($content['open_graph']['og_title'])) { 52 if (!$title && isset($content['open_graph']['og_title'])) {
@@ -62,7 +69,11 @@ class ContentProxy
62 $entry->setLanguage($content['language']); 69 $entry->setLanguage($content['language']);
63 $entry->setMimetype($content['content_type']); 70 $entry->setMimetype($content['content_type']);
64 $entry->setReadingTime(Utils::getReadingTime($html)); 71 $entry->setReadingTime(Utils::getReadingTime($html));
65 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); 72
73 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
74 if (false !== $domainName) {
75 $entry->setDomainName($domainName);
76 }
66 77
67 if (isset($content['open_graph']['og_image'])) { 78 if (isset($content['open_graph']['og_image'])) {
68 $entry->setPreviewPicture($content['open_graph']['og_image']); 79 $entry->setPreviewPicture($content['open_graph']['og_image']);
@@ -113,4 +124,17 @@ class ContentProxy
113 } 124 }
114 } 125 }
115 } 126 }
127
128 /**
129 * Validate that the given content as enough value to be used
130 * instead of fetch the content from the url.
131 *
132 * @param array $content
133 *
134 * @return bool true if valid otherwise false
135 */
136 private function validateContent(array $content)
137 {
138 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
139 }
116} 140}
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
index f58b5828..74bfb054 100644
--- a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
+++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
@@ -10,6 +10,40 @@ use Wallabag\UserBundle\Entity\User;
10 10
11class ContentProxyTest extends \PHPUnit_Framework_TestCase 11class ContentProxyTest extends \PHPUnit_Framework_TestCase
12{ 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(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://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
13 public function testWithEmptyContent() 47 public function testWithEmptyContent()
14 { 48 {
15 $tagger = $this->getTaggerMock(); 49 $tagger = $this->getTaggerMock();
@@ -121,6 +155,57 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
121 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 155 $this->assertEquals('1.1.1.1', $entry->getDomainName());
122 } 156 }
123 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
124 public function testAssignTagsWithArrayAndExtraSpaces() 209 public function testAssignTagsWithArrayAndExtraSpaces()
125 { 210 {
126 $graby = $this->getMockBuilder('Graby\Graby') 211 $graby = $this->getMockBuilder('Graby\Graby')