|
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\BaseBuilder; |
17 | 17 | use CodeIgniter\Database\RawSql; |
| 18 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
18 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
19 | 20 | use CodeIgniter\Test\Mock\MockConnection; |
| 21 | +use PHPUnit\Framework\Attributes\DataProvider; |
20 | 22 | use PHPUnit\Framework\Attributes\Group; |
21 | 23 |
|
22 | 24 | /** |
@@ -52,6 +54,124 @@ public function testSimpleLike(): void |
52 | 54 | $this->assertSame($expectedBinds, $builder->getBinds()); |
53 | 55 | } |
54 | 56 |
|
| 57 | + public function testLikeAny(): void |
| 58 | + { |
| 59 | + $builder = new BaseBuilder('job', $this->db); |
| 60 | + |
| 61 | + $builder->likeAny(['name', 'description'], 'veloper'); |
| 62 | + |
| 63 | + $expectedSQL = 'SELECT * FROM "job" WHERE ( "name" LIKE \'%veloper%\' ESCAPE \'!\' OR "description" LIKE \'%veloper%\' ESCAPE \'!\' )'; |
| 64 | + $expectedBinds = [ |
| 65 | + 'name' => [ |
| 66 | + '%veloper%', |
| 67 | + true, |
| 68 | + ], |
| 69 | + 'description' => [ |
| 70 | + '%veloper%', |
| 71 | + true, |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + $this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect()))); |
| 76 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testLikeAnyAfterWhere(): void |
| 80 | + { |
| 81 | + $builder = new BaseBuilder('job', $this->db); |
| 82 | + |
| 83 | + $builder->where('active', 1) |
| 84 | + ->likeAny(['name', 'description'], 'veloper'); |
| 85 | + |
| 86 | + $expectedSQL = 'SELECT * FROM "job" WHERE "active" = 1 AND ( "name" LIKE \'%veloper%\' ESCAPE \'!\' OR "description" LIKE \'%veloper%\' ESCAPE \'!\' )'; |
| 87 | + |
| 88 | + $this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect()))); |
| 89 | + } |
| 90 | + |
| 91 | + public function testOrLikeAnyAfterWhere(): void |
| 92 | + { |
| 93 | + $builder = new BaseBuilder('job', $this->db); |
| 94 | + |
| 95 | + $builder->where('active', 1) |
| 96 | + ->orLikeAny(['name', 'description'], 'veloper'); |
| 97 | + |
| 98 | + $expectedSQL = 'SELECT * FROM "job" WHERE "active" = 1 OR ( "name" LIKE \'%veloper%\' ESCAPE \'!\' OR "description" LIKE \'%veloper%\' ESCAPE \'!\' )'; |
| 99 | + |
| 100 | + $this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect()))); |
| 101 | + } |
| 102 | + |
| 103 | + public function testLikeAnyCaseInsensitiveSearch(): void |
| 104 | + { |
| 105 | + $builder = new BaseBuilder('job', $this->db); |
| 106 | + |
| 107 | + $builder->likeAny(['name', 'description'], 'VELOPER', 'both', null, true); |
| 108 | + |
| 109 | + $expectedSQL = 'SELECT * FROM "job" WHERE ( LOWER("name") LIKE \'%veloper%\' ESCAPE \'!\' OR LOWER("description") LIKE \'%veloper%\' ESCAPE \'!\' )'; |
| 110 | + $expectedBinds = [ |
| 111 | + 'name' => [ |
| 112 | + '%veloper%', |
| 113 | + true, |
| 114 | + ], |
| 115 | + 'description' => [ |
| 116 | + '%veloper%', |
| 117 | + true, |
| 118 | + ], |
| 119 | + ]; |
| 120 | + |
| 121 | + $this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect()))); |
| 122 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 123 | + } |
| 124 | + |
| 125 | + public function testLikeAnyWithRawSqlField(): void |
| 126 | + { |
| 127 | + $builder = new BaseBuilder('job', $this->db); |
| 128 | + $rawSql = new RawSql('LOWER(description)'); |
| 129 | + |
| 130 | + $builder->likeAny(['name', $rawSql], 'veloper', 'after'); |
| 131 | + |
| 132 | + $expectedSQL = 'SELECT * FROM "job" WHERE ( "name" LIKE \'veloper%\' ESCAPE \'!\' OR LOWER(description) LIKE \'veloper%\' ESCAPE \'!\' )'; |
| 133 | + $expectedBinds = [ |
| 134 | + 'name' => [ |
| 135 | + 'veloper%', |
| 136 | + true, |
| 137 | + ], |
| 138 | + $rawSql->getBindingKey() => [ |
| 139 | + 'veloper%', |
| 140 | + true, |
| 141 | + ], |
| 142 | + ]; |
| 143 | + |
| 144 | + $this->assertSame($expectedSQL, (string) preg_replace('/\s+/', ' ', trim($builder->getCompiledSelect()))); |
| 145 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param mixed $fields |
| 150 | + */ |
| 151 | + #[DataProvider('provideLikeAnyInvalidFieldsThrowInvalidArgumentException')] |
| 152 | + public function testLikeAnyInvalidFieldsThrowInvalidArgumentException($fields): void |
| 153 | + { |
| 154 | + $this->expectException(InvalidArgumentException::class); |
| 155 | + |
| 156 | + $builder = new BaseBuilder('job', $this->db); |
| 157 | + |
| 158 | + $builder->likeAny($fields, 'veloper'); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @return iterable<string, array{mixed}> |
| 163 | + */ |
| 164 | + public static function provideLikeAnyInvalidFieldsThrowInvalidArgumentException(): iterable |
| 165 | + { |
| 166 | + return [ |
| 167 | + 'empty list' => [[]], |
| 168 | + 'assoc array' => [['name' => 'description']], |
| 169 | + 'empty field' => [['name', '']], |
| 170 | + 'blank field' => [['name', ' ']], |
| 171 | + 'int field' => [['name', 1]], |
| 172 | + ]; |
| 173 | + } |
| 174 | + |
55 | 175 | /** |
56 | 176 | * @see https://github.com/codeigniter4/CodeIgniter4/issues/3970 |
57 | 177 | */ |
|
0 commit comments