QueuedExportTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Maatwebsite\Excel\Tests;
  3. use Illuminate\Queue\Events\JobProcessing;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Queue;
  6. use Maatwebsite\Excel\Excel;
  7. use Maatwebsite\Excel\Files\RemoteTemporaryFile;
  8. use Maatwebsite\Excel\Files\TemporaryFile;
  9. use Maatwebsite\Excel\Jobs\AppendDataToSheet;
  10. use Maatwebsite\Excel\Tests\Data\Stubs\AfterQueueExportJob;
  11. use Maatwebsite\Excel\Tests\Data\Stubs\EloquentCollectionWithMappingExport;
  12. use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExport;
  13. use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithFailedEvents;
  14. use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithFailedHook;
  15. use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithLocalePreferences;
  16. use Maatwebsite\Excel\Tests\Data\Stubs\ShouldQueueExport;
  17. use Throwable;
  18. class QueuedExportTest extends TestCase
  19. {
  20. /**
  21. * @test
  22. */
  23. public function can_queue_an_export()
  24. {
  25. $export = new QueuedExport();
  26. $export->queue('queued-export.xlsx')->chain([
  27. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
  28. ]);
  29. }
  30. /**
  31. * @test
  32. */
  33. public function can_queue_an_export_and_store_on_different_disk()
  34. {
  35. $export = new QueuedExport();
  36. $export->queue('queued-export.xlsx', 'test')->chain([
  37. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
  38. ]);
  39. }
  40. /**
  41. * @test
  42. */
  43. public function can_queue_export_with_remote_temp_disk()
  44. {
  45. config()->set('excel.temporary_files.remote_disk', 'test');
  46. // Delete the local temp file before each append job
  47. // to simulate using a shared remote disk, without
  48. // having a dependency on a local temp file.
  49. $jobs = 0;
  50. Queue::before(function (JobProcessing $event) use (&$jobs) {
  51. if ($event->job->resolveName() === AppendDataToSheet::class) {
  52. /** @var TemporaryFile $tempFile */
  53. $tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
  54. $this->assertInstanceOf(RemoteTemporaryFile::class, $tempFile);
  55. // Should exist remote
  56. $this->assertTrue(
  57. $tempFile->exists()
  58. );
  59. // File was deleted locally
  60. $this->assertFalse(
  61. file_exists($tempFile->getLocalPath())
  62. );
  63. $jobs++;
  64. }
  65. });
  66. $export = new QueuedExport();
  67. $export->queue('queued-export.xlsx')->chain([
  68. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
  69. ]);
  70. $array = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', Excel::XLSX);
  71. $this->assertCount(100, $array);
  72. $this->assertEquals(3, $jobs);
  73. }
  74. /**
  75. * @test
  76. */
  77. public function can_queue_export_with_remote_temp_disk_and_prefix()
  78. {
  79. config()->set('excel.temporary_files.remote_disk', 'test');
  80. config()->set('excel.temporary_files.remote_prefix', 'tmp/');
  81. $export = new QueuedExport();
  82. $export->queue('queued-export.xlsx')->chain([
  83. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
  84. ]);
  85. }
  86. /**
  87. * @test
  88. */
  89. public function can_implicitly_queue_an_export()
  90. {
  91. $export = new ShouldQueueExport();
  92. $export->store('queued-export.xlsx', 'test')->chain([
  93. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
  94. ]);
  95. }
  96. /**
  97. * @test
  98. */
  99. public function can_queue_export_with_mapping_on_eloquent_models()
  100. {
  101. $export = new EloquentCollectionWithMappingExport();
  102. $export->queue('queued-export.xlsx')->chain([
  103. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
  104. ]);
  105. $actual = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', 'Xlsx');
  106. $this->assertEquals([
  107. ['Patrick', 'Brouwers'],
  108. ], $actual);
  109. }
  110. /**
  111. * @test
  112. */
  113. public function can_catch_failures()
  114. {
  115. $export = new QueuedExportWithFailedHook();
  116. try {
  117. $export->queue('queued-export.xlsx');
  118. } catch (Throwable $e) {
  119. }
  120. $this->assertTrue(app('queue-has-failed'));
  121. }
  122. /**
  123. * @test
  124. */
  125. public function can_catch_failures_on_queue_export_job()
  126. {
  127. $export = new QueuedExportWithFailedEvents();
  128. try {
  129. $export->queue('queued-export.xlsx');
  130. } catch (Throwable $e) {
  131. }
  132. $this->assertTrue(app('queue-has-failed-from-queue-export-job'));
  133. }
  134. /**
  135. * @test
  136. */
  137. public function can_set_locale_on_queue_export_job()
  138. {
  139. $currentLocale = app()->getLocale();
  140. $export = new QueuedExportWithLocalePreferences('ru');
  141. $export->queue('queued-export.xlsx');
  142. $this->assertTrue(app('queue-has-correct-locale'));
  143. $this->assertEquals($currentLocale, app()->getLocale());
  144. }
  145. /**
  146. * @test
  147. */
  148. public function can_queue_export_not_flushing_the_cache()
  149. {
  150. config()->set('excel.cache.driver', 'illuminate');
  151. Cache::put('test', 'test');
  152. $export = new QueuedExport();
  153. $export->queue('queued-export.xlsx')->chain([
  154. new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
  155. ]);
  156. $array = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', Excel::XLSX);
  157. $this->assertCount(100, $array);
  158. $this->assertEquals('test', Cache::get('test'));
  159. }
  160. }