Passed
Pull Request — master (#380)
by Wilmer
03:09
created

ArrayExpressionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 32
c 2
b 1
f 0
dl 0
loc 77
rs 10
wmc 8

8 Methods

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