Passed
Branch add-more-test-schema (1ebac6)
by Wilmer
02:54
created

ArrayExpressionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 8
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\ArrayExpression;
10
11
/**
12
 * @group db
13
 *
14
 * @psalm-suppress PropertyNotSetInConstructor
15
 */
16
final class ArrayExpressionTest extends TestCase
17
{
18
    public function testConstruct(): void
19
    {
20
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
21
22
        $this->assertSame(['a', 'b', 'c'], $expression->getValue());
23
        $this->assertSame('string', $expression->getType());
24
        $this->assertSame(1, $expression->getDimension());
25
    }
26
27
    public function testOffsetExists(): void
28
    {
29
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
30
31
        $this->assertTrue(isset($expression[0]));
32
        $this->assertFalse(isset($expression[3]));
33
    }
34
35
    public function testOffsetGet(): void
36
    {
37
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
38
39
        $this->assertSame('a', $expression[0]);
40
        $this->assertSame('b', $expression[1]);
41
        $this->assertSame('c', $expression[2]);
42
    }
43
44
    public function testOffsetSet(): void
45
    {
46
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
47
48
        $expression[0] = 'd';
49
        $expression[1] = 'e';
50
        $expression[2] = 'f';
51
52
        $this->assertSame('d', $expression[0]);
53
        $this->assertSame('e', $expression[1]);
54
        $this->assertSame('f', $expression[2]);
55
    }
56
57
    public function testOffsetUnset(): void
58
    {
59
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
60
61
        unset($expression[0], $expression[1], $expression[2]);
62
63
64
65
        $this->assertFalse(isset($expression[0]));
66
        $this->assertFalse(isset($expression[1]));
67
        $this->assertFalse(isset($expression[2]));
68
    }
69
70
    public function testCount(): void
71
    {
72
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
73
74
        $this->assertCount(3, $expression);
75
    }
76
77
    public function testGetIterator(): void
78
    {
79
        $expression = new ArrayExpression(['a', 'b', 'c'], 'string', 1);
80
81
82
        $this->assertSame(['a', 'b', 'c'], iterator_to_array($expression->getIterator()));
83
    }
84
85
    public function testGetIteratorException(): void
86
    {
87
        $this->expectException(InvalidConfigException::class);
88
        $this->expectExceptionMessage('The ArrayExpression value must be an array.');
89
90
        $expression = new ArrayExpression('c', 'string', 2);
91
92
        $expression->getIterator();
93
    }
94
}
95