You are here

FeedsExJsonPathLines.test in Feeds extensible parsers 7

Same filename and directory in other branches
  1. 7.2 src/Tests/FeedsExJsonPathLines.test

File

src/Tests/FeedsExJsonPathLines.test
View source
<?php

/**
 * @file
 * Contains FeedsExJsonPathLinesUnitTests.
 */
if (class_exists('TUnit')) {

  /**
   * Unit tests for FeedsExJsonPathLines.
   */
  class FeedsExJsonPathLinesUnitTests extends FeedsExUnitTestBase {

    /**
     * The mocked FeedsSource.
     *
     * @var FeedsSource
     */
    protected $source;

    /**
     * The parser being tested.
     *
     * @var FeedsParser
     */
    protected $parser;

    /**
     * The fetcher result used during parsing.
     *
     * @var FeedsFetcherResult
     */
    protected $fetcherResult;

    /**
     * {@inheritdoc}
     */
    public static function getInfo() {
      return array(
        'name' => 'JSONPath JSON lines parser unit tests',
        'description' => 'Unit tests for FeedsExJsonPathLines.',
        'group' => 'Feeds EX',
      );
    }

    /**
     * {@inheritdoc}
     */
    public function setUp() {
      parent::setUp();
      require_once $this->moduleDir . '/src/FeedsExJsonPath.inc';
      require_once $this->moduleDir . '/src/FeedsExJsonPathLines.inc';
      $this->source = $this
        ->getMockFeedsSource();
      $this->parser = FeedsPlugin::instance('FeedsExJsonPathLines', strtolower($this
        ->randomName()));
      $this->parser
        ->setMessenger(new FeedsExTestMessenger());
      $this->parser
        ->addConfig(array(
        'sources' => array(
          'title' => array(
            'name' => 'Title',
            'value' => 'name',
          ),
        ),
      ));
      $this->fetcherResult = new FeedsFileFetcherResult($this->moduleDir . '/tests/resources/test.jsonl');
    }

    /**
     * Tests simple parsing.
     */
    public function testSimpleParsing() {
      $result = $this->parser
        ->parse($this->source, $this->fetcherResult);
      $this
        ->assertParserResultItemCount($result, 4);
      foreach (array(
        'Gilbert',
        'Alexa',
        'May',
        'Deloise',
      ) as $delta => $name) {
        $this
          ->assertEqual($name, $result->items[$delta]['title']);
      }
    }

    /**
     * Tests batch parsing.
     */
    public function testBatching() {
      $this
        ->variableSet('feeds_process_limit', 1);
      foreach (array(
        'Gilbert',
        'Alexa',
        'May',
        'Deloise',
      ) as $name) {
        $result = $this->parser
          ->parse($this->source, $this->fetcherResult);
        $this
          ->assertParserResultItemCount($result, 1);
        $this
          ->assertEqual($result->items[0]['title'], $name);
      }

      // We should be out of items.
      $result = $this->parser
        ->parse($this->source, $this->fetcherResult);
      $this
        ->assertParserResultItemCount($result, 0);
    }

    /**
     * Tests empty feed handling.
     */
    public function testEmptyFeed() {
      $this->parser
        ->parse($this->source, new FeedsFileFetcherResult($this->moduleDir . '/tests/resources/empty.txt'));
      $this
        ->assertEmptyFeedMessage($this->parser
        ->getMessenger()
        ->getMessages());
    }

  }
}