.
*
*/
namespace OCA\News\Utility\ArticleEnhancer;
use \OCA\News\Db\Item;
require_once(__DIR__ . "/../../../classloader.php");
class TestEnhancer extends ArticleEnhancer {
public function __construct($purifier, $fileFactory, $articleRegex,
$articleXPATH, $timeout){
parent::__construct($purifier, $fileFactory, $articleRegex,
$articleXPATH, $timeout);
}
}
class ArticleEnhancerTest extends \OCA\AppFramework\Utility\TestUtility {
private $purifier;
private $testEnhancer;
private $fileFactory;
private $timeout;
protected function setUp() {
$timeout = 30;
$this->fileFactory = $this->getMockBuilder('\OCA\News\Utility\SimplePieFileFactory')
->disableOriginalConstructor()
->getMock();
$this->purifier = $this->getMock('purifier', array('purify'));
$this->testEnhancer = new TestEnhancer(
$this->purifier,
$this->fileFactory,
'/explosm.net\/comics/',
'//*[@id=\'maincontent\']/div[2]/img',
$this->timeout
);
}
public function testDoesNotModifiyNotMatchingResults() {
$item = new Item();
$item->setUrl('http://explosm.net');
$this->assertEquals($item, $this->testEnhancer->enhance($item));
}
public function testDoesModifiyArticlesThatMatch() {
$file = new \stdClass;
$file->body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->fileFactory->expects($this->once())
->method('getFile')
->with($this->equalTo($item->getUrl()),
$this->equalTo($this->timeout))
->will($this->returnValue($file));
$this->purifier->expects($this->once())
->method('purify')
->with($this->equalTo(''))
->will($this->returnValue(''));
$result = $this->testEnhancer->enhance($item);
$this->assertEquals('', $result->getBody());
}
public function testModificationHandlesEmptyResults() {
$file = new \stdClass;
$file->body = '
';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->fileFactory->expects($this->once())
->method('getFile')
->with($this->equalTo($item->getUrl()),
$this->equalTo($this->timeout))
->will($this->returnValue($file));
$this->purifier->expects($this->once())
->method('purify')
->with($this->equalTo(null))
->will($this->returnValue(null));
$result = $this->testEnhancer->enhance($item);
$this->assertEquals(null, $result->getBody());
}
public function testModificationDoesNotBreakOnEmptyDom() {
$file = new \stdClass;
$file->body = '';
$item = new Item();
$item->setUrl('https://www.explosm.net/comics/312');
$item->setBody('Hello thar');
$this->fileFactory->expects($this->once())
->method('getFile')
->with($this->equalTo($item->getUrl()),
$this->equalTo($this->timeout))
->will($this->returnValue($file));
$this->purifier->expects($this->once())
->method('purify')
->with($this->equalTo(null))
->will($this->returnValue(null));
$result = $this->testEnhancer->enhance($item);
$this->assertEquals(null, $result->getBody());
}
public function testModificationDoesNotBreakOnBrokenDom() {
$file = new \stdClass;
$file->body = '