-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathWriteMultibyteTest.php
163 lines (144 loc) · 5.85 KB
/
WriteMultibyteTest.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
<?php
namespace SPSS\Tests;
use SPSS\Sav\Reader;
use SPSS\Sav\Record\Info\LongVariableNames;
use SPSS\Sav\Variable;
use SPSS\Sav\Writer;
class WriteMultibyteTest extends TestCase
{
public function testMultiByteLabel()
{
$data = [
'header' => [
'prodName' => '@(#) IBM SPSS STATISTICS',
'layoutCode' => 2,
'creationDate' => date('d M y'),
'creationTime' => date('H:i:s'),
],
'variables' => [
[
'name' => 'longname_longerthanexpected',
'label' => 'Data zákończenia',
'width' => 16,
'format' => 1,
],
[
'name' => 'ccc',
'label' => 'áá345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901233á',
'format' => 5,
'values' => [
1 => 'áá345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901233á',
],
],
],
];
$writer = new Writer($data);
$buffer = $writer->getBuffer();
$buffer->rewind();
$reader = Reader::fromString($buffer->getStream())->read();
// Short variable label
$this->assertEquals($data['variables'][0]['label'], $reader->variables[0]->label);
// Long variable label
$this->assertEquals(mb_substr($data['variables'][1]['values'][1], 0, -2, 'UTF-8'),
$reader->variables[1]->label);
// Long value label
$this->assertEquals(mb_substr($data['variables'][1]['label'], 0, -2, 'UTF-8'),
$reader->valueLabels[0]->labels[0]['label']);
}
/**
* ISSUE #20.
* Chinese value labels seem to work fine, but free text does not work
*/
public function testChinese()
{
$input = [
'header' => [
'prodName' => '@(#) IBM SPSS STATISTICS 64-bit Macintosh 23.0.0.0',
'creationDate' => '05 Oct 18',
'creationTime' => '01:36:53',
'weightIndex' => 0,
],
'variables' => [
[
'name' => 'test1',
'format' => Variable::FORMAT_TYPE_F,
'width' => 4,
'decimals' => 2,
'label' => 'test',
'values' => [
1 => '1测试中文标签1',
2 => '2测试中文标签2',
],
'missing' => [],
'columns' => 5,
'alignment' => Variable::ALIGN_RIGHT,
'measure' => Variable::MEASURE_SCALE,
'attributes' => [
'$@Role' => Variable::ROLE_PARTITION,
],
'data' => [1, 1, 1],
],
[
'name' => 'test2',
'format' => Variable::FORMAT_TYPE_A,
'width' => 100,
'label' => 'test',
'columns' => 100,
'alignment' => Variable::ALIGN_LEFT,
'measure' => Variable::MEASURE_NOMINAL,
'attributes' => [
'$@Role' => Variable::ROLE_SPLIT,
],
'data' => [
'测试中文数据1',
'测试中文数据2',
'测试中文数据3',
],
],
],
];
$writer = new Writer($input);
// Uncomment if you want to really save and check the resulting file in SPSS
// $writer->save('chinese1.sav');
$buffer = $writer->getBuffer();
$buffer->rewind();
$reader = Reader::fromString($buffer->getStream())->read();
$expected[0][0] = $input['variables'][0]['data'][0];
$expected[0][1] = $input['variables'][1]['data'][0];
$expected[1][0] = $input['variables'][0]['data'][1];
$expected[1][1] = $input['variables'][1]['data'][1];
$expected[2][0] = $input['variables'][0]['data'][2];
$expected[2][1] = $input['variables'][1]['data'][2];
$this->assertEquals($expected, $reader->data);
}
public function testMultiByteVariableName()
{
$data = [
'header' => [
'prodName' => '@(#) IBM SPSS STATISTICS',
'layoutCode' => 2,
'creationDate' => date('d M y'),
'creationTime' => date('H:i:s'),
],
'variables' => [
[
'name' => 'Å',
'width' => 16,
'format' => 1,
],
[
'name' => 'DSADÆØØÅÅÅÅÅSAAA',
'format' => 5,
],
],
];
$writer = new Writer($data);
$buffer = $writer->getBuffer();
$buffer->rewind();
$reader = Reader::fromString($buffer->getStream())->read();
// Short variable name
$this->assertEquals($data['variables'][0]['name'], $reader->info[LongVariableNames::SUBTYPE]['V00001']);
// Long variable name
$this->assertEquals($data['variables'][1]['name'], $reader->info[LongVariableNames::SUBTYPE]['V00002']);
}
}