class FeedsExJmesPathUnitTests in Feeds extensible parsers 7.2
Unit tests for FeedsExJmesPath.
Hierarchy
- class \FeedsExUnitTestBase extends \TUnit
- class \FeedsExJmesPathUnitTests
Expanded class hierarchy of FeedsExJmesPathUnitTests
File
- src/
Tests/ FeedsExJmesPath.test, line 11 - Contains tests for FeedsExJmesPath.
View source
class FeedsExJmesPathUnitTests extends FeedsExUnitTestBase {
/**
* The mocked FeedsSource.
*
* @var FeedsSource
*/
protected $source;
public static function getInfo() {
return array(
'name' => 'JMESPath parser unit tests',
'description' => 'Unit tests for FeedsExJmesPath.',
'group' => 'Feeds EX',
);
}
public function setUp() {
parent::setUp();
require_once $this->moduleDir . '/src/FeedsExJmesPath.inc';
$this->source = $this
->getMockFeedsSource();
}
/**
* Tests simple parsing.
*/
public function testSimpleParsing() {
$parser = FeedsConfigurable::instance('FeedsExJmesPath', 'test_parser');
$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
->assertEqual(count($result->items), 3, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
foreach ($result->items as $delta => $item) {
$this
->assertEqual('I am a title' . $delta, $item['title']);
$this
->assertEqual('I am a description' . $delta, $item['description']);
}
}
/**
* Tests a EUC-JP (Japanese) encoded file.
*
* This implicitly tests FeedsExBase's encoding conversion.
*/
public function testEUCJPEncoded() {
$parser = FeedsConfigurable::instance('FeedsExJmesPath', 'test_parser');
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_jp.json'));
$parser
->setConfig(array(
'context' => array(
'value' => 'items',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
'source_encoding' => array(
'EUC-JP',
),
));
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertEqual(count($result->items), 3, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
foreach ($result->items as $delta => $item) {
$this
->assertEqual('私はタイトルです' . $delta, $item['title']);
$this
->assertEqual('私が説明してい' . $delta, $item['description']);
}
}
/**
* Tests batch parsing.
*/
public function testBatchParsing() {
// Set batch limit.
$this
->variableSet('feeds_process_limit', 1);
$parser = FeedsConfigurable::instance('FeedsExJmesPath', 'test_parser');
$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
->assertEqual(count($result->items), 1, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
$this
->assertEqual('I am a title0', $result->items[0]['title']);
$this
->assertEqual('I am a description0', $result->items[0]['description']);
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertEqual(count($result->items), 1, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
$this
->assertEqual('I am a title1', $result->items[0]['title']);
$this
->assertEqual('I am a description1', $result->items[0]['description']);
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertEqual(count($result->items), 1, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
$this
->assertEqual('I am a title2', $result->items[0]['title']);
$this
->assertEqual('I am a description2', $result->items[0]['description']);
// We should be out of items.
$result = $parser
->parse($this->source, $fetcher_result);
$this
->assertEqual(count($result->items), 0, format_string('@count items parsed.', array(
'@count' => count($result->items),
)));
}
/**
* Tests JMESPath validation.
*/
public function testValidateExpression() {
// Invalid expression.
$parser = FeedsConfigurable::instance('FeedsExJmesPath', 'test_parser');
$expression = array(
'!! ',
);
$this
->assertEqual('Syntax error at character 0<br />!!<br />^<br />Invalid expression', $this
->invokeMethod($parser, 'validateExpression', $expression));
// Test that value was trimmed.
$this
->assertEqual($expression[0], '!!', 'Value was trimmed.');
// Empty string.
$this
->assertEqual(NULL, $this
->invokeMethod($parser, 'validateExpression', array(
'',
)));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsExJmesPathUnitTests:: |
protected | property | The mocked FeedsSource. | |
FeedsExJmesPathUnitTests:: |
public static | function | ||
FeedsExJmesPathUnitTests:: |
public | function |
Overrides FeedsExUnitTestBase:: |
|
FeedsExJmesPathUnitTests:: |
public | function | Tests batch parsing. | |
FeedsExJmesPathUnitTests:: |
public | function | Tests a EUC-JP (Japanese) encoded file. | |
FeedsExJmesPathUnitTests:: |
public | function | Tests simple parsing. | |
FeedsExJmesPathUnitTests:: |
public | function | Tests JMESPath validation. | |
FeedsExUnitTestBase:: |
protected | property | The module directory. | |
FeedsExUnitTestBase:: |
protected | function | Downloads JSONPath. | |
FeedsExUnitTestBase:: |
protected | function | Returns a mocked FeedsSource object. |