|
19 | 19 | use CodeIgniter\Database\SQLSRV\Builder as SQLSRVBuilder; |
20 | 20 | use CodeIgniter\Test\CIUnitTestCase; |
21 | 21 | use CodeIgniter\Test\Mock\MockConnection; |
| 22 | +use PHPUnit\Framework\Attributes\DataProvider; |
22 | 23 | use PHPUnit\Framework\Attributes\Group; |
23 | 24 |
|
24 | 25 | /** |
@@ -67,6 +68,65 @@ public function testSelectAcceptsArray(): void |
67 | 68 | $this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect())); |
68 | 69 | } |
69 | 70 |
|
| 71 | + /** |
| 72 | + * @param list<RawSql|string> $select |
| 73 | + */ |
| 74 | + #[DataProvider('provideSelectAcceptsArrayWithRawSql')] |
| 75 | + public function testSelectAcceptsArrayWithRawSql(array $select, string $expected): void |
| 76 | + { |
| 77 | + $builder = new BaseBuilder('employees', $this->db); |
| 78 | + |
| 79 | + $builder->select($select); |
| 80 | + |
| 81 | + $this->assertSame($expected, str_replace("\n", ' ', $builder->getCompiledSelect())); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @return list<list<RawSql|string>|string> |
| 86 | + */ |
| 87 | + public static function provideSelectAcceptsArrayWithRawSql(): iterable |
| 88 | + { |
| 89 | + yield from [ |
| 90 | + [ |
| 91 | + [ |
| 92 | + new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"), |
| 93 | + 'employee_id', |
| 94 | + ], |
| 95 | + <<<'SQL' |
| 96 | + SELECT IF(salary > 5000, 'High', 'Low') AS salary_level, "employee_id" FROM "employees" |
| 97 | + SQL, |
| 98 | + ], |
| 99 | + [ |
| 100 | + [ |
| 101 | + 'employee_id', |
| 102 | + new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"), |
| 103 | + ], |
| 104 | + <<<'SQL' |
| 105 | + SELECT "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees" |
| 106 | + SQL, |
| 107 | + ], |
| 108 | + [ |
| 109 | + [ |
| 110 | + new RawSql("CONCAT(first_name, ' ', last_name) AS full_name"), |
| 111 | + new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"), |
| 112 | + ], |
| 113 | + <<<'SQL' |
| 114 | + SELECT CONCAT(first_name, ' ', last_name) AS full_name, IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees" |
| 115 | + SQL, |
| 116 | + ], |
| 117 | + [ |
| 118 | + [ |
| 119 | + new RawSql("CONCAT(first_name, ' ', last_name) AS full_name"), |
| 120 | + 'employee_id', |
| 121 | + new RawSql("IF(salary > 5000, 'High', 'Low') AS salary_level"), |
| 122 | + ], |
| 123 | + <<<'SQL' |
| 124 | + SELECT CONCAT(first_name, ' ', last_name) AS full_name, "employee_id", IF(salary > 5000, 'High', 'Low') AS salary_level FROM "employees" |
| 125 | + SQL, |
| 126 | + ], |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
70 | 130 | public function testSelectAcceptsMultipleColumns(): void |
71 | 131 | { |
72 | 132 | $builder = new BaseBuilder('users', $this->db); |
|
0 commit comments