Skip to content

Commit

Permalink
Merge branch 'SpartnerNL:3.1' into 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dazza-dev authored Sep 11, 2024
2 parents 5065171 + 18495a7 commit b615bc9
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"ext-json": "*",
"php": "^7.0||^8.0",
"phpoffice/phpspreadsheet": "^1.18",
"phpoffice/phpspreadsheet": "^1.29.1",
"illuminate/support": "5.8.*||^6.0||^7.0||^8.0||^9.0||^10.0||^11.0",
"psr/simple-cache": "^1.0||^2.0||^3.0",
"composer/semver": "^3.3"
Expand Down
3 changes: 2 additions & 1 deletion config/excel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Maatwebsite\Excel\Excel;
use PhpOffice\PhpSpreadsheet\Reader\Csv;

return [
'exports' => [
Expand Down Expand Up @@ -127,7 +128,7 @@
'enclosure' => '"',
'escape_character' => '\\',
'contiguous' => false,
'input_encoding' => 'UTF-8',
'input_encoding' => Csv::GUESS_ENCODING,
],

/*
Expand Down
7 changes: 7 additions & 0 deletions src/Concerns/WithSkipDuplicates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Maatwebsite\Excel\Concerns;

interface WithSkipDuplicates
{
}
3 changes: 2 additions & 1 deletion src/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use Maatwebsite\Excel\Files\Filesystem;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\Helpers\FileTypeDetector;

class Excel implements Exporter, Importer
{
use RegistersCustomConcerns;
use Macroable, RegistersCustomConcerns;

const XLSX = 'Xlsx';

Expand Down
3 changes: 3 additions & 0 deletions src/Fakes/ExcelFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Traits\Macroable;
use Maatwebsite\Excel\Exporter;
use Maatwebsite\Excel\Importer;
use Maatwebsite\Excel\Reader;
Expand All @@ -16,6 +17,8 @@

class ExcelFake implements Exporter, Importer
{
use Macroable;

/**
* @var array
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Imports/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Maatwebsite\Excel\Concerns\PersistRelations;
use Maatwebsite\Excel\Concerns\SkipsOnError;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithSkipDuplicates;
use Maatwebsite\Excel\Concerns\WithUpsertColumns;
use Maatwebsite\Excel\Concerns\WithUpserts;
use Maatwebsite\Excel\Concerns\WithValidation;
Expand Down Expand Up @@ -122,6 +123,10 @@ private function massFlush(ToModel $import)
$import instanceof WithUpsertColumns ? $import->upsertColumns() : null
);

return;
} elseif ($import instanceof WithSkipDuplicates) {
$model::query()->insertOrIgnore($models->toArray());

return;
}

Expand Down Expand Up @@ -149,6 +154,10 @@ private function singleFlush(ToModel $import)
$import instanceof WithUpsertColumns ? $import->upsertColumns() : null
);

return;
} elseif ($import instanceof WithSkipDuplicates) {
$model::query()->insertOrIgnore([$model->getAttributes()]);

return;
}

Expand Down
12 changes: 11 additions & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ public function reopen(TemporaryFile $tempFile, string $writerType)
return $this;
}

/**
* Determine if the application is running in a serverless environment.
*
* @return bool
*/
public function isRunningServerless(): bool
{
return isset($_ENV['AWS_LAMBDA_RUNTIME_API']);
}

/**
* @param object $export
* @param TemporaryFile $temporaryFile
Expand All @@ -166,7 +176,7 @@ public function write($export, TemporaryFile $temporaryFile, string $writerType)
$export
);

if ($temporaryFile instanceof RemoteTemporaryFile && !$temporaryFile->existsLocally()) {
if ($temporaryFile instanceof RemoteTemporaryFile && !$temporaryFile->existsLocally() && !$this->isRunningServerless()) {
$temporaryFile = resolve(TemporaryFileFactory::class)
->makeLocal(Arr::last(explode('/', $temporaryFile->getLocalPath())));
}
Expand Down
145 changes: 145 additions & 0 deletions tests/Concerns/WithSkipDuplicatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Maatwebsite\Excel\Tests\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithSkipDuplicates;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
use Maatwebsite\Excel\Tests\TestCase;

class WithSkipDuplicatesTest extends TestCase
{
/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$this->loadLaravelMigrations(['--database' => 'testing']);
}

public function test_can_skip_duplicate_models_in_batches()
{
User::create([
'name' => 'Funny Banana',
'email' => '[email protected]',
'password' => 'password',
]);

DB::connection()->enableQueryLog();

$import = new class implements ToModel, WithBatchInserts, WithSkipDuplicates
{
use Importable;

/**
* @param array $row
* @return Model|null
*/
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => 'secret',
]);
}

/**
* @return string|array
*/
public function uniqueBy()
{
return 'email';
}

/**
* @return int
*/
public function batchSize(): int
{
return 2;
}
};

$import->import('import-users.xlsx');

$this->assertCount(1, DB::getQueryLog());
DB::connection()->disableQueryLog();

$this->assertDatabaseHas('users', [
'name' => 'Funny Banana',
'email' => '[email protected]',
'password' => 'password',
]);

$this->assertDatabaseHas('users', [
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'secret',
]);

$this->assertEquals(2, User::count());
}

public function test_can_skip_duplicate_models_in_rows()
{
User::create([
'name' => 'Funny Potato',
'email' => '[email protected]',
'password' => 'password',
]);

DB::connection()->enableQueryLog();

$import = new class implements ToModel, WithSkipDuplicates
{
use Importable;

/**
* @param array $row
* @return Model|Model[]|null
*/
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => 'secret',
]);
}

/**
* @return string|array
*/
public function uniqueBy()
{
return 'email';
}
};

$import->import('import-users.xlsx');

$this->assertCount(2, DB::getQueryLog());
DB::connection()->disableQueryLog();

$this->assertDatabaseHas('users', [
'name' => 'Funny Potato',
'email' => '[email protected]',
'password' => 'password',
]);

$this->assertDatabaseHas('users', [
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => 'secret',
]);

$this->assertEquals(2, User::count());
}
}

0 comments on commit b615bc9

Please sign in to comment.