creating tests for csv import whole process

This commit is contained in:
eduardohumberto 2018-02-23 16:20:16 -03:00
parent dd9ac397a3
commit 6e305d5a4a
3 changed files with 51 additions and 29 deletions

View File

@ -6,7 +6,6 @@ use Tainacan;
class CSV extends Importer { class CSV extends Importer {
public $delimiter = ','; public $delimiter = ',';
private $file;
public function __construct() { public function __construct() {
parent::__construct(); parent::__construct();
@ -30,25 +29,23 @@ class CSV extends Importer {
* @inheritdoc * @inheritdoc
*/ */
public function get_fields_source(){ public function get_fields_source(){
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file; $file = new \SplFileObject( $this->tmp_file, 'r' );
$this->file->seek(0 ); $file->seek(0 );
return $this->file->fgetcsv( $this->get_delimiter() ); return $file->fgetcsv( $this->get_delimiter() );
} }
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function process( $start, $end ){ public function process( $start, $end ){
$this->file = ( !isset( $this->file ) ) ? new \SplFileObject( $this->tmp_file, 'r' ) : $this->file; while ( $start < $end && count( $this->get_processed_items() ) <= $this->get_total_items() ){
while ( $start < $end ){
if( $start === 0 ){
$start++;
continue;
}
$processed_item = $this->process_item( $start ); $processed_item = $this->process_item( $start );
if( $processed_item) {
$this->insert( $start, $processed_item ); $this->insert( $start, $processed_item );
} else {
$this->set_log('error', 'failed on item '.$start );
break;
}
$start++; $start++;
} }
} }
@ -61,8 +58,20 @@ class CSV extends Importer {
$headers = $this->get_fields_source(); $headers = $this->get_fields_source();
// search the index in the file and get values // search the index in the file and get values
$this->file->seek( $index ); $file = new \SplFileObject( $this->tmp_file, 'r' );
$values = $this->file->fgetcsv( $this->get_delimiter() ); $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) { foreach ($headers as $index => $header) {
$processedItem[ $header ] = $values[ $index ]; $processedItem[ $header ] = $values[ $index ];

View File

@ -59,6 +59,13 @@ abstract class Importer {
return $this->last_index; return $this->last_index;
} }
/**
* @return array the last index from source
*/
public function get_logs(){
return $this->logs;
}
/** /**
* @param Tainacan\Entities\Collection $collection * @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 * internal function to upload the file
* *
@ -209,8 +226,7 @@ abstract class Importer {
$field = $Tainacan_Fields->fetch( $tainacan_field_id ); $field = $Tainacan_Fields->fetch( $tainacan_field_id );
if( $field instanceof Tainacan\Entities\Field ){ if( $field instanceof Tainacan\Entities\Field ){
$singleItemMetadata = new Tainacan\Entities\Item_Metadata_Entity(); $singleItemMetadata = new Tainacan\Entities\Item_Metadata_Entity( $item, $field);
$singleItemMetadata->set_field( $field );
$singleItemMetadata->set_value( $values ); $singleItemMetadata->set_value( $values );
$itemMetadataArray[] = $singleItemMetadata; $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_title( time() );
$item->set_collection( $this->collection ); $item->set_collection( $this->collection );
$insertedItem = $Tainacan_Items->insert( $item ); $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 * run the process
*/ */

View File

@ -37,7 +37,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
* @group importer * @group importer
*/ */
public function test_file_csv () { public function test_file_csv () {
global $Tainacan_Fields; global $Tainacan_Fields, $Tainacan_Items;
$csv_importer = new Importer\CSV(); $csv_importer = new Importer\CSV();
$id = $csv_importer->get_id(); $id = $csv_importer->get_id();
@ -107,6 +107,13 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// check is equal // check is equal
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map ); $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 ) );
} }
/** /**