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
|
|
|
], |
26
|
|
|
'param-unnamed-auto-int' => [ |
27
|
|
|
[2], |
28
|
|
|
Params::ALL_AUTO, |
29
|
|
|
[[2, PDO::PARAM_INT]], |
30
|
|
|
], |
31
|
|
|
'param-unnamed-auto-false' => [ |
32
|
|
|
[false], |
33
|
|
|
Params::ALL_AUTO, |
34
|
|
|
[[false, PDO::PARAM_BOOL]], |
35
|
|
|
], |
36
|
|
|
'param-unnamed-auto-true' => [ |
37
|
|
|
[true], |
38
|
|
|
Params::ALL_AUTO, |
39
|
|
|
[[true, PDO::PARAM_BOOL]], |
40
|
|
|
], |
41
|
|
|
'param-unnamed-auto-str' => [ |
42
|
|
|
['bar'], |
43
|
|
|
Params::ALL_AUTO, |
44
|
|
|
[['bar', PDO::PARAM_STR]], |
45
|
|
|
], |
46
|
|
|
'param-unnamed-str' => [ |
47
|
|
|
[2], |
48
|
|
|
Params::ALL_STRING, |
49
|
|
|
[[2, PDO::PARAM_STR]], |
50
|
|
|
], |
51
|
|
|
'param-unnamed-manual' => [ |
52
|
|
|
[[2, PDO::PARAM_BOOL]], |
53
|
|
|
Params::ALL_MANUAL, |
54
|
|
|
[[2, PDO::PARAM_BOOL]], |
55
|
|
|
], |
56
|
|
|
'param-named-auto' => [ |
57
|
|
|
['foo' => 2], |
58
|
|
|
Params::ALL_AUTO, |
59
|
|
|
['foo' => [2, PDO::PARAM_INT]], |
60
|
|
|
], |
61
|
|
|
'param-named-str' => [ |
62
|
|
|
['foo' => 2], |
63
|
|
|
Params::ALL_STRING, |
64
|
|
|
['foo' => [2, PDO::PARAM_STR]], |
65
|
|
|
], |
66
|
|
|
'param-named-manual' => [ |
67
|
|
|
['foo' => [2, PDO::PARAM_BOOL]], |
68
|
|
|
Params::ALL_MANUAL, |
69
|
|
|
['foo' => [2, PDO::PARAM_BOOL]], |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @dataProvider successProvider |
76
|
|
|
* |
77
|
|
|
* @param array $params |
78
|
|
|
* @param int $paramHandle |
79
|
|
|
* @param array $expectedParams |
80
|
|
|
*/ |
81
|
|
|
public function testSuccess( |
82
|
|
|
array $params, |
83
|
|
|
int $paramHandle, |
84
|
|
|
array $expectedParams |
85
|
|
|
) { |
86
|
|
|
$sut = $this->createSut($params, $paramHandle); |
87
|
|
|
|
88
|
|
|
$this->assertSame($expectedParams, $sut->getAll()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function constructExceptionsProvider(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'invalid parameter handling' => [[0 => null], 123], |
95
|
|
|
'string parameter key not expected' => [[0 => null, 'foo' => null], 123], |
96
|
|
|
'int parameter key not expected' => [[1 => null], Params::ALL_AUTO], |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @dataProvider constructExceptionsProvider |
102
|
|
|
* |
103
|
|
|
* @param array $params |
104
|
|
|
* @param int $paramHandle |
105
|
|
|
*/ |
106
|
|
|
public function testConstructExceptions(array $params, int $paramHandle) |
107
|
|
|
{ |
108
|
|
|
$this->expectException(InvalidArgumentException::class); |
109
|
|
|
|
110
|
|
|
$this->createSut($params, $paramHandle); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $params |
115
|
|
|
* @param int $paramHandle |
116
|
|
|
* |
117
|
|
|
* @return Params |
118
|
|
|
*/ |
119
|
|
|
protected function createSut(array $params, int $paramHandle): Params |
120
|
|
|
{ |
121
|
|
|
return new Params($params, $paramHandle); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|