Passed
Push — master ( e42f4d...053e33 )
by Wilmer
07:04 queued 04:48
created

JsonExpressionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 4
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
use function json_encode;
14
15
/**
16
 * @group db
17
 *
18
 * @psalm-suppress PropertyNotSetInConstructor
19
 */
20
final class JsonExpressionTest extends TestCase
21
{
22
    use TestTrait;
23
24
    public function testConstruct(): void
25
    {
26
        $expression = new JsonExpression(['a', 'b', 'c'], 'string');
27
28
        $this->assertSame(['a', 'b', 'c'], $expression->getValue());
29
        $this->assertSame('string', $expression->getType());
30
    }
31
32
    public function testConstructValueIsJsonExpression(): void
33
    {
34
        $expression = new JsonExpression(['a', 'b', 'c'], 'string');
35
        $expression2 = new JsonExpression($expression, 'string');
36
37
        $this->assertSame(['a', 'b', 'c'], $expression2->getValue());
38
        $this->assertSame('string', $expression2->getType());
39
    }
40
41
    public function testJsonSerialize(): void
42
    {
43
        $expression = new JsonExpression(['a', 'b', 'c'], 'string');
44
45
        $this->assertSame('["a","b","c"]', json_encode($expression->jsonSerialize(), JSON_THROW_ON_ERROR));
46
    }
47
48
    public function testJsonSerializeQueryInterfaceException(): void
49
    {
50
        $db = $this->getConnection();
51
52
        $query = (new Query($db))->select(['a', 'b', 'c']);
53
        $expression = new JsonExpression($query, 'string');
54
55
        $this->expectException(InvalidConfigException::class);
56
        $this->expectExceptionMessage(
57
            'The JsonExpression class can not be serialized to JSON when the value is a QueryInterface object'
58
        );
59
60
        $expression->jsonSerialize();
61
    }
62
}
63