Skip to content

Commit 3f45df6

Browse files
committed
Add more tests.
1 parent 04b9708 commit 3f45df6

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @link https://www.yiiframework.com/
4+
* @copyright Copyright (c) 2008 Yii Software LLC
5+
* @license https://www.yiiframework.com/license/
6+
*/
7+
8+
namespace yiiunit\framework\db\mysql\type;
9+
10+
use yii\db\JsonExpression;
11+
use yii\db\mysql\Schema;
12+
use yiiunit\framework\db\DatabaseTestCase;
13+
14+
/**
15+
* @group db
16+
* @group mysql
17+
*/
18+
class JsonTest extends DatabaseTestCase
19+
{
20+
protected $driverName = 'mysql';
21+
22+
public function testCreateTable(): void
23+
{
24+
$db = $this->getConnection();
25+
26+
if ($db->getSchema()->getTableSchema('json') !== null) {
27+
$db->createCommand()->dropTable('json')->execute();
28+
}
29+
30+
$command = $db->createCommand();
31+
$command->createTable('json', ['id' => Schema::TYPE_PK, 'data' => Schema::TYPE_JSON])->execute();
32+
33+
$this->assertTrue($db->getTableSchema('json') !== null);
34+
$this->assertSame('data', $db->getTableSchema('json')->getColumn('data')->name);
35+
$this->assertSame('json', $db->getTableSchema('json')->getColumn('data')->type);
36+
}
37+
38+
public function testInsertAndSelect(): void
39+
{
40+
$db = $this->getConnection(true);
41+
$version = $db->getServerVersion();
42+
43+
$command = $db->createCommand();
44+
$command->insert('storage', ['data' => ['a' => 1, 'b' => 2]])->execute();
45+
46+
if (\stripos($version, 'MariaDb') === false) {
47+
$rowExpected = '{"a": 1, "b": 2}';
48+
} else {
49+
$rowExpected = '{"a":1,"b":2}';
50+
}
51+
52+
$this->assertSame(
53+
$rowExpected,
54+
$command->setSql(
55+
<<<SQL
56+
SELECT `data` FROM `storage`
57+
SQL,
58+
)->queryScalar(),
59+
);
60+
}
61+
62+
public function testInsertJsonExpressionAndSelect(): void
63+
{
64+
$db = $this->getConnection(true);
65+
$version = $db->getServerVersion();
66+
67+
$command = $db->createCommand();
68+
$command->insert('storage', ['data' => new JsonExpression(['a' => 1, 'b' => 2])])->execute();
69+
70+
if (\stripos($version, 'MariaDb') === false) {
71+
$rowExpected = '{"a": 1, "b": 2}';
72+
} else {
73+
$rowExpected = '{"a":1,"b":2}';
74+
}
75+
76+
$this->assertSame(
77+
$rowExpected,
78+
$command->setSql(
79+
<<<SQL
80+
SELECT `data` FROM `storage`
81+
SQL,
82+
)->queryScalar(),
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)