wallabag/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php

112 lines
3.5 KiB
PHP
Raw Normal View History

2015-12-30 13:26:30 +01:00
<?php
namespace Wallabag\ImportBundle\Tests\Import;
use Wallabag\UserBundle\Entity\User;
use Wallabag\ImportBundle\Import\WallabagV1Import;
use Monolog\Logger;
use Monolog\Handler\TestHandler;
class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
{
protected $user;
protected $em;
protected $logHandler;
protected $contentProxy;
2015-12-30 13:26:30 +01:00
private function getWallabagV1Import($unsetUser = false)
{
$this->user = new User();
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
->disableOriginalConstructor()
->getMock();
2016-02-11 13:39:21 +01:00
$wallabag = new WallabagV1Import($this->em, $this->contentProxy);
2015-12-30 13:26:30 +01:00
$this->logHandler = new TestHandler();
$logger = new Logger('test', array($this->logHandler));
$wallabag->setLogger($logger);
2015-12-30 13:26:30 +01:00
if (false === $unsetUser) {
$wallabag->setUser($this->user);
2015-12-30 13:26:30 +01:00
}
return $wallabag;
2015-12-30 13:26:30 +01:00
}
public function testInit()
{
$wallabagV1Import = $this->getWallabagV1Import();
2016-01-06 06:34:57 +01:00
$this->assertEquals('wallabag v1', $wallabagV1Import->getName());
$this->assertNotEmpty($wallabagV1Import->getUrl());
$this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
2015-12-30 13:26:30 +01:00
}
public function testImport()
{
$wallabagV1Import = $this->getWallabagV1Import();
$wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
$entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
->disableOriginalConstructor()
->getMock();
2016-02-11 15:48:20 +01:00
$entryRepo->expects($this->exactly(4))
->method('findByUrlAndUserId')
2016-02-11 15:48:20 +01:00
->will($this->onConsecutiveCalls(false, true, false, false));
2015-12-30 13:26:30 +01:00
$this->em
->expects($this->any())
->method('getRepository')
->willReturn($entryRepo);
2016-02-11 15:48:20 +01:00
$entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
->disableOriginalConstructor()
->getMock();
$this->contentProxy
->expects($this->once())
->method('updateEntry')
->willReturn($entry);
2015-12-30 13:26:30 +01:00
$res = $wallabagV1Import->import();
$this->assertTrue($res);
2016-02-11 15:48:20 +01:00
$this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary());
2015-12-30 13:26:30 +01:00
}
public function testImportBadFile()
{
$wallabagV1Import = $this->getWallabagV1Import();
$wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
2015-12-30 13:26:30 +01:00
$this->assertEquals('ERROR', $records[0]['level_name']);
}
public function testImportUserNotDefined()
{
$wallabagV1Import = $this->getWallabagV1Import(true);
$wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
$res = $wallabagV1Import->import();
$this->assertFalse($res);
$records = $this->logHandler->getRecords();
$this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
2015-12-30 13:26:30 +01:00
$this->assertEquals('ERROR', $records[0]['level_name']);
}
}