|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Db\Sqlite\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Yiisoft\Db\Sqlite\Tests\Support\TestTrait; |
| 9 | +use Yiisoft\Db\Query\Query; |
| 10 | + |
| 11 | +use function str_repeat; |
| 12 | + |
| 13 | +/** |
| 14 | + * @group sqlite |
| 15 | + */ |
| 16 | +final class ColumnSchemaTest extends TestCase |
| 17 | +{ |
| 18 | + use TestTrait; |
| 19 | + |
| 20 | + public function testPhpTypeCast(): void |
| 21 | + { |
| 22 | + $db = $this->getConnection(true); |
| 23 | + |
| 24 | + $command = $db->createCommand(); |
| 25 | + $schema = $db->getSchema(); |
| 26 | + $tableSchema = $schema->getTableSchema('type'); |
| 27 | + |
| 28 | + $command->insert( |
| 29 | + 'type', |
| 30 | + [ |
| 31 | + 'int_col' => 1, |
| 32 | + 'char_col' => str_repeat('x', 100), |
| 33 | + 'char_col3' => null, |
| 34 | + 'float_col' => 1.234, |
| 35 | + 'blob_col' => "\x10\x11\x12", |
| 36 | + 'timestamp_col' => '2023-07-11 14:50:23', |
| 37 | + 'bool_col' => false, |
| 38 | + ] |
| 39 | + ); |
| 40 | + $command->execute(); |
| 41 | + $query = (new Query($db))->from('type')->one(); |
| 42 | + |
| 43 | + $this->assertNotNull($tableSchema); |
| 44 | + |
| 45 | + $intColPhpType = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']); |
| 46 | + $charColPhpType = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']); |
| 47 | + $charCol3PhpType = $tableSchema->getColumn('char_col3')?->phpTypecast($query['char_col3']); |
| 48 | + $floatColPhpType = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']); |
| 49 | + $blobColPhpType = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']); |
| 50 | + $timestampColPhpType = $tableSchema->getColumn('timestamp_col')?->phpTypecast($query['timestamp_col']); |
| 51 | + $boolColPhpType = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']); |
| 52 | + |
| 53 | + $this->assertSame(1, $intColPhpType); |
| 54 | + $this->assertSame(str_repeat('x', 100), $charColPhpType); |
| 55 | + $this->assertNull($charCol3PhpType); |
| 56 | + $this->assertSame(1.234, $floatColPhpType); |
| 57 | + $this->assertSame("\x10\x11\x12", $blobColPhpType); |
| 58 | + $this->assertSame('2023-07-11 14:50:23', $timestampColPhpType); |
| 59 | + $this->assertFalse($boolColPhpType); |
| 60 | + |
| 61 | + $db->close(); |
| 62 | + } |
| 63 | +} |
0 commit comments