2
2
3
3
namespace Amp \Postgres \Test ;
4
4
5
+ use Amp \Postgres \PostgresExecutor ;
5
6
use PHPUnit \Framework \TestCase ;
6
- use function Amp \Postgres \Internal \cast ;
7
+ use function Amp \Postgres \Internal \encodeParam ;
7
8
8
9
enum IntegerEnum: int
9
10
{
@@ -24,84 +25,89 @@ enum UnitEnum
24
25
case Case;
25
26
}
26
27
27
- class CastTest extends TestCase
28
+ class EncodeParamTest extends TestCase
28
29
{
30
+ private function encodeParam (mixed $ param ): string |int |float |null
31
+ {
32
+ return encodeParam ($ this ->createMock (PostgresExecutor::class), $ param );
33
+ }
34
+
29
35
public function testSingleDimensionalStringArray (): void
30
36
{
31
37
$ array = ["one " , "two " , "three " ];
32
38
$ string = '{"one","two","three"} ' ;
33
39
34
- $ this ->assertSame ($ string , cast ($ array ));
40
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
35
41
}
36
42
37
43
public function testMultiDimensionalStringArray (): void
38
44
{
39
45
$ array = ["one " , "two " , ["three " , "four " ], "five " ];
40
46
$ string = '{"one","two",{"three","four"},"five"} ' ;
41
47
42
- $ this ->assertSame ($ string , cast ($ array ));
48
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
43
49
}
44
50
45
51
public function testQuotedStrings (): void
46
52
{
47
53
$ array = ["one " , "two " , ["three " , "four " ], "five " ];
48
54
$ string = '{"one","two",{"three","four"},"five"} ' ;
49
55
50
- $ this ->assertSame ($ string , cast ($ array ));
56
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
51
57
}
52
58
53
59
public function testEscapedQuoteDelimiter (): void
54
60
{
55
61
$ array = ['va"lue1 ' , 'value"2 ' ];
56
62
$ string = '{"va \\"lue1","value \\"2"} ' ;
57
63
58
- $ this ->assertSame ($ string , cast ($ array ));
64
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
59
65
}
60
66
61
67
public function testNullValue (): void
62
68
{
63
69
$ array = ["one " , null , "three " ];
64
70
$ string = '{"one",NULL,"three"} ' ;
65
71
66
- $ this ->assertSame ($ string , cast ($ array ));
72
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
67
73
}
68
74
69
75
public function testSingleDimensionalIntegerArray (): void
70
76
{
71
77
$ array = [1 , 2 , 3 ];
72
78
$ string = '{ ' . \implode (', ' , $ array ) . '} ' ;
73
79
74
- $ this ->assertSame ($ string , cast ($ array ));
80
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
75
81
}
76
82
77
83
public function testIntegerArrayWithNull (): void
78
84
{
79
85
$ array = [1 , 2 , null , 3 ];
80
86
$ string = '{1,2,NULL,3} ' ;
81
87
82
- $ this ->assertSame ($ string , cast ($ array ));
88
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
83
89
}
84
90
85
91
public function testMultidimensionalIntegerArray (): void
86
92
{
87
93
$ array = [1 , 2 , [3 , 4 ], [5 ], 6 , 7 , [[8 , 9 ], 10 ]];
88
94
$ string = '{1,2,{3,4},{5},6,7,{{8,9},10}} ' ;
89
95
90
- $ this ->assertSame ($ string , cast ($ array ));
96
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
91
97
}
92
98
93
99
public function testEscapedBackslashesInQuotedValue (): void
94
100
{
95
101
$ array = ["test \\ing " , "esca \\ped \\" ];
96
102
$ string = '{"test \\\\ing","esca \\\\ped \\\\"} ' ;
97
103
98
- $ this ->assertSame ($ string , cast ($ array ));
104
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
99
105
}
100
106
101
107
public function testBackedEnum (): void
102
108
{
103
- $ this ->assertSame (3 , cast (IntegerEnum::Three));
104
- $ this ->assertSame ('three ' , cast (StringEnum::Three));
109
+ $ this ->assertSame (3 , $ this -> encodeParam (IntegerEnum::Three));
110
+ $ this ->assertSame ('three ' , $ this -> encodeParam (StringEnum::Three));
105
111
}
106
112
107
113
public function testBackedEnumInArray (): void
@@ -112,38 +118,38 @@ public function testBackedEnumInArray(): void
112
118
];
113
119
$ string = '{{1,2,3},{"one","two","three"}} ' ;
114
120
115
- $ this ->assertSame ($ string , cast ($ array ));
121
+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
116
122
}
117
123
118
124
public function testUnitEnum (): void
119
125
{
120
126
$ this ->expectException (\TypeError::class);
121
127
$ this ->expectExceptionMessage ('An object in parameter values must be ' );
122
128
123
- cast (UnitEnum::Case);
129
+ $ this -> encodeParam (UnitEnum::Case);
124
130
}
125
131
126
132
public function testUnitEnumInArray (): void
127
133
{
128
134
$ this ->expectException (\TypeError::class);
129
- $ this ->expectExceptionMessage ('An object in parameter arrays must be ' );
135
+ $ this ->expectExceptionMessage ('An object in parameter values must be ' );
130
136
131
- cast ([UnitEnum::Case]);
137
+ $ this -> encodeParam ([UnitEnum::Case]);
132
138
}
133
139
134
140
public function testObjectWithoutToStringMethod (): void
135
141
{
136
142
$ this ->expectException (\TypeError::class);
137
143
$ this ->expectExceptionMessage ('An object in parameter values must be ' );
138
144
139
- cast (new \stdClass );
145
+ $ this -> encodeParam (new \stdClass );
140
146
}
141
147
142
148
public function testObjectWithoutToStringMethodInArray (): void
143
149
{
144
150
$ this ->expectException (\TypeError::class);
145
- $ this ->expectExceptionMessage ('An object in parameter arrays must be ' );
151
+ $ this ->expectExceptionMessage ('An object in parameter values must be ' );
146
152
147
- cast ([new \stdClass ]);
153
+ $ this -> encodeParam ([new \stdClass ]);
148
154
}
149
155
}
0 commit comments