WriteMultibyteTest::testMultiByteLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
eloc 23
c 4
b 1
f 0
nc 1
nop 0
dl 0
loc 39
rs 9.552
1
<?php
2
3
namespace SPSS\Tests;
4
5
use SPSS\Sav\Reader;
6
use SPSS\Sav\Variable;
7
use SPSS\Sav\Writer;
8
9
class WriteMultibyteTest extends TestCase
10
{
11
12
    public function testMultiByteLabel()
13
    {
14
        $data   = [
15
            'header'    => [
16
                'prodName'     => '@(#) IBM SPSS STATISTICS',
17
                'layoutCode'   => 2,
18
                'creationDate' => date('d M y'),
19
                'creationTime' => date('H:i:s'),
20
            ],
21
            'variables' => [
22
                new Variable('longname_longerthanexpected', [
23
                    'label'  => 'Data zákończenia',
24
                    'width'  => 16,
25
                    'format' => 1
26
                ]),
27
                new Variable('ccc', [
28
                    'label'  => 'áá345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901233á',
29
                    'format' => 5,
30
                    'values' => [
31
                        1 => 'áá345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901233á',
32
                    ],
33
                ]),
34
            ],
35
        ];
36
        $writer = new Writer($data);
37
38
        $buffer = $writer->getBuffer();
39
        $buffer->rewind();
40
41
        $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

41
        $reader = Reader::fromString(/** @scrutinizer ignore-type */ $buffer->getStream())->read();
Loading history...
42
43
        // Short variable label
44
        $this->assertEquals($data['variables'][0]->label, $reader->variables[0]->label);
45
46
        // Long variable label
47
        $this->assertEquals(mb_substr($data['variables'][1]->values[1], 0, -2, 'UTF-8'), $reader->variables[1]->label);
48
        
49
        // Long value label
50
        $this->assertEquals(mb_substr($data['variables'][1]->label, 0, -2, 'UTF-8'), $reader->valueLabels[0]->labels[0]['label']);
51
    }
52
    
53
    /**
54
     * ISSUE #20
55
     * 
56
     * Chinese value labels seem to work fine, but free text does not work
57
     */
58
    public function testChinese()
59
    {
60
        $input = [
61
            'header'    => [
62
                'prodName'     => '@(#) IBM SPSS STATISTICS 64-bit Macintosh 23.0.0.0',
63
                'creationDate' => '05 Oct 18',
64
                'creationTime' => '01:36:53',
65
                'weightIndex'  => 0,
66
            ],
67
            'variables' => [
68
                new Variable('test1', [
69
                    'format'     => Variable::FORMAT_TYPE_F,
70
                    'width'      => 4, 
71
                    'decimals'   => 2,
72
                    'label'      => 'test',
73
                    'values'     => [
74
                        1 => '1测试中文标签1',
75
                        2 => '2测试中文标签2',
76
                    ],
77
                    'missing'    => [],
78
                    'columns'    => 5,
79
                    'alignment'  => Variable::ALIGN_RIGHT,
80
                    'measure'    => Variable::MEASURE_SCALE,
81
                    'attributes' => [
82
                        '$@Role' => Variable::ROLE_PARTITION,
83
                    ],
84
                    'data'       => [1, 1, 1],
85
                ]),
86
                new Variable('test2', [
87
                    'format'     => Variable::FORMAT_TYPE_A,
88
                    'width'      => 100,
89
                    'label'      => 'test',
90
                    'columns'    => 100,
91
                    'alignment'  => Variable::ALIGN_LEFT,
92
                    'measure'    => Variable::MEASURE_NOMINAL,
93
                    'attributes' => [
94
                        '$@Role' => Variable::ROLE_SPLIT,
95
                    ],
96
                    'data'       => [
97
                        '测试中文数据1',
98
                        '测试中文数据2',
99
                        '测试中文数据3'
100
                    ],
101
                ]),
102
            ],
103
        ];
104
        
105
        $writer = new Writer($input);
106
        
107
        // Uncomment if you want to really save and check the resulting file in SPSS
108
        //$writer->save('chinese1.sav');
109
        $buffer = $writer->getBuffer();
110
        $buffer->rewind();
111
112
        $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

112
        $reader = Reader::fromString(/** @scrutinizer ignore-type */ $buffer->getStream())->read();
Loading history...
113
        $expected[0][0] = $input['variables'][0]->data[0];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$expected was never initialized. Although not strictly required by PHP, it is generally a good practice to add $expected = array(); before regardless.
Loading history...
114
        $expected[0][1] = $input['variables'][1]->data[0];
115
        $expected[1][0] = $input['variables'][0]->data[1];
116
        $expected[1][1] = $input['variables'][1]->data[1];
117
        $expected[2][0] = $input['variables'][0]->data[2];
118
        $expected[2][1] = $input['variables'][1]->data[2];
119
        $this->assertEquals($expected, $reader->data);
120
    }
121
122
}
123