namespace Tests\Wallabag\CoreBundle\Helper;
-use Psr\Log\NullLogger;
-use Monolog\Logger;
+use Graby\Graby;
use Monolog\Handler\TestHandler;
-use Wallabag\CoreBundle\Helper\ContentProxy;
+use Monolog\Logger;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\NullLogger;
+use Symfony\Component\Validator\ConstraintViolation;
+use Symfony\Component\Validator\ConstraintViolationList;
+use Symfony\Component\Validator\Validator\RecursiveValidator;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
-use Wallabag\UserBundle\Entity\User;
+use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
-use Graby\Graby;
-use Symfony\Component\Validator\Validator\RecursiveValidator;
-use Symfony\Component\Validator\ConstraintViolationList;
-use Symfony\Component\Validator\ConstraintViolation;
+use Wallabag\UserBundle\Entity\User;
-class ContentProxyTest extends \PHPUnit_Framework_TestCase
+class ContentProxyTest extends TestCase
{
private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://user@:80');
- $this->assertEquals('http://user@:80', $entry->getUrl());
+ $this->assertSame('http://user@:80', $entry->getUrl());
$this->assertEmpty($entry->getTitle());
- $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
+ $this->assertSame($this->fetchingErrorMessage, $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());
+ $this->assertSame(0.0, $entry->getReadingTime());
+ $this->assertNull($entry->getDomainName());
}
public function testWithEmptyContent()
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://0.0.0.0');
- $this->assertEquals('http://0.0.0.0', $entry->getUrl());
+ $this->assertSame('http://0.0.0.0', $entry->getUrl());
$this->assertEmpty($entry->getTitle());
- $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
+ $this->assertSame($this->fetchingErrorMessage, $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getMimetype());
$this->assertEmpty($entry->getLanguage());
- $this->assertEquals(0.0, $entry->getReadingTime());
- $this->assertEquals('0.0.0.0', $entry->getDomainName());
+ $this->assertSame(0.0, $entry->getReadingTime());
+ $this->assertSame('0.0.0.0', $entry->getDomainName());
}
public function testWithEmptyContentButOG()
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://domain.io');
- $this->assertEquals('http://domain.io', $entry->getUrl());
- $this->assertEquals('my title', $entry->getTitle());
- $this->assertEquals($this->fetchingErrorMessage.'<p><i>But we found a short description: </i></p>desc', $entry->getContent());
+ $this->assertSame('http://domain.io', $entry->getUrl());
+ $this->assertSame('my title', $entry->getTitle());
+ $this->assertSame($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getLanguage());
$this->assertEmpty($entry->getHttpStatus());
$this->assertEmpty($entry->getMimetype());
- $this->assertEquals(0.0, $entry->getReadingTime());
- $this->assertEquals('domain.io', $entry->getDomainName());
+ $this->assertSame(0.0, $entry->getReadingTime());
+ $this->assertSame('domain.io', $entry->getDomainName());
}
public function testWithContent()
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://0.0.0.0');
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
$this->assertContains('this is my content', $entry->getContent());
- $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
- $this->assertEquals('text/html', $entry->getMimetype());
- $this->assertEquals('fr', $entry->getLanguage());
- $this->assertEquals('200', $entry->getHttpStatus());
- $this->assertEquals(4.0, $entry->getReadingTime());
- $this->assertEquals('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
}
public function testWithContentAndNoOgImage()
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://0.0.0.0');
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
$this->assertContains('this is my content', $entry->getContent());
$this->assertNull($entry->getPreviewPicture());
- $this->assertEquals('text/html', $entry->getMimetype());
- $this->assertEquals('fr', $entry->getLanguage());
- $this->assertEquals('200', $entry->getHttpStatus());
- $this->assertEquals(4.0, $entry->getReadingTime());
- $this->assertEquals('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
}
public function testWithContentAndBadLanguage()
$tagger->expects($this->once())
->method('tag');
- $validator = $this->getValidator();
- $validator->expects($this->exactly(2))
+ $validator = $this->getValidator(false);
+ $validator->expects($this->once())
->method('validate')
- ->will($this->onConsecutiveCalls(
- new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')]),
- new ConstraintViolationList()
- ));
+ ->willReturn(new ConstraintViolationList([new ConstraintViolation('oops', 'oops', [], 'oops', 'language', 'dontexist')]));
$graby = $this->getMockBuilder('Graby\Graby')
->setMethods(['fetchContent'])
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://0.0.0.0');
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
$this->assertContains('this is my content', $entry->getContent());
- $this->assertEquals('text/html', $entry->getMimetype());
+ $this->assertSame('text/html', $entry->getMimetype());
$this->assertNull($entry->getLanguage());
- $this->assertEquals('200', $entry->getHttpStatus());
- $this->assertEquals(4.0, $entry->getReadingTime());
- $this->assertEquals('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
}
public function testWithContentAndBadOgImage()
$tagger->expects($this->once())
->method('tag');
- $validator = $this->getValidator();
+ $validator = $this->getValidator(false);
$validator->expects($this->exactly(2))
->method('validate')
->will($this->onConsecutiveCalls(
$entry = new Entry(new User());
$proxy->updateEntry($entry, 'http://0.0.0.0');
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
$this->assertContains('this is my content', $entry->getContent());
$this->assertNull($entry->getPreviewPicture());
- $this->assertEquals('text/html', $entry->getMimetype());
- $this->assertEquals('fr', $entry->getLanguage());
- $this->assertEquals('200', $entry->getHttpStatus());
- $this->assertEquals(4.0, $entry->getReadingTime());
- $this->assertEquals('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
}
public function testWithForcedContent()
$tagger->expects($this->once())
->method('tag');
- $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
+ $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true);
$entry = new Entry(new User());
$proxy->updateEntry(
$entry,
]
);
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('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());
- $this->assertEquals('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('24/03/2014', $entry->getPublishedAt()->format('d/m/Y'));
$this->assertContains('Jeremy', $entry->getPublishedBy());
$this->assertContains('Nico', $entry->getPublishedBy());
$this->assertContains('Thomas', $entry->getPublishedBy());
+ $this->assertNotNull($entry->getHeaders(), 'Headers are stored, so value is not null');
$this->assertContains('no-cache', $entry->getHeaders());
}
]
);
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('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());
- $this->assertEquals('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('08/09/2016', $entry->getPublishedAt()->format('d/m/Y'));
}
public function testWithForcedContentAndBadDate()
]
);
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('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());
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame(4.0, $entry->getReadingTime());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
$this->assertNull($entry->getPublishedAt());
$records = $handler->getRecords();
]
);
- $this->assertEquals('http://1.1.1.1', $entry->getUrl());
- $this->assertEquals('this is my title', $entry->getTitle());
+ $this->assertSame('http://1.1.1.1', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
$this->assertNotContains($escapedString, $entry->getContent());
- $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
- $this->assertEquals('text/html', $entry->getMimetype());
- $this->assertEquals('fr', $entry->getLanguage());
- $this->assertEquals('200', $entry->getHttpStatus());
- $this->assertEquals('1.1.1.1', $entry->getDomainName());
+ $this->assertSame('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
+ $this->assertSame('text/html', $entry->getMimetype());
+ $this->assertSame('fr', $entry->getLanguage());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
+ }
+
+ public function testWithImageAsContent()
+ {
+ $tagger = $this->getTaggerMock();
+ $tagger->expects($this->once())
+ ->method('tag');
+
+ $graby = $this->getMockBuilder('Graby\Graby')
+ ->setMethods(['fetchContent'])
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $graby->expects($this->any())
+ ->method('fetchContent')
+ ->willReturn([
+ 'html' => '<p><img src="http://1.1.1.1/image.jpg" /></p>',
+ 'title' => 'this is my title',
+ 'url' => 'http://1.1.1.1/image.jpg',
+ 'content_type' => 'image/jpeg',
+ 'status' => '200',
+ 'open_graph' => [],
+ ]);
+
+ $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage);
+ $entry = new Entry(new User());
+ $proxy->updateEntry($entry, 'http://0.0.0.0');
+
+ $this->assertSame('http://1.1.1.1/image.jpg', $entry->getUrl());
+ $this->assertSame('this is my title', $entry->getTitle());
+ $this->assertContains('http://1.1.1.1/image.jpg', $entry->getContent());
+ $this->assertSame('http://1.1.1.1/image.jpg', $entry->getPreviewPicture());
+ $this->assertSame('image/jpeg', $entry->getMimetype());
+ $this->assertSame('200', $entry->getHttpStatus());
+ $this->assertSame('1.1.1.1', $entry->getDomainName());
}
private function getTaggerMock()
return new NullLogger();
}
- private function getValidator()
+ private function getValidator($withDefaultMock = true)
{
- return $this->getMockBuilder(RecursiveValidator::class)
+ $mock = $this->getMockBuilder(RecursiveValidator::class)
->setMethods(['validate'])
->disableOriginalConstructor()
->getMock();
+
+ if ($withDefaultMock) {
+ $mock->expects($this->any())
+ ->method('validate')
+ ->willReturn(new ConstraintViolationList());
+ }
+
+ return $mock;
}
}