22
33namespace Amp \Postgres \Test ;
44
5+ use Amp \Postgres \PostgresExecutor ;
56use PHPUnit \Framework \TestCase ;
6- use function Amp \Postgres \Internal \cast ;
7+ use function Amp \Postgres \Internal \encodeParam ;
78
89enum IntegerEnum: int
910{
@@ -24,84 +25,89 @@ enum UnitEnum
2425 case Case;
2526}
2627
27- class CastTest extends TestCase
28+ class EncodeParamTest extends TestCase
2829{
30+ private function encodeParam (mixed $ param ): string |int |float |null
31+ {
32+ return encodeParam ($ this ->createMock (PostgresExecutor::class), $ param );
33+ }
34+
2935 public function testSingleDimensionalStringArray (): void
3036 {
3137 $ array = ["one " , "two " , "three " ];
3238 $ string = '{"one","two","three"} ' ;
3339
34- $ this ->assertSame ($ string , cast ($ array ));
40+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
3541 }
3642
3743 public function testMultiDimensionalStringArray (): void
3844 {
3945 $ array = ["one " , "two " , ["three " , "four " ], "five " ];
4046 $ string = '{"one","two",{"three","four"},"five"} ' ;
4147
42- $ this ->assertSame ($ string , cast ($ array ));
48+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
4349 }
4450
4551 public function testQuotedStrings (): void
4652 {
4753 $ array = ["one " , "two " , ["three " , "four " ], "five " ];
4854 $ string = '{"one","two",{"three","four"},"five"} ' ;
4955
50- $ this ->assertSame ($ string , cast ($ array ));
56+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
5157 }
5258
5359 public function testEscapedQuoteDelimiter (): void
5460 {
5561 $ array = ['va"lue1 ' , 'value"2 ' ];
5662 $ string = '{"va \\"lue1","value \\"2"} ' ;
5763
58- $ this ->assertSame ($ string , cast ($ array ));
64+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
5965 }
6066
6167 public function testNullValue (): void
6268 {
6369 $ array = ["one " , null , "three " ];
6470 $ string = '{"one",NULL,"three"} ' ;
6571
66- $ this ->assertSame ($ string , cast ($ array ));
72+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
6773 }
6874
6975 public function testSingleDimensionalIntegerArray (): void
7076 {
7177 $ array = [1 , 2 , 3 ];
7278 $ string = '{ ' . \implode (', ' , $ array ) . '} ' ;
7379
74- $ this ->assertSame ($ string , cast ($ array ));
80+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
7581 }
7682
7783 public function testIntegerArrayWithNull (): void
7884 {
7985 $ array = [1 , 2 , null , 3 ];
8086 $ string = '{1,2,NULL,3} ' ;
8187
82- $ this ->assertSame ($ string , cast ($ array ));
88+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
8389 }
8490
8591 public function testMultidimensionalIntegerArray (): void
8692 {
8793 $ array = [1 , 2 , [3 , 4 ], [5 ], 6 , 7 , [[8 , 9 ], 10 ]];
8894 $ string = '{1,2,{3,4},{5},6,7,{{8,9},10}} ' ;
8995
90- $ this ->assertSame ($ string , cast ($ array ));
96+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
9197 }
9298
9399 public function testEscapedBackslashesInQuotedValue (): void
94100 {
95101 $ array = ["test \\ing " , "esca \\ped \\" ];
96102 $ string = '{"test \\\\ing","esca \\\\ped \\\\"} ' ;
97103
98- $ this ->assertSame ($ string , cast ($ array ));
104+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
99105 }
100106
101107 public function testBackedEnum (): void
102108 {
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));
105111 }
106112
107113 public function testBackedEnumInArray (): void
@@ -112,38 +118,38 @@ public function testBackedEnumInArray(): void
112118 ];
113119 $ string = '{{1,2,3},{"one","two","three"}} ' ;
114120
115- $ this ->assertSame ($ string , cast ($ array ));
121+ $ this ->assertSame ($ string , $ this -> encodeParam ($ array ));
116122 }
117123
118124 public function testUnitEnum (): void
119125 {
120126 $ this ->expectException (\TypeError::class);
121127 $ this ->expectExceptionMessage ('An object in parameter values must be ' );
122128
123- cast (UnitEnum::Case);
129+ $ this -> encodeParam (UnitEnum::Case);
124130 }
125131
126132 public function testUnitEnumInArray (): void
127133 {
128134 $ this ->expectException (\TypeError::class);
129- $ this ->expectExceptionMessage ('An object in parameter arrays must be ' );
135+ $ this ->expectExceptionMessage ('An object in parameter values must be ' );
130136
131- cast ([UnitEnum::Case]);
137+ $ this -> encodeParam ([UnitEnum::Case]);
132138 }
133139
134140 public function testObjectWithoutToStringMethod (): void
135141 {
136142 $ this ->expectException (\TypeError::class);
137143 $ this ->expectExceptionMessage ('An object in parameter values must be ' );
138144
139- cast (new \stdClass );
145+ $ this -> encodeParam (new \stdClass );
140146 }
141147
142148 public function testObjectWithoutToStringMethodInArray (): void
143149 {
144150 $ this ->expectException (\TypeError::class);
145- $ this ->expectExceptionMessage ('An object in parameter arrays must be ' );
151+ $ this ->expectExceptionMessage ('An object in parameter values must be ' );
146152
147- cast ([new \stdClass ]);
153+ $ this -> encodeParam ([new \stdClass ]);
148154 }
149155}
0 commit comments