1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

Fix some Scrutinizer issues

This commit is contained in:
Jeremy Benoist 2016-03-27 20:35:56 +02:00
parent 5d6f6f56a2
commit 4d0ec0e721
9 changed files with 183 additions and 32 deletions

View file

@ -83,7 +83,8 @@ class InstallCommand extends ContainerAwareCommand
$help = 'Needs one of sqlite, mysql or pgsql PDO drivers';
}
$rows[] = array($label, $status, $help);
$rows = [];
$rows[] = [$label, $status, $help];
foreach ($this->functionExists as $functionRequired) {
$label = '<comment>'.$functionRequired.'</comment>';
@ -97,12 +98,12 @@ class InstallCommand extends ContainerAwareCommand
$help = 'You need the '.$functionRequired.' function activated';
}
$rows[] = array($label, $status, $help);
$rows[] = [$label, $status, $help];
}
$table = new Table($this->defaultOutput);
$table
->setHeaders(array('Checked', 'Status', 'Recommendation'))
->setHeaders(['Checked', 'Status', 'Recommendation'])
->setRows($rows)
->render();
@ -126,7 +127,7 @@ class InstallCommand extends ContainerAwareCommand
$this->defaultOutput->writeln('Droping database, creating database and schema, clearing the cache');
$this
->runCommand('doctrine:database:drop', array('--force' => true))
->runCommand('doctrine:database:drop', ['--force' => true])
->runCommand('doctrine:database:create')
->runCommand('doctrine:schema:create')
->runCommand('cache:clear')
@ -158,7 +159,7 @@ class InstallCommand extends ContainerAwareCommand
$this->defaultOutput->writeln('Droping database, creating database and schema');
$this
->runCommand('doctrine:database:drop', array('--force' => true))
->runCommand('doctrine:database:drop', ['--force' => true])
->runCommand('doctrine:database:create')
->runCommand('doctrine:schema:create')
;
@ -168,7 +169,7 @@ class InstallCommand extends ContainerAwareCommand
$this->defaultOutput->writeln('Droping schema and creating schema');
$this
->runCommand('doctrine:schema:drop', array('--force' => true))
->runCommand('doctrine:schema:drop', ['--force' => true])
->runCommand('doctrine:schema:create')
;
}
@ -388,19 +389,19 @@ class InstallCommand extends ContainerAwareCommand
* @param string $command
* @param array $parameters Parameters to this command (usually 'force' => true)
*/
protected function runCommand($command, $parameters = array())
protected function runCommand($command, $parameters = [])
{
$parameters = array_merge(
array('command' => $command),
['command' => $command],
$parameters,
array(
[
'--no-debug' => true,
'--env' => $this->defaultInput->getOption('env') ?: 'dev',
)
]
);
if ($this->defaultInput->getOption('no-interaction')) {
$parameters = array_merge($parameters, array('--no-interaction' => true));
$parameters = array_merge($parameters, ['--no-interaction' => true]);
}
$this->getApplication()->setAutoExit(false);

View file

@ -92,13 +92,11 @@ class EntryController extends Controller
}
/**
* @param Request $request
*
* @Route("/new", name="new")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addEntryAction(Request $request)
public function addEntryAction()
{
return $this->render('WallabagCoreBundle:Entry:new.html.twig');
}

View file

@ -32,14 +32,21 @@ class ContentProxy
* Fetch content using graby and hydrate given entry with results information.
* In case we couldn't find content, we'll try to use Open Graph data.
*
* @param Entry $entry Entry to update
* @param string $url Url to grab content for
* We can also force the content, in case of an import from the v1 for example, so the function won't
* fetch the content from the website but rather use information given with the $content parameter.
*
* @param Entry $entry Entry to update
* @param string $url Url to grab content for
* @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
*
* @return Entry
*/
public function updateEntry(Entry $entry, $url)
public function updateEntry(Entry $entry, $url, array $content = [])
{
$content = $this->graby->fetchContent($url);
// do we have to fetch the content or the provided one is ok?
if (empty($content) || false === $this->validateContent($content)) {
$content = $this->graby->fetchContent($url);
}
$title = $content['title'];
if (!$title && isset($content['open_graph']['og_title'])) {
@ -62,7 +69,11 @@ class ContentProxy
$entry->setLanguage($content['language']);
$entry->setMimetype($content['content_type']);
$entry->setReadingTime(Utils::getReadingTime($html));
$entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
$domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
if (false !== $domainName) {
$entry->setDomainName($domainName);
}
if (isset($content['open_graph']['og_image'])) {
$entry->setPreviewPicture($content['open_graph']['og_image']);
@ -113,4 +124,17 @@ class ContentProxy
}
}
}
/**
* Validate that the given content as enough value to be used
* instead of fetch the content from the url.
*
* @param array $content
*
* @return bool true if valid otherwise false
*/
private function validateContent(array $content)
{
return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
}
}

View file

@ -10,6 +10,40 @@ use Wallabag\UserBundle\Entity\User;
class ContentProxyTest extends \PHPUnit_Framework_TestCase
{
public function testWithBadUrl()
{
$tagger = $this->getTaggerMock();
$tagger->expects($this->once())
->method('tag');
$graby = $this->getMockBuilder('Graby\Graby')
->setMethods(array('fetchContent'))
->disableOriginalConstructor()
->getMock();
$graby->expects($this->any())
->method('fetchContent')
->willReturn(array(
'html' => false,
'title' => '',
'url' => '',
'content_type' => '',
'language' => '',
));
$proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
$entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
$this->assertEquals('http://user@:80', $entry->getUrl());
$this->assertEmpty($entry->getTitle());
$this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getMimetype());
$this->assertEmpty($entry->getLanguage());
$this->assertEquals(0.0, $entry->getReadingTime());
$this->assertEquals(false, $entry->getDomainName());
}
public function testWithEmptyContent()
{
$tagger = $this->getTaggerMock();
@ -121,6 +155,57 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('1.1.1.1', $entry->getDomainName());
}
public function testWithForcedContent()
{
$tagger = $this->getTaggerMock();
$tagger->expects($this->once())
->method('tag');
$graby = $this->getMockBuilder('Graby\Graby')->getMock();
$proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
'html' => str_repeat('this is my content', 325),
'title' => 'this is my title',
'url' => 'http://1.1.1.1',
'content_type' => 'text/html',
'language' => 'fr',
]);
$this->assertEquals('http://1.1.1.1', $entry->getUrl());
$this->assertEquals('this is my title', $entry->getTitle());
$this->assertContains('this is my content', $entry->getContent());
$this->assertEquals('text/html', $entry->getMimetype());
$this->assertEquals('fr', $entry->getLanguage());
$this->assertEquals(4.0, $entry->getReadingTime());
$this->assertEquals('1.1.1.1', $entry->getDomainName());
}
public function testTaggerThrowException()
{
$graby = $this->getMockBuilder('Graby\Graby')
->disableOriginalConstructor()
->getMock();
$tagger = $this->getTaggerMock();
$tagger->expects($this->once())
->method('tag')
->will($this->throwException(new \Exception()));
$tagRepo = $this->getTagRepositoryMock();
$proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger());
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
'html' => str_repeat('this is my content', 325),
'title' => 'this is my title',
'url' => 'http://1.1.1.1',
'content_type' => 'text/html',
'language' => 'fr',
]);
$this->assertCount(0, $entry->getTags());
}
public function testAssignTagsWithArrayAndExtraSpaces()
{
$graby = $this->getMockBuilder('Graby\Graby')