-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathQueuedImportTest.php
239 lines (191 loc) · 8.06 KB
/
QueuedImportTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?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');
}
public function test_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');
}
public function test_can_queue_an_import()
{
$import = new QueuedImport();
$chain = $import->queue('import-batches.xlsx')->chain([
new AfterQueueImportJob(5000),
]);
$this->assertInstanceOf(PendingDispatch::class, $chain);
}
public function test_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');
config()->set('excel.cache.batch.memory_limit', 80);
// Reset the cache settings
$this->app->make(SettingsProvider::class)->provide();
$import = new QueuedImport();
$chain = $import->queue('import-batches.xlsx');
$this->assertInstanceOf(PendingDispatch::class, $chain);
}
public function test_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);
}
public function test_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');
}
public function test_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);
}
public function test_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());
}
public function test_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());
}
public function test_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');
}
public function test_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());
}
}
public function test_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());
}
}
public function test_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);
}
}