| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace Maatwebsite\Excel\Tests;
- use Illuminate\Foundation\Bus\PendingDispatch;
- use Illuminate\Queue\Events\JobExceptionOccurred;
- use Illuminate\Queue\Events\JobProcessed;
- use Illuminate\Queue\Events\JobProcessing;
- use Illuminate\Support\Facades\Bus;
- use Illuminate\Support\Facades\Queue;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Files\RemoteTemporaryFile;
- use Maatwebsite\Excel\Files\TemporaryFile;
- use Maatwebsite\Excel\Jobs\AfterImportJob;
- use Maatwebsite\Excel\Jobs\ReadChunk;
- use Maatwebsite\Excel\SettingsProvider;
- use Maatwebsite\Excel\Tests\Data\Stubs\AfterQueueImportJob;
- use Maatwebsite\Excel\Tests\Data\Stubs\QueuedImport;
- use Maatwebsite\Excel\Tests\Data\Stubs\QueuedImportWithFailure;
- use Maatwebsite\Excel\Tests\Data\Stubs\QueuedImportWithMiddleware;
- use Maatwebsite\Excel\Tests\Data\Stubs\QueuedImportWithRetryUntil;
- use Throwable;
- class QueuedImportTest extends TestCase
- {
- /**
- * Setup the test environment.
- */
- protected function setUp(): void
- {
- parent::setUp();
- $this->loadLaravelMigrations(['--database' => 'testing']);
- $this->loadMigrationsFrom(__DIR__ . '/Data/Stubs/Database/Migrations');
- }
- /**
- * @test
- */
- public function cannot_queue_import_that_does_not_implement_should_queue()
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Importable should implement ShouldQueue to be queued.');
- $import = new class
- {
- use Importable;
- };
- $import->queue('import-batches.xlsx');
- }
- /**
- * @test
- */
- public function can_queue_an_import()
- {
- $import = new QueuedImport();
- $chain = $import->queue('import-batches.xlsx')->chain([
- new AfterQueueImportJob(5000),
- ]);
- $this->assertInstanceOf(PendingDispatch::class, $chain);
- }
- /**
- * @test
- */
- public function can_queue_an_import_with_batch_cache_and_file_store()
- {
- config()->set('queue.default', 'sync');
- config()->set('excel.cache.driver', 'batch');
- config()->set('excel.cache.illuminate.store', 'file');
- // Reset the cache settings
- $this->app->make(SettingsProvider::class)->provide();
- $import = new QueuedImport();
- $chain = $import->queue('import-batches.xlsx');
- $this->assertInstanceOf(PendingDispatch::class, $chain);
- }
- /**
- * @test
- */
- public function can_queue_import_with_remote_temp_disk()
- {
- config()->set('excel.temporary_files.remote_disk', 'test');
- // Delete the local temp file before each read chunk job
- // to simulate using a shared remote disk, without
- // having a dependency on a local temp file.
- Queue::before(function (JobProcessing $event) {
- if ($event->job->resolveName() === ReadChunk::class) {
- /** @var TemporaryFile $tempFile */
- $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
- $this->assertInstanceOf(RemoteTemporaryFile::class, $tempFile);
- // Should exist remote
- $this->assertTrue(
- $tempFile->exists()
- );
- $this->assertTrue(
- unlink($tempFile->getLocalPath())
- );
- }
- });
- $import = new QueuedImport();
- $chain = $import->queue('import-batches.xlsx')->chain([
- new AfterQueueImportJob(5000),
- ]);
- $this->assertInstanceOf(PendingDispatch::class, $chain);
- }
- /**
- * @test
- */
- public function can_keep_extension_for_temp_file_on_remote_disk()
- {
- config()->set('excel.temporary_files.remote_disk', 'test');
- Queue::before(function (JobProcessing $event) {
- if ($event->job->resolveName() === ReadChunk::class) {
- /** @var TemporaryFile $tempFile */
- $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
- $this->assertStringContains('.xlsx', $tempFile->getLocalPath());
- }
- });
- (new QueuedImport())->queue('import-batches.xlsx');
- }
- /**
- * @test
- */
- public function can_queue_import_with_remote_temp_disk_and_prefix()
- {
- config()->set('excel.temporary_files.remote_disk', 'test');
- config()->set('excel.temporary_files.remote_prefix', 'tmp/');
- $import = new QueuedImport();
- $chain = $import->queue('import-batches.xlsx')->chain([
- new AfterQueueImportJob(5000),
- ]);
- $this->assertInstanceOf(PendingDispatch::class, $chain);
- }
- /**
- * @test
- */
- public function can_automatically_delete_temp_file_on_failure_when_using_remote_disk()
- {
- config()->set('excel.temporary_files.remote_disk', 'test');
- $tempFile = '';
- Queue::exceptionOccurred(function (JobExceptionOccurred $event) use (&$tempFile) {
- if ($event->job->resolveName() === ReadChunk::class) {
- $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
- }
- });
- try {
- (new QueuedImportWithFailure())->queue('import-batches.xlsx');
- } catch (Throwable $e) {
- $this->assertEquals('Something went wrong in the chunk', $e->getMessage());
- }
- $this->assertFalse($tempFile->existsLocally());
- $this->assertTrue($tempFile->exists());
- }
- /**
- * @test
- */
- public function cannot_automatically_delete_temp_file_on_failure_when_using_local_disk()
- {
- $tempFile = '';
- Queue::exceptionOccurred(function (JobExceptionOccurred $event) use (&$tempFile) {
- if ($event->job->resolveName() === ReadChunk::class) {
- $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
- }
- });
- try {
- (new QueuedImportWithFailure())->queue('import-batches.xlsx');
- } catch (Throwable $e) {
- $this->assertEquals('Something went wrong in the chunk', $e->getMessage());
- }
- $this->assertTrue($tempFile->exists());
- }
- /**
- * @test
- */
- public function can_force_remote_download_and_deletion_for_each_chunk_on_queue()
- {
- config()->set('excel.temporary_files.remote_disk', 'test');
- config()->set('excel.temporary_files.force_resync_remote', true);
- Bus::fake([AfterImportJob::class]);
- Queue::after(function (JobProcessed $event) {
- if ($event->job->resolveName() === ReadChunk::class) {
- $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
- // Should not exist locally after each chunk
- $this->assertFalse(
- $tempFile->existsLocally()
- );
- }
- });
- (new QueuedImport())->queue('import-batches.xlsx');
- }
- /**
- * @test
- */
- public function can_define_middleware_method_on_queued_import()
- {
- try {
- (new QueuedImportWithMiddleware())->queue('import-batches.xlsx');
- } catch (Throwable $e) {
- $this->assertEquals('Job reached middleware method', $e->getMessage());
- }
- }
- /**
- * @test
- */
- public function can_define_retry_until_method_on_queued_import()
- {
- try {
- (new QueuedImportWithRetryUntil())->queue('import-batches.xlsx');
- } catch (Throwable $e) {
- $this->assertEquals('Job reached retryUntil method', $e->getMessage());
- }
- }
- /**
- * @test
- */
- public function can_define_max_exceptions_property_on_queued_import()
- {
- $maxExceptionsCount = 0;
- Queue::exceptionOccurred(function (JobExceptionOccurred $event) use (&$maxExceptionsCount) {
- if ($event->job->resolveName() === ReadChunk::class) {
- $maxExceptionsCount = $this->inspectJobProperty($event->job, 'maxExceptions');
- }
- });
- try {
- $import = new QueuedImportWithFailure();
- $import->maxExceptions = 3;
- $import->queue('import-batches.xlsx');
- } catch (Throwable $e) {
- $this->assertEquals('Something went wrong in the chunk', $e->getMessage());
- }
- $this->assertEquals(3, $maxExceptionsCount);
- }
- }
|