123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Collection;
- use Illuminate\Validation\Rule;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\OnEachRow;
- use Maatwebsite\Excel\Concerns\SkipsFailures;
- use Maatwebsite\Excel\Concerns\SkipsOnFailure;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithBatchInserts;
- use Maatwebsite\Excel\Concerns\WithValidation;
- use Maatwebsite\Excel\Row;
- use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
- use Maatwebsite\Excel\Tests\TestCase;
- use Maatwebsite\Excel\Validators\Failure;
- use PHPUnit\Framework\Assert;
- class SkipsOnFailureTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->loadLaravelMigrations(['--database' => 'testing']);
- }
- /**
- * @test
- */
- public function can_skip_on_error()
- {
- $import = new class implements ToModel, WithValidation, SkipsOnFailure
- {
- use Importable;
- public $failures = 0;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new User([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- }
- /**
- * @return array
- */
- public function rules(): array
- {
- return [
- '1' => Rule::in(['patrick@maatwebsite.nl']),
- ];
- }
- /**
- * @param Failure[] $failures
- */
- public function onFailure(Failure ...$failures)
- {
- $failure = $failures[0];
- Assert::assertEquals(2, $failure->row());
- Assert::assertEquals('1', $failure->attribute());
- Assert::assertEquals(['The selected 1 is invalid.'], $failure->errors());
- Assert::assertEquals(['Taylor Otwell', 'taylor@laravel.com'], $failure->values());
- Assert::assertEquals(2, $failure->jsonSerialize()['row']);
- Assert::assertEquals('1', $failure->jsonSerialize()['attribute']);
- Assert::assertEquals(['The selected 1 is invalid.'], $failure->jsonSerialize()['errors']);
- Assert::assertEquals(['Taylor Otwell', 'taylor@laravel.com'], $failure->jsonSerialize()['values']);
- $this->failures += \count($failures);
- }
- };
- $import->import('import-users.xlsx');
- $this->assertEquals(1, $import->failures);
- // Shouldn't have rollbacked other imported rows.
- $this->assertDatabaseHas('users', [
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- // Should have skipped inserting
- $this->assertDatabaseMissing('users', [
- 'email' => 'taylor@laravel.com',
- ]);
- }
- /**
- * @test
- */
- public function skips_only_failed_rows_in_batch()
- {
- $import = new class implements ToModel, WithValidation, WithBatchInserts, SkipsOnFailure
- {
- use Importable;
- public $failures = 0;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new User([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- }
- /**
- * @return array
- */
- public function rules(): array
- {
- return [
- '1' => Rule::in(['patrick@maatwebsite.nl']),
- ];
- }
- /**
- * @param Failure[] $failures
- */
- public function onFailure(Failure ...$failures)
- {
- $failure = $failures[0];
- Assert::assertEquals(2, $failure->row());
- Assert::assertEquals('1', $failure->attribute());
- Assert::assertEquals(['The selected 1 is invalid.'], $failure->errors());
- $this->failures += \count($failures);
- }
- /**
- * @return int
- */
- public function batchSize(): int
- {
- return 100;
- }
- };
- $import->import('import-users.xlsx');
- $this->assertEquals(1, $import->failures);
- // Shouldn't have rollbacked/skipped the rest of the batch.
- $this->assertDatabaseHas('users', [
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- // Should have skipped inserting
- $this->assertDatabaseMissing('users', [
- 'email' => 'taylor@laravel.com',
- ]);
- }
- /**
- * @test
- */
- public function can_skip_failures_and_collect_all_failures_at_the_end()
- {
- $import = new class implements ToModel, WithValidation, SkipsOnFailure
- {
- use Importable, SkipsFailures;
- /**
- * @param array $row
- * @return Model|null
- */
- public function model(array $row)
- {
- return new User([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- }
- /**
- * @return array
- */
- public function rules(): array
- {
- return [
- '1' => Rule::in(['patrick@maatwebsite.nl']),
- ];
- }
- };
- $import->import('import-users.xlsx');
- $this->assertCount(1, $import->failures());
- /** @var Failure $failure */
- $failure = $import->failures()->first();
- $this->assertEquals(2, $failure->row());
- $this->assertEquals('1', $failure->attribute());
- $this->assertEquals(['The selected 1 is invalid.'], $failure->errors());
- // Shouldn't have rollbacked other imported rows.
- $this->assertDatabaseHas('users', [
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- // Should have skipped inserting
- $this->assertDatabaseMissing('users', [
- 'email' => 'taylor@laravel.com',
- ]);
- }
- /**
- * @test
- */
- public function can_validate_using_oneachrow_and_skipsonfailure()
- {
- $import = new class implements OnEachRow, WithValidation, SkipsOnFailure
- {
- use Importable, SkipsFailures;
- /**
- * @param Row $row
- * @return Model|null
- */
- public function onRow(Row $row)
- {
- $row = $row->toArray();
- return User::create([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- }
- /**
- * @return array
- */
- public function rules(): array
- {
- return [
- '1' => Rule::in(['patrick@maatwebsite.nl']),
- ];
- }
- };
- $this->assertEmpty(User::all());
- $import->import('import-users.xlsx');
- $this->assertCount(1, $import->failures());
- // Shouldn't have rollbacked other imported rows.
- $this->assertDatabaseHas('users', [
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- // Should have skipped inserting
- $this->assertDatabaseMissing('users', [
- 'email' => 'taylor@laravel.com',
- ]);
- }
- /**
- * @test
- */
- public function can_validate_using_tocollection_and_skipsonfailure()
- {
- $import = new class implements ToCollection, WithValidation, SkipsOnFailure
- {
- use Importable, SkipsFailures;
- /**
- * @param Row $row
- * @return Model|null
- */
- public function collection(Collection $rows)
- {
- $rows = $rows->each(function ($row) {
- return User::create([
- 'name' => $row[0],
- 'email' => $row[1],
- 'password' => 'secret',
- ]);
- });
- }
- /**
- * @return array
- */
- public function rules(): array
- {
- return [
- '1' => Rule::in(['patrick@maatwebsite.nl']),
- ];
- }
- };
- $this->assertEmpty(User::all());
- $import->import('import-users.xlsx');
- $this->assertCount(1, $import->failures());
- // Shouldn't have rollbacked other imported rows.
- $this->assertDatabaseHas('users', [
- 'email' => 'patrick@maatwebsite.nl',
- ]);
- // Should have skipped inserting
- $this->assertDatabaseMissing('users', [
- 'email' => 'taylor@laravel.com',
- ]);
- }
- }
|