Passed
Push — master ( d8671c...01d1e3 )
by Alex
03:39
created

PackDataUnitTest::packDataExceptionDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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