UpdateTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testToStringThrowsAnExceptionIfNotInitialized() 0 5 1
A testToStringSimple() 0 14 1
A testGetParams() 0 11 1
A getSut() 0 3 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
            ->values(['id' => '1234', 'bar_id' => '2345'])
27
            ->where('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
28
29
        $parts   = [];
30
        $parts[] = 'UPDATE foo';
31
        $parts[] = 'SET id = 1234, bar_id = 2345';
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
            ->modifier('BAR')
43
            ->values(['id' => '1234', 'bar_id' => new Expr('?', [2345]), 'baz' => ['a', 'b'], 'quix' => null])
44
            ->where('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
45
46
        $parts   = [];
47
        $parts[] = 'UPDATE BAR foo';
48
        $parts[] = "SET id = 1234, bar_id = ?, baz = '[\"a\",\"b\"]', quix = NULL";
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 = [[2345, PDO::PARAM_INT], ['bar-foo', PDO::PARAM_STR]];
59
60
        $query = $this->getSut('foo')
61
            ->values(['id' => '1234', 'bar_id' => new Expr('?', [2345])])
62
            ->where('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo']));
63
64
        $params = $query->getParams();
65
66
        $this->assertSame($expectedParams, $params);
67
    }
68
69
    /**
70
     * @param string ...$tables
71
     *
72
     * @return IUpdate
73
     */
74
    protected function getSut(string ...$tables): IUpdate
75
    {
76
        return new Update(...$tables);
77
    }
78
}
79