Passed
Push — master ( 0ce2d3...012822 )
by Alexander
01:00
created

QueryHelperTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 70
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCoalesce() 0 6 1
A testCustomSqlFunction() 0 6 1
A testSingleParam() 0 8 1
A testCaseWithoutElse() 0 11 1
A testCaseWithElse() 0 16 1
A testCaseWithValue() 0 15 1
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
}