|
17 | 17 | use CodeIgniter\Database\RawSql; |
18 | 18 | use CodeIgniter\Test\CIUnitTestCase; |
19 | 19 | use CodeIgniter\Test\Mock\MockConnection; |
| 20 | +use PHPUnit\Framework\Attributes\DataProvider; |
20 | 21 | use PHPUnit\Framework\Attributes\Group; |
21 | 22 |
|
22 | 23 | /** |
@@ -231,4 +232,114 @@ public function testDBPrefixAndCoulmnWithTablename(): void |
231 | 232 | $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
232 | 233 | $this->assertSame($expectedBinds, $builder->getBinds()); |
233 | 234 | } |
| 235 | + |
| 236 | + public function testLikeMultipleFields(): void |
| 237 | + { |
| 238 | + $builder = new BaseBuilder('job', $this->db); |
| 239 | + |
| 240 | + $builder->like([ |
| 241 | + 'name' => 'veloper', |
| 242 | + 'title' => 'dev', |
| 243 | + ]); |
| 244 | + |
| 245 | + $expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' AND \"title\" LIKE '%dev%' ESCAPE '!'"; |
| 246 | + $expectedBinds = [ |
| 247 | + 'name' => [ |
| 248 | + '%veloper%', |
| 249 | + true, |
| 250 | + ], |
| 251 | + 'title' => [ |
| 252 | + '%dev%', |
| 253 | + true, |
| 254 | + ], |
| 255 | + ]; |
| 256 | + |
| 257 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 258 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 259 | + } |
| 260 | + |
| 261 | + public function testLikeMultipleCallsWithRawSqlAndString(): void |
| 262 | + { |
| 263 | + $builder = new BaseBuilder('users', $this->db); |
| 264 | + |
| 265 | + $sql = "concat(users.name, ' ', users.surname)"; |
| 266 | + $rawSql = new RawSql($sql); |
| 267 | + |
| 268 | + $builder->like($rawSql, 'value')->like('name', 'veloper'); |
| 269 | + |
| 270 | + $expectedSQL = "SELECT * FROM \"users\" WHERE {$sql} LIKE '%value%' ESCAPE '!' AND \"name\" LIKE '%veloper%' ESCAPE '!'"; |
| 271 | + $expectedBinds = [ |
| 272 | + $rawSql->getBindingKey() => [ |
| 273 | + '%value%', |
| 274 | + true, |
| 275 | + ], |
| 276 | + 'name' => [ |
| 277 | + '%veloper%', |
| 278 | + true, |
| 279 | + ], |
| 280 | + ]; |
| 281 | + |
| 282 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 283 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 284 | + } |
| 285 | + |
| 286 | + public function testOrLikeMultipleFields(): void |
| 287 | + { |
| 288 | + $builder = new BaseBuilder('job', $this->db); |
| 289 | + |
| 290 | + $builder->orLike([ |
| 291 | + 'name' => 'veloper', |
| 292 | + 'title' => 'dev', |
| 293 | + ]); |
| 294 | + |
| 295 | + $expectedSQL = "SELECT * FROM \"job\" WHERE \"name\" LIKE '%veloper%' ESCAPE '!' OR \"title\" LIKE '%dev%' ESCAPE '!'"; |
| 296 | + $expectedBinds = [ |
| 297 | + 'name' => [ |
| 298 | + '%veloper%', |
| 299 | + true, |
| 300 | + ], |
| 301 | + 'title' => [ |
| 302 | + '%dev%', |
| 303 | + true, |
| 304 | + ], |
| 305 | + ]; |
| 306 | + |
| 307 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 308 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 309 | + } |
| 310 | + |
| 311 | + #[DataProvider('provideLikeMethodsWithEmptyArray')] |
| 312 | + public function testLikeMethodsWithEmptyArray(string $method): void |
| 313 | + { |
| 314 | + $builder = new BaseBuilder('job', $this->db); |
| 315 | + |
| 316 | + $builder->groupStart() |
| 317 | + ->{$method}([]) |
| 318 | + ->where('id', 1) |
| 319 | + ->groupEnd(); |
| 320 | + |
| 321 | + $expectedSQL = 'SELECT * FROM "job" WHERE ( "id" = 1 )'; |
| 322 | + $expectedBinds = [ |
| 323 | + 'id' => [ |
| 324 | + 1, |
| 325 | + true, |
| 326 | + ], |
| 327 | + ]; |
| 328 | + |
| 329 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 330 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 331 | + } |
| 332 | + |
| 333 | + /** |
| 334 | + * @return array<string, array{0: string}> |
| 335 | + */ |
| 336 | + public static function provideLikeMethodsWithEmptyArray(): iterable |
| 337 | + { |
| 338 | + return [ |
| 339 | + 'like' => ['like'], |
| 340 | + 'orLike' => ['orLike'], |
| 341 | + 'notLike' => ['notLike'], |
| 342 | + 'orNotLike' => ['orNotLike'], |
| 343 | + ]; |
| 344 | + } |
234 | 345 | } |
0 commit comments