55namespace Yiisoft \Db \Tests \Common ;
66
77use PHPUnit \Framework \TestCase ;
8+ use Yiisoft \Db \Expression \Expression ;
9+ use Yiisoft \Db \Helper \UuidHelper ;
10+ use Yiisoft \Db \Query \Query ;
11+ use Yiisoft \Db \Schema \SchemaInterface ;
812use Yiisoft \Db \Tests \Support \TestTrait ;
913
1014use function array_shift ;
@@ -22,6 +26,58 @@ public function testCustomTypes(string $expected, string $type, int|null $length
2226 $ this ->checkBuildString ($ expected , $ type , $ length , $ calls );
2327 }
2428
29+ /**
30+ * @dataProvider \Yiisoft\Db\Tests\Provider\ColumnSchemaBuilderProvider::createColumnTypes
31+ */
32+ public function testCreateColumnTypes (string $ expected , string $ type , int |null $ length , array $ calls ): void
33+ {
34+ $ this ->checkCreateColumn ($ expected , $ type , $ length , $ calls );
35+ }
36+
37+ public function testUuid (): void
38+ {
39+ $ db = $ this ->getConnection ();
40+ $ schema = $ db ->getSchema ();
41+
42+ $ tableName = '{{%column_schema_builder_types}} ' ;
43+ if ($ db ->getTableSchema ($ tableName , true )) {
44+ $ db ->createCommand ()->dropTable ($ tableName )->execute ();
45+ }
46+
47+ $ db ->createCommand ()->createTable ($ tableName , [
48+ 'uuid_pk ' => $ schema ->createColumn (SchemaInterface::TYPE_UUID_PK ),
49+ 'int_col ' => $ schema ->createColumn (SchemaInterface::TYPE_INTEGER ),
50+ ])->execute ();
51+ $ tableSchema = $ db ->getTableSchema ($ tableName , true );
52+ $ this ->assertNotNull ($ tableSchema );
53+
54+ $ uuidValue = $ uuidSource = '738146be-87b1-49f2-9913-36142fb6fcbe ' ;
55+
56+ if ($ db ->getName () === 'oci ' ) {
57+ $ uuidValue = new Expression ('HEXTORAW(REGEXP_REPLACE(:uuid, \'- \', \'\')) ' , [':uuid ' => $ uuidValue ]);
58+ } elseif ($ db ->getName () === 'mysql ' ) {
59+ $ uuidValue = UuidHelper::uuidToBlob ($ uuidValue );
60+ }
61+
62+ $ db ->createCommand ()->insert ($ tableName , [
63+ 'int_col ' => 1 ,
64+ 'uuid_pk ' => $ uuidValue ,
65+ ])->execute ();
66+
67+ $ uuid = (new Query ($ db ))
68+ ->select (['[[uuid_pk]] ' ])
69+ ->from ($ tableName )
70+ ->where (['int_col ' => 1 ])
71+ ->scalar ()
72+ ;
73+
74+ $ uuidString = strtolower (UuidHelper::toUuid ($ uuid ));
75+
76+ $ this ->assertEquals ($ uuidSource , $ uuidString );
77+
78+ $ db ->close ();
79+ }
80+
2581 protected function checkBuildString (string $ expected , string $ type , int |null $ length , array $ calls ): void
2682 {
2783 $ db = $ this ->getConnection ();
@@ -38,4 +94,47 @@ protected function checkBuildString(string $expected, string $type, int|null $le
3894
3995 $ db ->close ();
4096 }
97+
98+ protected function checkCreateColumn (string $ expected , string $ type , int |null $ length , array $ calls ): void
99+ {
100+ $ db = $ this ->getConnection ();
101+
102+ if (str_contains ($ expected , 'UUID_TO_BIN ' )) {
103+ $ serverVersion = $ db ->getServerVersion ();
104+ if (str_contains ($ serverVersion , 'MariaDB ' )) {
105+ $ db ->close ();
106+ $ this ->markTestSkipped ('UUID_TO_BIN not supported MariaDB as defaultValue ' );
107+ }
108+ if (version_compare ($ serverVersion , '8 ' , '< ' )) {
109+ $ db ->close ();
110+ $ this ->markTestSkipped ('UUID_TO_BIN not exists in MySQL 5.7 ' );
111+ }
112+ }
113+
114+ $ schema = $ db ->getSchema ();
115+ $ builder = $ schema ->createColumn ($ type , $ length );
116+
117+ foreach ($ calls as $ call ) {
118+ $ method = array_shift ($ call );
119+ call_user_func_array ([$ builder , $ method ], $ call );
120+ }
121+
122+ $ tableName = '{{%column_schema_builder_types}} ' ;
123+ if ($ db ->getTableSchema ($ tableName , true )) {
124+ $ db ->createCommand ()->dropTable ($ tableName )->execute ();
125+ }
126+
127+ $ command = $ db ->createCommand ()->createTable ($ tableName , [
128+ 'column ' => $ builder ,
129+ ]);
130+
131+ $ this ->assertStringContainsString ("\t" . $ expected . "\n" , $ command ->getRawSql ());
132+
133+ $ command ->execute ();
134+
135+ $ tableSchema = $ db ->getTableSchema ($ tableName , true );
136+ $ this ->assertNotNull ($ tableSchema );
137+
138+ $ db ->close ();
139+ }
41140}
0 commit comments