Completed
Branch feature/scrutinizer (5874ea)
by X
03:07
created

UnserializeTest::provideDataForIntegerTest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace xKerman\Restricted\Test;
4
5
use PHPUnit\Framework\TestCase;
6
7
use function xKerman\Restricted\unserialize;
8
9
class UnserializeTest extends TestCase
10
{
11
    public function testNull()
12
    {
13
        $this->assertNull(unserialize('N;'));
14
    }
15
16
    public function provideDataForBooleanTest()
17
    {
18
        return [
19
            'true' => [
20
                'input' => 'b:1;',
21
                'expected' => true,
22
            ],
23
            'false' => [
24
                'input' => 'b:0;',
25
                'expected' => false,
26
            ],
27
        ];
28
    }
29
30
    /**
31
     * @dataProvider provideDataForBooleanTest
32
     */
33
    public function testBoolean($input, $expected)
34
    {
35
        $this->assertSame($expected, unserialize($input));
36
    }
37
38
    public function provideDataForIntegerTest()
39
    {
40
        return [
41
            'positive integer w/o plus sign' => [
42
                'input' => 'i:1;',
43
                'expected' => 1,
44
            ],
45
            'positive integer w/ plus sign' => [
46
                'input' => 'i:+1;',
47
                'expected' => 1,
48
            ],
49
            'positive integer greater than 9' => [
50
                'input' => 'i:10;',
51
                'expected' => 10,
52
            ],
53
            'negative integer' => [
54
                'input' => 'i:-1;',
55
                'expected' => -1,
56
            ],
57
            'integer w/ leading zero' => [
58
                'input' => 'i:0987;',
59
                'expected' => 987,
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * @dataProvider provideDataForIntegerTest
66
     */
67
    public function testInteger($input, $expected)
68
    {
69
        $this->assertSame($expected, unserialize($input));
70
    }
71
72
    public function provideDataForDoubleTest()
73
    {
74
        return [
75
            'positive double w/o plus sign and point' => [
76
                'input' => 'd:1;',
77
                'expected' => 1.0,
78
            ],
79
            'positive double w/ plus sign' => [
80
                'input' => 'd:+1.5;',
81
                'expected' => 1.5,
82
            ],
83
            'negative double' => [
84
                'input' => 'd:-1.5;',
85
                'expected' => -1.5,
86
            ],
87
            'positive double w/o dicimal part' => [
88
                'input' => 'd:2.;',
89
                'expected' => 2.0,
90
            ],
91
            'positive double w/o integer part and plus sign' => [
92
                'input' => 'd:.5;',
93
                'expected' => 0.5,
94
            ],
95
            'positive double w/o integer part w/ plus sign' => [
96
                'input' => 'd:+.5;',
97
                'expected' => 0.5,
98
            ],
99
            'negative double w/o integer part' => [
100
                'input' => 'd:-.5;',
101
                'expected' => -0.5,
102
            ],
103
            'positive double represented as exponential notion w/o plus sign' => [
104
                'input' => 'd:1.0e10;',
105
                'expected' =>1e10,
106
            ],
107
            'positive double represented as exponential notion w/ plus sign' => [
108
                'input' => 'd:+1.0e10;',
109
                'expected' =>1e10,
110
            ],
111
            'negative double represented as exponential notion' => [
112
                'input' => 'd:-1.0e10;',
113
                'expected' => -1e10,
114
            ],
115
116
            'positive double represented as exponential notion w/ positive exponential part' => [
117
                'input' => 'd:25E+2;',
118
                'expected' => 2500.0,
119
            ],
120
            'positive double represented as exponential notion w/ negative exponential part' => [
121
                'input' => 'd:25E-2;',
122
                'expeced' => 0.25,
123
            ],
124
            'positive infinity' => [
125
                'input' => 'd:INF;',
126
                'expected' => INF,
127
            ],
128
            'negative infinity' => [
129
                'input' => 'd:-INF;',
130
                'expected' => -INF,
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * @dataProvider provideDataForDoubleTest
137
     */
138
    public function testDouble($input, $expected)
139
    {
140
        $this->assertSame($expected, unserialize($input));
141
    }
142
143
    public function testNan()
144
    {
145
        $this->assertNan(unserialize('d:NAN;'));
146
    }
147
148
    public function provideDataForStringTest()
149
    {
150
        return [
151
            'empty string' => [
152
                'input' => 's:0:"";',
153
                'expected' => '',
154
            ],
155
            'single character (a)' => [
156
                'input' => 's:1:"a";',
157
                'expected' => 'a',
158
            ],
159
            'single character (double quote)' => [
160
                'input' => serialize('"'),
161
                'expected' => '"',
162
            ],
163
            'single character (single quote)' => [
164
                'input' => serialize("'"),
165
                'expected' => "'",
166
            ],
167
            'single character (null byte)' => [
168
                'input' => serialize("\x00"),
169
                'expected' => "\x00",
170
            ],
171
            'japanese character' => [
172
                'input' => serialize('こんにちは'),
173
                'expected' => 'こんにちは',
174
            ],
175
        ];
176
    }
177
178
    /**
179
     * @dataProvider provideDataForStringTest
180
     */
181
    public function testString($input, $expected)
182
    {
183
        $this->assertSame($expected, unserialize($input));
184
    }
185
186
    public function provideDataForArrayTest()
187
    {
188
        return [
189
            'empty array' => [
190
                'input' => 'a:0:{}',
191
                'expected' => [],
192
            ],
193
            'one element array' => [
194
                'input' => 'a:1:{i:0;s:1:"a";}',
195
                'expected' => ['a'],
196
            ],
197
            'nested array' => [
198
                'input' => serialize([[], [[1], null], "a" => ["b" => "c"]]),
199
                'expected' => [[], [[1], null], "a" => ["b" => "c"]],
200
            ],
201
        ];
202
    }
203
204
    /**
205
     * @dataProvider provideDataForArrayTest
206
     */
207
    public function testArray($input, $expected)
208
    {
209
        $this->assertSame($expected, unserialize($input));
210
    }
211
}
212