InsertTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 90
rs 10
wmc 7

7 Methods

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