Passed
Push — main ( 77200e...ac11d9 )
by Peter
02:38
created

InsertTest::testValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Statement;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\TestCase;
9
use QB\Generic\Expr\Expr;
10
use RuntimeException;
11
12
class InsertTest extends TestCase
13
{
14
    /**
15
     * @suppress PhanNoopCast
16
     */
17
    public function testToStringThrowsAnExceptionIfNotInitialized()
18
    {
19
        $this->expectException(RuntimeException::class);
20
21
        (string)$this->getSut('foo');
22
    }
23
24
    /**
25
     * @suppress PhanNoopCast
26
     */
27
    public function testAddValuesThrowsAnExceptionIfCountIsWrong()
28
    {
29
        $this->expectException(InvalidArgumentException::class);
30
31
        $this->getSut('foo')
32
            ->setColumns('a', 'b')
33
            ->addValues('A');
34
    }
35
36
    /**
37
     * @suppress PhanNoopCast
38
     */
39
    public function testAddColumnsThrowsAnExceptionIfCountIsWrong()
40
    {
41
        $this->expectException(InvalidArgumentException::class);
42
43
        $this->getSut('foo')
44
            ->addValues('A')
45
            ->setColumns('a', 'b');
46
    }
47
48
    public function testInsertSimple()
49
    {
50
        $sql = (string)$this->getSut('foo')->addValues('1234', '2345');
51
52
        $parts   = [];
53
        $parts[] = 'INSERT INTO foo';
54
        $parts[] = 'VALUES (1234, 2345)';
55
56
        $expectedSql = implode(PHP_EOL, $parts);
57
58
        $this->assertSame($expectedSql, $sql);
59
    }
60
61
    public function testComplex()
62
    {
63
        $sql = (string)$this->getSut('foo')
64
            ->addModifier('BAR')
65
            ->setColumns('id', 'bar_id', 'baz')
66
            ->addValues('1234', new Expr('?', ['a']), '"a"')
67
            ->addValues('3456', '4567', '"b"');
68
69
        $parts   = [];
70
        $parts[] = 'INSERT BAR INTO foo (id, bar_id, baz)';
71
        $parts[] = 'VALUES (1234, ?, "a"),';
72
        $parts[] = '(3456, 4567, "b")';
73
74
        $expectedSql = implode(PHP_EOL, $parts);
75
76
        $this->assertSame($expectedSql, $sql);
77
    }
78
79
    public function testGetParams()
80
    {
81
        $expectedParams = [];
82
83
        $values = ['id' => '1234', 'bar_id' => '2345'];
84
85
        $query = $this->getSut('foo')
86
            ->addValues(...array_values($values))
87
            ->setColumns(...array_keys($values));
88
89
        $params = $query->getParams();
90
91
        $this->assertSame($expectedParams, $params);
92
    }
93
94
    public function testValues()
95
    {
96
        $values = ['id' => '1234', 'bar_id' => '2345'];
97
98
        $query = $this->getSut('foo')
99
            ->addValues(...array_values($values))
100
            ->setColumns(...array_keys($values));
101
102
        $actualValues = $query->values();
103
104
        $this->assertSame(array_values($values), $actualValues);
105
    }
106
107
    /**
108
     * @param string $table
109
     *
110
     * @return Insert
111
     */
112
    protected function getSut(string $table): Insert
113
    {
114
        return (new Insert())->setInto($table);
115
    }
116
}
117