You are here

class FeedsExHtmlUnitTests in Feeds extensible parsers 7.2

Unit tests for FeedsExHtml.

Hierarchy

Expanded class hierarchy of FeedsExHtmlUnitTests

File

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

View source
class FeedsExHtmlUnitTests extends FeedsExUnitTestBase {

  /**
   * The mocked FeedsSource.
   *
   * @var FeedsSource
   */
  protected $source;
  public static function getInfo() {
    return array(
      'name' => 'HTML parser unit tests',
      'description' => 'Unit tests for FeedsExHtml.',
      'group' => 'Feeds EX',
    );
  }
  public function setUp() {
    parent::setUp();
    require_once $this->moduleDir . '/src/FeedsExXml.inc';
    require_once $this->moduleDir . '/src/FeedsExHtml.inc';
    $this->source = $this
      ->getMockFeedsSource();
  }

  /**
   * Tests simple parsing.
   */
  public function testSimpleParsing() {
    $parser = FeedsConfigurable::instance('FeedsExHtml', 'test_parser');
    $fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test.html'));
    $parser
      ->setConfig(array(
      'context' => array(
        'value' => '//div[@class="post"]',
      ),
      'sources' => array(
        'title' => array(
          'name' => 'Title',
          'value' => 'h3',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'p',
        ),
      ),
    ));
    $result = $parser
      ->parse($this->source, $fetcher_result);
    $this
      ->assertEqual(count($result->items), 3, format_string('@count items parsed.', array(
      '@count' => count($result->items),
    )));
    $this
      ->assertEqual('I am a title<thing>Stuff</thing>', $result->items[0]['title']);
    $this
      ->assertEqual('I am a description0', $result->items[0]['description']);
    $this
      ->assertEqual('I am a title1', $result->items[1]['title']);
    $this
      ->assertEqual('I am a description1', $result->items[1]['description']);
    $this
      ->assertEqual('I am a title2', $result->items[2]['title']);
    $this
      ->assertEqual('I am a description2', $result->items[2]['description']);
  }

  /**
   * Tests parsing a CP866 (Russian) encoded file.
   */
  public function testCP866Encoded() {
    $parser = FeedsConfigurable::instance('FeedsExHtml', 'test_parser');
    $fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_ru.html'));
    $parser
      ->setConfig(array(
      'context' => array(
        'value' => '//div[@class="post"]',
      ),
      'sources' => array(
        'title' => array(
          'name' => 'Title',
          'value' => 'h3',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'p',
        ),
      ),
    ));
    $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('FeedsExHtml', 'test_parser');
    $fetcher_result = new FeedsFetcherResult(file_get_contents($this->moduleDir . '/tests/resources/test_jp.html'));
    $parser
      ->setConfig(array(
      'context' => array(
        'value' => '//div[@class="post"]',
      ),
      'sources' => array(
        'title' => array(
          'name' => 'Title',
          'value' => 'h3',
        ),
        'description' => array(
          'name' => 'Title',
          'value' => 'p',
        ),
      ),
      '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 the link propery is set.
   */
  public function testLinkIsSet() {
    $this
      ->setProperty($this->source, 'config', array(
      'FeedsFileFetcher' => array(
        'source' => 'file fetcher source path',
      ),
    ));
    $parser = FeedsConfigurable::instance('FeedsExHtml', '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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsExHtmlUnitTests::$source protected property The mocked FeedsSource.
FeedsExHtmlUnitTests::getInfo public static function
FeedsExHtmlUnitTests::setUp public function Overrides FeedsExUnitTestBase::setUp
FeedsExHtmlUnitTests::testCP866Encoded public function Tests parsing a CP866 (Russian) encoded file.
FeedsExHtmlUnitTests::testEUCJPEncodedNoDeclaration public function Tests a EUC-JP (Japanese) encoded file without the encoding declaration.
FeedsExHtmlUnitTests::testLinkIsSet public function Tests that the link propery is set.
FeedsExHtmlUnitTests::testSimpleParsing public function Tests simple parsing.
FeedsExUnitTestBase::$moduleDir protected property The module directory.
FeedsExUnitTestBase::downloadJsonPath protected function Downloads JSONPath.
FeedsExUnitTestBase::getMockFeedsSource protected function Returns a mocked FeedsSource object.