22
22
use Hyperf \Database \Schema \Blueprint ;
23
23
use Hyperf \Database \Schema \Schema ;
24
24
use Hyperf \DbConnection \Db ;
25
+ use Hyperf \Stringable \Str ;
25
26
use HyperfTest \Database \PgSQL \Stubs \ContainerStub ;
26
- use HyperfTest \Database \PgSQL \Stubs \SwooleVersionStub ;
27
27
use Mockery as m ;
28
28
use PHPUnit \Framework \Attributes \CoversNothing ;
29
+ use PHPUnit \Framework \Attributes \RequiresPhpExtension ;
29
30
use PHPUnit \Framework \TestCase ;
30
31
31
32
/**
@@ -42,9 +43,9 @@ protected function tearDown(): void
42
43
m::close ();
43
44
}
44
45
46
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
45
47
public function testCreateDatabase ()
46
48
{
47
- SwooleVersionStub::skipV6 ();
48
49
$ grammar = new PostgresGrammar ();
49
50
50
51
$ connection = m::mock (Connection::class);
@@ -58,9 +59,9 @@ public function testCreateDatabase()
58
59
$ this ->assertEquals (true , $ builder ->createDatabase ('my_temporary_database ' ));
59
60
}
60
61
62
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
61
63
public function testDropDatabaseIfExists ()
62
64
{
63
- SwooleVersionStub::skipV6 ();
64
65
$ grammar = new PostgresGrammar ();
65
66
66
67
$ connection = m::mock (Connection::class);
@@ -73,9 +74,9 @@ public function testDropDatabaseIfExists()
73
74
$ this ->assertEquals (true , $ builder ->dropDatabaseIfExists ('my_database_a ' ));
74
75
}
75
76
77
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
76
78
public function testWhereFullText ()
77
79
{
78
- SwooleVersionStub::skipV6 ();
79
80
$ builder = $ this ->getPostgresBuilderWithProcessor ();
80
81
$ builder ->select ('* ' )->from ('users ' )->whereFullText ('body ' , 'Hello World ' );
81
82
$ this ->assertSame ('select * from "users" where (to_tsvector( \'english \', "body")) @@ plainto_tsquery( \'english \', ?) ' , $ builder ->toSql ());
@@ -112,9 +113,95 @@ public function testWhereFullText()
112
113
$ this ->assertEquals (['Car Plane ' ], $ builder ->getBindings ());
113
114
}
114
115
116
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
117
+ public function testJoinLateralPostgres ()
118
+ {
119
+ $ builder = $ this ->getPostgresBuilderWithProcessor ();
120
+ $ builder ->getConnection ()->shouldReceive ('getDatabaseName ' );
121
+ $ builder ->from ('users ' )->joinLateral (function ($ q ) {
122
+ $ q ->from ('contacts ' )->whereColumn ('contracts.user_id ' , 'users.id ' );
123
+ }, 'sub ' );
124
+ $ this ->assertSame ('select * from "users" inner join lateral (select * from "contacts" where "contracts"."user_id" = "users"."id") as "sub" on true ' , $ builder ->toSql ());
125
+ }
126
+
127
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
128
+ public function testJoinLateralTest (): void
129
+ {
130
+ $ container = ContainerStub::getContainer ();
131
+ $ container ->shouldReceive ('get ' )->with (Db::class)->andReturn (new Db ($ container ));
132
+ Schema::dropIfExists ('join_posts ' );
133
+ Schema::dropIfExists ('join_users ' );
134
+ Schema::create ('join_users ' , static function (Blueprint $ table ) {
135
+ $ table ->id ('id ' );
136
+ $ table ->string ('name ' );
137
+ });
138
+
139
+ Schema::create ('join_posts ' , static function (Blueprint $ table ) {
140
+ $ table ->id ('id ' );
141
+ $ table ->string ('title ' );
142
+ $ table ->integer ('rating ' );
143
+ $ table ->unsignedBigInteger ('user_id ' );
144
+ });
145
+ Db::table ('join_users ' )->insert ([
146
+ ['name ' => Str::random ()],
147
+ ['name ' => Str::random ()],
148
+ ]);
149
+
150
+ Db::table ('join_posts ' )->insert ([
151
+ ['title ' => Str::random (), 'rating ' => 1 , 'user_id ' => 1 ],
152
+ ['title ' => Str::random (), 'rating ' => 3 , 'user_id ' => 1 ],
153
+ ['title ' => Str::random (), 'rating ' => 7 , 'user_id ' => 1 ],
154
+ ]);
155
+ $ subquery = Db::table ('join_posts ' )
156
+ ->select ('title as best_post_title ' , 'rating as best_post_rating ' )
157
+ ->whereColumn ('user_id ' , 'join_users.id ' )
158
+ ->orderBy ('rating ' , 'desc ' )
159
+ ->limit (2 );
160
+
161
+ $ userWithPosts = Db::table ('join_users ' )
162
+ ->where ('id ' , 1 )
163
+ ->joinLateral ($ subquery , 'best_post ' )
164
+ ->get ();
165
+
166
+ $ this ->assertCount (2 , $ userWithPosts );
167
+ $ this ->assertEquals (7 , $ userWithPosts [0 ]['best_post_rating ' ]);
168
+ $ this ->assertEquals (3 , $ userWithPosts [1 ]['best_post_rating ' ]);
169
+
170
+ $ userWithoutPosts = Db::table ('join_users ' )
171
+ ->where ('id ' , 2 )
172
+ ->joinLateral ($ subquery , 'best_post ' )
173
+ ->get ();
174
+
175
+ $ this ->assertCount (0 , $ userWithoutPosts );
176
+
177
+ $ subquery = Db::table ('join_posts ' )
178
+ ->select ('title as best_post_title ' , 'rating as best_post_rating ' )
179
+ ->whereColumn ('user_id ' , 'join_users.id ' )
180
+ ->orderBy ('rating ' , 'desc ' )
181
+ ->limit (2 );
182
+
183
+ $ userWithPosts = Db::table ('join_users ' )
184
+ ->where ('id ' , 1 )
185
+ ->leftJoinLateral ($ subquery , 'best_post ' )
186
+ ->get ();
187
+
188
+ $ this ->assertCount (2 , $ userWithPosts );
189
+ $ this ->assertEquals (7 , $ userWithPosts [0 ]['best_post_rating ' ]);
190
+ $ this ->assertEquals (3 , $ userWithPosts [1 ]['best_post_rating ' ]);
191
+
192
+ $ userWithoutPosts = Db::table ('join_users ' )
193
+ ->where ('id ' , 2 )
194
+ ->leftJoinLateral ($ subquery , 'best_post ' )
195
+ ->get ();
196
+
197
+ $ this ->assertCount (1 , $ userWithoutPosts );
198
+ $ this ->assertNull ($ userWithoutPosts [0 ]['best_post_title ' ]);
199
+ $ this ->assertNull ($ userWithoutPosts [0 ]['best_post_rating ' ]);
200
+ }
201
+
202
+ #[RequiresPhpExtension('swoole ' , '< 6.0 ' )]
115
203
public function testWhereFullTextForReal ()
116
204
{
117
- SwooleVersionStub::skipV6 ();
118
205
$ container = ContainerStub::getContainer ();
119
206
$ container ->shouldReceive ('get ' )->with (Db::class)->andReturn (new Db ($ container ));
120
207
0 commit comments