PackDataUnitTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 19
c 1
b 0
f 0
dl 0
loc 99
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A packDataExceptionDataProvider() 0 14 1
A testPackData() 0 7 1
A packDataDataProvider() 0 30 1
A testExceptionWhilePackingData() 0 7 1
1
<?php
2
namespace Mezon\CustomClient\Tests;
3
4
use Exception;
5
use PHPUnit\Framework\TestCase;
6
use Mezon\CustomClient\CurlWrapper;
7
8
/**
9
 * 
10
 * @psalm-suppress PropertyNotSetInConstructor
11
 */
12
class PackDataUnitTest extends TestCase
13
{
14
15
    /**
16
     * Testing data provider
17
     *
18
     * @return array testing data
19
     */
20
    public function packDataDataProvider(): array
21
    {
22
        return [
23
            // #0, list of scalars
24
            [
25
                [
26
                    'a' => 1,
27
                    'b' => 2
28
                ],
29
                'a=1&b=2'
30
            ],
31
            // #1, nested array
32
            [
33
                [
34
                    'arr' => [
35
                        'key' => 'value'
36
                    ]
37
                ],
38
                'arr[key]=value'
39
            ],
40
            // #2, deeper nested arrays
41
            [
42
                [
43
                    'field' => [
44
                        [
45
                            'key' => 'value'
46
                        ]
47
                    ]
48
                ],
49
                'field[0][key]=value'
50
            ]
51
        ];
52
    }
53
54
    /**
55
     * Testing method packData
56
     *
57
     * @param array $data
58
     *            data to be packed
59
     * @param string $expected
60
     *            expected result
61
     * @dataProvider packDataDataProvider
62
     */
63
    public function testPackData(array $data, string $expected): void
64
    {
65
        // test body
66
        $result = CurlWrapper::packData($data);
67
68
        // assertions
69
        $this->assertEquals($expected, $result);
70
    }
71
72
    /**
73
     * Data provider
74
     *
75
     * @return array Testing data
76
     */
77
    public function packDataExceptionDataProvider(): array
78
    {
79
        return [
80
            // #0, the nearest object
81
            [
82
                [
83
                    'obj' => new \stdClass()
84
                ]
85
            ],
86
            // #1, deeper object
87
            [
88
                [
89
                    'obj1' => [
90
                        'obj2' => new \stdClass()
91
                    ]
92
                ]
93
            ]
94
        ];
95
    }
96
97
    /**
98
     * Testing exception throwing
99
     *
100
     * @param array $data
101
     *            test data
102
     * @dataProvider packDataExceptionDataProvider
103
     */
104
    public function testExceptionWhilePackingData(array $data): void
105
    {
106
        // assertions
107
        $this->expectException(\Exception::class);
108
109
        // test body
110
        CurlWrapper::packData($data);
111
    }
112
}
113