-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathHeadingRowImportTest.php
151 lines (117 loc) · 3.91 KB
/
HeadingRowImportTest.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
<?php
namespace Maatwebsite\Excel\Tests;
use Maatwebsite\Excel\HeadingRowImport;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
class HeadingRowImportTest extends TestCase
{
protected function tearDown(): void
{
HeadingRowFormatter::reset();
parent::tearDown();
}
public function test_can_import_only_heading_row()
{
$import = new HeadingRowImport();
$headings = $import->toArray('import-users-with-headings.xlsx');
$this->assertEquals([
[
['name', 'email'],
],
], $headings);
}
public function test_can_import_only_heading_row_with_custom_heading_row_formatter()
{
HeadingRowFormatter::extend('custom', function ($value) {
return 'custom-' . $value;
});
HeadingRowFormatter::default('custom');
$import = new HeadingRowImport();
$headings = $import->toArray('import-users-with-headings.xlsx');
$this->assertEquals([
[
['custom-name', 'custom-email'],
],
], $headings);
}
public function test_can_import_only_heading_row_with_custom_heading_row_formatter_with_key()
{
HeadingRowFormatter::extend('custom', function ($value, $key) {
return $key;
});
HeadingRowFormatter::default('custom');
$import = new HeadingRowImport();
$headings = $import->toArray('import-users-with-headings.xlsx');
$this->assertEquals([
[
[0, 1],
],
], $headings);
}
public function test_can_import_only_heading_row_with_custom_row_number()
{
$import = new HeadingRowImport(2);
$headings = $import->toArray('import-users-with-headings.xlsx');
$this->assertEquals([
[
['patrick_brouwers', 'patrick_at_maatwebsitenl'],
],
], $headings);
}
public function test_can_import_only_heading_row_for_multiple_sheets()
{
$import = new HeadingRowImport();
$headings = $import->toArray('import-multiple-sheets.xlsx');
$this->assertEquals([
[
['1a1', '1b1'], // slugged first row of sheet 1
],
[
['2a1', '2b1'], // slugged first row of sheet 2
],
], $headings);
}
public function test_can_import_only_heading_row_for_multiple_sheets_with_key()
{
HeadingRowFormatter::extend('custom', function ($value, $key) {
return $key;
});
HeadingRowFormatter::default('custom');
$import = new HeadingRowImport();
$headings = $import->toArray('import-multiple-sheets.xlsx');
$this->assertEquals([
[
[0, 1], // slugged first row of sheet 1
],
[
[0, 1], // slugged first row of sheet 2
],
], $headings);
}
public function test_can_import_only_heading_row_for_multiple_sheets_with_custom_row_number()
{
$import = new HeadingRowImport(2);
$headings = $import->toArray('import-multiple-sheets.xlsx');
$this->assertEquals([
[
['1a2', '1b2'], // slugged 2nd row of sheet 1
],
[
['2a2', '2b2'], // slugged 2nd row of sheet 2
],
], $headings);
}
public function test_can_import_heading_row_with_custom_formatter_defined_in_config()
{
HeadingRowFormatter::extend('custom2', function ($value) {
return 'custom2-' . $value;
});
config()->set('excel.imports.heading_row.formatter', 'custom2');
$import = new HeadingRowImport();
$headings = $import->toArray('import-users-with-headings.xlsx');
$this->assertEquals([
[
['custom2-name', 'custom2-email'],
],
], $headings);
}
}