1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Horat1us\Yii\Tests\Helpers; |
4
|
|
|
|
5
|
|
|
use Horat1us\Yii\Helpers\QueryHelper; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use yii\db\Expression; |
8
|
|
|
|
9
|
|
|
class QueryHelperTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testCoalesce() |
12
|
|
|
{ |
13
|
|
|
$expression = QueryHelper::coalesce('a', 'b'); |
14
|
|
|
|
15
|
|
|
$this->assertEquals('COALESCE((a), (b))', $expression); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testCustomSqlFunction() |
19
|
|
|
{ |
20
|
|
|
$expression = QueryHelper::sqlCall('MIN', 1, 2, new Expression('3')); |
21
|
|
|
|
22
|
|
|
$this->assertEquals('MIN((1), (2), (3))', $expression); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testSingleParam() |
26
|
|
|
{ |
27
|
|
|
$command = 'SELECT CURRENT_DATE'; |
28
|
|
|
$function = 'EXISTS'; |
29
|
|
|
$expression = QueryHelper::sqlCall($function, $command); |
30
|
|
|
|
31
|
|
|
$this->assertEquals("{$function}(({$command}))", $expression); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testCaseWithoutElse() |
35
|
|
|
{ |
36
|
|
|
$condition = 'date = 01.01.1970'; |
37
|
|
|
$statement = '1234'; |
38
|
|
|
|
39
|
|
|
$expression = QueryHelper::caseWhen([ |
40
|
|
|
$condition => $statement, |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->assertEquals("CASE WHEN {$condition} THEN {$statement} END", $expression); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testCaseWithElse() |
47
|
|
|
{ |
48
|
|
|
$condition = 'true = false'; |
49
|
|
|
$statement = '1234'; |
50
|
|
|
$else = '4321'; |
51
|
|
|
|
52
|
|
|
$expression = QueryHelper::caseWhen( |
53
|
|
|
[ |
54
|
|
|
$condition => $statement, |
55
|
|
|
], |
56
|
|
|
'', |
57
|
|
|
$else |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$this->assertEquals("CASE WHEN {$condition} THEN {$statement} ELSE {$else} END", $expression); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testCaseWithValue() |
64
|
|
|
{ |
65
|
|
|
$value = 'variable'; |
66
|
|
|
$condition = "{$value} > 0"; |
67
|
|
|
$statement = '1'; |
68
|
|
|
|
69
|
|
|
$expression = QueryHelper::caseWhen( |
70
|
|
|
[ |
71
|
|
|
$condition => $statement |
72
|
|
|
], |
73
|
|
|
$value |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->assertEquals("CASE {$value} WHEN {$condition} THEN {$statement} END", $expression); |
77
|
|
|
} |
78
|
|
|
} |