|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Tests\Database\Schema; |
| 15 | + |
| 16 | +use CodeIgniter\PHPStan\Database\Schema\Column; |
| 17 | +use CodeIgniter\PHPStan\Database\Schema\ColumnTypeResolver; |
| 18 | +use PHPStan\Type\VerbosityLevel; |
| 19 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +#[Group('unit')] |
| 27 | +final class ColumnTypeResolverTest extends TestCase |
| 28 | +{ |
| 29 | + #[DataProvider('provideMapsDeclaredTypeToPhpStanTypeCases')] |
| 30 | + public function testMapsDeclaredTypeToPhpStanType(string $declaredType, bool $nullable, string $expected): void |
| 31 | + { |
| 32 | + $type = (new ColumnTypeResolver())->resolve(new Column('column', $declaredType, $nullable, false, null)); |
| 33 | + |
| 34 | + self::assertSame($expected, $type->describe(VerbosityLevel::precise())); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return iterable<string, array{string, bool, string}> |
| 39 | + */ |
| 40 | + public static function provideMapsDeclaredTypeToPhpStanTypeCases(): iterable |
| 41 | + { |
| 42 | + yield 'INTEGER' => ['INTEGER', false, 'int']; |
| 43 | + |
| 44 | + yield 'INT' => ['INT', false, 'int']; |
| 45 | + |
| 46 | + yield 'BIGINT' => ['BIGINT', false, 'int']; |
| 47 | + |
| 48 | + yield 'VARCHAR' => ['VARCHAR', false, 'string']; |
| 49 | + |
| 50 | + yield 'TEXT' => ['TEXT', false, 'string']; |
| 51 | + |
| 52 | + yield 'BLOB' => ['BLOB', false, 'string']; |
| 53 | + |
| 54 | + yield 'REAL' => ['REAL', false, 'float']; |
| 55 | + |
| 56 | + yield 'FLOAT' => ['FLOAT', false, 'float']; |
| 57 | + |
| 58 | + yield 'DOUBLE' => ['DOUBLE', false, 'float']; |
| 59 | + |
| 60 | + yield 'DATETIME (numeric -> string)' => ['DATETIME', false, 'string']; |
| 61 | + |
| 62 | + yield 'DECIMAL (numeric -> string)' => ['DECIMAL', false, 'string']; |
| 63 | + |
| 64 | + yield 'nullable INTEGER' => ['INTEGER', true, 'int|null']; |
| 65 | + |
| 66 | + yield 'nullable VARCHAR' => ['VARCHAR', true, 'string|null']; |
| 67 | + } |
| 68 | +} |
0 commit comments