1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-08-01 17:38:38 +00:00

Move test files directly under tests/ directory

This commit is contained in:
Yassine Guedidi 2024-02-19 00:45:58 +01:00
parent a37b385c23
commit 24da70e338
117 changed files with 4 additions and 4 deletions

View file

@ -0,0 +1,252 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\ChromeImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class ChromeImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
public function testInit()
{
$chromeImport = $this->getChromeImport();
$this->assertSame('Chrome', $chromeImport->getName());
$this->assertNotEmpty($chromeImport->getUrl());
$this->assertSame('import.chrome.description', $chromeImport->getDescription());
}
public function testImport()
{
$chromeImport = $this->getChromeImport(false, 1);
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/chrome-bookmarks');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(1))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn($entry);
$res = $chromeImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$chromeImport = $this->getChromeImport(false, 1);
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/chrome-bookmarks');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(1))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $chromeImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary());
}
public function testImportWithRabbit()
{
$chromeImport = $this->getChromeImport();
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/chrome-bookmarks');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(1))
->method('publish');
$chromeImport->setProducer($producer);
$res = $chromeImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
}
public function testImportWithRedis()
{
$chromeImport = $this->getChromeImport();
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/chrome-bookmarks');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'chrome');
$producer = new Producer($queue);
$chromeImport->setProducer($producer);
$res = $chromeImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('chrome'));
}
public function testImportBadFile()
{
$chromeImport = $this->getChromeImport();
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $chromeImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag Browser Import: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$chromeImport = $this->getChromeImport(true);
$chromeImport->setFilepath(__DIR__ . '/../fixtures/Import/chrome-bookmarks');
$res = $chromeImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag Browser Import: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getChromeImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new ChromeImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,252 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\FirefoxImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class FirefoxImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
public function testInit()
{
$firefoxImport = $this->getFirefoxImport();
$this->assertSame('Firefox', $firefoxImport->getName());
$this->assertNotEmpty($firefoxImport->getUrl());
$this->assertSame('import.firefox.description', $firefoxImport->getDescription());
}
public function testImport()
{
$firefoxImport = $this->getFirefoxImport(false, 2);
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/firefox-bookmarks.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn($entry);
$res = $firefoxImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$firefoxImport = $this->getFirefoxImport(false, 1);
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/firefox-bookmarks.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $firefoxImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary());
}
public function testImportWithRabbit()
{
$firefoxImport = $this->getFirefoxImport();
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/firefox-bookmarks.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(1))
->method('publish');
$firefoxImport->setProducer($producer);
$res = $firefoxImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
}
public function testImportWithRedis()
{
$firefoxImport = $this->getFirefoxImport();
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/firefox-bookmarks.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'firefox');
$producer = new Producer($queue);
$firefoxImport->setProducer($producer);
$res = $firefoxImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('firefox'));
}
public function testImportBadFile()
{
$firefoxImport = $this->getFirefoxImport();
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $firefoxImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag Browser Import: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$firefoxImport = $this->getFirefoxImport(true);
$firefoxImport->setFilepath(__DIR__ . '/../fixtures/Import/firefox-bookmarks.json');
$res = $firefoxImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag Browser Import: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getFirefoxImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new FirefoxImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\Import\ImportChain;
use Wallabag\CoreBundle\Import\ImportInterface;
class ImportChainTest extends TestCase
{
public function testGetAll()
{
$import = $this->getMockBuilder(ImportInterface::class)
->disableOriginalConstructor()
->getMock();
$importChain = new ImportChain();
$importChain->addImport($import, 'alias');
$this->assertCount(1, $importChain->getAll());
$this->assertSame($import, $importChain->getAll()['alias']);
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Wallabag\CoreBundle\Import\ImportChain;
use Wallabag\CoreBundle\Import\ImportCompilerPass;
class ImportCompilerPassTest extends TestCase
{
public function testProcessNoDefinition()
{
$container = new ContainerBuilder();
$res = $this->process($container);
$this->assertNull($res);
}
public function testProcess()
{
$container = new ContainerBuilder();
$container
->register(ImportChain::class)
->setPublic(false)
;
$container
->register('foo')
->addTag('wallabag_core.import', ['alias' => 'pocket'])
;
$this->process($container);
$this->assertTrue($container->hasDefinition(ImportChain::class));
$definition = $container->getDefinition(ImportChain::class);
$this->assertTrue($definition->hasMethodCall('addImport'));
$calls = $definition->getMethodCalls();
$this->assertSame('pocket', $calls[0][1][1]);
}
protected function process(ContainerBuilder $container)
{
$repeatedPass = new ImportCompilerPass();
$repeatedPass->process($container);
}
}

View file

@ -0,0 +1,268 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\InstapaperImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class InstapaperImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
protected $uow;
public function testInit()
{
$instapaperImport = $this->getInstapaperImport();
$this->assertSame('Instapaper', $instapaperImport->getName());
$this->assertNotEmpty($instapaperImport->getUrl());
$this->assertSame('import.instapaper.description', $instapaperImport->getDescription());
}
public function testImport()
{
$instapaperImport = $this->getInstapaperImport(false, 4);
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/instapaper-export.csv');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(4))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(4))
->method('updateEntry')
->willReturn($entry);
$res = $instapaperImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$instapaperImport = $this->getInstapaperImport(false, 1);
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/instapaper-export.csv');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(4))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, true, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->once())
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->once())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $instapaperImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary());
}
public function testImportWithRabbit()
{
$instapaperImport = $this->getInstapaperImport();
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/instapaper-export.csv');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(4))
->method('publish');
$instapaperImport->setProducer($producer);
$res = $instapaperImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
}
public function testImportWithRedis()
{
$instapaperImport = $this->getInstapaperImport();
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/instapaper-export.csv');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'instapaper');
$producer = new Producer($queue);
$instapaperImport->setProducer($producer);
$res = $instapaperImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('instapaper'));
}
public function testImportBadFile()
{
$instapaperImport = $this->getInstapaperImport();
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $instapaperImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('InstapaperImport: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$instapaperImport = $this->getInstapaperImport(true);
$instapaperImport->setFilepath(__DIR__ . '/../fixtures/Import/instapaper-export.csv');
$res = $instapaperImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('InstapaperImport: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getInstapaperImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->uow = $this->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->getMock();
$this->em
->expects($this->any())
->method('getUnitOfWork')
->willReturn($this->uow);
$this->uow
->expects($this->any())
->method('getScheduledEntityInsertions')
->willReturn([]);
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$import = new InstapaperImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$import->setUser($this->user);
}
return $import;
}
}

View file

@ -0,0 +1,254 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\PocketHtmlImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class PocketHtmlImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
public function testInit()
{
$pocketHtmlImport = $this->getPocketHtmlImport();
$this->assertSame('Pocket HTML', $pocketHtmlImport->getName());
$this->assertNotEmpty($pocketHtmlImport->getUrl());
$this->assertSame('import.pocket_html.description', $pocketHtmlImport->getDescription());
}
public function testImport()
{
$pocketHtmlImport = $this->getPocketHtmlImport(false, 2);
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/ril_export.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn($entry);
$res = $pocketHtmlImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $pocketHtmlImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$pocketHtmlImport = $this->getPocketHtmlImport(false, 1);
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/ril_export.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $pocketHtmlImport
->setMarkAsRead(true)
->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $pocketHtmlImport->getSummary());
}
public function testImportWithRabbit()
{
$pocketHtmlImport = $this->getPocketHtmlImport();
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/ril_export.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(2))
->method('publish');
$pocketHtmlImport->setProducer($producer);
$res = $pocketHtmlImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $pocketHtmlImport->getSummary());
}
public function testImportWithRedis()
{
$pocketHtmlImport = $this->getPocketHtmlImport();
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/ril_export.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'pocket_html');
$producer = new Producer($queue);
$pocketHtmlImport->setProducer($producer);
$res = $pocketHtmlImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $pocketHtmlImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('pocket_html'));
}
public function testImportBadFile()
{
$pocketHtmlImport = $this->getPocketHtmlImport();
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $pocketHtmlImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Pocket HTML Import: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$pocketHtmlImport = $this->getPocketHtmlImport(true);
$pocketHtmlImport->setFilepath(__DIR__ . '/../fixtures/Import/ril_export.html');
$res = $pocketHtmlImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Pocket HTML Import: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getPocketHtmlImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new PocketHtmlImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,605 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\PocketImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class PocketImportTest extends TestCase
{
protected $token;
protected $user;
protected $em;
protected $contentProxy;
protected $logHandler;
protected $tagsAssigner;
protected $uow;
public function testInit()
{
$pocketImport = $this->getPocketImport();
$this->assertSame('Pocket', $pocketImport->getName());
$this->assertNotEmpty($pocketImport->getUrl());
$this->assertSame('import.pocket.description', $pocketImport->getDescription());
}
public function testOAuthRequest()
{
$mockHttpClient = new MockHttpClient([new MockResponse(json_encode(['code' => 'wunderbar_code']), ['response_headers' => ['Content-Type: application/json']])]);
$pocketImport = $this->getPocketImport();
$pocketImport->setClient($mockHttpClient);
$code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
$this->assertSame('wunderbar_code', $code);
}
public function testOAuthRequestBadResponse()
{
$mockHttpClient = new MockHttpClient([new MockResponse('', ['http_code' => 403])]);
$pocketImport = $this->getPocketImport();
$pocketImport->setClient($mockHttpClient);
$code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
$this->assertFalse($code);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('PocketImport: Failed to request token', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testOAuthAuthorize()
{
$mockHttpClient = new MockHttpClient([new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']])]);
$pocketImport = $this->getPocketImport();
$pocketImport->setClient($mockHttpClient);
$res = $pocketImport->authorize('wunderbar_code');
$this->assertTrue($res);
$this->assertSame('wunderbar_token', $pocketImport->getAccessToken());
}
public function testOAuthAuthorizeBadResponse()
{
$mockHttpClient = new MockHttpClient([new MockResponse('', ['http_code' => 403])]);
$pocketImport = $this->getPocketImport();
$pocketImport->setClient($mockHttpClient);
$res = $pocketImport->authorize('wunderbar_code');
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('PocketImport: Failed to authorize client', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
/**
* Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
*/
public function testImport()
{
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse(<<<'JSON'
{
"status": 1,
"list": {
"229279689": {
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "1",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 0,
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"is_index": "0",
"has_video": "1",
"has_image": "1",
"word_count": "3197",
"images": {
"1": {
"item_id": "229279689",
"image_id": "1",
"src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
"width": "0",
"height": "0",
"credit": "Jamie Squire/Getty Images",
"caption": ""
}
},
"videos": {
"1": {
"item_id": "229279689",
"video_id": "1",
"src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
"width": "420",
"height": "315",
"type": "1",
"vid": "Er34PbFkVGk"
}
},
"tags": {
"grantland": {
"item_id": "1147652870",
"tag": "grantland"
},
"Ryder Cup": {
"item_id": "1147652870",
"tag": "Ryder Cup"
}
}
},
"229279690": {
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "1",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 1,
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"is_index": "0",
"has_video": "0",
"has_image": "0",
"word_count": "3197"
}
}
}
JSON
, ['response_headers' => ['Content-Type: application/json']]),
]);
$pocketImport = $this->getPocketImport('ConsumerKey', 1);
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->exactly(2))
->method('getRepository')
->willReturn($entryRepo);
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived() && (bool) $persistedEntry->isStarred();
}));
$entry = new Entry($this->user);
$this->contentProxy
->expects($this->once())
->method('updateEntry')
->willReturn($entry);
$pocketImport->setClient($mockHttpClient);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary());
}
/**
* Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
*/
public function testImportAndMarkAllAsRead()
{
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse(<<<'JSON'
{
"status": 1,
"list": {
"229279689": {
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "1",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 0,
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "1",
"has_image": "1",
"word_count": "3197"
},
"229279690": {
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "0",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 1,
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "0",
"has_image": "0",
"word_count": "3197"
}
}
}
JSON
, ['response_headers' => ['Content-Type: application/json']]),
]);
$pocketImport = $this->getPocketImport('ConsumerKey', 2);
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, false));
$this->em
->expects($this->exactly(2))
->method('getRepository')
->willReturn($entryRepo);
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$entry = new Entry($this->user);
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn($entry);
$pocketImport->setClient($mockHttpClient);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $pocketImport->getSummary());
}
/**
* Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
*/
public function testImportWithRabbit()
{
$body = <<<'JSON'
{
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "1",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 0,
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "0",
"has_image": "0",
"word_count": "3197"
}
JSON;
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse(<<<JSON
{
"status": 1,
"list": {
"229279690": $body
}
}
JSON
, ['response_headers' => ['Content-Type: application/json']]),
]);
$pocketImport = $this->getPocketImport();
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = new Entry($this->user);
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$bodyAsArray = json_decode($body, true);
// because with just use `new User()` so it doesn't have an id
$bodyAsArray['userId'] = null;
$producer
->expects($this->once())
->method('publish')
->with(json_encode($bodyAsArray));
$pocketImport->setClient($mockHttpClient);
$pocketImport->setProducer($producer);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary());
}
/**
* Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
*/
public function testImportWithRedis()
{
$body = <<<'JSON'
{
"item_id": "229279689",
"resolved_id": "229279689",
"given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
"favorite": "1",
"status": "1",
"time_added": "1473020899",
"time_updated": "1473020899",
"time_read": "0",
"time_favorited": "0",
"sort_id": 0,
"resolved_title": "The Massive Ryder Cup Preview",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
"excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.",
"is_article": "1",
"has_video": "0",
"has_image": "0",
"word_count": "3197"
}
JSON;
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse(<<<JSON
{
"status": 1,
"list": {
"229279690": $body
}
}
JSON
, ['response_headers' => ['Content-Type: application/json']]),
]);
$pocketImport = $this->getPocketImport();
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = new Entry($this->user);
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'pocket');
$producer = new Producer($queue);
$pocketImport->setClient($mockHttpClient);
$pocketImport->setProducer($producer);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('pocket'));
}
public function testImportBadResponse()
{
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse('', ['http_code' => 403]),
]);
$pocketImport = $this->getPocketImport();
$pocketImport->setClient($mockHttpClient);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('PocketImport: Failed to import', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportWithExceptionFromGraby()
{
$mockHttpClient = new MockHttpClient([
new MockResponse(json_encode(['access_token' => 'wunderbar_token']), ['response_headers' => ['Content-Type: application/json']]),
new MockResponse(<<<'JSON'
{
"status": 1,
"list": {
"229279689": {
"status": "1",
"favorite": "1",
"resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview"
}
}
}
JSON
, ['response_headers' => ['Content-Type: application/json']]),
]);
$pocketImport = $this->getPocketImport('ConsumerKey', 1);
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->once())
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->once())
->method('getRepository')
->willReturn($entryRepo);
$entry = new Entry($this->user);
$this->contentProxy
->expects($this->once())
->method('updateEntry')
->will($this->throwException(new \Exception()));
$pocketImport->setClient($mockHttpClient);
$pocketImport->authorize('wunderbar_code');
$res = $pocketImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary());
}
private function getPocketImport($consumerKey = 'ConsumerKey', $dispatched = 0)
{
$this->user = new User();
$config = new Config($this->user);
$config->setPocketConsumerKey('xxx');
$this->user->setConfig($config);
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->uow = $this->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->getMock();
$this->em
->expects($this->any())
->method('getUnitOfWork')
->willReturn($this->uow);
$this->uow
->expects($this->any())
->method('getScheduledEntityInsertions')
->willReturn([]);
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$pocket = new PocketImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
$pocket->setUser($this->user);
return $pocket;
}
}

View file

@ -0,0 +1,252 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\ReadabilityImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class ReadabilityImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
public function testInit()
{
$readabilityImport = $this->getReadabilityImport();
$this->assertSame('Readability', $readabilityImport->getName());
$this->assertNotEmpty($readabilityImport->getUrl());
$this->assertSame('import.readability.description', $readabilityImport->getDescription());
}
public function testImport()
{
$readabilityImport = $this->getReadabilityImport(false, 3);
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/readability.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(3))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(3))
->method('updateEntry')
->willReturn($entry);
$res = $readabilityImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $readabilityImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$readabilityImport = $this->getReadabilityImport(false, 1);
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/readability-read.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $readabilityImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $readabilityImport->getSummary());
}
public function testImportWithRabbit()
{
$readabilityImport = $this->getReadabilityImport();
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/readability.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(3))
->method('publish');
$readabilityImport->setProducer($producer);
$res = $readabilityImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 3], $readabilityImport->getSummary());
}
public function testImportWithRedis()
{
$readabilityImport = $this->getReadabilityImport();
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/readability.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'readability');
$producer = new Producer($queue);
$readabilityImport->setProducer($producer);
$res = $readabilityImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 3], $readabilityImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('readability'));
}
public function testImportBadFile()
{
$readabilityImport = $this->getReadabilityImport();
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $readabilityImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('ReadabilityImport: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$readabilityImport = $this->getReadabilityImport(true);
$readabilityImport->setFilepath(__DIR__ . '/../fixtures/Import/readability.json');
$res = $readabilityImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('ReadabilityImport: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getReadabilityImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new ReadabilityImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,254 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\ShaarliImport;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class ShaarliImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
public function testInit()
{
$shaarliImport = $this->getShaarliImport();
$this->assertSame('Shaarli', $shaarliImport->getName());
$this->assertNotEmpty($shaarliImport->getUrl());
$this->assertSame('import.shaarli.description', $shaarliImport->getDescription());
}
public function testImport()
{
$shaarliImport = $this->getShaarliImport(false, 2);
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/shaarli-bookmarks.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->willReturn(false);
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn($entry);
$res = $shaarliImport->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $shaarliImport->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$shaarliImport = $this->getShaarliImport(false, 1);
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/shaarli-bookmarks.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $shaarliImport
->setMarkAsRead(true)
->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $shaarliImport->getSummary());
}
public function testImportWithRabbit()
{
$shaarliImport = $this->getShaarliImport();
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/shaarli-bookmarks.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(2))
->method('publish');
$shaarliImport->setProducer($producer);
$res = $shaarliImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $shaarliImport->getSummary());
}
public function testImportWithRedis()
{
$shaarliImport = $this->getShaarliImport();
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/shaarli-bookmarks.html');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'shaarli');
$producer = new Producer($queue);
$shaarliImport->setProducer($producer);
$res = $shaarliImport->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $shaarliImport->getSummary());
$this->assertNotEmpty($redisMock->lpop('shaarli'));
}
public function testImportBadFile()
{
$shaarliImport = $this->getShaarliImport();
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $shaarliImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag HTML Import: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$shaarliImport = $this->getShaarliImport(true);
$shaarliImport->setFilepath(__DIR__ . '/../fixtures/Import/shaarli-bookmarks.html');
$res = $shaarliImport->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('Wallabag HTML Import: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getShaarliImport($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new ShaarliImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,278 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\WallabagV1Import;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class WallabagV1ImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
protected $uow;
protected $fetchingErrorMessageTitle = 'No title found';
protected $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
public function testInit()
{
$wallabagV1Import = $this->getWallabagV1Import();
$this->assertSame('wallabag v1', $wallabagV1Import->getName());
$this->assertNotEmpty($wallabagV1Import->getUrl());
$this->assertSame('import.wallabag_v1.description', $wallabagV1Import->getDescription());
}
public function testImport()
{
$wallabagV1Import = $this->getWallabagV1Import(false, 1);
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, false, false));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->exactly(1))
->method('updateEntry')
->willReturn($entry);
$res = $wallabagV1Import->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$wallabagV1Import = $this->getWallabagV1Import(false, 3);
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1-read.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(3))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, false, false));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(3))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $wallabagV1Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
}
public function testImportWithRabbit()
{
$wallabagV1Import = $this->getWallabagV1Import();
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(2))
->method('publish');
$wallabagV1Import->setProducer($producer);
$res = $wallabagV1Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
}
public function testImportWithRedis()
{
$wallabagV1Import = $this->getWallabagV1Import();
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$entry = $this->getMockBuilder(Entry::class)
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'wallabag_v1');
$producer = new Producer($queue);
$wallabagV1Import->setProducer($producer);
$res = $wallabagV1Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
$this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
}
public function testImportBadFile()
{
$wallabagV1Import = $this->getWallabagV1Import();
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.jsonx');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('WallabagImport: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$wallabagV1Import = $this->getWallabagV1Import(true);
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v1.json');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('WallabagImport: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->uow = $this->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->getMock();
$this->em
->expects($this->any())
->method('getUnitOfWork')
->willReturn($this->uow);
$this->uow
->expects($this->any())
->method('getScheduledEntityInsertions')
->willReturn([]);
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new WallabagV1Import(
$this->em,
$this->contentProxy,
$this->tagsAssigner,
$dispatcher,
$logger,
$this->fetchingErrorMessageTitle,
$this->fetchingErrorMessage
);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}

View file

@ -0,0 +1,296 @@
<?php
namespace Tests\Wallabag\CoreBundle\Import;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use M6Web\Component\RedisMock\RedisMockFactory;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Predis\Client;
use Simpleue\Queue\RedisQueue;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\CoreBundle\Helper\TagsAssigner;
use Wallabag\CoreBundle\Import\WallabagV2Import;
use Wallabag\CoreBundle\Redis\Producer;
use Wallabag\CoreBundle\Repository\EntryRepository;
class WallabagV2ImportTest extends TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
protected $tagsAssigner;
protected $uow;
public function testInit()
{
$wallabagV2Import = $this->getWallabagV2Import();
$this->assertSame('wallabag v2', $wallabagV2Import->getName());
$this->assertNotEmpty($wallabagV2Import->getUrl());
$this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription());
}
public function testImport()
{
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(6))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, false));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn(new Entry($this->user));
$res = $wallabagV2Import->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
}
public function testImportAndMarkAllAsRead()
{
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2-read.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(2))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, false));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->willReturn(new Entry($this->user));
// check that every entry persisted are archived
$this->em
->expects($this->any())
->method('persist')
->with($this->callback(function ($persistedEntry) {
return (bool) $persistedEntry->isArchived();
}));
$res = $wallabagV2Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
}
public function testImportWithRabbit()
{
$wallabagV2Import = $this->getWallabagV2Import();
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$producer = $this->getMockBuilder(\OldSound\RabbitMqBundle\RabbitMq\Producer::class)
->disableOriginalConstructor()
->getMock();
$producer
->expects($this->exactly(6))
->method('publish');
$wallabagV2Import->setProducer($producer);
$res = $wallabagV2Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
}
public function testImportWithRedis()
{
$wallabagV2Import = $this->getWallabagV2Import();
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->never())
->method('findByUrlAndUserId');
$this->em
->expects($this->never())
->method('getRepository');
$this->contentProxy
->expects($this->never())
->method('updateEntry');
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$queue = new RedisQueue($redisMock, 'wallabag_v2');
$producer = new Producer($queue);
$wallabagV2Import->setProducer($producer);
$res = $wallabagV2Import->setMarkAsRead(true)->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
$this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
}
public function testImportBadFile()
{
$wallabagV1Import = $this->getWallabagV2Import();
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.jsonx');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('WallabagImport: unable to read file', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$wallabagV1Import = $this->getWallabagV2Import(true);
$wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertStringContainsString('WallabagImport: user is not defined', $records[0]['message']);
$this->assertSame('ERROR', $records[0]['level_name']);
}
public function testImportEmptyFile()
{
$wallabagV2Import = $this->getWallabagV2Import();
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2-empty.json');
$res = $wallabagV2Import->import();
$this->assertFalse($res);
$this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
}
public function testImportWithExceptionFromGraby()
{
$wallabagV2Import = $this->getWallabagV2Import(false, 2);
$wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/Import/wallabag-v2.json');
$entryRepo = $this->getMockBuilder(EntryRepository::class)
->disableOriginalConstructor()
->getMock();
$entryRepo->expects($this->exactly(6))
->method('findByUrlAndUserId')
->will($this->onConsecutiveCalls(false, true, false));
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
$this->contentProxy
->expects($this->exactly(2))
->method('updateEntry')
->will($this->throwException(new \Exception()));
$res = $wallabagV2Import->import();
$this->assertTrue($res);
$this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
}
private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
{
$this->user = new User();
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->uow = $this->getMockBuilder(UnitOfWork::class)
->disableOriginalConstructor()
->getMock();
$this->em
->expects($this->any())
->method('getUnitOfWork')
->willReturn($this->uow);
$this->uow
->expects($this->any())
->method('getScheduledEntityInsertions')
->willReturn([]);
$this->contentProxy = $this->getMockBuilder(ContentProxy::class)
->disableOriginalConstructor()
->getMock();
$this->tagsAssigner = $this->getMockBuilder(TagsAssigner::class)
->disableOriginalConstructor()
->getMock();
$dispatcher = $this->getMockBuilder(EventDispatcher::class)
->disableOriginalConstructor()
->getMock();
$dispatcher
->expects($this->exactly($dispatched))
->method('dispatch');
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$wallabag = new WallabagV2Import($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher, $logger);
if (false === $unsetUser) {
$wallabag->setUser($this->user);
}
return $wallabag;
}
}