123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Support\Str;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithMappedCells;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\TestCase;
- use PHPUnit\Framework\Assert;
- class WithMappedCellsTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->loadLaravelMigrations(['--database' => 'testing']);
- }
- /**
- * @test
- */
- public function can_import_with_references_to_cells()
- {
- $import = new class implements WithMappedCells, ToArray
- {
- use Importable;
- /**
- * @return array
- */
- public function mapping(): array
- {
- return [
- 'name' => 'B1',
- 'email' => 'B2',
- ];
- }
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- 'name' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- ], $array);
- }
- };
- $import->import('mapped-import.xlsx');
- }
- /**
- * @test
- */
- public function can_import_with_nested_references_to_cells()
- {
- $import = new class implements WithMappedCells, ToArray
- {
- use Importable;
- /**
- * @return array
- */
- public function mapping(): array
- {
- return [
- [
- 'name' => 'B1',
- 'email' => 'B2',
- ],
- [
- 'name' => 'D1',
- 'email' => 'D2',
- ],
- ];
- }
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- Assert::assertEquals([
- [
- 'name' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- ],
- [
- 'name' => 'Typingbeaver',
- 'email' => 'typingbeaver@mailbox.org',
- ],
- ], $array);
- }
- };
- $import->import('mapped-import.xlsx');
- }
- /**
- * @test
- */
- public function can_import_with_references_to_cells_to_model()
- {
- $import = new class implements WithMappedCells, ToModel
- {
- use Importable;
- /**
- * @return array
- */
- public function mapping(): array
- {
- return [
- 'name' => 'B1',
- 'email' => 'B2',
- ];
- }
- /**
- * @param array $array
- * @return User
- */
- public function model(array $array)
- {
- Assert::assertEquals([
- 'name' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- ], $array);
- $array['password'] = Str::random();
- return new User($array);
- }
- };
- $import->import('mapped-import.xlsx');
- $this->assertDatabaseHas('users', [
- 'name' => 'Patrick Brouwers',
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- }
- }
|