SavRandomReadWriteTest::testWriteRead()   B
last analyzed

Complexity

Conditions 7
Paths 20

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 23
c 1
b 0
f 0
nc 20
nop 1
dl 0
loc 41
rs 8.6186
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'] += $var->getOcts();
48
            $variables[] = $var;
49
        }
50
51
52
        yield [[
0 ignored issues
show
Bug Best Practice introduced by
The expression yield array(array('heade...uments' => $documents)) returns the type Generator which is incompatible with the documented return type array.
Loading history...
53
            'header' => $header,
54
            'variables' => $variables,
55
            'documents' => $documents
56
        ]];
57
58
        $header['casesCount'] = 5;
59
        for($i = 0; $i < 1000; $i++) {
60
            $variable = $this->generateVariable([
61
                'id' => $this->generateRandomString(mt_rand(2, 100)),
62
                'casesCount' => $header['casesCount']
63
            ]);
64
            $header['nominalCaseSize'] = $variable->getOcts();
65
            yield [[
66
                'header' => $header,
67
                'variables' => [$variable],
68
                'documents' => $documents
69
            ]];
70
        }
71
    }
72
73
    /**
74
     * @dataProvider provider
75
     * @param array $data
76
     * @throws \Exception
77
     */
78
    public function testWriteRead($data)
79
    {
80
        $writer = new Writer($data);
81
82
        $buffer = $writer->getBuffer();
83
        $buffer->rewind();
84
85
        $reader = Reader::fromString($buffer->getStream())->read();
0 ignored issues
show
Bug introduced by
$buffer->getStream() of type resource is incompatible with the type string expected by parameter $str of SPSS\Sav\Reader::fromString(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        $reader = Reader::fromString(/** @scrutinizer ignore-type */ $buffer->getStream())->read();
Loading history...
86
87
        $this->checkHeader($data['header'], $reader);
88
89
        if ($data['documents']) {
90
            foreach ($data['documents'] as $key => $doc) {
91
                $this->assertEquals($doc, $reader->documents[$key], 'Invalid document line.');
92
            }
93
        }
94
95
        $index = 0;
96
        if (isset($reader->info[Record\Info\VeryLongString::SUBTYPE])) {
97
            $veryLongStrings = $reader->info[Record\Info\VeryLongString::SUBTYPE]->toArray();
98
        } else {
99
            $veryLongStrings = [];
100
        }
101
        foreach ($data['variables'] as $var) {
102
            /** @var Record\Variable $readVariable */
103
            $readVariable = $reader->variables[$index];
104
105
106
            $this->assertEquals($var->label, $readVariable->label);
107
            $this->assertEquals($var->format, $readVariable->print[1]);
108
            $this->assertEquals($var->decimals, $readVariable->print[3]);
109
110
            // Check variable data
111
            foreach ($var->data as $case => $value) {
112
                $this->assertEquals($value, $reader->data[$case][$index]);
113
            }
114
115
            if (isset($veryLongStrings[$readVariable->name])) {
116
                $index += Utils::widthToSegments($veryLongStrings[$readVariable->name]) - 1;
117
            }
118
            $index++;
119
        }
120
121
        // TODO: valueLabels
122
        // TODO: info
123
    }
124
125
}
126