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

UpdateTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testToStringThrowsAnExceptionIfNotInitialized() 0 5 1
A getSut() 0 3 1
A testGetParams() 0 9 1
A testToStringSimple() 0 14 1
A testToStringComplex() 0 15 1
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