-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathQueuedExportTest.php
171 lines (130 loc) · 5.38 KB
/
QueuedExportTest.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
<?php
namespace Maatwebsite\Excel\Tests;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Queue;
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Files\RemoteTemporaryFile;
use Maatwebsite\Excel\Files\TemporaryFile;
use Maatwebsite\Excel\Jobs\AppendDataToSheet;
use Maatwebsite\Excel\Tests\Data\Stubs\AfterQueueExportJob;
use Maatwebsite\Excel\Tests\Data\Stubs\EloquentCollectionWithMappingExport;
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExport;
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithFailedEvents;
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithFailedHook;
use Maatwebsite\Excel\Tests\Data\Stubs\QueuedExportWithLocalePreferences;
use Maatwebsite\Excel\Tests\Data\Stubs\ShouldQueueExport;
use Throwable;
class QueuedExportTest extends TestCase
{
public function test_can_queue_an_export()
{
$export = new QueuedExport();
$export->queue('queued-export.xlsx')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
]);
}
public function test_can_queue_an_export_and_store_on_different_disk()
{
$export = new QueuedExport();
$export->queue('queued-export.xlsx', 'test')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
]);
}
public function test_can_queue_export_with_remote_temp_disk()
{
config()->set('excel.temporary_files.remote_disk', 'test');
// Delete the local temp file before each append job
// to simulate using a shared remote disk, without
// having a dependency on a local temp file.
$jobs = 0;
Queue::before(function (JobProcessing $event) use (&$jobs) {
if ($event->job->resolveName() === AppendDataToSheet::class) {
/** @var TemporaryFile $tempFile */
$tempFile = $this->inspectJobProperty($event->job, 'temporaryFile');
$this->assertInstanceOf(RemoteTemporaryFile::class, $tempFile);
// Should exist remote
$this->assertTrue(
$tempFile->exists()
);
// File was deleted locally
$this->assertFalse(
file_exists($tempFile->getLocalPath())
);
$jobs++;
}
});
$export = new QueuedExport();
$export->queue('queued-export.xlsx')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
]);
$array = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', Excel::XLSX);
$this->assertCount(100, $array);
$this->assertEquals(3, $jobs);
}
public function test_can_queue_export_with_remote_temp_disk_and_prefix()
{
config()->set('excel.temporary_files.remote_disk', 'test');
config()->set('excel.temporary_files.remote_prefix', 'tmp/');
$export = new QueuedExport();
$export->queue('queued-export.xlsx')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
]);
}
public function test_can_implicitly_queue_an_export()
{
$export = new ShouldQueueExport();
$export->store('queued-export.xlsx', 'test')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Test/queued-export.xlsx'),
]);
}
public function test_can_queue_export_with_mapping_on_eloquent_models()
{
$export = new EloquentCollectionWithMappingExport();
$export->queue('queued-export.xlsx')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
]);
$actual = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', 'Xlsx');
$this->assertEquals([
['Patrick', 'Brouwers'],
], $actual);
}
public function test_can_catch_failures()
{
$export = new QueuedExportWithFailedHook();
try {
$export->queue('queued-export.xlsx');
} catch (Throwable $e) {
}
$this->assertTrue(app('queue-has-failed'));
}
public function test_can_catch_failures_on_queue_export_job()
{
$export = new QueuedExportWithFailedEvents();
try {
$export->queue('queued-export.xlsx');
} catch (Throwable $e) {
}
$this->assertTrue(app('queue-has-failed-from-queue-export-job'));
}
public function test_can_set_locale_on_queue_export_job()
{
$currentLocale = app()->getLocale();
$export = new QueuedExportWithLocalePreferences('ru');
$export->queue('queued-export.xlsx');
$this->assertTrue(app('queue-has-correct-locale'));
$this->assertEquals($currentLocale, app()->getLocale());
}
public function test_can_queue_export_not_flushing_the_cache()
{
config()->set('excel.cache.driver', 'illuminate');
Cache::put('test', 'test');
$export = new QueuedExport();
$export->queue('queued-export.xlsx')->chain([
new AfterQueueExportJob(__DIR__ . '/Data/Disks/Local/queued-export.xlsx'),
]);
$array = $this->readAsArray(__DIR__ . '/Data/Disks/Local/queued-export.xlsx', Excel::XLSX);
$this->assertCount(100, $array);
$this->assertEquals('test', Cache::get('test'));
}
}