Passed
Push — main ( 225a1c...d5eb66 )
by Peter
02:35
created

UpdateTest::testGetParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
        $query = $this->getSut('foo')
60
            ->addWhere('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
61
62
        $params = $query->getParams();
63
64
        $this->assertSame($expectedParams, $params);
65
    }
66
67
    /**
68
     * @param string ...$tables
69
     *
70
     * @return IUpdate
71
     */
72
    protected function getSut(string ...$tables): IUpdate
73
    {
74
        return (new Update())->addFrom(...$tables);
75
    }
76
}
77