creating tests for csv import whole process
This commit is contained in:
parent
dd9ac397a3
commit
6e305d5a4a
|
@ -6,7 +6,6 @@ use Tainacan;
|
|||
class CSV extends Importer {
|
||||
|
||||
public $delimiter = ',';
|
||||
private $file;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
@ -30,25 +29,23 @@ class CSV extends Importer {
|
|||
* @inheritdoc
|
||||
*/
|
||||
public function get_fields_source(){
|
||||
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file;
|
||||
$this->file->seek(0 );
|
||||
return $this->file->fgetcsv( $this->get_delimiter() );
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek(0 );
|
||||
return $file->fgetcsv( $this->get_delimiter() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function process( $start, $end ){
|
||||
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file;
|
||||
|
||||
while ( $start < $end ){
|
||||
if( $start === 0 ){
|
||||
$start++;
|
||||
continue;
|
||||
}
|
||||
|
||||
while ( $start < $end && count( $this->get_processed_items() ) <= $this->get_total_items() ){
|
||||
$processed_item = $this->process_item( $start );
|
||||
$this->insert( $start, $processed_item );
|
||||
if( $processed_item) {
|
||||
$this->insert( $start, $processed_item );
|
||||
} else {
|
||||
$this->set_log('error', 'failed on item '.$start );
|
||||
break;
|
||||
}
|
||||
$start++;
|
||||
}
|
||||
}
|
||||
|
@ -61,8 +58,20 @@ class CSV extends Importer {
|
|||
$headers = $this->get_fields_source();
|
||||
|
||||
// search the index in the file and get values
|
||||
$this->file->seek( $index );
|
||||
$values = $this->file->fgetcsv( $this->get_delimiter() );
|
||||
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||
$file->seek( $index );
|
||||
|
||||
if( $index === 0 ){
|
||||
$file->current();
|
||||
$file->next();
|
||||
$values = $file->fgetcsv( $this->get_delimiter() );
|
||||
}else{
|
||||
$values = $file->fgetcsv( $this->get_delimiter() );
|
||||
}
|
||||
|
||||
if( count( $headers ) !== count( $values ) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($headers as $index => $header) {
|
||||
$processedItem[ $header ] = $values[ $index ];
|
||||
|
|
|
@ -59,6 +59,13 @@ abstract class Importer {
|
|||
return $this->last_index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array the last index from source
|
||||
*/
|
||||
public function get_logs(){
|
||||
return $this->logs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Tainacan\Entities\Collection $collection
|
||||
*/
|
||||
|
@ -111,6 +118,16 @@ abstract class Importer {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* log the actions from importer
|
||||
*
|
||||
* @param $type
|
||||
* @param $message
|
||||
*/
|
||||
public function set_log( $type, $message ){
|
||||
$this->logs[] = [ 'type' => $type, 'message' => $message ];
|
||||
}
|
||||
|
||||
/**
|
||||
* internal function to upload the file
|
||||
*
|
||||
|
@ -209,8 +226,7 @@ abstract class Importer {
|
|||
$field = $Tainacan_Fields->fetch( $tainacan_field_id );
|
||||
|
||||
if( $field instanceof Tainacan\Entities\Field ){
|
||||
$singleItemMetadata = new Tainacan\Entities\Item_Metadata_Entity();
|
||||
$singleItemMetadata->set_field( $field );
|
||||
$singleItemMetadata = new Tainacan\Entities\Item_Metadata_Entity( $item, $field);
|
||||
$singleItemMetadata->set_value( $values );
|
||||
$itemMetadataArray[] = $singleItemMetadata;
|
||||
}
|
||||
|
@ -218,7 +234,7 @@ abstract class Importer {
|
|||
}
|
||||
}
|
||||
|
||||
if( !empty( $itemMetadata ) && $this->collection instanceof Tainacan\Entities\Collection ){
|
||||
if( !empty( $itemMetadataArray ) && $this->collection instanceof Tainacan\Entities\Collection ){
|
||||
$item->set_title( time() );
|
||||
$item->set_collection( $this->collection );
|
||||
$insertedItem = $Tainacan_Items->insert( $item );
|
||||
|
@ -253,16 +269,6 @@ abstract class Importer {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* log the actions from importer
|
||||
*
|
||||
* @param $type
|
||||
* @param $message
|
||||
*/
|
||||
public function set_log( $type, $message ){
|
||||
$this->logs[] = [ 'type' => $type, 'message' => $message ];
|
||||
}
|
||||
|
||||
/**
|
||||
* run the process
|
||||
*/
|
||||
|
|
|
@ -37,7 +37,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
|||
* @group importer
|
||||
*/
|
||||
public function test_file_csv () {
|
||||
global $Tainacan_Fields;
|
||||
global $Tainacan_Fields, $Tainacan_Items;
|
||||
|
||||
$csv_importer = new Importer\CSV();
|
||||
$id = $csv_importer->get_id();
|
||||
|
@ -107,6 +107,13 @@ class ImporterTests extends TAINACAN_UnitTestCase {
|
|||
// check is equal
|
||||
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map );
|
||||
|
||||
//execute the process
|
||||
$_SESSION['tainacan_importer'][$id]->run();
|
||||
|
||||
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
|
||||
|
||||
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue