ParamsTest::successProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 49
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 62
rs 9.1127

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 QB\Generic\Params;
6
7
use InvalidArgumentException;
8
use PDO;
9
use PHPUnit\Framework\TestCase;
10
11
class ParamsTest extends TestCase
12
{
13
    /**
14
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
15
     *
16
     * @return array[]
17
     */
18
    public function successProvider(): array
19
    {
20
        return [
21
            'param-unnamed-auto-null'  => [
22
                [null],
23
                Params::ALL_AUTO,
24
                [[null, PDO::PARAM_NULL]],
25
                true,
26
            ],
27
            'param-unnamed-auto-int'   => [
28
                [2],
29
                Params::ALL_AUTO,
30
                [[2, PDO::PARAM_INT]],
31
                true,
32
            ],
33
            'param-unnamed-auto-false' => [
34
                [false],
35
                Params::ALL_AUTO,
36
                [[false, PDO::PARAM_BOOL]],
37
                true,
38
            ],
39
            'param-unnamed-auto-true'  => [
40
                [true],
41
                Params::ALL_AUTO,
42
                [[true, PDO::PARAM_BOOL]],
43
                true,
44
            ],
45
            'param-unnamed-auto-str'   => [
46
                ['bar'],
47
                Params::ALL_AUTO,
48
                [['bar', PDO::PARAM_STR]],
49
                true,
50
            ],
51
            'param-unnamed-str'        => [
52
                [2],
53
                Params::ALL_STRING,
54
                [[2, PDO::PARAM_STR]],
55
                true,
56
            ],
57
            'param-unnamed-manual'     => [
58
                [[2, PDO::PARAM_BOOL]],
59
                Params::ALL_MANUAL,
60
                [[2, PDO::PARAM_BOOL]],
61
                true,
62
            ],
63
            'param-named-auto'         => [
64
                ['foo' => 2],
65
                Params::ALL_AUTO,
66
                ['foo' => [2, PDO::PARAM_INT]],
67
                false,
68
            ],
69
            'param-named-str'          => [
70
                ['foo' => 2],
71
                Params::ALL_STRING,
72
                ['foo' => [2, PDO::PARAM_STR]],
73
                false,
74
            ],
75
            'param-named-manual'       => [
76
                ['foo' => [2, PDO::PARAM_BOOL]],
77
                Params::ALL_MANUAL,
78
                ['foo' => [2, PDO::PARAM_BOOL]],
79
                false,
80
            ],
81
        ];
82
    }
83
84
    /**
85
     * @dataProvider successProvider
86
     *
87
     * @param array $params
88
     * @param int   $paramHandle
89
     * @param array $expectedParams
90
     * @param bool  $usesUnnamedParams
91
     */
92
    public function testSuccess(
93
        array $params,
94
        int $paramHandle,
95
        array $expectedParams,
96
        bool $usesUnnamedParams
97
    ) {
98
        $sut = $this->createSut($params, $paramHandle);
99
100
        $this->assertSame($expectedParams, $sut->getAll());
101
102
        if ($usesUnnamedParams) {
103
            $this->assertTrue($sut->usesUnnamedParams());
104
            $this->assertFalse($sut->usesNamedParams());
105
        } else {
106
            $this->assertFalse($sut->usesUnnamedParams());
107
            $this->assertTrue($sut->usesNamedParams());
108
        }
109
    }
110
111
    public function constructExceptionsProvider(): array
112
    {
113
        return [
114
            'invalid parameter handling'        => [[0 => null], 123],
115
            'string parameter key not expected' => [[0 => null, 'foo' => null], 123],
116
            'int parameter key not expected'    => [[1 => null], Params::ALL_AUTO],
117
            'int key parameter skipped'         => [[0 => null, 3 => null], Params::ALL_AUTO],
118
        ];
119
    }
120
121
    /**
122
     * @dataProvider constructExceptionsProvider
123
     *
124
     * @param array $params
125
     * @param int   $paramHandle
126
     */
127
    public function testConstructExceptions(array $params, int $paramHandle)
128
    {
129
        $this->expectException(InvalidArgumentException::class);
130
131
        $this->createSut($params, $paramHandle);
132
    }
133
134
    /**
135
     * @param array $params
136
     * @param int   $paramHandle
137
     *
138
     * @return Params
139
     */
140
    protected function createSut(array $params, int $paramHandle): Params
141
    {
142
        return new Params($params, $paramHandle);
143
    }
144
}
145