|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
The MIT License (MIT) |
|
4
|
|
|
|
|
5
|
|
|
Copyright (c) 2015 PortPHP |
|
6
|
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
in the Software without restriction, including without limitation the rights |
|
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
furnished to do so, subject to the following conditions: |
|
13
|
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
copies or substantial portions of the Software. |
|
16
|
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace Port\Spreadsheet\Tests; |
|
26
|
|
|
|
|
27
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
|
28
|
|
|
use Port\Spreadsheet\SpreadsheetWriter; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
class SpreadsheetWriterTest extends \PHPUnit_Framework_TestCase |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritDoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
if (!extension_loaded('zip')) { |
|
41
|
|
|
$this->markTestSkipped(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Test that column names not prepended to first row if SpreadsheetWriter's 4-th |
|
47
|
|
|
* parameter not given |
|
48
|
|
|
* |
|
49
|
|
|
* @author Igor Mukhin <[email protected]> |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testHeaderNotPrependedByDefault() |
|
52
|
|
|
{ |
|
53
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
54
|
|
|
|
|
55
|
|
|
$writer = new SpreadsheetWriter(new \SplFileObject($file, 'w'), null, 'Xlsx'); |
|
56
|
|
|
$writer->prepare(); |
|
57
|
|
|
$writer->writeItem(array( |
|
58
|
|
|
'col 1 name' => 'col 1 value', |
|
59
|
|
|
'col 2 name' => 'col 2 value', |
|
60
|
|
|
'col 3 name' => 'col 3 value', |
|
61
|
|
|
)); |
|
62
|
|
|
$writer->finish(); |
|
63
|
|
|
|
|
64
|
|
|
$excel = IOFactory::load($file); |
|
65
|
|
|
$sheet = $excel->getActiveSheet()->toArray(); |
|
66
|
|
|
|
|
67
|
|
|
// Values should be at first line |
|
68
|
|
|
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[0]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Test that column names prepended at first row |
|
73
|
|
|
* and values have been written at second line |
|
74
|
|
|
* if SpreadsheetWriter's 4-th parameter set to true |
|
75
|
|
|
* |
|
76
|
|
|
* @author Igor Mukhin <[email protected]> |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testHeaderPrependedWhenOptionSetToTrue() |
|
79
|
|
|
{ |
|
80
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
81
|
|
|
|
|
82
|
|
|
$writer = new SpreadsheetWriter(new \SplFileObject($file, 'w'), null, 'Xlsx', true); |
|
83
|
|
|
$writer->prepare(); |
|
84
|
|
|
$writer->writeItem(array( |
|
85
|
|
|
'col 1 name' => 'col 1 value', |
|
86
|
|
|
'col 2 name' => 'col 2 value', |
|
87
|
|
|
'col 3 name' => 'col 3 value', |
|
88
|
|
|
)); |
|
89
|
|
|
$writer->finish(); |
|
90
|
|
|
|
|
91
|
|
|
$excel = IOFactory::load($file); |
|
92
|
|
|
$sheet = $excel->getActiveSheet()->toArray(); |
|
93
|
|
|
|
|
94
|
|
|
// Check column names at first line |
|
95
|
|
|
$this->assertEquals(array('col 1 name', 'col 2 name', 'col 3 name'), $sheet[0]); |
|
96
|
|
|
|
|
97
|
|
|
// Check values at second line |
|
98
|
|
|
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[1]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testWriteItemAppendWithSheetTitle() |
|
105
|
|
|
{ |
|
106
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
107
|
|
|
|
|
108
|
|
|
$writer = new SpreadsheetWriter(new \SplFileObject($file, 'w'), 'Sheet 1'); |
|
109
|
|
|
|
|
110
|
|
|
$writer->prepare(); |
|
111
|
|
|
$writer->writeItem(array('first', 'last')); |
|
112
|
|
|
|
|
113
|
|
|
$writer->writeItem(array( |
|
114
|
|
|
'first' => 'James', |
|
115
|
|
|
'last' => 'Bond', |
|
116
|
|
|
)); |
|
117
|
|
|
|
|
118
|
|
|
$writer->writeItem(array( |
|
119
|
|
|
'first' => '', |
|
120
|
|
|
'last' => 'Dr. No', |
|
121
|
|
|
)); |
|
122
|
|
|
|
|
123
|
|
|
$writer->finish(); |
|
124
|
|
|
|
|
125
|
|
|
// Open file with append mode ('a') to add a sheet |
|
126
|
|
|
$writer = new SpreadsheetWriter(new \SplFileObject($file, 'a'), 'Sheet 2'); |
|
127
|
|
|
|
|
128
|
|
|
$writer->prepare(); |
|
129
|
|
|
|
|
130
|
|
|
$writer->writeItem(array('first', 'last')); |
|
131
|
|
|
|
|
132
|
|
|
$writer->writeItem(array( |
|
133
|
|
|
'first' => 'Miss', |
|
134
|
|
|
'last' => 'Moneypenny', |
|
135
|
|
|
)); |
|
136
|
|
|
|
|
137
|
|
|
$writer->finish(); |
|
138
|
|
|
|
|
139
|
|
|
$excel = IOFactory::load($file); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertTrue($excel->sheetNameExists('Sheet 1')); |
|
142
|
|
|
$this->assertEquals(3, $excel->getSheetByName('Sheet 1')->getHighestRow()); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertTrue($excel->sheetNameExists('Sheet 2')); |
|
145
|
|
|
$this->assertEquals(2, $excel->getSheetByName('Sheet 2')->getHighestRow()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* |
|
150
|
|
|
*/ |
|
151
|
|
|
public function testWriteItemWithoutSheetTitle() |
|
152
|
|
|
{ |
|
153
|
|
|
$outputFile = new \SplFileObject(tempnam(sys_get_temp_dir(), null)); |
|
154
|
|
|
$writer = new SpreadsheetWriter($outputFile); |
|
155
|
|
|
|
|
156
|
|
|
$writer->prepare(); |
|
157
|
|
|
|
|
158
|
|
|
$writer->writeItem(array('first', 'last')); |
|
159
|
|
|
|
|
160
|
|
|
$writer->finish(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|