-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathExcelTest.php
437 lines (364 loc) · 12 KB
/
ExcelTest.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
namespace Maatwebsite\Excel\Tests;
use Illuminate\Contracts\View\View;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Facades\Excel as ExcelFacade;
use Maatwebsite\Excel\Importer;
use Maatwebsite\Excel\Tests\Data\Stubs\EmptyExport;
use Maatwebsite\Excel\Tests\Helpers\FileHelper;
use PHPUnit\Framework\Assert;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ExcelTest extends TestCase
{
/**
* @var Excel
*/
protected $SUT;
protected function setUp(): void
{
parent::setUp();
$this->SUT = $this->app->make(Excel::class);
}
public function test_can_download_an_export_object_with_facade()
{
$export = new EmptyExport();
$response = ExcelFacade::download($export, 'filename.xlsx');
$this->assertInstanceOf(BinaryFileResponse::class, $response);
$this->assertEquals('attachment; filename=filename.xlsx', str_replace('"', '', $response->headers->get('Content-Disposition')));
}
public function test_can_download_an_export_object()
{
$export = new EmptyExport();
$response = $this->SUT->download($export, 'filename.xlsx');
$this->assertInstanceOf(BinaryFileResponse::class, $response);
$this->assertEquals('attachment; filename=filename.xlsx', str_replace('"', '', $response->headers->get('Content-Disposition')));
}
public function test_can_store_an_export_object_on_default_disk()
{
$export = new EmptyExport;
$name = 'filename.xlsx';
$path = FileHelper::absolutePath($name, 'local');
@unlink($path);
$this->assertFileMissing($path);
$response = $this->SUT->store($export, $name);
$this->assertTrue($response);
$this->assertFileExists($path);
}
public function test_can_store_an_export_object_on_another_disk()
{
$export = new EmptyExport;
$name = 'filename.xlsx';
$path = FileHelper::absolutePath($name, 'test');
@unlink($path);
$this->assertFileMissing($path);
$response = $this->SUT->store($export, $name, 'test');
$this->assertTrue($response);
$this->assertFileExists($path);
}
public function test_can_store_csv_export_with_default_settings()
{
$export = new EmptyExport;
$name = 'filename.csv';
$path = FileHelper::absolutePath($name, 'local');
@unlink($path);
$this->assertFileMissing($path);
$response = $this->SUT->store($export, $name);
$this->assertTrue($response);
$this->assertFileExists($path);
}
public function test_can_get_raw_export_contents()
{
$export = new EmptyExport;
$response = $this->SUT->raw($export, Excel::XLSX);
$this->assertNotEmpty($response);
}
public function test_can_store_tsv_export_with_default_settings()
{
$export = new EmptyExport;
$name = 'filename.tsv';
$path = FileHelper::absolutePath($name, 'local');
@unlink($path);
$this->assertFileMissing($path);
$response = $this->SUT->store($export, $name);
$this->assertTrue($response);
$this->assertFileExists($path);
}
public function test_can_store_csv_export_with_custom_settings()
{
$export = new class implements WithEvents, FromCollection, WithCustomCsvSettings
{
use RegistersEventListeners;
/**
* @return Collection
*/
public function collection()
{
return collect([
['A1', 'B1'],
['A2', 'B2'],
]);
}
/**
* @return array
*/
public function getCsvSettings(): array
{
return [
'line_ending' => PHP_EOL,
'enclosure' => '"',
'delimiter' => ';',
'include_separator_line' => true,
'excel_compatibility' => false,
];
}
};
$this->SUT->store($export, 'filename.csv');
$contents = file_get_contents(__DIR__ . '/Data/Disks/Local/filename.csv');
$this->assertStringContains('sep=;', $contents);
$this->assertStringContains('"A1";"B1"', $contents);
$this->assertStringContains('"A2";"B2"', $contents);
}
public function test_cannot_use_from_collection_and_from_view_on_same_export()
{
$this->expectException(\Maatwebsite\Excel\Exceptions\ConcernConflictException::class);
$this->expectExceptionMessage('Cannot use FromQuery, FromArray or FromCollection and FromView on the same sheet');
$export = new class implements FromCollection, FromView
{
use Exportable;
/**
* @return Collection
*/
public function collection()
{
return collect();
}
/**
* @return View
*/
public function view(): View
{
return view('users');
}
};
$export->download('filename.csv');
}
public function test_can_import_a_simple_xlsx_file_to_array()
{
$import = new class
{
use Importable;
};
$this->assertEquals([
[
['test', 'test'],
['test', 'test'],
],
], $import->toArray('import.xlsx'));
}
public function test_can_import_a_simple_xlsx_file_to_collection()
{
$import = new class
{
use Importable;
};
$this->assertEquals(new Collection([
new Collection([
new Collection(['test', 'test']),
new Collection(['test', 'test']),
]),
]), $import->toCollection('import.xlsx'));
}
public function test_can_import_a_simple_xlsx_file_to_collection_without_import_object()
{
$this->assertEquals(new Collection([
new Collection([
new Collection(['test', 'test']),
new Collection(['test', 'test']),
]),
]), ExcelFacade::toCollection(null, 'import.xlsx'));
}
public function test_can_import_a_simple_xlsx_file()
{
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$imported = $this->SUT->import($import, 'import.xlsx');
$this->assertInstanceOf(Importer::class, $imported);
}
public function test_can_import_a_tsv_file()
{
$import = new class implements ToArray, WithCustomCsvSettings
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
'tconst',
'titleType',
'primaryTitle',
'originalTitle',
'isAdult',
'startYear',
'endYear',
'runtimeMinutes',
'genres',
], $array[0]);
}
/**
* @return array
*/
public function getCsvSettings(): array
{
return [
'delimiter' => "\t",
];
}
};
$imported = $this->SUT->import($import, 'import-titles.tsv');
$this->assertInstanceOf(Importer::class, $imported);
}
public function test_can_chain_imports()
{
$import1 = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$import2 = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$imported = $this->SUT
->import($import1, 'import.xlsx')
->import($import2, 'import.xlsx');
$this->assertInstanceOf(Importer::class, $imported);
}
public function test_can_import_a_simple_xlsx_file_from_uploaded_file()
{
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$this->SUT->import($import, $this->givenUploadedFile(__DIR__ . '/Data/Disks/Local/import.xlsx'));
}
public function test_can_import_a_simple_xlsx_file_from_real_path()
{
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$this->SUT->import($import, __DIR__ . '/Data/Disks/Local/import.xlsx');
}
public function test_import_will_throw_error_when_no_reader_type_could_be_detected_when_no_extension()
{
$this->expectException(\Maatwebsite\Excel\Exceptions\NoTypeDetectedException::class);
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$this->SUT->import($import, UploadedFile::fake()->create('import'));
}
public function test_import_will_throw_error_when_no_reader_type_could_be_detected_with_unknown_extension()
{
$this->expectException(\Maatwebsite\Excel\Exceptions\NoTypeDetectedException::class);
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
//
}
};
$this->SUT->import($import, 'unknown-reader-type.zip');
}
public function test_can_import_without_extension_with_explicit_reader_type()
{
$import = new class implements ToArray
{
/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
['test', 'test'],
['test', 'test'],
], $array);
}
};
$this->SUT->import(
$import,
$this->givenUploadedFile(__DIR__ . '/Data/Disks/Local/import.xlsx', 'import'),
null,
Excel::XLSX
);
}
}