|
14 | 14 | namespace CodeIgniter\Database\Live\SQLSRV;
|
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\Live\AbstractGetFieldDataTestCase;
|
| 17 | +use CodeIgniter\Database\SQLSRV\Connection; |
17 | 18 | use Config\Database;
|
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
18 | 20 | use PHPUnit\Framework\Attributes\Group;
|
19 | 21 |
|
20 | 22 | /**
|
@@ -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,58 @@ 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): void |
| 243 | + { |
| 244 | + $this->assertInstanceOf(Connection::class, $this->db); |
| 245 | + |
| 246 | + $normalizeDefault = self::getPrivateMethodInvoker($this->db, 'normalizeDefault'); |
| 247 | + $this->assertSame($expected, $normalizeDefault($input)); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * @return iterable<string, array{string|null, string|null}> |
| 252 | + */ |
| 253 | + public static function provideNormalizeDefault(): iterable |
| 254 | + { |
| 255 | + return [ |
| 256 | + // Null cases |
| 257 | + 'null input' => [null, null], |
| 258 | + 'NULL literal wrapped in parentheses' => ['(NULL)', null], |
| 259 | + 'null literal lowercase' => ['(null)', null], |
| 260 | + 'null literal mixed case' => ['(Null)', null], |
| 261 | + 'null literal random case' => ['(nULL)', null], |
| 262 | + 'null string' => ["('null')", 'null'], |
| 263 | + |
| 264 | + // String literal cases |
| 265 | + 'simple string' => ["('hello')", 'hello'], |
| 266 | + 'empty string' => ['(())', ''], |
| 267 | + 'string with space' => ["('hello world')", 'hello world'], |
| 268 | + 'empty string literal' => ["('')", ''], |
| 269 | + 'string with escaped quote' => ["('can''t')", "can't"], |
| 270 | + 'string with multiple escaped quotes' => ["('it''s a ''test''')", "it's a 'test'"], |
| 271 | + 'concatenated multiline expression' => ["('line1'+char(10)+'line2')", "line1'+char(10)+'line2"], |
| 272 | + |
| 273 | + // Numeric cases |
| 274 | + 'zero with double parentheses' => ['((0))', '0'], |
| 275 | + 'positive integer with double parentheses' => ['((123))', '123'], |
| 276 | + 'negative integer with double parentheses' => ['((-456))', '-456'], |
| 277 | + 'float with double parentheses' => ['((3.14))', '3.14'], |
| 278 | + |
| 279 | + // Function/expression cases |
| 280 | + 'function call' => ['(getdate())', 'getdate()'], |
| 281 | + 'newid function' => ['(newid())', 'newid()'], |
| 282 | + 'user_name function' => ['(user_name())', 'user_name()'], |
| 283 | + 'current_timestamp' => ['(current_timestamp)', 'current_timestamp'], |
| 284 | + 'mathematical expression' => ['((1+1))', '1+1'], |
| 285 | + 'multiplication expression' => ['((100*2))', '100*2'], |
| 286 | + |
| 287 | + // Edge cases |
| 288 | + 'multiple nested parentheses' => ["((('nested')))", 'nested'], |
| 289 | + 'value without parentheses' => ['plain_value', 'plain_value'], |
| 290 | + 'value with parentheses' => ['( plain_value )', 'plain_value'], |
| 291 | + 'function with parameters' => ['(complex_func(1, 2))', 'complex_func(1, 2)'], |
| 292 | + ]; |
| 293 | + } |
238 | 294 | }
|
0 commit comments