SitemapParserTest.php in Feeds 8.3
File
tests/src/Unit/Feeds/Parser/SitemapParserTest.php
View source
<?php
namespace Drupal\Tests\feeds\Unit\Feeds\Parser;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\Feeds\Parser\SitemapParser;
use Drupal\feeds\Result\RawFetcherResult;
use Drupal\feeds\State;
use Drupal\Tests\feeds\Unit\FeedsUnitTestCase;
use Exception;
class SitemapParserTest extends FeedsUnitTestCase {
protected $parser;
protected $feedType;
protected $feed;
protected $state;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createMock('Drupal\\feeds\\FeedTypeInterface');
$configuration = [
'feed_type' => $this->feedType,
];
$this->parser = new SitemapParser($configuration, 'sitemap', []);
$this->parser
->setStringTranslation($this
->getStringTranslationStub());
$this->state = new State();
$this->feed = $this
->createMock('Drupal\\feeds\\FeedInterface');
$this->feed
->expects($this
->any())
->method('getType')
->will($this
->returnValue($this->feedType));
}
public function testParse() {
$file = dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/tests/resources/sitemap-example.xml';
$fetcher_result = new RawFetcherResult(file_get_contents($file), $this
->getMockFileSystem());
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
$this
->assertSame(count($result), 5);
$this
->assertSame($result[0]
->get('url'), 'http://www.example.com/');
$this
->assertSame($result[3]
->get('priority'), '0.3');
}
public function testInvalidFeed() {
$fetcher_result = new RawFetcherResult('beep boop', $this
->getMockFileSystem());
$this
->expectException(Exception::class);
$result = $this->parser
->parse($this->feed, $fetcher_result, $this->state);
}
public function testEmptyFeed() {
$result = new RawFetcherResult('', $this
->getMockFileSystem());
$this
->expectException(EmptyFeedException::class);
$this->parser
->parse($this->feed, $result, $this->state);
}
public function testGetMappingSources() {
$this
->assertSame(count($this->parser
->getMappingSources()), 4);
}
}