Passed
Pull Request — master (#215)
by Dmitriy
13:45
created

DumperTest::jsonDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 236
Code Lines 161

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 161
nc 1
nop 0
dl 0
loc 236
rs 8
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Tests\Unit;
6
7
use PHPUnit\Framework\TestCase;
8
use stdClass;
9
use Yiisoft\Yii\Debug as D;
10
use Yiisoft\Yii\Debug\Dumper;
11
12
final class DumperTest extends TestCase
13
{
14
    /**
15
     * @dataProvider asJsonObjectMapDataProvider
16
     *
17
     * @param string $expectedResult
18
     *
19
     * @group JOM
20
     */
21
    public function testAsJsonObjectsMap(mixed $var, $expectedResult): void
22
    {
23
        $exportResult = Dumper::create($var)->asJsonObjectsMap();
24
        $this->assertEquals($expectedResult, $exportResult);
25
    }
26
27
    public function asJsonObjectMapDataProvider(): array
28
    {
29
        $user = new stdClass();
30
        $user->id = 1;
31
        $objectId = spl_object_id($user);
32
33
        $decoratedUser = clone $user;
34
        $decoratedUser->name = 'Name';
35
        $decoratedUser->originalUser = $user;
36
        $decoratedObjectId = spl_object_id($decoratedUser);
37
38
        return [
39
            [
40
                $user,
41
                <<<S
42
                [{"stdClass#{$objectId}":{"public \$id":1}}]
43
                S,
44
            ],
45
            [
46
                $decoratedUser,
47
                <<<S
48
                [{"stdClass#{$decoratedObjectId}":{"public \$id":1,"public \$name":"Name","public \$originalUser":"object@stdClass#{$objectId}"}},{"stdClass#{$objectId}":{"public \$id":1}}]
49
                S,
50
            ],
51
        ];
52
    }
53
54
    /**
55
     * @dataProvider jsonDataProvider()
56
     */
57
    public function testAsJson($variable, string $result): void
58
    {
59
        $output = Dumper::create($variable)->asJson();
60
        $this->assertEqualsWithoutLE($result, $output);
61
    }
62
63
    public function jsonDataProvider(): array
64
    {
65
        $objectWithClosureInProperty = new stdClass();
66
        // @formatter:off
67
        $objectWithClosureInProperty->a = fn () => 1;
68
        // @formatter:on
69
        $objectWithClosureInPropertyId = spl_object_id($objectWithClosureInProperty);
70
        $objectWithClosureInPropertyClosureId = spl_object_id($objectWithClosureInProperty->a);
0 ignored issues
show
Bug introduced by
$objectWithClosureInProperty->a of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

70
        $objectWithClosureInPropertyClosureId = spl_object_id(/** @scrutinizer ignore-type */ $objectWithClosureInProperty->a);
Loading history...
71
72
        $emptyObject = new stdClass();
73
        $emptyObjectId = spl_object_id($emptyObject);
74
75
        // @formatter:off
76
        $shortFunctionObject = fn () => 1;
77
        // @formatter:on
78
        $shortFunctionObjectId = spl_object_id($shortFunctionObject);
0 ignored issues
show
Bug introduced by
$shortFunctionObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

78
        $shortFunctionObjectId = spl_object_id(/** @scrutinizer ignore-type */ $shortFunctionObject);
Loading history...
79
80
        // @formatter:off
81
        $staticShortFunctionObject = static fn () => 1;
82
        // @formatter:on
83
        $staticShortFunctionObjectId = spl_object_id($staticShortFunctionObject);
0 ignored issues
show
Bug introduced by
$staticShortFunctionObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

83
        $staticShortFunctionObjectId = spl_object_id(/** @scrutinizer ignore-type */ $staticShortFunctionObject);
Loading history...
84
85
        // @formatter:off
86
        $functionObject = function () {
87
            return 1;
88
        };
89
        // @formatter:on
90
        $functionObjectId = spl_object_id($functionObject);
0 ignored issues
show
Bug introduced by
$functionObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

90
        $functionObjectId = spl_object_id(/** @scrutinizer ignore-type */ $functionObject);
Loading history...
91
92
        // @formatter:off
93
        $staticFunctionObject = static function () {
94
            return 1;
95
        };
96
        // @formatter:on
97
        $staticFunctionObjectId = spl_object_id($staticFunctionObject);
0 ignored issues
show
Bug introduced by
$staticFunctionObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

97
        $staticFunctionObjectId = spl_object_id(/** @scrutinizer ignore-type */ $staticFunctionObject);
Loading history...
98
99
        // @formatter:off
100
        $closureWithNullCollisionOperatorObject = fn () => $_ENV['var'] ?? null;
101
        // @formatter:on
102
        $closureWithNullCollisionOperatorObjectId = spl_object_id($closureWithNullCollisionOperatorObject);
0 ignored issues
show
Bug introduced by
$closureWithNullCollisionOperatorObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

102
        $closureWithNullCollisionOperatorObjectId = spl_object_id(/** @scrutinizer ignore-type */ $closureWithNullCollisionOperatorObject);
Loading history...
103
104
        // @formatter:off
105
        $closureWithUsualClassNameObject = fn (Dumper $date) => new \DateTimeZone('');
0 ignored issues
show
Unused Code introduced by
The parameter $date is not used and could be removed. ( Ignorable by Annotation )

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

105
        $closureWithUsualClassNameObject = fn (/** @scrutinizer ignore-unused */ Dumper $date) => new \DateTimeZone('');

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
        // @formatter:on
107
        $closureWithUsualClassNameObjectId = spl_object_id($closureWithUsualClassNameObject);
0 ignored issues
show
Bug introduced by
$closureWithUsualClassNameObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

107
        $closureWithUsualClassNameObjectId = spl_object_id(/** @scrutinizer ignore-type */ $closureWithUsualClassNameObject);
Loading history...
108
109
        // @formatter:off
110
        $closureWithAliasedClassNameObject = fn (Dumper $date) => new \DateTimeZone('');
0 ignored issues
show
Unused Code introduced by
The parameter $date is not used and could be removed. ( Ignorable by Annotation )

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

110
        $closureWithAliasedClassNameObject = fn (/** @scrutinizer ignore-unused */ Dumper $date) => new \DateTimeZone('');

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
        // @formatter:on
112
        $closureWithAliasedClassNameObjectId = spl_object_id($closureWithAliasedClassNameObject);
0 ignored issues
show
Bug introduced by
$closureWithAliasedClassNameObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

112
        $closureWithAliasedClassNameObjectId = spl_object_id(/** @scrutinizer ignore-type */ $closureWithAliasedClassNameObject);
Loading history...
113
114
        // @formatter:off
115
        $closureWithAliasedNamespaceObject = fn (D\Dumper $date) => new \DateTimeZone('');
0 ignored issues
show
Unused Code introduced by
The parameter $date is not used and could be removed. ( Ignorable by Annotation )

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

115
        $closureWithAliasedNamespaceObject = fn (/** @scrutinizer ignore-unused */ D\Dumper $date) => new \DateTimeZone('');

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
        // @formatter:on
117
        $closureWithAliasedNamespaceObjectId = spl_object_id($closureWithAliasedNamespaceObject);
0 ignored issues
show
Bug introduced by
$closureWithAliasedNamespaceObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

117
        $closureWithAliasedNamespaceObjectId = spl_object_id(/** @scrutinizer ignore-type */ $closureWithAliasedNamespaceObject);
Loading history...
118
119
        // @formatter:off
120
        $closureInArrayObject = fn () => new \DateTimeZone('');
121
        // @formatter:on
122
        $closureInArrayObjectId = spl_object_id($closureInArrayObject);
0 ignored issues
show
Bug introduced by
$closureInArrayObject of type callable is incompatible with the type object expected by parameter $object of spl_object_id(). ( Ignorable by Annotation )

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

122
        $closureInArrayObjectId = spl_object_id(/** @scrutinizer ignore-type */ $closureInArrayObject);
Loading history...
123
124
        $fileResource = tmpfile();
125
        $fileResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/');
126
127
        $closedFileResource = tmpfile();
128
        fclose($closedFileResource);
129
        $closedFileResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/');
0 ignored issues
show
Unused Code introduced by
The assignment to $closedFileResourceUri is dead and can be removed.
Loading history...
130
131
        $opendirResource = opendir('/tmp');
132
        //$opendirResourceUri = preg_quote(stream_get_meta_data($fileResource)['uri'], '/');
133
134
        $curlResource = curl_init('https://example.com');
135
        $curlResourceObjectId = spl_object_id($curlResource);
0 ignored issues
show
Bug introduced by
It seems like $curlResource can also be of type resource; however, parameter $object of spl_object_id() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

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

135
        $curlResourceObjectId = spl_object_id(/** @scrutinizer ignore-type */ $curlResource);
Loading history...
136
137
        return [
138
            'empty object' => [
139
                $emptyObject,
140
                <<<S
141
                {"stdClass#{$emptyObjectId}":"{stateless object}"}
142
                S,
143
            ],
144
            'short function' => [
145
                $shortFunctionObject,
146
                <<<S
147
                {"Closure#{$shortFunctionObjectId}":"fn () => 1"}
148
                S,
149
            ],
150
            'short static function' => [
151
                $staticShortFunctionObject,
152
                <<<S
153
                {"Closure#{$staticShortFunctionObjectId}":"static fn () => 1"}
154
                S,
155
            ],
156
            'function' => [
157
                $functionObject,
158
                <<<S
159
                {"Closure#{$functionObjectId}":"function () {\\n            return 1;\\n        }"}
160
                S,
161
            ],
162
            'static function' => [
163
                $staticFunctionObject,
164
                <<<S
165
                {"Closure#{$staticFunctionObjectId}":"static function () {\\n            return 1;\\n        }"}
166
                S,
167
            ],
168
            'string' => [
169
                'Hello, Yii!',
170
                '"Hello, Yii!"',
171
            ],
172
            'empty string' => [
173
                '',
174
                '""',
175
            ],
176
            'null' => [
177
                null,
178
                'null',
179
            ],
180
            'integer' => [
181
                1,
182
                '1',
183
            ],
184
            'integer with separator' => [
185
                1_23_456,
0 ignored issues
show
Bug introduced by
The constant Yiisoft\Yii\Debug\Tests\Unit\1_23_456 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
186
                '123456',
187
            ],
188
            'boolean' => [
189
                true,
190
                'true',
191
            ],
192
            'fileResource' => [
193
                fopen('php://input', 'rb'),
194
                '{"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"Input","mode":"rb","unread_bytes":0,"seekable":true,"uri":"php:\/\/input"}',
195
            ],
196
            'empty array' => [
197
                [],
198
                '[]',
199
            ],
200
            'array of 3 elements, automatic keys' => [
201
                [
202
                    'one',
203
                    'two',
204
                    'three',
205
                ],
206
                '["one","two","three"]',
207
            ],
208
            'array of 3 elements, custom keys' => [
209
                [
210
                    2 => 'one',
211
                    'two' => 'two',
212
                    0 => 'three',
213
                ],
214
                '{"2":"one","two":"two","0":"three"}',
215
            ],
216
            'closure in array' => [
217
                // @formatter:off
218
                [$closureInArrayObject],
219
                // @formatter:on
220
                <<<S
221
                [{"Closure#{$closureInArrayObjectId}":"fn () => new \\\DateTimeZone('')"}]
222
                S,
223
            ],
224
            'original class name' => [
225
                $closureWithUsualClassNameObject,
226
                <<<S
227
                {"Closure#{$closureWithUsualClassNameObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"}
228
                S,
229
            ],
230
            'class alias' => [
231
                $closureWithAliasedClassNameObject,
232
                <<<S
233
                {"Closure#{$closureWithAliasedClassNameObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"}
234
                S,
235
            ],
236
            'namespace alias' => [
237
                $closureWithAliasedNamespaceObject,
238
                <<<S
239
                {"Closure#{$closureWithAliasedNamespaceObjectId}":"fn (\\\Yiisoft\\\Yii\\\Debug\\\Dumper \$date) => new \\\DateTimeZone('')"}
240
                S,
241
            ],
242
            'closure with null-collision operator' => [
243
                $closureWithNullCollisionOperatorObject,
244
                <<<S
245
                {"Closure#{$closureWithNullCollisionOperatorObjectId}":"fn () => \$_ENV['var'] ?? null"}
246
                S,
247
            ],
248
            'utf8 supported' => [
249
                '🤣',
250
                '"🤣"',
251
            ],
252
            'closure in property supported' => [
253
                $objectWithClosureInProperty,
254
                <<<S
255
                {"stdClass#{$objectWithClosureInPropertyId}":{"public \$a":{"Closure#{$objectWithClosureInPropertyClosureId}":"fn () => 1"}}}
256
                S,
257
            ],
258
            'binary string' => [
259
                pack('H*', md5('binary string')),
260
                '"ɍ��^��\u00191\u0017�]�-f�"',
261
            ],
262
            'file resource' => [
263
                $fileResource,
264
                <<<S
265
                {"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"plainfile","stream_type":"STDIO","mode":"r+b","unread_bytes":0,"seekable":true,"uri":"{$fileResourceUri}"}
266
                S,
267
            ],
268
            'closed file resource' => [
269
                $closedFileResource,
270
                '"{closed resource}"',
271
            ],
272
            'opendir resource' => [
273
                $opendirResource,
274
                <<<S
275
                {"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"plainfile","stream_type":"dir","mode":"r","unread_bytes":0,"seekable":true}
276
                S,
277
            ],
278
            'curl resource' => [
279
                $curlResource,
280
                <<<S
281
                {"CurlHandle#{$curlResourceObjectId}":"{stateless object}"}
282
                S,
283
            ],
284
            'stdout' => [
285
                STDOUT,
286
                <<<S
287
                {"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"wb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stdout"}
288
                S,
289
            ],
290
            'stderr' => [
291
                STDERR,
292
                <<<S
293
                {"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"wb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stderr"}
294
                S,
295
            ],
296
            'stdin' => [
297
                STDIN,
298
                <<<S
299
                {"timed_out":false,"blocked":true,"eof":false,"wrapper_type":"PHP","stream_type":"STDIO","mode":"rb","unread_bytes":0,"seekable":false,"uri":"php:\/\/stdin"}
300
                S,
301
            ],
302
        ];
303
    }
304
305
    /**
306
     * Asserting two strings equality ignoring line endings.
307
     */
308
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
309
    {
310
        $expected = str_replace(["\r\n", '\r\n'], ["\n", '\n'], $expected);
311
        $actual = str_replace(["\r\n", '\r\n'], ["\n", '\n'], $actual);
312
        $this->assertEquals($expected, $actual, $message);
313
    }
314
}
315