44
55namespace Yiisoft \Db \Sqlite \Tests ;
66
7+ use PDO ;
78use PHPUnit \Framework \TestCase ;
9+ use Yiisoft \Db \Command \Param ;
10+ use Yiisoft \Db \Expression \JsonExpression ;
11+ use Yiisoft \Db \Sqlite \ColumnSchema ;
12+ use Yiisoft \Db \Schema \SchemaInterface ;
813use Yiisoft \Db \Sqlite \Tests \Support \TestTrait ;
914use Yiisoft \Db \Query \Query ;
1015
@@ -36,6 +41,8 @@ public function testPhpTypeCast(): void
3641 'timestamp_col ' => '2023-07-11 14:50:23 ' ,
3742 'bool_col ' => false ,
3843 'bit_col ' => 0b0110_0110 , // 102
44+ 'json_col ' => [['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]],
45+ 'json_text_col ' => (new Query ($ db ))->select (new Param ('[1,2,3,"string",null] ' , PDO ::PARAM_STR )),
3946 ]
4047 );
4148 $ command ->execute ();
@@ -51,6 +58,8 @@ public function testPhpTypeCast(): void
5158 $ timestampColPhpType = $ tableSchema ->getColumn ('timestamp_col ' )?->phpTypecast($ query ['timestamp_col ' ]);
5259 $ boolColPhpType = $ tableSchema ->getColumn ('bool_col ' )?->phpTypecast($ query ['bool_col ' ]);
5360 $ bitColPhpType = $ tableSchema ->getColumn ('bit_col ' )?->phpTypecast($ query ['bit_col ' ]);
61+ $ jsonColPhpType = $ tableSchema ->getColumn ('json_col ' )?->phpTypecast($ query ['json_col ' ]);
62+ $ jsonTextColPhpType = $ tableSchema ->getColumn ('json_text_col ' )?->phpTypecast($ query ['json_text_col ' ]);
5463
5564 $ this ->assertSame (1 , $ intColPhpType );
5665 $ this ->assertSame (str_repeat ('x ' , 100 ), $ charColPhpType );
@@ -60,7 +69,19 @@ public function testPhpTypeCast(): void
6069 $ this ->assertSame ('2023-07-11 14:50:23 ' , $ timestampColPhpType );
6170 $ this ->assertFalse ($ boolColPhpType );
6271 $ this ->assertSame (0b0110_0110 , $ bitColPhpType );
72+ $ this ->assertSame ([['a ' => 1 , 'b ' => null , 'c ' => [1 , 3 , 5 ]]], $ jsonColPhpType );
73+ $ this ->assertSame ([1 , 2 , 3 , 'string ' , null ], $ jsonTextColPhpType );
6374
6475 $ db ->close ();
6576 }
77+
78+ public function testTypeCastJson (): void
79+ {
80+ $ columnSchema = new ColumnSchema ('json_col ' );
81+ $ columnSchema ->dbType (SchemaInterface::TYPE_JSON );
82+ $ columnSchema ->type (SchemaInterface::TYPE_JSON );
83+
84+ $ this ->assertSame (['a ' => 1 ], $ columnSchema ->phpTypeCast ('{"a":1} ' ));
85+ $ this ->assertEquals (new JsonExpression (['a ' => 1 ], SchemaInterface::TYPE_JSON ), $ columnSchema ->dbTypeCast (['a ' => 1 ]));
86+ }
6687}
0 commit comments