Passed
Push — main ( f01a67...ddbd64 )
by Peter
02:42
created

InsertTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 103
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddColumnsThrowsAnExceptionIfCountIsWrong() 0 7 1
A getSut() 0 3 1
A testAddValuesThrowsAnExceptionIfCountIsWrong() 0 7 1
A testInsertSimple() 0 11 1
A testGetValues() 0 11 1
A testComplex() 0 16 1
A testToStringThrowsAnExceptionIfNotInitialized() 0 5 1
A testGetParams() 0 13 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 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 (?, ?)';
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', '2345', 'a')
67
            ->addValues('3456', '4567', 'b');
68
69
        $parts   = [];
70
        $parts[] = 'INSERT BAR INTO foo (id, bar_id, baz)';
71
        $parts[] = 'VALUES (?, ?, ?),';
72
        $parts[] = '(?, ?, ?)';
73
74
        $expectedSql = implode(PHP_EOL, $parts);
75
76
        $this->assertSame($expectedSql, $sql);
77
    }
78
79
    public function testGetParams()
80
    {
81
        $expectedParams = [['1234', PDO::PARAM_STR], ['2345', PDO::PARAM_STR]];
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 testGetValues()
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->getValues();
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