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