diff --git a/src/Concerns/HasCsvProperties.php b/src/Concerns/HasCsvProperties.php index 9f2bf84..e603c80 100644 --- a/src/Concerns/HasCsvProperties.php +++ b/src/Concerns/HasCsvProperties.php @@ -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 { @@ -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') ); } diff --git a/src/Concerns/InteractsWithCsvFiles.php b/src/Concerns/InteractsWithCsvFiles.php index 6b4b816..9f43a71 100644 --- a/src/Concerns/InteractsWithCsvFiles.php +++ b/src/Concerns/InteractsWithCsvFiles.php @@ -3,7 +3,6 @@ namespace Coderflex\LaravelCsv\Concerns; use League\Csv\Reader; -use Throwable; trait InteractsWithCsvFiles { @@ -17,7 +16,7 @@ protected function readCSV(string $path): Reader { $stream = fopen($path, 'r'); $csv = Reader::createFromStream($stream); - + $csv->setHeaderOffset(0) ->skipEmptyRecords(); diff --git a/src/Http/Livewire/ImportCsv.php b/src/Http/Livewire/ImportCsv.php index 561bb0f..c14ea1a 100644 --- a/src/Http/Livewire/ImportCsv.php +++ b/src/Http/Livewire/ImportCsv.php @@ -53,7 +53,7 @@ public function updatedFile() $this->validateOnly('file'); $this->setCsvProperties(); - + $this->resetValidation(); } @@ -95,7 +95,6 @@ protected function setCsvProperties() }); })->validate(); - [$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties(); } diff --git a/tests/ImportCsvTest.php b/tests/ImportCsvTest.php index 4284f58..05db6e7 100644 --- a/tests/ImportCsvTest.php +++ b/tests/ImportCsvTest.php @@ -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 = [ diff --git a/tests/stubs/file_with_duplicate_headers.csv b/tests/stubs/file_with_duplicate_headers.csv new file mode 100644 index 0000000..3d396da --- /dev/null +++ b/tests/stubs/file_with_duplicate_headers.csv @@ -0,0 +1,12 @@ +"first_name","first_name","last_name","email","company","vip","birthday","created_at","updated_at" +1,asdf;lskadf,Haleigh,Ondricka,mary.marks@sanford.com,Jaskolski-Hill,1,"1927-02-16","2022-07-26 13:41:40","2022-07-26 13:41:40" +2,Emilia,Schiller,danielle96@waters.com,Pagac Inc,1,"1975-12-21","2022-07-26 13:41:40","2022-07-26 13:41:40" +3,Eric,Stamm,jkoss@yahoo.com,Heller PLC,1,"1981-05-13","2022-07-26 13:41:40","2022-07-26 13:41:40" +4,Cecelia,Schmeler,eschultz@fadel.com,Heaney LLC,1,"1943-04-30","2022-07-26 13:41:40","2022-07-26 13:41:40" +5,Lewis,Metz,maximillia.morissette@yahoo.com,Schowalter PLC,0,"1978-12-17","2022-07-26 13:41:40","2022-07-26 13:41:40" +6,Asa,Carroll,giuseppe.barrows@gmail.com,Windler Group,0,"2016-01-27","2022-07-26 13:41:40","2022-07-26 13:41:40" +6,Asa,Carroll,giuseppe.barrows@gmail.com,Windler Group,0,"2016-01-27","2022-07-26 13:41:40","2022-07-26 13:41:40" +7,Francesca,Thompson,winona15@hammes.com,Ferry PLC,0,"2004-05-19","2022-07-26 13:41:40","2022-07-26 13:41:40" +8,Crystal,Bode,nicolette73@gutmann.info,0,"1930-08-04","2022-07-26 13:41:40","2022-07-26 13:41:40" +9,Dorian,Gislason,ruby66@gmail.com,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" diff --git a/tests/stubs/file_with_errors.csv b/tests/stubs/file_with_errors.csv deleted file mode 100644 index e69de29..0000000