View source
<?php
namespace Drupal\Tests\feeds_ex\Unit\Feeds\Parser;
use Drupal\feeds\Result\FetcherResult;
use Drupal\feeds_ex\Feeds\Parser\JsonPathLinesParser;
use Drupal\feeds_ex\Messenger\TestMessenger;
use Drupal\feeds_ex\Utility\JsonUtility;
class JsonPathLinesParserTest extends ParserTestBase {
protected $fetcherResult;
public function setUp() {
parent::setUp();
$configuration = [
'feed_type' => $this->feedType,
];
$utility = new JsonUtility();
$utility
->setStringTranslation($this
->getStringTranslationStub());
$this->parser = new JsonPathLinesParser($configuration, 'jsonpathlines', [], $utility);
$this->parser
->setStringTranslation($this
->getStringTranslationStub());
$this->parser
->setFeedsExMessenger(new TestMessenger());
$config = [
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'name',
],
],
];
$this->parser
->setConfiguration($config);
$this->fetcherResult = new FetcherResult($this->moduleDir . '/tests/resources/test.jsonl');
}
public function testSimpleParsing() {
$result = $this->parser
->parse($this->feed, $this->fetcherResult, $this->state);
$this
->assertCount(4, $result);
foreach ([
'Gilbert',
'Alexa',
'May',
'Deloise',
] as $delta => $name) {
$this
->assertSame($name, $result[$delta]
->get('title'));
}
}
public function testBatching() {
$config = [
'sources' => [
'title' => [
'name' => 'Title',
'value' => 'name',
],
],
'line_limit' => 1,
];
$this->parser
->setConfiguration($config);
foreach ([
'Gilbert',
'Alexa',
'May',
'Deloise',
] as $name) {
$result = $this->parser
->parse($this->feed, $this->fetcherResult, $this->state);
$this
->assertCount(1, $result);
$this
->assertSame($result[0]
->get('title'), $name);
}
$result = $this->parser
->parse($this->feed, $this->fetcherResult, $this->state);
$this
->assertCount(0, $result);
}
public function testEmptyFeed() {
$this->parser
->parse($this->feed, new FetcherResult($this->moduleDir . '/tests/resources/empty.txt'), $this->state);
$this
->assertEmptyFeedMessage($this->parser
->getMessenger()
->getMessages());
}
}