Passed
Push — main ( d5eb66...436d8c )
by Peter
02:54
created

UpdateTest::testGetValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Statement;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use QB\Generic\Expr\Expr;
10
11
class UpdateTest extends TestCase
12
{
13
    /**
14
     * @suppress PhanNoopCast
15
     */
16
    public function testToStringThrowsAnExceptionIfNotInitialized()
17
    {
18
        $this->expectException(\RuntimeException::class);
19
20
        (string)$this->getSut();
21
    }
22
23
    public function testToStringSimple()
24
    {
25
        $sql = (string)$this->getSut('foo')
26
            ->setValues(['id' => '1234', 'bar_id' => '2345'])
27
            ->addWhere('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
28
29
        $parts   = [];
30
        $parts[] = 'UPDATE foo';
31
        $parts[] = 'SET id = ?, bar_id = ?';
32
        $parts[] = 'WHERE foo.bar = "foo-bar" AND bar.foo = ?';
33
34
        $expectedSql = implode(PHP_EOL, $parts);
35
36
        $this->assertSame($expectedSql, $sql);
37
    }
38
39
    public function testToStringComplex()
40
    {
41
        $sql = (string)$this->getSut('foo')
42
            ->addModifier('BAR')
43
            ->setValues(['id' => '1234', 'bar_id' => '2345'])
44
            ->addWhere('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
45
46
        $parts   = [];
47
        $parts[] = 'UPDATE BAR foo';
48
        $parts[] = 'SET id = ?, bar_id = ?';
49
        $parts[] = 'WHERE foo.bar = "foo-bar" AND bar.foo = ?';
50
51
        $expectedSql = implode(PHP_EOL, $parts);
52
53
        $this->assertSame($expectedSql, $sql);
54
    }
55
56
    public function testGetParams()
57
    {
58
        $expectedParams = [['bar-foo', PDO::PARAM_STR]];
59
60
        $query = $this->getSut('foo')
61
            ->setValues(['id' => '1234', 'bar_id' => '2345'])
62
            ->addWhere('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
63
64
        $params = $query->getParams();
65
66
        $this->assertSame($expectedParams, $params);
67
    }
68
69
    public function testGetValues()
70
    {
71
        $values = ['id' => '1234', 'bar_id' => '2345'];
72
73
        $expectedValues = array_values($values);
74
75
        $query = $this->getSut('foo')
76
            ->setValues($values)
77
            ->addWhere('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
78
79
        $actualValues = $query->getValues();
80
81
        $this->assertSame($expectedValues, $actualValues);
82
    }
83
84
    /**
85
     * @param string ...$tables
86
     *
87
     * @return IUpdate
88
     */
89
    protected function getSut(string ...$tables): IUpdate
90
    {
91
        return (new Update())->addFrom(...$tables);
92
    }
93
}
94