1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace QB\PostgreSQL\Clause; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use QB\Generic\IQueryPart; |
9
|
|
|
use QB\PostgreSQL\Statement\Select; |
10
|
|
|
|
11
|
|
|
class CombiningQueryTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
protected IQueryPart $queryPart; |
14
|
|
|
|
15
|
|
|
public function setUp(): void |
16
|
|
|
{ |
17
|
|
|
$this->queryPart = (new Select())->columns('2'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return array[] |
22
|
|
|
*/ |
23
|
|
|
public function toStringProvider(): array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
[CombiningQuery::TYPE_UNION, null, "UNION\nSELECT 2"], |
27
|
|
|
[CombiningQuery::TYPE_UNION, CombiningQuery::MODIFIER_ALL, "UNION ALL\nSELECT 2"], |
28
|
|
|
[CombiningQuery::TYPE_EXCEPT, null, "EXCEPT\nSELECT 2"], |
29
|
|
|
[CombiningQuery::TYPE_EXCEPT, CombiningQuery::MODIFIER_ALL, "EXCEPT ALL\nSELECT 2"], |
30
|
|
|
[CombiningQuery::TYPE_INTERSECT, null, "INTERSECT\nSELECT 2"], |
31
|
|
|
[CombiningQuery::TYPE_INTERSECT, CombiningQuery::MODIFIER_ALL, "INTERSECT ALL\nSELECT 2"], |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider toStringProvider |
37
|
|
|
* |
38
|
|
|
* @param string $type |
39
|
|
|
* @param string|null $modifier |
40
|
|
|
* @param string $expectedSql |
41
|
|
|
*/ |
42
|
|
|
public function testToString(string $type, ?string $modifier, string $expectedSql) |
43
|
|
|
{ |
44
|
|
|
$lock = new CombiningQuery($type, $this->queryPart, $modifier); |
45
|
|
|
$sql = (string)$lock; |
46
|
|
|
|
47
|
|
|
$this->assertSame($expectedSql, $sql); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array[] |
52
|
|
|
*/ |
53
|
|
|
public function constructValidationProvider(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'invalid type' => ['foo', null], |
57
|
|
|
'invalid modifier' => [CombiningQuery::TYPE_UNION, 'foo'], |
58
|
|
|
'invalid mysql modifier' => [CombiningQuery::TYPE_UNION, CombiningQuery::MODIFIER_DISTINCT], |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @dataProvider constructValidationProvider |
64
|
|
|
* @suppress PhanNoopNew |
65
|
|
|
* |
66
|
|
|
* @param string $type |
67
|
|
|
* @param string|null $modifier |
68
|
|
|
*/ |
69
|
|
|
public function testConstructValidation(string $type, ?string $modifier) |
70
|
|
|
{ |
71
|
|
|
$this->expectException(\InvalidArgumentException::class); |
72
|
|
|
|
73
|
|
|
new CombiningQuery($type, $this->queryPart, $modifier); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|