loadLaravelMigrations(['--database' => 'testing']); } /** * @test */ public function can_import_each_row_to_model_with_heading_row() { $import = new class implements ToModel, WithHeadingRow { use Importable; /** * @param array $row * @return Model */ public function model(array $row): Model { return new User([ 'name' => $row['name'], 'email' => $row['email'], 'password' => 'secret', ]); } }; $import->import('import-users-with-headings.xlsx'); $this->assertDatabaseHas('users', [ 'name' => 'Patrick Brouwers', 'email' => 'patrick@maatwebsite.nl', ]); $this->assertDatabaseHas('users', [ 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ]); } /** * @test */ public function can_import_each_row_to_model_with_different_heading_row() { $import = new class implements ToModel, WithHeadingRow { use Importable; /** * @param array $row * @return Model */ public function model(array $row): Model { return new User([ 'name' => $row['name'], 'email' => $row['email'], 'password' => 'secret', ]); } /** * @return int */ public function headingRow(): int { return 4; } }; $import->import('import-users-with-different-heading-row.xlsx'); $this->assertDatabaseHas('users', [ 'name' => 'Patrick Brouwers', 'email' => 'patrick@maatwebsite.nl', ]); $this->assertDatabaseHas('users', [ 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ]); } /** * @test */ public function can_import_to_array_with_heading_row() { $import = new class implements ToArray, WithHeadingRow { use Importable; /** * @param array $array */ public function array(array $array) { Assert::assertEquals([ [ 'name' => 'Patrick Brouwers', 'email' => 'patrick@maatwebsite.nl', ], [ 'name' => 'Taylor Otwell', 'email' => 'taylor@laravel.com', ], ], $array); } }; $import->import('import-users-with-headings.xlsx'); } /** * @test */ public function can_import_empty_rows_with_header() { $import = new class() implements ToArray, WithHeadingRow { use Importable; /** * @param array $array */ public function array(array $array) { Assert::assertEmpty($array); } }; $import->import('import-empty-users-with-headings.xlsx'); } /** * @test */ public function can_import_empty_models_with_header() { $import = new class() implements ToModel, WithHeadingRow { use Importable; /** * @param array $row * @return Model */ public function model(array $row): Model { return new User([ 'name' => $row['name'], 'email' => $row['email'], 'password' => 'secret', ]); } }; $import->import('import-empty-users-with-headings.xlsx'); $this->assertEmpty(User::all()); } /** * @test */ public function can_cast_empty_headers_to_indexed_int() { $import = new class() implements ToCollection, WithHeadingRow { use Importable; public $called = false; public function collection(Collection $collection) { $this->called = true; Assert::assertEquals([ 0 => 0, 1 => 'email', 2 => 'status', 3 => 3, ], $collection->first()->keys()->toArray()); } }; $import->import('import-users-with-mixed-headings.xlsx'); $this->assertTrue($import->called); } }