Skip to content

Commit

Permalink
update tests with file headers check
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Aug 31, 2022
1 parent d88bad8 commit f440dcd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/Concerns/HasCsvProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function getCsvRecordsProperty(): TabularDataReader

/**
* Handle CSV Information properties from the given file
*
* @return array|Illuminate\Support\MessageBag
*
* @return array|\Illuminate\Support\MessageBag
*/
public function handleCsvProperties(): array|MessageBag
{
Expand All @@ -50,12 +50,11 @@ public function handleCsvProperties(): array|MessageBag
$fileRowCount = $this->csvRecords->count();

return [$fileHeaders, $fileRowCount];

} catch(\League\Csv\SyntaxError $exception) {
} catch (\League\Csv\SyntaxError $exception) {
Log::warning($exception->getMessage());

return $this->addError(
'file_error',
'file_error',
__('Your CSV file has error/errors, or is empty. Please check, and try again')
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Concerns/InteractsWithCsvFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Coderflex\LaravelCsv\Concerns;

use League\Csv\Reader;
use Throwable;

trait InteractsWithCsvFiles
{
Expand All @@ -17,7 +16,7 @@ protected function readCSV(string $path): Reader
{
$stream = fopen($path, 'r');
$csv = Reader::createFromStream($stream);

$csv->setHeaderOffset(0)
->skipEmptyRecords();

Expand Down
3 changes: 1 addition & 2 deletions src/Http/Livewire/ImportCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function updatedFile()
$this->validateOnly('file');

$this->setCsvProperties();

$this->resetValidation();
}

Expand Down Expand Up @@ -95,7 +95,6 @@ protected function setCsvProperties()
});
})->validate();


[$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties();
}

Expand Down
16 changes: 16 additions & 0 deletions tests/ImportCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@
->assertHasErrors(['file_error']);
});

it('throws a validation error if the csv file has duplicate headers', function () {
$model = Customer::class;

$file = UploadedFile::fake()
->createWithContent(
'customers.csv',
file_get_contents('stubs/file_with_duplicate_headers.csv', true)
);

livewire(ImportCsv::class, [
'model' => $model,
])
->set('file', $file)
->assertSet('model', $model)
->assertHasErrors(['file_error']);
});

it('transfers columnsToMap into an associative array', function () {
$columnsToMap = [
Expand Down
12 changes: 12 additions & 0 deletions tests/stubs/file_with_duplicate_headers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"first_name","first_name","last_name","email","company","vip","birthday","created_at","updated_at"
1,asdf;lskadf,Haleigh,Ondricka,[email protected],Jaskolski-Hill,1,"1927-02-16","2022-07-26 13:41:40","2022-07-26 13:41:40"
2,Emilia,Schiller,[email protected],Pagac Inc,1,"1975-12-21","2022-07-26 13:41:40","2022-07-26 13:41:40"
3,Eric,Stamm,[email protected],Heller PLC,1,"1981-05-13","2022-07-26 13:41:40","2022-07-26 13:41:40"
4,Cecelia,Schmeler,[email protected],Heaney LLC,1,"1943-04-30","2022-07-26 13:41:40","2022-07-26 13:41:40"
5,Lewis,Metz,[email protected],Schowalter PLC,0,"1978-12-17","2022-07-26 13:41:40","2022-07-26 13:41:40"
6,Asa,Carroll,[email protected],Windler Group,0,"2016-01-27","2022-07-26 13:41:40","2022-07-26 13:41:40"
6,Asa,Carroll,[email protected],Windler Group,0,"2016-01-27","2022-07-26 13:41:40","2022-07-26 13:41:40"
7,Francesca,Thompson,[email protected],Ferry PLC,0,"2004-05-19","2022-07-26 13:41:40","2022-07-26 13:41:40"
8,Crystal,Bode,[email protected],0,"1930-08-04","2022-07-26 13:41:40","2022-07-26 13:41:40"
9,Dorian,Gislason,[email protected],Dooley-Conroy,1,"1975-03-13","2022-07-26 13:41:40","2022-07-26 13:41:40"
@hotmail.com,Reinger LLC,0,"1972-11-08","2022-07-26 13:41:40","2022-07-26 13:41:40"
Empty file removed tests/stubs/file_with_errors.csv
Empty file.

0 comments on commit f440dcd

Please sign in to comment.