Projet

Général

Profil

Paste
Télécharger (3,04 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / parser_csv.test @ 41cc1b08

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for ParserCSV library.
6
 */
7

    
8
/**
9
 * Test aggregating a feed as node items.
10
 *
11
 * Using DrupalWebTestCase as DrupalUnitTestCase is broken in SimpleTest 2.8.
12
 * Not inheriting from Feeds base class as ParserCSV should be moved out of
13
 * Feeds at some time.
14
 */
15
class ParserCSVTest extends DrupalWebTestCase  {
16
  protected $profile = 'testing';
17

    
18
  public static function getInfo() {
19
    return array(
20
      'name' => 'CSV Parser unit tests',
21
      'description' => 'Base level test for Feeds\' built in CSV parser.',
22
      'group' => 'Feeds',
23
    );
24
  }
25

    
26
  /**
27
   * Test method.
28
   */
29
  public function test() {
30
    drupal_load('module', 'feeds');
31
    feeds_include_library('ParserCSV.inc', 'ParserCSV');
32

    
33
    $this->_testSimple();
34
    $this->_testBatching();
35
  }
36

    
37
  /**
38
   * Simple test of parsing functionality.
39
   */
40
  protected function _testSimple() {
41
    // Pull in the $control_result array.
42
    include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
43

    
44
    $delimiters = $this->getDelimiters();
45
    foreach($delimiters as $delimiterType => $delimiter) {
46
      $file =  $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
47
      $iterator = new ParserCSVIterator($file);
48
      $parser = new ParserCSV();
49
      $parser->setDelimiter($delimiter);
50
      $rows = $parser->parse($iterator);
51
      $this->assertFalse($parser->lastLinePos(), t('CSV reports all lines parsed, with delimiter: ') . $delimiterType);
52
      $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Parsed result matches control result.'));
53
    }
54
  }
55

    
56
  /**
57
   * Test batching.
58
   */
59
  protected function _testBatching() {
60
    // Pull in the $control_result array
61
    include $this->absolutePath() . '/tests/feeds/nodes.csv.php';
62

    
63
    $delimiters = $this->getDelimiters();
64
    foreach($delimiters as $delimiterType => $delimiter) {
65
      $file =  $this->absolutePath() . '/tests/feeds/nodes_' . $delimiterType . '.csv';
66
      // Set up parser with 2 lines to parse per call.
67
      $iterator = new ParserCSVIterator($file);
68
      $parser = new ParserCSV();
69
      $parser->setDelimiter($delimiter);
70
      $parser->setLineLimit(2);
71
      $rows = array();
72
      $pos = 0;
73

    
74
      // Call parser until all lines are parsed, then compare to control result.
75
      do {
76
        $parser->setStartByte($pos);
77
        $rows = array_merge($rows, $parser->parse($iterator));
78
        $pos = $parser->lastLinePos();
79
        $this->assertTrue($parser->lastLinePos() || count($rows) == 10, t('Parser reports line limit correctly'));
80
      }
81
      while ($pos = $parser->lastLinePos());
82

    
83
      $this->assertEqual(md5(serialize($rows)), md5(serialize($control_result)), t('Batch parsed result matches control result for delimiter: ') . $delimiterType);
84
    }
85
  }
86

    
87
  /**
88
   * Absolute path to feeds.
89
   */
90
  public function absolutePath() {
91
    return DRUPAL_ROOT . '/' . drupal_get_path('module', 'feeds');
92
  }
93

    
94
  static function getDelimiters() {
95
    return array(
96
      'comma' => ',',
97
      'pipe' => '|',
98
      'semicolon' => ';',
99
      'plus' => '+',
100
      'tab' => "\t",
101
    );
102
  }
103
}