23
23
use Yiisoft \Db \Schema \Column \ColumnInterface ;
24
24
use Yiisoft \Db \Schema \Data \JsonLazyArray ;
25
25
use Yiisoft \Db \Schema \Data \LazyArrayInterface ;
26
+ use Yiisoft \Db \Tests \Support \Assert ;
26
27
27
28
/**
28
29
* @group pgsql
@@ -34,41 +35,45 @@ final class ArrayExpressionBuilderTest extends TestCase
34
35
public static function buildProvider (): array
35
36
{
36
37
return [
37
- [null , null , 'NULL ' , []],
38
- [[], null , 'ARRAY[] ' , []],
39
- [[1 , 2 , 3 ], null , 'ARRAY[:qp0,:qp1,:qp2 ] ' , [' :qp0 ' => 1 , ' :qp1 ' => 2 , ' :qp2 ' => 3 ]],
40
- [
38
+ ' null ' => [null , null , 'NULL ' , []],
39
+ ' empty ' => [[], null , 'ARRAY[] ' , []],
40
+ ' list ' => [[1 , 2 , 3 ], null , 'ARRAY[1,2,3 ] ' , []],
41
+ ' ArrayIterator ' => [
41
42
new ArrayIterator (['a ' , 'b ' , 'c ' ]),
42
43
'varchar ' ,
43
44
'ARRAY[:qp0,:qp1,:qp2]::varchar[] ' ,
44
- [':qp0 ' => 'a ' , ':qp1 ' => 'b ' , ':qp2 ' => 'c ' ],
45
+ [
46
+ ':qp0 ' => new Param ('a ' , DataType::STRING ),
47
+ ':qp1 ' => new Param ('b ' , DataType::STRING ),
48
+ ':qp2 ' => new Param ('c ' , DataType::STRING ),
49
+ ],
45
50
],
46
- [
51
+ ' LazyArray ' => [
47
52
new LazyArray ('{1,2,3} ' ),
48
53
'int[] ' ,
49
54
':qp0::int[] ' ,
50
55
[':qp0 ' => new Param ('{1,2,3} ' , DataType::STRING )],
51
56
],
52
- [
57
+ ' LazyArray external ' => [
53
58
new \Yiisoft \Db \Schema \Data \LazyArray ('[1,2,3] ' ),
54
59
ColumnBuilder::integer (),
55
- 'ARRAY[:qp0,:qp1,:qp2 ]::integer[] ' ,
56
- [' :qp0 ' => 1 , ' :qp1 ' => 2 , ' :qp2 ' => 3 ],
60
+ 'ARRAY[1,2,3 ]::integer[] ' ,
61
+ [],
57
62
],
58
- [
63
+ ' StructuredLazyArray ' => [
59
64
new StructuredLazyArray ('(1,2,3) ' ),
60
65
'int ' ,
61
- 'ARRAY[:qp0,:qp1,:qp2 ]::int[] ' ,
62
- [' :qp0 ' => 1 , ' :qp1 ' => 2 , ' :qp2 ' => 3 ],
66
+ 'ARRAY[1,2,3 ]::int[] ' ,
67
+ [],
63
68
],
64
- [
69
+ ' JsonLazyArray ' => [
65
70
new JsonLazyArray ('[1,2,3] ' ),
66
71
ColumnBuilder::array (ColumnBuilder::integer ()),
67
- 'ARRAY[:qp0,:qp1,:qp2 ]::integer[] ' ,
68
- [' :qp0 ' => 1 , ' :qp1 ' => 2 , ' :qp2 ' => 3 ],
72
+ 'ARRAY[1,2,3 ]::integer[] ' ,
73
+ [],
69
74
],
70
- [[new Expression ('now() ' )], null , 'ARRAY[now()] ' , []],
71
- [
75
+ ' Expression ' => [[new Expression ('now() ' )], null , 'ARRAY[now()] ' , []],
76
+ ' JsonExpression w/o type ' => [
72
77
[new JsonExpression (['a ' => null , 'b ' => 123 , 'c ' => [4 , 5 ]]), new JsonExpression ([true ])],
73
78
null ,
74
79
'ARRAY[:qp0,:qp1] ' ,
@@ -77,7 +82,7 @@ public static function buildProvider(): array
77
82
':qp1 ' => new Param ('[true] ' , DataType::STRING ),
78
83
],
79
84
],
80
- [
85
+ ' JsonExpression ' => [
81
86
[new JsonExpression (['a ' => null , 'b ' => 123 , 'c ' => [4 , 5 ]]), new JsonExpression ([true ])],
82
87
'jsonb ' ,
83
88
'ARRAY[:qp0,:qp1]::jsonb[] ' ,
@@ -86,57 +91,51 @@ public static function buildProvider(): array
86
91
':qp1 ' => new Param ('[true] ' , DataType::STRING ),
87
92
],
88
93
],
89
- [
94
+ ' StructuredExpression ' => [
90
95
[
91
96
null ,
92
97
new StructuredExpression (['value ' => 11.11 , 'currency_code ' => 'USD ' ]),
93
98
new StructuredExpression (['value ' => null , 'currency_code ' => null ]),
94
99
],
95
100
null ,
96
- 'ARRAY[:qp0 ,ROW(:qp1,:qp2 ),ROW(:qp3,:qp4 )] ' ,
97
- [':qp0 ' => null , ' :qp1 ' => 11.11 , ' :qp2 ' => ' USD ' , ' :qp3 ' => null , ' :qp4 ' => null ],
101
+ 'ARRAY[NULL ,ROW(11.11,:qp0 ),ROW(NULL,NULL )] ' ,
102
+ [':qp0 ' => new Param ( ' USD ' , DataType:: STRING ) ],
98
103
],
99
- [
104
+ ' Query w/o type ' => [
100
105
(new Query (self ::getDb ()))->select ('id ' )->from ('users ' )->where (['active ' => 1 ]),
101
106
null ,
102
107
'ARRAY(SELECT "id" FROM "users" WHERE "active"=:qp0) ' ,
103
108
[':qp0 ' => 1 ],
104
109
],
105
- [
110
+ ' Query ' => [
106
111
[(new Query (self ::getDb ()))->select ('id ' )->from ('users ' )->where (['active ' => 1 ])],
107
112
'integer[][] ' ,
108
113
'ARRAY[ARRAY(SELECT "id" FROM "users" WHERE "active"=:qp0)::integer[]]::integer[][] ' ,
109
114
[':qp0 ' => 1 ],
110
115
],
111
- [
116
+ ' bool ' => [
112
117
[[[true ], [false , null ]], [['t ' , 'f ' ], null ], null ],
113
118
'bool[][][] ' ,
114
- 'ARRAY[ARRAY[ARRAY[:qp0]::bool[],ARRAY[:qp1,:qp2]::bool[]]::bool[][],ARRAY[ARRAY[:qp3,:qp4]::bool[],NULL]::bool[][],NULL]::bool[][][] ' ,
115
- [
116
- ':qp0 ' => true ,
117
- ':qp1 ' => false ,
118
- ':qp2 ' => null ,
119
- ':qp3 ' => 't ' ,
120
- ':qp4 ' => 'f ' ,
121
- ],
119
+ 'ARRAY[ARRAY[ARRAY[TRUE]::bool[],ARRAY[FALSE,NULL]::bool[]]::bool[][],ARRAY[ARRAY[TRUE,TRUE]::bool[],NULL]::bool[][],NULL]::bool[][][] ' ,
120
+ [],
122
121
],
123
- [
122
+ ' associative ' => [
124
123
['a ' => '1 ' , 'b ' => null ],
125
124
ColumnType::STRING ,
126
- 'ARRAY[:qp0,:qp1 ]::varchar(255)[] ' ,
127
- [':qp0 ' => '1 ' , ' :qp1 ' => null ],
125
+ 'ARRAY[:qp0,NULL ]::varchar(255)[] ' ,
126
+ [':qp0 ' => new Param ( '1 ' , DataType:: STRING ) ],
128
127
],
129
- [
128
+ ' string ' => [
130
129
'{1,2,3} ' ,
131
130
'string[] ' ,
132
131
':qp0::varchar(255)[] ' ,
133
132
[':qp0 ' => new Param ('{1,2,3} ' , DataType::STRING )],
134
133
],
135
- [
134
+ ' null multi-level ' => [
136
135
[[1 , null ], null ],
137
136
'int[][] ' ,
138
- 'ARRAY[ARRAY[:qp0,:qp1 ]::int[],NULL]::int[][] ' ,
139
- [' :qp0 ' => ' 1 ' , ' :qp1 ' => null ],
137
+ 'ARRAY[ARRAY[1,NULL ]::int[],NULL]::int[][] ' ,
138
+ [],
140
139
],
141
140
];
142
141
}
@@ -156,6 +155,6 @@ public function testBuild(
156
155
$ expression = new ArrayExpression ($ value , $ type );
157
156
158
157
$ this ->assertSame ($ expected , $ builder ->build ($ expression , $ params ));
159
- $ this -> assertEquals ($ expectedParams , $ params );
158
+ Assert:: arraysEquals ($ expectedParams , $ params );
160
159
}
161
160
}
0 commit comments