class FeedsExXmlUnitTests in Feeds extensible parsers 7.2
Unit tests for FeedsExXml.
Hierarchy
- class \FeedsExUnitTestBase extends \TUnit
- class \FeedsExXmlUnitTests
Expanded class hierarchy of FeedsExXmlUnitTests
File
- src/
Tests/ FeedsExXml.test, line 11 - Contains tests for FeedsExXml.
View source
class FeedsExXmlUnitTests extends FeedsExUnitTestBase {
/**
* The mocked FeedsSource.
*
* @var FeedsSource
*/
protected $source;
public static function getInfo() {
return array(
'name' => 'XML parser unit tests',
'description' => 'Unit tests for FeedsExXml.',
'group' => 'Feeds EX',
);
}
public function setUp() {
parent::setUp();
require_once $this->moduleDir . '/src/FeedsExXml.inc';
$this->source = $this
->getMockFeedsSource();
}
/**
* Tests simple parsing.
*/
public function testSimpleParsing() {
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'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 parsing a CP866 (Russian) encoded file.
*/
public function testCP866Encoded() {
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_ru.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'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('Я название' . $delta, $item['title']);
$this
->assertEqual('Я описание' . $delta, $item['description']);
}
}
/**
* Tests a EUC-JP (Japanese) encoded file without the encoding declaration.
*
* This implicitly tests FeedsExBase's encoding conversion.
*/
public function testEUCJPEncodedNoDeclaration() {
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_jp.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'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 batching.
*/
public function testBatching() {
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.xml'));
$parser
->setConfig(array(
'context' => array(
'value' => '/items/item',
),
'sources' => array(
'title' => array(
'name' => 'Title',
'value' => 'title',
),
'description' => array(
'name' => 'Title',
'value' => 'description',
),
),
));
$this
->variableSet('feeds_process_limit', 1);
foreach (range(0, 2) as $delta) {
$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 title' . $delta, $result->items[0]['title']);
$this
->assertEqual('I am a description' . $delta, $result->items[0]['description']);
}
}
/**
* Tests that the link propery is set.
*/
public function testLinkIsSet() {
$this
->setProperty($this->source, 'config', array(
'FeedsFileFetcher' => array(
'source' => 'file fetcher source path',
),
));
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$parser
->setConfig(array(
'context' => array(
'value' => '/beep',
),
));
$result = $parser
->parse($this->source, new FeedsFetcherResult('<?xml version="1.0" encoding="UTF-8"?><item></item>'));
$this
->assertEqual($result->link, 'file fetcher source path');
}
/**
* Tests XPath validation.
*/
public function testValidateExpression() {
// Invalid expression.
$parser = FeedsConfigurable::instance('FeedsExXml', 'test_parser');
$expression = array(
'!!',
);
$this
->assertEqual('Invalid expression', $this
->invokeMethod($parser, 'validateExpression', $expression));
// Test that value was trimmed.
$this
->assertEqual($expression[0], '!!', 'Value was trimmed.');
// Unknown namespace.
$this
->assertEqual(NULL, $this
->invokeMethod($parser, 'validateExpression', array(
'thing:asdf',
)));
// Empty.
$this
->assertEqual(NULL, $this
->invokeMethod($parser, 'validateExpression', array(
'',
)));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsExUnitTestBase:: |
protected | property | The module directory. | |
FeedsExUnitTestBase:: |
protected | function | Downloads JSONPath. | |
FeedsExUnitTestBase:: |
protected | function | Returns a mocked FeedsSource object. | |
FeedsExXmlUnitTests:: |
protected | property | The mocked FeedsSource. | |
FeedsExXmlUnitTests:: |
public static | function | ||
FeedsExXmlUnitTests:: |
public | function |
Overrides FeedsExUnitTestBase:: |
|
FeedsExXmlUnitTests:: |
public | function | Tests batching. | |
FeedsExXmlUnitTests:: |
public | function | Tests parsing a CP866 (Russian) encoded file. | |
FeedsExXmlUnitTests:: |
public | function | Tests a EUC-JP (Japanese) encoded file without the encoding declaration. | |
FeedsExXmlUnitTests:: |
public | function | Tests that the link propery is set. | |
FeedsExXmlUnitTests:: |
public | function | Tests simple parsing. | |
FeedsExXmlUnitTests:: |
public | function | Tests XPath validation. |