Exemplo n.º 1
0
 /**
  * @testdox Import rows that specify an existing PK will update existing records.
  * @test
  */
 public function primaryKey()
 {
     $testtable = $this->db->getTable('test_table');
     $rec1 = $testtable->saveRecord(array('title' => 'PK Test'));
     $this->assertEquals(1, $testtable->getRecordCount());
     $this->assertNull($rec1->description());
     // Add a field's value.
     $csv = '"ID","Title","Description"' . "\r\n" . '"1","One","A description"' . "\r\n";
     $uploaded = $this->saveDataFile($csv);
     $csv = new \Tabulate\CSV(null, $uploaded);
     $csv->loadData();
     $column_map = array('id' => 'ID', 'title' => 'Title', 'description' => 'Description');
     $csv->importData($testtable, $column_map);
     // Make sure there's still only one record, and that it's been updated.
     $this->assertEquals(1, $testtable->getRecordCount());
     $rec2 = $testtable->getRecord(1);
     $this->assertEquals('One', $rec2->title());
     $this->assertEquals('A description', $rec2->description());
     // Leave out a required field.
     $csv = '"ID","Description"' . "\r\n" . '"1","New description"' . "\r\n";
     $uploaded2 = $this->saveDataFile($csv);
     $csv2 = new \Tabulate\CSV(null, $uploaded2);
     $csv2->loadData();
     $column_map2 = array('id' => 'ID', 'description' => 'Description');
     $csv2->importData($testtable, $column_map2);
     // Make sure there's still only one record, and that it's been updated.
     $this->assertEquals(1, $testtable->getRecordCount());
     $rec3 = $testtable->getRecord(1);
     $this->assertEquals('One', $rec3->title());
     $this->assertEquals('New description', $rec3->description());
 }