/* Decoded by unphp.net */ markTestSkipped("The upsert feature is available on Laravel 8.10+"); } parent::setUp(); $this->loadLaravelMigrations(array("--database" => "testing")); } public function test_can_upsert_models_in_batches() { User::create(array("name" => "Funny Banana", "email" => "patrick@maatwebsite.nl", "password" => "password")); DB::connection()->enableQueryLog(); $import = new class implements ToModel, WithBatchInserts, WithUpserts { use Importable; public function model(array $row) { return new User(array("name" => $row[0], "email" => $row[1], "password" => "secret")); } public function uniqueBy() { return "email"; } public function batchSize() : int { return 2; } }; $import->import("import-users.xlsx"); $this->assertCount(1, DB::getQueryLog()); DB::connection()->disableQueryLog(); $this->assertDatabaseHas("users", array("name" => "Patrick Brouwers", "email" => "patrick@maatwebsite.nl", "password" => "secret")); $this->assertDatabaseHas("users", array("name" => "Taylor Otwell", "email" => "taylor@laravel.com", "password" => "secret")); $this->assertEquals(2, User::count()); } public function test_can_upsert_models_in_rows() { User::create(array("name" => "Funny Potato", "email" => "patrick@maatwebsite.nl", "password" => "password")); DB::connection()->enableQueryLog(); $import = new class implements ToModel, WithUpserts { use Importable; public function model(array $row) { return new User(array("name" => $row[0], "email" => $row[1], "password" => "secret")); } public function uniqueBy() { return "email"; } }; $import->import("import-users.xlsx"); $this->assertCount(2, DB::getQueryLog()); DB::connection()->disableQueryLog(); $this->assertDatabaseHas("users", array("name" => "Patrick Brouwers", "email" => "patrick@maatwebsite.nl", "password" => "secret")); $this->assertDatabaseHas("users", array("name" => "Taylor Otwell", "email" => "taylor@laravel.com", "password" => "secret")); $this->assertEquals(2, User::count()); } public function test_can_upsert_models_in_batches_with_defined_upsert_columns() { User::create(array("name" => "Funny Banana", "email" => "patrick@maatwebsite.nl", "password" => "password")); DB::connection()->enableQueryLog(); $import = new class implements ToModel, WithBatchInserts, WithUpserts, WithUpsertColumns { use Importable; public function model(array $row) { return new User(array("name" => $row[0], "email" => $row[1], "password" => "secret")); } public function uniqueBy() { return "email"; } public function upsertColumns() { return array("name"); } public function batchSize() : int { return 2; } }; $import->import("import-users.xlsx"); $this->assertCount(1, DB::getQueryLog()); DB::connection()->disableQueryLog(); $this->assertDatabaseHas("users", array("name" => "Patrick Brouwers", "email" => "patrick@maatwebsite.nl", "password" => "password")); $this->assertDatabaseHas("users", array("name" => "Taylor Otwell", "email" => "taylor@laravel.com", "password" => "secret")); $this->assertEquals(2, User::count()); } public function test_can_upsert_models_in_rows_with_defined_upsert_columns() { User::create(array("name" => "Funny Potato", "email" => "patrick@maatwebsite.nl", "password" => "password")); DB::connection()->enableQueryLog(); $import = new class implements ToModel, WithUpserts, WithUpsertColumns { use Importable; public function model(array $row) { return new User(array("name" => $row[0], "email" => $row[1], "password" => "secret")); } public function uniqueBy() { return "email"; } public function upsertColumns() { return array("name"); } }; $import->import("import-users.xlsx"); $this->assertCount(2, DB::getQueryLog()); DB::connection()->disableQueryLog(); $this->assertDatabaseHas("users", array("name" => "Patrick Brouwers", "email" => "patrick@maatwebsite.nl", "password" => "password")); $this->assertDatabaseHas("users", array("name" => "Taylor Otwell", "email" => "taylor@laravel.com", "password" => "secret")); $this->assertEquals(2, User::count()); } } ?>