Passed
Push — main ( 4b03c8...85c7ac )
by Peter
02:31
created

InsertTest::testComplex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\PostgreSQL\Statement;
6
7
use QB\Generic\Clause\Table;
8
use QB\Generic\Statement\InsertTest as GenericInsertTest;
9
10
class InsertTest extends GenericInsertTest
11
{
12
    public function testWitDefaultValues()
13
    {
14
        $sql = (string)$this->getSut('foo');
15
16
        $parts   = [];
17
        $parts[] = 'INSERT INTO foo';
18
        $parts[] = 'DEFAULT VALUES';
19
20
        $expectedSql = implode(PHP_EOL, $parts);
21
22
        $this->assertSame($expectedSql, $sql);
23
    }
24
25
    public function testComplex()
26
    {
27
        $sql = (string)$this->getSut('foo')
28
            ->addColumns('id', 'bar_id', 'baz')
29
            ->addValues('1234', '2345', 'a')
30
            ->addValues('3456', '4567', 'b');
31
32
        $parts   = [];
33
        $parts[] = 'INSERT INTO foo (id, bar_id, baz)';
34
        $parts[] = 'VALUES (?, ?, ?),';
35
        $parts[] = '(?, ?, ?)';
36
37
        $expectedSql = implode(PHP_EOL, $parts);
38
39
        $this->assertSame($expectedSql, $sql);
40
    }
41
42
    public function testAddMultipleRows()
43
    {
44
        $query = $this->getSut('offices')
45
            ->setInto(new Table('offices'))
46
            ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory')
47
            ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA')
48
            ->addValues('bcd', 'Budapest', '+36 70 101 1234', '', 'Hungary', '1011', 'NA')
49
            ->addValues('cde', 'Pécs', '+36 70 222 3456', 'Rákóczi út', 'Hungary', '723', 'NA')
50
            ->setReturning('*');
51
52
        $parts   = [];
53
        $parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)';
54
        $parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?),';
55
        $parts[] = '(?, ?, ?, ?, ?, ?, ?),';
56
        $parts[] = '(?, ?, ?, ?, ?, ?, ?)';
57
        $parts[] = 'RETURNING *';
58
59
        $expectedSql = implode(PHP_EOL, $parts);
60
61
        $this->assertSame($expectedSql, (string)$query);
62
    }
63
64
    public function testAddOnConflictDoNothing()
65
    {
66
        $query = $this->getSut('offices')
67
            ->setInto(new Table('offices'))
68
            ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory')
69
            ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA')
70
            ->setDoNothing();
71
72
        $parts   = [];
73
        $parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)';
74
        $parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)';
75
        $parts[] = 'ON CONFLICT DO NOTHING';
76
77
        $expectedSql = implode(PHP_EOL, $parts);
78
79
        $this->assertSame($expectedSql, (string)$query);
80
    }
81
82
    public function testAddOnConflictDoUpdate()
83
    {
84
        $query = $this->getSut('offices')
85
            ->setInto(new Table('offices'))
86
            ->addColumns('officeCode', 'city', 'phone', 'addressLine1', 'country', 'postalCode', 'territory')
87
            ->addValues('abc', 'Berlin', '+49 101 123 4567', '', 'Germany', '10111', 'NA')
88
            ->setOnConflict('officeCode', 'city')
89
            ->setDoUpdate('officeCode = EXCLUDED.officeCode', 'city = EXCLUDED.city')
90
            ->setReturning('*');
91
92
        $parts   = [];
93
        $parts[] = 'INSERT INTO offices (officeCode, city, phone, addressLine1, country, postalCode, territory)';
94
        $parts[] = 'VALUES (?, ?, ?, ?, ?, ?, ?)';
95
        $parts[] = 'ON CONFLICT (officeCode, city) DO UPDATE';
96
        $parts[] = 'SET officeCode = EXCLUDED.officeCode, city = EXCLUDED.city';
97
        $parts[] = 'RETURNING *';
98
99
        $expectedSql = implode(PHP_EOL, $parts);
100
101
        $this->assertSame($expectedSql, (string)$query);
102
    }
103
104
    /**
105
     * @param string $table
106
     *
107
     * @return Insert
108
     */
109
    protected function getSut(string $table): Insert
110
    {
111
        return (new Insert())->setInto($table);
112
    }
113
}
114