|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SPSS\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SPSS\Sav\Reader; |
|
6
|
|
|
use SPSS\Sav\Record; |
|
7
|
|
|
use SPSS\Sav\Writer; |
|
8
|
|
|
use SPSS\Utils; |
|
9
|
|
|
|
|
10
|
|
|
class SavRandomReadWriteTest extends TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function provider() |
|
16
|
|
|
{ |
|
17
|
|
|
$header = [ |
|
18
|
|
|
'recType' => Record\Header::NORMAL_REC_TYPE, |
|
19
|
|
|
'prodName' => '@(#) SPSS DATA FILE', |
|
20
|
|
|
'layoutCode' => 2, |
|
21
|
|
|
'nominalCaseSize' => 0, |
|
22
|
|
|
'casesCount' => 1, //mt_rand(10, 100), |
|
23
|
|
|
'compression' => 1, |
|
24
|
|
|
'weightIndex' => 0, |
|
25
|
|
|
'bias' => 100, |
|
26
|
|
|
'creationDate' => date('d M y'), |
|
27
|
|
|
'creationTime' => date('H:i:s'), |
|
28
|
|
|
'fileLabel' => 'test read/write', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
$documents = [ |
|
32
|
|
|
$this->generateRandomString(mt_rand(5, Record\Document::LENGTH)), |
|
33
|
|
|
$this->generateRandomString(mt_rand(5, Record\Document::LENGTH)), |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
$variables = []; |
|
37
|
|
|
|
|
38
|
|
|
// Generate random variables |
|
39
|
|
|
|
|
40
|
|
|
$count = 1; //mt_rand(1, 20); |
|
41
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
42
|
|
|
$var = $this->generateVariable([ |
|
43
|
|
|
'id' => $this->generateRandomString(mt_rand(2, 100)), |
|
44
|
|
|
'casesCount' => $header['casesCount'], |
|
45
|
|
|
] |
|
46
|
|
|
); |
|
47
|
|
|
$header['nominalCaseSize'] += Utils::widthToOcts($var['width']); |
|
48
|
|
|
$variables[] = $var; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
yield [compact('header', 'variables', 'documents')]; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$header['casesCount'] = 5; |
|
55
|
|
|
for($i = 0; $i < 1000; $i++) { |
|
56
|
|
|
$variable = $this->generateVariable([ |
|
57
|
|
|
'id' => $this->generateRandomString(mt_rand(2, 100)), |
|
58
|
|
|
'casesCount' => $header['casesCount'] |
|
59
|
|
|
]); |
|
60
|
|
|
$header['nominalCaseSize'] = Utils::widthToOcts($variable['width']); |
|
61
|
|
|
yield [[ |
|
62
|
|
|
'header' => $header, |
|
63
|
|
|
'variables' => [$variable], |
|
64
|
|
|
'documents' => $documents |
|
65
|
|
|
]]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @dataProvider provider |
|
71
|
|
|
* @param array $data |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testWriteRead($data) |
|
75
|
|
|
{ |
|
76
|
|
|
$writer = new Writer($data); |
|
77
|
|
|
|
|
78
|
|
|
$buffer = $writer->getBuffer(); |
|
79
|
|
|
$buffer->rewind(); |
|
80
|
|
|
|
|
81
|
|
|
$reader = Reader::fromString($buffer->getStream())->read(); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$this->checkHeader($data['header'], $reader); |
|
84
|
|
|
|
|
85
|
|
|
if ($data['documents']) { |
|
86
|
|
|
foreach ($data['documents'] as $key => $doc) { |
|
87
|
|
|
$this->assertEquals($doc, $reader->documents[$key], 'Invalid document line.'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$index = 0; |
|
92
|
|
|
if (isset($reader->info[Record\Info\VeryLongString::SUBTYPE])) { |
|
93
|
|
|
$veryLongStrings = $reader->info[Record\Info\VeryLongString::SUBTYPE]->toArray(); |
|
94
|
|
|
} else { |
|
95
|
|
|
$veryLongStrings = []; |
|
96
|
|
|
} |
|
97
|
|
|
foreach ($data['variables'] as $var) { |
|
98
|
|
|
/** @var Record\Variable $readVariable */ |
|
99
|
|
|
$readVariable = $reader->variables[$index]; |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals($var['label'], $readVariable->label); |
|
103
|
|
|
$this->assertEquals($var['format'], $readVariable->print[1]); |
|
104
|
|
|
$this->assertEquals($var['decimals'], $readVariable->print[3]); |
|
105
|
|
|
|
|
106
|
|
|
// Check variable data |
|
107
|
|
|
foreach ($var['data'] as $case => $value) { |
|
108
|
|
|
$this->assertEquals($value, $reader->data[$case][$index]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (isset($veryLongStrings[$readVariable->name])) { |
|
112
|
|
|
$index += Utils::widthToSegments($veryLongStrings[$readVariable->name]) - 1; |
|
113
|
|
|
} |
|
114
|
|
|
$index++; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// TODO: valueLabels |
|
118
|
|
|
// TODO: info |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|