123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Database\Eloquent\Builder;
- 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\WithUpsertColumns;
- use Maatwebsite\Excel\Concerns\WithUpserts;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\TestCase;
- class WithUpsertsTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- if (!method_exists(Builder::class, 'upsert')) {
- $this->markTestSkipped('The upsert feature is available on Laravel 8.10+');
- }
- parent::setUp();
- $this->loadLaravelMigrations(['--database' => 'testing']);
- }
- /**
- * @test
- */
- public function can_upsert_models_in_batches()
- {
- User::create([
- 'name' => 'Funny Banana',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithBatchInserts, WithUpserts
- {
- 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' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'secret',
- ]);
- $this->assertDatabaseHas('users', [
- 'name' => 'Taylor Otwell',
- 'email' => 'taylor@laravel.com',
- 'password' => 'secret',
- ]);
- $this->assertEquals(2, User::count());
- }
- /**
- * @test
- */
- public function can_upsert_models_in_rows()
- {
- User::create([
- 'name' => 'Funny Potato',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithUpserts
- {
- 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' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'secret',
- ]);
- $this->assertDatabaseHas('users', [
- 'name' => 'Taylor Otwell',
- 'email' => 'taylor@laravel.com',
- 'password' => 'secret',
- ]);
- $this->assertEquals(2, User::count());
- }
- /**
- * @test
- */
- public function can_upsert_models_in_batches_with_defined_upsert_columns()
- {
- User::create([
- 'name' => 'Funny Banana',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithBatchInserts, WithUpserts, WithUpsertColumns
- {
- 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 array
- */
- public function upsertColumns()
- {
- return ['name'];
- }
- /**
- * @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' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- $this->assertDatabaseHas('users', [
- 'name' => 'Taylor Otwell',
- 'email' => 'taylor@laravel.com',
- 'password' => 'secret',
- ]);
- $this->assertEquals(2, User::count());
- }
- /**
- * @test
- */
- public function can_upsert_models_in_rows_with_defined_upsert_columns()
- {
- User::create([
- 'name' => 'Funny Potato',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- DB::connection()->enableQueryLog();
- $import = new class implements ToModel, WithUpserts, WithUpsertColumns
- {
- 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';
- }
- /**
- * @return array
- */
- public function upsertColumns()
- {
- return ['name'];
- }
- };
- $import->import('import-users.xlsx');
- $this->assertCount(2, DB::getQueryLog());
- DB::connection()->disableQueryLog();
- $this->assertDatabaseHas('users', [
- 'name' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- 'password' => 'password',
- ]);
- $this->assertDatabaseHas('users', [
- 'name' => 'Taylor Otwell',
- 'email' => 'taylor@laravel.com',
- 'password' => 'secret',
- ]);
- $this->assertEquals(2, User::count());
- }
- }
|