|
14 | 14 | namespace CodeIgniter\Database\Builder; |
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\BaseBuilder; |
| 17 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
17 | 18 | use CodeIgniter\Test\CIUnitTestCase; |
18 | 19 | use CodeIgniter\Test\Mock\MockConnection; |
| 20 | +use PHPUnit\Framework\Attributes\DataProvider; |
19 | 21 | use PHPUnit\Framework\Attributes\Group; |
20 | 22 |
|
21 | 23 | /** |
@@ -71,6 +73,135 @@ public function testOrHavingBy(): void |
71 | 73 | $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
72 | 74 | } |
73 | 75 |
|
| 76 | + #[DataProvider('provideHavingBetweenMethods')] |
| 77 | + public function testHavingBetweenMethods(string $method, string $sql): void |
| 78 | + { |
| 79 | + $builder = new BaseBuilder('user', $this->db); |
| 80 | + |
| 81 | + $builder->select('name') |
| 82 | + ->groupBy('name') |
| 83 | + ->{$method}('total', [10, 20]); |
| 84 | + |
| 85 | + $expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING "total" ' . $sql . ' 10 AND 20'; |
| 86 | + $expectedBinds = [ |
| 87 | + 'total' => [ |
| 88 | + 10, |
| 89 | + true, |
| 90 | + ], |
| 91 | + 'total.1' => [ |
| 92 | + 20, |
| 93 | + true, |
| 94 | + ], |
| 95 | + ]; |
| 96 | + |
| 97 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 98 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return iterable<string, array{string, string}> |
| 103 | + */ |
| 104 | + public static function provideHavingBetweenMethods(): iterable |
| 105 | + { |
| 106 | + return [ |
| 107 | + 'between' => ['havingBetween', 'BETWEEN'], |
| 108 | + 'not between' => ['havingNotBetween', 'NOT BETWEEN'], |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + #[DataProvider('provideOrHavingBetweenMethods')] |
| 113 | + public function testOrHavingBetweenMethods(string $method, string $sql): void |
| 114 | + { |
| 115 | + $builder = new BaseBuilder('user', $this->db); |
| 116 | + |
| 117 | + $builder->select('name') |
| 118 | + ->groupBy('name') |
| 119 | + ->having('active', 1) |
| 120 | + ->{$method}('total', [10, 20]); |
| 121 | + |
| 122 | + $expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING "active" = 1 OR "total" ' . $sql . ' 10 AND 20'; |
| 123 | + |
| 124 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return iterable<string, array{string, string}> |
| 129 | + */ |
| 130 | + public static function provideOrHavingBetweenMethods(): iterable |
| 131 | + { |
| 132 | + return [ |
| 133 | + 'or between' => ['orHavingBetween', 'BETWEEN'], |
| 134 | + 'or not between' => ['orHavingNotBetween', 'NOT BETWEEN'], |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + public function testHavingBetweenWithGroupedConditions(): void |
| 139 | + { |
| 140 | + $builder = new BaseBuilder('user', $this->db); |
| 141 | + |
| 142 | + $builder->select('name') |
| 143 | + ->groupBy('name') |
| 144 | + ->havingGroupStart() |
| 145 | + ->havingBetween('total', [10, 20]) |
| 146 | + ->orHavingNotBetween('score', [30, 40]) |
| 147 | + ->havingGroupEnd() |
| 148 | + ->having('active', 1); |
| 149 | + |
| 150 | + $expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING ( "total" BETWEEN 10 AND 20 OR "score" NOT BETWEEN 30 AND 40 ) AND "active" = 1'; |
| 151 | + |
| 152 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 153 | + } |
| 154 | + |
| 155 | + public function testHavingBetweenNoEscape(): void |
| 156 | + { |
| 157 | + $builder = new BaseBuilder('user', $this->db); |
| 158 | + |
| 159 | + $builder->select('name') |
| 160 | + ->groupBy('name') |
| 161 | + ->havingBetween('SUM(id)', [10, 20], escape: false); |
| 162 | + |
| 163 | + $expectedSQL = 'SELECT "name" FROM "user" GROUP BY "name" HAVING SUM(id) BETWEEN 10 AND 20'; |
| 164 | + $expectedBinds = [ |
| 165 | + 'SUM(id)' => [ |
| 166 | + 10, |
| 167 | + false, |
| 168 | + ], |
| 169 | + 'SUM(id).1' => [ |
| 170 | + 20, |
| 171 | + false, |
| 172 | + ], |
| 173 | + ]; |
| 174 | + |
| 175 | + $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 176 | + $this->assertSame($expectedBinds, $builder->getBinds()); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @param mixed $values |
| 181 | + */ |
| 182 | + #[DataProvider('provideHavingBetweenInvalidValuesThrowInvalidArgumentException')] |
| 183 | + public function testHavingBetweenInvalidValuesThrowInvalidArgumentException($values): void |
| 184 | + { |
| 185 | + $this->expectException(InvalidArgumentException::class); |
| 186 | + |
| 187 | + $builder = new BaseBuilder('user', $this->db); |
| 188 | + $builder->havingBetween('total', $values); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return iterable<string, array{mixed}> |
| 193 | + */ |
| 194 | + public static function provideHavingBetweenInvalidValuesThrowInvalidArgumentException(): iterable |
| 195 | + { |
| 196 | + return [ |
| 197 | + 'null' => [null], |
| 198 | + 'not array' => ['not array'], |
| 199 | + 'empty array' => [[]], |
| 200 | + 'one value' => [[10]], |
| 201 | + 'three values' => [[10, 20, 30]], |
| 202 | + ]; |
| 203 | + } |
| 204 | + |
74 | 205 | public function testHavingIn(): void |
75 | 206 | { |
76 | 207 | $builder = new BaseBuilder('user', $this->db); |
|
0 commit comments