1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\Generic\Statement; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use PDO; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use QB\Generic\Expr\Expr; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
class InsertTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @suppress PhanNoopCast |
17
|
|
|
*/ |
18
|
|
|
public function testToStringThrowsAnExceptionIfNotInitialized() |
19
|
|
|
{ |
20
|
|
|
$this->expectException(RuntimeException::class); |
21
|
|
|
|
22
|
|
|
(string)$this->getSut('foo'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @suppress PhanNoopCast |
27
|
|
|
*/ |
28
|
|
|
public function testAddValuesThrowsAnExceptionIfCountIsWrong() |
29
|
|
|
{ |
30
|
|
|
$this->expectException(InvalidArgumentException::class); |
31
|
|
|
|
32
|
|
|
$this->getSut('foo') |
33
|
|
|
->columns('a', 'b') |
34
|
|
|
->values('A'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @suppress PhanNoopCast |
39
|
|
|
*/ |
40
|
|
|
public function testAddColumnsThrowsAnExceptionIfCountIsWrong() |
41
|
|
|
{ |
42
|
|
|
$this->expectException(InvalidArgumentException::class); |
43
|
|
|
|
44
|
|
|
$this->getSut('foo') |
45
|
|
|
->values('A') |
46
|
|
|
->columns('a', 'b'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testInsertSimple() |
50
|
|
|
{ |
51
|
|
|
$sql = (string)$this->getSut('foo')->values('1234', ['2345', '3456'], null); |
52
|
|
|
|
53
|
|
|
$parts = []; |
54
|
|
|
$parts[] = 'INSERT INTO foo'; |
55
|
|
|
$parts[] = 'VALUES (1234, \'["2345","3456"]\', NULL)'; |
56
|
|
|
|
57
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
58
|
|
|
|
59
|
|
|
$this->assertSame($expectedSql, $sql); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testComplex() |
63
|
|
|
{ |
64
|
|
|
$sql = (string)$this->getSut('foo') |
65
|
|
|
->modifier('BAR') |
66
|
|
|
->columns('id', 'bar_id', 'baz') |
67
|
|
|
->values('1234', new Expr('?', ['a']), '"a"') |
68
|
|
|
->values('3456', '4567', '"b"'); |
69
|
|
|
|
70
|
|
|
$parts = []; |
71
|
|
|
$parts[] = 'INSERT BAR INTO foo (id, bar_id, baz)'; |
72
|
|
|
$parts[] = 'VALUES (1234, ?, "a"),'; |
73
|
|
|
$parts[] = '(3456, 4567, "b")'; |
74
|
|
|
|
75
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
76
|
|
|
|
77
|
|
|
$this->assertSame($expectedSql, $sql); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testGetParams() |
81
|
|
|
{ |
82
|
|
|
$expectedParams = [[2345, PDO::PARAM_INT]]; |
83
|
|
|
|
84
|
|
|
$values = ['id' => '1234', 'bar_id' => new Expr('?', [2345])]; |
85
|
|
|
|
86
|
|
|
$query = $this->getSut('foo') |
87
|
|
|
->values(...array_values($values)) |
88
|
|
|
->columns(...array_keys($values)); |
89
|
|
|
|
90
|
|
|
$params = $query->getParams(); |
91
|
|
|
|
92
|
|
|
$this->assertSame($expectedParams, $params); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $table |
97
|
|
|
* |
98
|
|
|
* @return Insert |
99
|
|
|
*/ |
100
|
|
|
protected function getSut(string $table): Insert |
101
|
|
|
{ |
102
|
|
|
return (new Insert())->into($table); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|