You are here

class ParserCSVTest in Feeds 8.2

Test aggregating a feed as node items.

Using DrupalWebTestCase as DrupalUnitTestCase is broken in SimpleTest 2.8. Not inheriting from Feeds base class as ParserCSV should be moved out of Feeds at some time.

Hierarchy

  • class \Drupal\feeds\Tests\ParserCSVTest extends \Drupal\simpletest\UnitTestBase

Expanded class hierarchy of ParserCSVTest

File

lib/Drupal/feeds/Tests/ParserCSVTest.php, line 21
Tests for ParserCSV library.

Namespace

Drupal\feeds\Tests
View source
class ParserCSVTest extends UnitTestBase {
  protected $profile = 'testing';
  public static function getInfo() {
    return array(
      'name' => 'CSV Parser unit tests',
      'description' => 'Base level test for Feeds\' built in CSV parser.',
      'group' => 'Feeds',
    );
  }

  /**
   * Test method.
   */
  public function test() {
    $this
      ->_testSimple();
    $this
      ->_testBatching();
  }

  /**
   * Simple test of parsing functionality.
   */
  protected function _testSimple() {

    // Pull in the $control_result array.
    include $this
      ->absolutePath() . '/tests/feeds/nodes.csv.php';
    $delimiters = $this
      ->getDelimiters();
    foreach ($delimiters as $delimiterType => $delimiter) {
      $file = $this
        ->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
      $iterator = new ParserCSVIterator($file);
      $parser = new ParserCSV();
      $parser
        ->setDelimiter($delimiter);
      $rows = $parser
        ->parse($iterator);
      $this
        ->assertFalse($parser
        ->lastLinePos(), t('CSV reports all lines parsed, with delimiter: ') . $delimiterType);
      $this
        ->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
    }
  }

  /**
   * Test batching.
   */
  protected function _testBatching() {

    // Pull in the $control_result array
    include $this
      ->absolutePath() . '/tests/feeds/nodes.csv.php';
    $delimiters = $this
      ->getDelimiters();
    foreach ($delimiters as $delimiterType => $delimiter) {
      $file = $this
        ->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';

      // Set up parser with 2 lines to parse per call.
      $iterator = new ParserCSVIterator($file);
      $parser = new ParserCSV();
      $parser
        ->setDelimiter($delimiter);
      $parser
        ->setLineLimit(2);
      $rows = array();
      $pos = 0;

      // Call parser until all lines are parsed, then compare to control result.
      do {
        $parser
          ->setStartByte($pos);
        $rows = array_merge($rows, $parser
          ->parse($iterator));
        $pos = $parser
          ->lastLinePos();
        $this
          ->assertTrue($parser
          ->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
      } while ($pos = $parser
        ->lastLinePos());
      $this
        ->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Batch parsed result matches control result for delimiter: ') . $delimiterType);
    }
  }

  /**
   * Absolute path to feeds.
   */
  public function absolutePath() {
    return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
  }
  static function getDelimiters() {
    return array(
      'comma' => ',',
      'pipe' => '|',
      'semicolon' => ';',
      'plus' => '+',
      'tab' => "\t",
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParserCSVTest::$profile protected property
ParserCSVTest::absolutePath public function Absolute path to feeds.
ParserCSVTest::getDelimiters static function
ParserCSVTest::getInfo public static function
ParserCSVTest::test public function Test method.
ParserCSVTest::_testBatching protected function Test batching.
ParserCSVTest::_testSimple protected function Simple test of parsing functionality.