|
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\Live\AbstractGetFieldDataTestCase; |
17 | 17 | use Config\Database; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
18 | 19 | use PHPUnit\Framework\Attributes\Group; |
| 20 | +use ReflectionClass; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * @internal |
@@ -68,31 +70,31 @@ public function testGetFieldDataDefault(): void |
68 | 70 | 'type' => 'int', |
69 | 71 | 'max_length' => 10, |
70 | 72 | 'nullable' => false, |
71 | | - 'default' => '((0))', // int 0 |
| 73 | + 'default' => '0', |
72 | 74 | // 'primary_key' => 0, |
73 | 75 | ], |
74 | 76 | (object) [ |
75 | 77 | 'name' => 'text_default_null', |
76 | 78 | 'type' => 'varchar', |
77 | 79 | 'max_length' => 64, |
78 | 80 | 'nullable' => true, |
79 | | - 'default' => '(NULL)', // NULL value |
| 81 | + 'default' => null, |
80 | 82 | // 'primary_key' => 0, |
81 | 83 | ], |
82 | 84 | (object) [ |
83 | 85 | 'name' => 'text_default_text_null', |
84 | 86 | 'type' => 'varchar', |
85 | 87 | 'max_length' => 64, |
86 | 88 | 'nullable' => false, |
87 | | - 'default' => "('null')", // string "null" |
| 89 | + 'default' => 'null', // string "null" |
88 | 90 | // 'primary_key' => 0, |
89 | 91 | ], |
90 | 92 | (object) [ |
91 | 93 | 'name' => 'text_default_abc', |
92 | 94 | 'type' => 'varchar', |
93 | 95 | 'max_length' => 64, |
94 | 96 | 'nullable' => false, |
95 | | - 'default' => "('abc')", // string "abc" |
| 97 | + 'default' => 'abc', |
96 | 98 | // 'primary_key' => 0, |
97 | 99 | ], |
98 | 100 | ]; |
@@ -235,4 +237,59 @@ public function testGetFieldDataType(): void |
235 | 237 | ]; |
236 | 238 | $this->assertSameFieldData($expected, $fields); |
237 | 239 | } |
| 240 | + |
| 241 | + #[DataProvider('provideNormalizeDefault')] |
| 242 | + public function testNormalizeDefault(?string $input, ?string $expected, string $description): void |
| 243 | + { |
| 244 | + $reflection = new ReflectionClass($this->db); |
| 245 | + $method = $reflection->getMethod('normalizeDefault'); |
| 246 | + |
| 247 | + $result = $method->invoke($this->db, $input); |
| 248 | + |
| 249 | + $this->assertSame($expected, $result, "Failed test: {$description}"); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * @return iterable<array{string|null, string|null, string}> |
| 254 | + */ |
| 255 | + public static function provideNormalizeDefault(): iterable |
| 256 | + { |
| 257 | + return [ |
| 258 | + // [input, expected_output, description] |
| 259 | + |
| 260 | + // Null cases |
| 261 | + [null, null, 'null input'], |
| 262 | + ['(NULL)', null, 'NULL literal wrapped in parentheses'], |
| 263 | + ['(null)', null, 'null literal lowercase'], |
| 264 | + ['(Null)', null, 'null literal mixed case'], |
| 265 | + ['(nULL)', null, 'null literal random case'], |
| 266 | + |
| 267 | + // String literal cases |
| 268 | + ["('hello')", 'hello', 'simple string'], |
| 269 | + ["('hello world')", 'hello world', 'string with space'], |
| 270 | + ["('')", '', 'empty string literal'], |
| 271 | + ["('can''t')", "can't", 'string with escaped quote'], |
| 272 | + ["('it''s a ''test''')", "it's a 'test'", 'string with multiple escaped quotes'], |
| 273 | + ["('line1'+char(10)+'line2')", "line1'+char(10)+'line2", 'concatenated multiline expression'], |
| 274 | + |
| 275 | + // Numeric cases |
| 276 | + ['((0))', '0', 'zero with double parentheses'], |
| 277 | + ['((123))', '123', 'positive integer with double parentheses'], |
| 278 | + ['((-456))', '-456', 'negative integer with double parentheses'], |
| 279 | + ['((3.14))', '3.14', 'float with double parentheses'], |
| 280 | + |
| 281 | + // Function/expression cases |
| 282 | + ['(getdate())', 'getdate()', 'function call'], |
| 283 | + ['(newid())', 'newid()', 'newid function'], |
| 284 | + ['(user_name())', 'user_name()', 'user_name function'], |
| 285 | + ['(current_timestamp)', 'current_timestamp', 'current_timestamp'], |
| 286 | + ['((1+1))', '1+1', 'mathematical expression'], |
| 287 | + ['((100*2))', '100*2', 'multiplication expression'], |
| 288 | + |
| 289 | + // Edge cases |
| 290 | + ["((('nested')))", 'nested', 'multiple nested parentheses'], |
| 291 | + ['plain_value', 'plain_value', 'value without parentheses'], |
| 292 | + ['(complex_func(1, 2))', 'complex_func(1, 2)', 'function with parameters'], |
| 293 | + ]; |
| 294 | + } |
238 | 295 | } |
0 commit comments