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