You are here

class FeedsExQueryPathXmlUnitTests in Feeds extensible parsers 7.2

Unit tests for FeedsExQueryPathXml.

Hierarchy

Expanded class hierarchy of FeedsExQueryPathXmlUnitTests

File

src/Tests/FeedsExQueryPathXml.test, line 11
Contains tests for FeedsExQueryPathXml.

View source
class FeedsExQueryPathXmlUnitTests extends FeedsExUnitTestBase {

  /**
   * The mocked FeedsSource.
   *
   * @var FeedsSource
   */
  protected $source;
  public static function getInfo() {
    return array(
      'name' => 'QueryPath XML parser unit tests',
      'description' => 'Unit tests for FeedsExQueryPathXml.',
      'group' => 'Feeds EX',
      'dependencies' => array(
        'querypath',
      ),
    );
  }
  public function setUp() {
    parent::setUp();
    $query_path = drupal_get_path('module', 'querypath');
    require_once DRUPAL_ROOT . '/' . $query_path . '/QueryPath/QueryPath.php';
    require_once $this->moduleDir . '/src/FeedsExXml.inc';
    require_once $this->moduleDir . '/src/FeedsExQueryPathXml.inc';
    $this->source = $this
      ->getMockFeedsSource();
  }

  /**
   * Tests simple parsing.
   */
  public function testSimpleParsing() {
    $parser = FeedsConfigurable::instance('FeedsExQueryPathXml', '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',
          'attribute' => '',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'description',
          'attribute' => '',
        ),
      ),
    ));
    $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 grabbing an attribute.
   */
  public function testAttributeParsing() {
    $parser = FeedsConfigurable::instance('FeedsExQueryPathXml', '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',
          'attribute' => 'attr',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'description',
          'attribute' => '',
        ),
      ),
    ));
    $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('attribute' . $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('FeedsExQueryPathXml', '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',
          'attribute' => '',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'description',
          'attribute' => '',
        ),
      ),
    ));
    $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('FeedsExQueryPathXml', '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',
          'attribute' => '',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'description',
          'attribute' => '',
        ),
      ),
      '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 that batch parsing works.
   */
  public function testBatchParsing() {
    $parser = FeedsConfigurable::instance('FeedsExQueryPathXml', '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',
          'attribute' => '',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'description',
          'attribute' => '',
        ),
      ),
    ));

    // Yay TUnit!
    $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 QueryPath validation.
   */
  public function testValidateExpression() {

    // Invalid expression.
    $parser = FeedsConfigurable::instance('FeedsExQueryPathXml', 'test_parser');
    $expression = array(
      '!!',
    );
    $this
      ->assertEqual('CSS selector is not well formed.', $this
      ->invokeMethod($parser, 'validateExpression', $expression));

    // Test that value was trimmed.
    $this
      ->assertEqual($expression[0], '!!', 'Value was trimmed.');

    // Empty.
    $this
      ->assertEqual(NULL, $this
      ->invokeMethod($parser, 'validateExpression', array(
      '',
    )));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExQueryPathXmlUnitTests::$source protected property The mocked FeedsSource.
FeedsExQueryPathXmlUnitTests::getInfo public static function
FeedsExQueryPathXmlUnitTests::setUp public function Overrides FeedsExUnitTestBase::setUp
FeedsExQueryPathXmlUnitTests::testAttributeParsing public function Tests grabbing an attribute.
FeedsExQueryPathXmlUnitTests::testBatchParsing public function Tests that batch parsing works.
FeedsExQueryPathXmlUnitTests::testCP866Encoded public function Tests parsing a CP866 (Russian) encoded file.
FeedsExQueryPathXmlUnitTests::testEUCJPEncodedNoDeclaration public function Tests a EUC-JP (Japanese) encoded file without the encoding declaration.
FeedsExQueryPathXmlUnitTests::testSimpleParsing public function Tests simple parsing.
FeedsExQueryPathXmlUnitTests::testValidateExpression public function Tests QueryPath validation.
FeedsExUnitTestBase::$moduleDir protected property The module directory.
FeedsExUnitTestBase::downloadJsonPath protected function Downloads JSONPath.
FeedsExUnitTestBase::getMockFeedsSource protected function Returns a mocked FeedsSource object.