Development

Changeset 20719

You must first sign up to be able to contribute.

Changeset 20719

Show
Ignore:
Timestamp:
08/03/09 13:59:11 (4 years ago)
Author:
Tomek
Message:

More unit tests, class can now handle different encodings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/tmCsvPlugin/lib/tmCsvReader.class.php

    r20712 r20719  
    77 * @license LGPL  
    88 */ 
    9  
    109 
    1110class tmCsvException extends Exception 
     
    2928  const ERR_HEADER = 4; 
    3029   
    31    
    3230  private $hasHeader; 
    3331   
     
    3533   * @var array The header as parsed from the CSV file in a format array('column'=>'column'). We do not allow for 2 columns with the same name. 
    3634   */ 
    37   private $header, $schema; 
     35  private $header, $schema; 
    3836   
    3937  /** 
     
    9088      $this->escape = null; 
    9189    } 
    92  
     90     
    9391    if(isset($options['from'])) { 
    9492      $this->from = $options['from']; 
     
    9694      $this->from = 'auto'; 
    9795    } 
    98  
     96     
    9997    if(isset($options['to'])) { 
    10098      $this->to = $options['to']; 
     
    116114      throw new CsvException('Header option must equal to true or false', self::ERR_WRONG_OPTION); 
    117115    } 
    118      
    119116     
    120117    $this->header = null; 
     
    141138  } 
    142139   
    143   public function removeHeader($columns)  
    144   { 
    145    if(! $this->hasHeader) { 
     140  public function removeHeader($columns) 
     141  { 
     142    if(! $this->hasHeader) { 
    146143      throw new CsvException('Header is not set to be parsed', self::ERR_WRONG_OPTION); 
    147144    } 
     
    149146      $this->readHeader(); 
    150147    } 
    151    
     148     
    152149    if(! is_array($columns)) { 
    153       $columns = array ( $columns => $columns ); 
    154     } 
    155  
     150      $columns = array ( 
     151        $columns => $columns ); 
     152    } 
     153     
    156154    foreach($columns as $c => $column) { 
    157         unset($this->currentHeader[$c]); 
    158         unset($this->currentHeader[$column]); 
    159     } 
    160      
     155      unset($this->currentHeader[$c]); 
     156      unset($this->currentHeader[$column]); 
     157    } 
     158   
    161159  } 
    162160   
     
    166164  public function getRemoved() 
    167165  { 
    168    if(! $this->hasHeader) { 
     166    if(! $this->hasHeader) { 
    169167      throw new CsvException('Header is not set to be parsed', self::ERR_WRONG_OPTION); 
    170168    } 
     
    175173    return array_diff_assoc($this->header, $this->currentHeader); 
    176174  } 
    177    
    178175   
    179176  private function readIntoMemory() 
     
    227224       
    228225    //for PHP 5.3: fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure, $this->escape) 
    229       $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 
    230  
    231     var_dump($data); 
     226    $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 
     227     
    232228    if($data === false || count($data) < 2) { 
    233229      $this->state = self::ALL_READ; 
     
    236232     
    237233    //encoding 
    238     foreach($data as $k=>$field) { 
    239     $data[$k] = $this->encode($field); 
     234    foreach($data as $k => $field) { 
     235      $data[$k] = $this->encode($field); 
    240236    } 
    241237     
     
    243239      //we need to consider removed columns 
    244240      //first, we compare data we've got with the original header 
     241       
    245242 
    246243      //@todo check the option on what do we do in this case 
    247244      if(count($data) > count($this->header)) { 
    248         array_splice($data,count($this->header)); 
    249       } 
    250  
     245        array_splice($data, count($this->header)); 
     246      } 
     247       
    251248      //@todo check the option on what do we do in this case 
    252249      if(count($data) < count($this->header)) { 
    253         $data = array_merge($data, array_fill(0, count($this->header) - count($data), null)); 
    254       } 
    255    
    256       $structuredData = array(); 
     250        $data = array_merge($data, array_fill(0, count($this->header) - count($data), null)); 
     251      } 
     252       
     253      $structuredData = array (); 
    257254      reset($this->header); 
    258255      //next, current 
    259       foreach ($data as $field) { 
    260         if(array_key_exists(current($this->header),$this->currentHeader)) { 
     256      foreach($data as $field) { 
     257        if(array_key_exists(current($this->header), $this->currentHeader)) { 
    261258          $structuredData[current($this->header)] = $field; 
    262259        } 
     
    289286  { 
    290287    rewind($this->fhandle); 
    291  
     288     
    292289    $this->header = array (); 
    293290    $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 
    294291    foreach($data as $field) { 
    295       if(key_exists($field,$this->header)) { 
     292      if(key_exists($field, $this->header)) { 
    296293        throw new tmCsvException("Field: '$field' is repeated twice in a header.", self::ERR_HEADER); 
    297294      } 
     
    300297    $this->currentHeader = $this->header; 
    301298  } 
    302  
    303299   
    304300  public function next() 
  • plugins/tmCsvPlugin/test/unit/doctrine01Test.php

    r20712 r20719  
    3030$csv->setTable(Doctrine::getTable('Employee')); 
    3131$testResults = $csv->parse(); 
    32 //$testResults['valid']->save(); 
    3332$first = $testResults['valid'][0]; 
    3433$t->cmp_ok($first['id'], '===',null); 
     
    4746$csv->setTemplate($eTmpl); 
    4847$parsed = $csv->parse(); 
    49  
    5048$third = $parsed['valid'][1]; 
    5149$t->is($third['firstname'], 'Zabuch'); 
    5250$t->is($third['lastname'], 'lastname4'); 
    53 var_dump($third->toArray()); 
    54 $third->save(); 
    55 var_dump($third->toArray()); 
    56 $third->save(); 
    57 $third['age'] = 776; 
    58 var_dump($third->toArray()); 
    5951$csv->close(); 
    6052 
    61  
    62 $eTmpl = new Employee(); 
    63 $eTmpl['firstname'] = 'Zabuch'; 
    64 var_dump($eTmpl->toArray()); 
    65 $eTmpl2 = $eTmpl->copy(true); 
    66 //$eTmpl["updated_at"] = '2006-09-09'; 
    67 $eTmpl2->save(); 
    68 var_dump($eTmpl2->toArray()); 
    69  
  • plugins/tmCsvPlugin/test/unit/encodingTest.php

    r20712 r20719  
    77$t = new lime_test(null, new lime_output_color()); 
    88 
     9$t->comment('Testing encoding'); 
    910 
    1011if(setlocale(LC_ALL, 'pl_PL.ISO-8859-2') === false) { 
     
    1415$csv = new tmCsvReader($testFile, array('header'=>false, 'from'=>'iso-8859-2')); 
    1516$arr = $csv->toArray(); 
    16 var_dump($arr[0]); 
    1717$csv->close(); 
    1818$strUtf8 =  "\x67\xc5\xbc\x65\x67\xc5\xbc\xc3\xb3\xc5\x82\x6b\x61"; 
    1919$t->is($strUtf8, $arr[0][0]); 
    2020$strUtf8="\xc4\x99\xc3\xb3\xc5\x82\xc5\x84\xc5\xbc\xc5\xba\xc4\x87\xc4\x85\xc5\x9b"; 
    21 var_dump($strUtf8); 
    2221$t->is($strUtf8, $arr[0][1]); 
     22 
     23if(setlocale(LC_ALL, 'en_US.UTF-8') === false) { 
     24  $t->fail("Can't set the locale needed for this test"); 
     25} 
     26 
     27$testFile = '_encoding_utf-8.csv'; 
     28$csv = new tmCsvReader($testFile, array('header'=>false, 'from'=>'utf-8', 'to'=>'iso-8859-2')); 
     29$arr = $csv->toArray(); 
     30//var_dump($arr[0]); 
     31$csv->close(); 
     32$strIso =  "\x67\xbf\x65\x67\xbf\xf3\xb3\x6b\x61"; 
     33$t->is($strIso, $arr[0][0]); 
     34$strIso = "\xea\xf3\xb3\xf1\xbf\xbc\xe6\xb1\xb6"; 
     35//var_dump($strUtf8); 
     36$t->is($strIso, $arr[0][1]); 
  • plugins/tmCsvPlugin/test/unit/readCsvTest.php

    r20625 r20719  
    5353$csv->close(); 
    5454 
     55 
     56$testFile = '_employees02.csv'; 
     57$csv = new tmCsvReader($testFile, array ( 
     58  'header' => true )); 
     59 
     60$expectedValues = array ( 
     61  0 => array ( 
     62  'id1' => '17', 'id2' => '12', 'foreignKey' => '34', 'name' => 'Nam\'\'e1', 'lastname' => 'very long last 
     63name with 
     64few 
     65newlines embedded', 'age' => '17', 'date' => '2009-12-01', 'bonus' => '17' ) ); 
     66$t->is_deeply($expectedValues,$csv->toArray());