| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
- use Maatwebsite\Excel\Concerns\ToArray;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithFormatData;
- use Maatwebsite\Excel\Tests\TestCase;
- use PHPUnit\Framework\Assert;
- class WithFormatDataTest extends TestCase
- {
- /**
- * @test
- */
- public function by_default_import_to_array()
- {
- $import = new class implements ToArray
- {
- use Importable;
- public $called = false;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertSame(44328, $array[0][0]);
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function can_import_to_array_with_format_data()
- {
- config()->set('excel.imports.read_only', false);
- $import = new class implements ToArray, WithFormatData
- {
- use Importable;
- public $called = false;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertSame('5/12/2021', $array[0][0]);
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function can_import_to_array_with_format_data_and_skips_empty_rows()
- {
- config()->set('excel.imports.read_only', false);
- $import = new class implements ToArray, WithFormatData, SkipsEmptyRows
- {
- use Importable;
- public $called = false;
- /**
- * @param array $array
- */
- public function array(array $array)
- {
- $this->called = true;
- Assert::assertSame('5/12/2021', $array[0][0]);
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function by_default_import_to_collection()
- {
- $import = new class implements ToCollection
- {
- use Importable;
- public $called = false;
- /**
- * @param array $row
- * @return Model|null
- */
- public function collection(collection $collection)
- {
- $this->called = true;
- Assert::assertSame(44328, $collection[0][0]);
- return null;
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function can_import_to_collection_with_format_data()
- {
- config()->set('excel.imports.read_only', false);
- $import = new class implements ToCollection, WithFormatData
- {
- use Importable;
- public $called = false;
- /**
- * @param array $row
- * @return Model|null
- */
- public function collection(collection $collection)
- {
- $this->called = true;
- Assert::assertSame('5/12/2021', $collection[0][0]);
- return null;
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function by_default_import_to_model()
- {
- $import = new class implements ToModel
- {
- use Importable;
- public $called = false;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- $this->called = true;
- Assert::assertSame(44328, $row[0]);
- return null;
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- /**
- * @test
- */
- public function can_import_to_model_with_format_data()
- {
- config()->set('excel.imports.read_only', false);
- $import = new class implements ToModel, WithFormatData
- {
- use Importable;
- public $called = false;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- $this->called = true;
- Assert::assertSame('5/12/2021', $row[0]);
- return null;
- }
- };
- $import->import('import-format-data.xlsx');
- $this->assertTrue($import->called);
- }
- }
|