|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Tests\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Writer\ExcelWriter; |
|
6
|
|
|
|
|
7
|
|
|
class ExcelWriterTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function setUp() |
|
10
|
|
|
{ |
|
11
|
|
|
if (!extension_loaded('zip')) { |
|
12
|
|
|
$this->markTestSkipped(); |
|
13
|
|
|
} |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function testWriteItemAppendWithSheetTitle() |
|
17
|
|
|
{ |
|
18
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
19
|
|
|
|
|
20
|
|
|
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), 'Sheet 1'); |
|
21
|
|
|
|
|
22
|
|
|
$writer->prepare(); |
|
23
|
|
|
$writer->writeItem(array('first', 'last')); |
|
24
|
|
|
|
|
25
|
|
|
$writer->writeItem(array( |
|
26
|
|
|
'first' => 'James', |
|
27
|
|
|
'last' => 'Bond' |
|
28
|
|
|
)); |
|
29
|
|
|
|
|
30
|
|
|
$writer->writeItem(array( |
|
31
|
|
|
'first' => '', |
|
32
|
|
|
'last' => 'Dr. No' |
|
33
|
|
|
)); |
|
34
|
|
|
|
|
35
|
|
|
$writer->finish(); |
|
36
|
|
|
|
|
37
|
|
|
// Open file with append mode ('a') to add a sheet |
|
38
|
|
|
$writer = new ExcelWriter(new \SplFileObject($file, 'a'), 'Sheet 2'); |
|
39
|
|
|
|
|
40
|
|
|
$writer->prepare(); |
|
41
|
|
|
|
|
42
|
|
|
$writer->writeItem(array('first', 'last')); |
|
43
|
|
|
|
|
44
|
|
|
$writer->writeItem(array( |
|
45
|
|
|
'first' => 'Miss', |
|
46
|
|
|
'last' => 'Moneypenny' |
|
47
|
|
|
)); |
|
48
|
|
|
|
|
49
|
|
|
$writer->finish(); |
|
50
|
|
|
|
|
51
|
|
|
$excel = \PHPExcel_IOFactory::load($file); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertTrue($excel->sheetNameExists('Sheet 1')); |
|
54
|
|
|
$this->assertEquals(3, $excel->getSheetByName('Sheet 1')->getHighestRow()); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue($excel->sheetNameExists('Sheet 2')); |
|
57
|
|
|
$this->assertEquals(2, $excel->getSheetByName('Sheet 2')->getHighestRow()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testWriteItemWithoutSheetTitle() |
|
61
|
|
|
{ |
|
62
|
|
|
$outputFile = new \SplFileObject(tempnam(sys_get_temp_dir(), null)); |
|
63
|
|
|
$writer = new ExcelWriter($outputFile); |
|
64
|
|
|
|
|
65
|
|
|
$writer->prepare(); |
|
66
|
|
|
|
|
67
|
|
|
$writer->writeItem(array('first', 'last')); |
|
68
|
|
|
|
|
69
|
|
|
$writer->finish(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Test that column names not prepended to first row if ExcelWriter's 4-th |
|
74
|
|
|
* parameter not given |
|
75
|
|
|
* |
|
76
|
|
|
* @author Igor Mukhin <[email protected]> |
|
77
|
|
|
*/ |
|
78
|
|
|
public function testHeaderNotPrependedByDefault() |
|
79
|
|
|
{ |
|
80
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
81
|
|
|
|
|
82
|
|
|
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007'); |
|
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 = \PHPExcel_IOFactory::load($file); |
|
92
|
|
|
$sheet = $excel->getActiveSheet()->toArray(); |
|
93
|
|
|
|
|
94
|
|
|
# Values should be at first line |
|
95
|
|
|
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[0]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Test that column names prepended at first row |
|
101
|
|
|
* and values have been written at second line |
|
102
|
|
|
* if ExcelWriter's 4-th parameter set to true |
|
103
|
|
|
* |
|
104
|
|
|
* @author Igor Mukhin <[email protected]> |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testHeaderPrependedWhenOptionSetToTrue() |
|
107
|
|
|
{ |
|
108
|
|
|
$file = tempnam(sys_get_temp_dir(), null); |
|
109
|
|
|
|
|
110
|
|
|
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007', true); |
|
111
|
|
|
$writer->prepare(); |
|
112
|
|
|
$writer->writeItem(array( |
|
113
|
|
|
'col 1 name'=>'col 1 value', |
|
114
|
|
|
'col 2 name'=>'col 2 value', |
|
115
|
|
|
'col 3 name'=>'col 3 value' |
|
116
|
|
|
)); |
|
117
|
|
|
$writer->finish(); |
|
118
|
|
|
|
|
119
|
|
|
$excel = \PHPExcel_IOFactory::load($file); |
|
120
|
|
|
$sheet = $excel->getActiveSheet()->toArray(); |
|
121
|
|
|
|
|
122
|
|
|
# Check column names at first line |
|
123
|
|
|
$this->assertEquals(array('col 1 name', 'col 2 name', 'col 3 name'), $sheet[0]); |
|
124
|
|
|
|
|
125
|
|
|
# Check values at second line |
|
126
|
|
|
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[1]); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|