|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Expression; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
9
|
|
|
use Yiisoft\Db\Expression\JsonExpression; |
|
10
|
|
|
use Yiisoft\Db\Query\Query; |
|
11
|
|
|
use Yiisoft\Db\Tests\Support\TestTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @group db |
|
15
|
|
|
*/ |
|
16
|
|
|
final class JsonExpressionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
use TestTrait; |
|
19
|
|
|
|
|
20
|
|
|
public function testConstruct(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$expression = new JsonExpression(['a', 'b', 'c'], 'string'); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertSame(['a', 'b', 'c'], $expression->getValue()); |
|
25
|
|
|
$this->assertSame('string', $expression->getType()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testConstructValueIsJsonExpression(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$expression = new JsonExpression(['a', 'b', 'c'], 'string'); |
|
31
|
|
|
$expression2 = new JsonExpression($expression, 'string'); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertSame(['a', 'b', 'c'], $expression2->getValue()); |
|
34
|
|
|
$this->assertSame('string', $expression2->getType()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testJsonSerialize(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$expression = new JsonExpression(['a', 'b', 'c'], 'string'); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertSame('["a","b","c"]', json_encode($expression->jsonSerialize(), JSON_THROW_ON_ERROR)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testJsonSerializeQueryInterfaceException(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$db = $this->getConnection(); |
|
47
|
|
|
|
|
48
|
|
|
$query = (new Query($db))->select(['a', 'b', 'c']); |
|
49
|
|
|
$expression = new JsonExpression($query, 'string'); |
|
50
|
|
|
|
|
51
|
|
|
$this->expectException(InvalidConfigException::class); |
|
52
|
|
|
$this->expectExceptionMessage( |
|
53
|
|
|
'The JsonExpression class can not be serialized to JSON when the value is a QueryInterface object' |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$expression->jsonSerialize(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|