Skip to content

Commit 744eb43

Browse files
authored
Add ColumnSchemaTest (#228)
1 parent d936981 commit 744eb43

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

tests/ColumnSchemaTest.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

tests/Provider/SchemaProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static function columns(): array
171171
'scale' => 2,
172172
'defaultValue' => 33.22,
173173
],
174-
'time' => [
174+
'timestamp_col' => [
175175
'type' => 'timestamp',
176176
'dbType' => 'TIMESTAMP(6)',
177177
'phpType' => 'string',

tests/Support/Fixture/oci.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ CREATE TABLE "type" (
174174
"float_col2" double precision DEFAULT 1.23,
175175
"blob_col" blob DEFAULT NULL,
176176
"numeric_col" decimal(5,2) DEFAULT 33.22,
177-
"time" timestamp DEFAULT to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') NOT NULL,
177+
"timestamp_col" timestamp DEFAULT to_timestamp('2002-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss') NOT NULL,
178178
"bool_col" char NOT NULL check ("bool_col" in (0,1)),
179179
"bool_col2" char DEFAULT 1 check("bool_col2" in (0,1)),
180180
"ts_default" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,

0 commit comments

Comments
 (0)