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
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
class DeleteTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @suppress PhanNoopCast |
16
|
|
|
*/ |
17
|
|
|
public function testToStringThrowsAnExceptionIfNotInitialized() |
18
|
|
|
{ |
19
|
|
|
$this->expectException(RuntimeException::class); |
20
|
|
|
|
21
|
|
|
(string)$this->getSut(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testToStringSimple() |
25
|
|
|
{ |
26
|
|
|
$sql = (string)$this->getSut('foo'); |
27
|
|
|
|
28
|
|
|
$parts = []; |
29
|
|
|
$parts[] = 'DELETE FROM foo'; |
30
|
|
|
|
31
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
32
|
|
|
|
33
|
|
|
$this->assertSame($expectedSql, $sql); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testToStringComplex() |
37
|
|
|
{ |
38
|
|
|
$sql = (string)$this->getSut('foo') |
39
|
|
|
->where('foo.bar = "foo-bar"', 'bar.foo = 17'); |
40
|
|
|
|
41
|
|
|
$parts = []; |
42
|
|
|
$parts[] = 'DELETE FROM foo'; |
43
|
|
|
$parts[] = 'WHERE foo.bar = "foo-bar" AND bar.foo = 17'; |
44
|
|
|
|
45
|
|
|
$expectedSql = implode(PHP_EOL, $parts); |
46
|
|
|
|
47
|
|
|
$this->assertSame($expectedSql, $sql); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetParams() |
51
|
|
|
{ |
52
|
|
|
$expectedParams = [['bar-foo', PDO::PARAM_STR]]; |
53
|
|
|
$query = $this->getSut('foo') |
54
|
|
|
->where('foo.bar = "foo-bar"', new Expr('bar.foo = ?', ['bar-foo'])); |
55
|
|
|
|
56
|
|
|
$params = $query->getParams(); |
57
|
|
|
|
58
|
|
|
$this->assertSame($expectedParams, $params); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string ...$tables |
63
|
|
|
* |
64
|
|
|
* @return IDelete |
65
|
|
|
*/ |
66
|
|
|
protected function getSut(string ...$tables): IDelete |
67
|
|
|
{ |
68
|
|
|
return (new Delete())->from(...$tables); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|