View source
<?php
if (class_exists('TUnit')) {
class FeedsExJsonPathUnitTests extends FeedsExUnitTestBase {
protected $source;
public static function getInfo() {
return array(
'name' => 'JSONPath parser unit tests',
'description' => 'Unit tests for FeedsExJsonPath.',
'group' => 'Feeds EX',
);
}
public function setUp() {
parent::setUp();
require_once $this->moduleDir . '/src/FeedsExJsonPath.inc';
$this->source = $this
->getMockFeedsSource();
}
public function testSimpleParsing() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.json'));
$parser
->setConfig(array(
'context' => array(
'value' => '$.items.*',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 3);
foreach ($result->items as $delta => $item) {
$this
->assertEqual('I am a title' . $delta, $item['title']);
$this
->assertEqual('I am a description' . $delta, $item['description']);
}
}
public function testErrorHandling() {
json_decode('\\"asdfasfd');
$parser = $this
->getParserInstance();
$errors = $this
->invokeMethod($parser, 'getErrors');
$this
->assertEqual(3, $errors[0]['severity']);
}
public function testBatchParsing() {
$this
->variableSet('feeds_process_limit', 1);
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.json'));
$parser
->setConfig(array(
'context' => array(
'value' => '$.items.*',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
foreach (range(0, 2) as $delta) {
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 1);
$this
->assertEqual('I am a title' . $delta, $result->items[0]['title']);
$this
->assertEqual('I am a description' . $delta, $result->items[0]['description']);
}
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 0);
}
public function testRootContext() {
$parser = $this
->getParserInstance();
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.json'));
$parser
->setConfig(array(
'context' => array(
'value' => '.',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'items[0].title',
),
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertParserResultItemCount($result, 1);
$this
->assertEqual('I am a title0', $result->items[0]['title']);
}
public function testValidateExpression() {
$parser = $this
->getParserInstance();
$expression = '!! ';
$this
->assertEqual('Unable to parse token !! in expression: .!!', $this
->invokeMethod($parser, 'validateExpression', array(
&$expression,
)));
$this
->assertEqual($expression, '!!', 'Value was trimmed.');
}
public function testInvalidJson() {
$parser = $this
->getParserInstance();
$parser
->setConfig(array(
'context' => array(
'value' => '$.items[asdfasdf]',
),
'sources' => array(),
));
$args = array(
$this->source,
new FeedsFetcherResult('{"items": "not an array"}'),
);
$this
->assertException(array(
$parser,
'parse',
), $args, 'RuntimeException', t('The context expression must return an object or array.'));
$args = array(
$this->source,
new FeedsFetcherResult('invalid json'),
);
$this
->assertException(array(
$parser,
'parse',
), $args, 'RuntimeException', t('The JSON is invalid.'));
$log_messages = $this->source
->getLogMessages();
$this
->assertEqual(count($log_messages), 1);
$this
->assertEqual($log_messages[0]['message'], 'Syntax error');
$this
->assertEqual($log_messages[0]['type'], 'feeds_ex');
$this
->assertEqual($log_messages[0]['severity'], 3);
}
public function testEmptyFeed() {
$parser = $this
->getParserInstance();
$parser
->parse($this->source, new FeedsFetcherResult(' '));
$this
->assertEmptyFeedMessage($parser
->getMessenger()
->getMessages());
}
protected function getParserInstance() {
$parser = FeedsPlugin::instance('FeedsExJsonPath', strtolower($this
->randomName()));
$parser
->setMessenger(new FeedsExTestMessenger());
return $parser;
}
}
}