/
ExportHelperTest.php
145 lines (122 loc) · 4.57 KB
/
ExportHelperTest.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
<?php
namespace luya\yii\tests\helpers;
use Yii;
use luya\yii\helpers\ExportHelper;
use luya\yii\tests\HelpersTestCase;
use yii\db\ActiveRecord;
class CsvModelStub extends ActiveRecord
{
public static function getDb()
{
return Yii::$app->sqllite;
}
public static function tableName()
{
return 'csvmodelstub';
}
public function rules()
{
return [
[['id', 'name'], 'safe'],
];
}
}
class ExportHelperTest extends HelpersTestCase
{
private function getArray()
{
return [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
];
}
private function initActiveRecord()
{
Yii::$app->sqllite->createCommand()->createTable('csvmodelstub', [
'id' => 'INT(11) PRIMARY KEY',
'name' => 'varchar(120)'])->execute();
}
public function testCsvArrayExport()
{
$this->assertEquals('"id","name"'.PHP_EOL.'"1","John"'.PHP_EOL.'"2","Jane"'. PHP_EOL, ExportHelper::csv($this->getArray()));
}
public function testCsvArrayRowTypes()
{
$this->assertEquals('"1","0","0","","[array]","string","1","1"'. PHP_EOL, ExportHelper::csv([
[
true, false, 0, null, ['bar'], 'string', 1, '1'
]
], [], false));
}
public function testCsvArrayExportWithProperties()
{
$this->assertEquals('"id"'.PHP_EOL.'"1"'.PHP_EOL.'"2"'. PHP_EOL, ExportHelper::csv($this->getArray(), ['id']));
}
public function testCsvArrayExportWithPropertiesAndDifferentArrangedSortedColumns()
{
$this->assertEquals('"id","name"'.PHP_EOL.'"1","John"'.PHP_EOL.'"2","Jane"'. PHP_EOL, ExportHelper::csv($this->getArray(), ['name', 'id']));
}
public function testCsvArrayExportNoHeader()
{
$this->assertEquals('"1","John"'.PHP_EOL.'"2","Jane"'. PHP_EOL, ExportHelper::csv($this->getArray(), [], false));
}
public function testCsvArrayExportWithPropertiesNoHeader()
{
$this->assertEquals('"1"'.PHP_EOL.'"2"'. PHP_EOL, ExportHelper::csv($this->getArray(), ['id'], false));
}
public function testActiveRecordCsv()
{
$this->initActiveRecord();
foreach ($this->getArray() as $item) {
$m = new CsvModelStub();
$m->attributes = $item;
$m->save();
}
// active query find
$this->assertEquals('"Id","Name"'.PHP_EOL.'"1","John"'.PHP_EOL.'"2","Jane"'. PHP_EOL, ExportHelper::csv(CsvModelStub::find()));
$this->assertEquals('"1","John"'.PHP_EOL.'"2","Jane"'. PHP_EOL, ExportHelper::csv(CsvModelStub::find(), [], false));
$this->assertEquals('"1"'.PHP_EOL.'"2"'. PHP_EOL, ExportHelper::csv(CsvModelStub::find(), ['id'], false));
$this->assertEquals('"Id"'.PHP_EOL.'"1"'.PHP_EOL.'"2"'. PHP_EOL, ExportHelper::csv(CsvModelStub::find(), ['id']));
}
public function testException()
{
$this->expectException("luya\Exception");
ExportHelper::csv('foobarstring');
}
public function testXlsArrayExportWithHeader()
{
$filename = tempnam('', 'xlsx_test');
$this->saveArrayToXlsx($filename, $this->getArray(), ['id', 'name'], true);
$this->assertSame($this->getXmlSheetContent($filename), $this->getXmlSheetContent('tests/data/export/withheader.xlsx'));
}
public function testXlsArrayExportWithoutHeader()
{
$filename = tempnam('', 'xlsx_test');
$this->saveArrayToXlsx($filename, $this->getArray(), ['id', 'name'], false);
$this->assertSame($this->getXmlSheetContent($filename), $this->getXmlSheetContent('tests/data/export/withoutheader.xlsx'));
}
protected function getXmlSheetContent($filename)
{
$zip = new \ZipArchive();
$zip->open($filename);
for ($z = 0; $z < $zip->numFiles; $z++) {
$inside_zip_filename = $zip->getNameIndex($z);
if (basename($inside_zip_filename) == 'sheet1.xml') {
return $zip->getFromName($inside_zip_filename);
}
}
}
protected function saveArrayToXlsx($filename, array $input, array $keys = [], $header = true)
{
$string = ExportHelper::xlsx($input, $keys, $header);
file_put_contents($filename, $string);
}
public function testSpecialCharsEncoding()
{
$content = ExportHelper::csv([
['&', "'", 'a"b"c'],
['nix', 'nix', '=1+2";=1+2']
], [], false);
$this->assertSameTrimmed('"&","\'","a""b""c" "nix","nix","\'=1+2"";=1+2"', $content);
}
}