123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\TestCase;
- use PHPUnit\Framework\Assert;
- class WithHeadingRowTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->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);
- }
- }
|