1212use Yiisoft \Db \Schema \Column \IntegerColumn ;
1313use Yiisoft \Db \Schema \Column \JsonColumn ;
1414use Yiisoft \Db \Schema \Column \StringColumn ;
15+ use Yiisoft \Db \Sqlite \Connection ;
1516use Yiisoft \Db \Sqlite \Tests \Support \TestTrait ;
1617use Yiisoft \Db \Query \Query ;
1718use Yiisoft \Db \Tests \AbstractColumnTest ;
@@ -25,13 +26,9 @@ final class ColumnTest extends AbstractColumnTest
2526{
2627 use TestTrait;
2728
28- public function testPhpTypeCast ( ): void
29+ private function insertTypeValues ( Connection $ db ): void
2930 {
30- $ db = $ this ->getConnection (true );
31-
3231 $ command = $ db ->createCommand ();
33- $ schema = $ db ->getSchema ();
34- $ tableSchema = $ schema ->getTableSchema ('type ' );
3532
3633 $ command ->insert (
3734 'type ' ,
@@ -49,9 +46,95 @@ public function testPhpTypeCast(): void
4946 ]
5047 );
5148 $ command ->execute ();
52- $ query = (new Query ($ db ))->from ('type ' )->one ();
49+ }
50+
51+ private function assertResultValues (array $ result ): void
52+ {
53+ $ this ->assertSame (1 , $ result ['int_col ' ]);
54+ $ this ->assertSame (str_repeat ('x ' , 100 ), $ result ['char_col ' ]);
55+ $ this ->assertNull ($ result ['char_col3 ' ]);
56+ $ this ->assertSame (1.234 , $ result ['float_col ' ]);
57+ $ this ->assertSame ("\x10\x11\x12" , $ result ['blob_col ' ]);
58+ $ this ->assertSame ('2023-07-11 14:50:23 ' , $ result ['timestamp_col ' ]);
59+ $ this ->assertFalse ($ result ['bool_col ' ]);
60+ $ this ->assertSame (0b0110_0110 , $ result ['bit_col ' ]);
61+ $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ result ['json_col ' ]);
62+ $ this ->assertSame ('[1,2,3,"string",null] ' , $ result ['json_text_col ' ]);
63+ }
64+
65+ public function testQueryWithTypecasting (): void
66+ {
67+ $ db = $ this ->getConnection (true );
68+
69+ $ this ->insertTypeValues ($ db );
70+
71+ $ query = (new Query ($ db ))->from ('type ' )->withTypecasting ();
72+
73+ $ result = $ query ->one ();
74+
75+ $ this ->assertResultValues ($ result );
76+
77+ $ result = $ query ->all ();
78+
79+ $ this ->assertResultValues ($ result [0 ]);
80+
81+ $ db ->close ();
82+ }
83+
84+ public function testCommandWithPhpTypecasting (): void
85+ {
86+ $ db = $ this ->getConnection (true );
87+
88+ $ this ->insertTypeValues ($ db );
5389
54- $ this ->assertNotNull ($ tableSchema );
90+ $ command = $ db ->createCommand ('SELECT * FROM type ' )->withPhpTypecasting ();
91+
92+ $ result = $ command ->queryOne ();
93+
94+ $ this ->assertResultValues ($ result );
95+
96+ $ result = $ command ->queryAll ();
97+
98+ $ this ->assertResultValues ($ result [0 ]);
99+
100+ $ db ->close ();
101+ }
102+
103+ public function testSelectWithPhpTypecasting (): void
104+ {
105+ $ db = $ this ->getConnection ();
106+
107+ $ result = $ db ->createCommand ("SELECT null, 1, 2.5, true, false, 'string' " )
108+ ->withPhpTypecasting ()
109+ ->queryOne ();
110+
111+ $ this ->assertSame ([
112+ 'null ' => null ,
113+ 1 => 1 ,
114+ '2.5 ' => 2.5 ,
115+ 'true ' => 1 ,
116+ 'false ' => 0 ,
117+ '\'string \'' => 'string ' ,
118+ ], $ result );
119+
120+ $ result = $ db ->createCommand ('SELECT 2.5 ' )
121+ ->withPhpTypecasting ()
122+ ->queryScalar ();
123+
124+ $ this ->assertSame (2.5 , $ result );
125+
126+ $ db ->close ();
127+ }
128+
129+ public function testPhpTypeCast (): void
130+ {
131+ $ db = $ this ->getConnection (true );
132+ $ schema = $ db ->getSchema ();
133+ $ tableSchema = $ schema ->getTableSchema ('type ' );
134+
135+ $ this ->insertTypeValues ($ db );
136+
137+ $ query = (new Query ($ db ))->from ('type ' )->one ();
55138
56139 $ intColPhpType = $ tableSchema ->getColumn ('int_col ' )?->phpTypecast($ query ['int_col ' ]);
57140 $ charColPhpType = $ tableSchema ->getColumn ('char_col ' )?->phpTypecast($ query ['char_col ' ]);
0 commit comments