TranslatorTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 113
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B rulesProvider() 0 92 1
A testRules() 0 15 3
1
<?php
2
3
4
namespace leandrogehlen\querybuilder\tests\unit;
5
6
use leandrogehlen\querybuilder\Translator;
7
8
class TranslatorTest extends TestCase
9
{
10
11
    protected function rulesProvider()
12
    {
13
        return [
14
            [
15
                ['condition' => "and", 'rules' => [
16
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'equal', 'value' => 'joe'],
17
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'not_equal', 'value' => 'bruce']
18
                ]],
19
                ['(name = :p0 and name <> :p1)', [':p0' => 'joe', ':p1' => 'bruce']]
20
            ],
21
            [
22
                ['condition' => "and", 'rules' => [
23
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'in', 'value' => [1,2,3]],
24
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'not_in', 'value' => [4,5]]
25
                ]],
26
                ['(id IN (:p0, :p1, :p2) and id NOT IN (:p3, :p4))', [':p0'=>1, ':p1'=>2, ':p2'=>3, ':p3'=>4, ':p4'=>5]]
27
            ],
28
            [
29
                ['condition' => "and", 'rules' => [
30
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'less', 'value' => 100],
31
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'less_or_equal', 'value' => 50],
32
                ]],
33
                ['(id < :p0 and id <= :p1)', [':p0'=>100, ':p1'=>50]]
34
            ],
35
            [
36
                ['condition' => "and", 'rules' => [
37
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'greater', 'value' => 10],
38
                    [ 'field' => 'id', 'type' => 'integer', 'operator' => 'greater_or_equal', 'value' => 20],
39
                ]],
40
                ['(id > :p0 and id >= :p1)', [':p0'=>10, ':p1'=>20]]
41
            ],
42
            [
43
                ['condition' => "and", 'rules' => [
44
                    [ 'field' => 'date', 'type' => 'date', 'operator' => 'between', 'value' => ['2015-01-01','2015-01-30']],
45
                ]],
46
                ['(date BETWEEN :p0 AND :p1)', [':p0'=>'2015-01-01', ':p1'=>'2015-01-30']]
47
            ],
48
            [
49
                ['condition' => "and", 'rules' => [
50
                    [ 'field' => 'date', 'type' => 'date', 'operator' => 'not_between', 'value' => ['2015-01-01','2015-01-30']],
51
                ]],
52
                ['(date NOT BETWEEN :p0 AND :p1)', [':p0'=>'2015-01-01', ':p1'=>'2015-01-30']]
53
            ],
54
            [
55
                ['condition' => "and", 'rules' => [
56
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'begins_with', 'value' => 'joe'],
57
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'not_begins_with', 'value' => 'bruce'],
58
                ]],
59
                ['(name LIKE :p0 and name NOT LIKE :p1)', [':p0'=>'joe%', ':p1'=> 'bruce%']]
60
            ],
61
            [
62
                ['condition' => "and", 'rules' => [
63
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'contains', 'value' => 'thomas'],
64
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'not_contains', 'value' => 'paul'],
65
                ]],
66
                ['(name LIKE :p0 and name NOT LIKE :p1)', [':p0'=>'%thomas%', ':p1'=> '%paul%']]
67
            ],
68
            [
69
                ['condition' => "and", 'rules' => [
70
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'ends_with', 'value' => 'brian'],
71
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'not_ends_with', 'value' => 'david'],
72
                ]],
73
                ['(name LIKE :p0 and name NOT LIKE :p1)', [':p0'=>'%brian', ':p1'=> '%david']]
74
            ],
75
            [
76
                ['condition' => "or", 'rules' => [
77
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'is_empty'],
78
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'is_not_empty'],
79
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'is_null'],
80
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'is_not_null'],
81
                ]],
82
                ['(name = "" or name <> "" or name IS NULL or name IS NOT NULL)', []]
83
            ],
84
            [
85
                ['condition' => "and", 'rules' => [
86
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'begins_with', 'value' => 'kurt'],
87
                    [ 'field' => 'name', 'type' => 'string', 'operator' => 'ends_with', 'value' => 'cobain'],
88
                    ['condition' => 'or', 'rules'=>[
89
                        [ 'field' => 'name', 'type' => 'string', 'operator' => 'equal', 'value' => 'joe'],
90
                        [ 'field' => 'name', 'type' => 'string', 'operator' => 'equal', 'value' => 'paul'],
91
                        ['condition' => 'and', 'rules'=>[
92
                            [ 'field' => 'id', 'type' => 'integer', 'operator' => 'equal', 'value' => 10],
93
                        ]]
94
                    ]]
95
                ]],
96
                ['(name LIKE :p0 and name LIKE :p1 and (name = :p2 or name = :p3 or (id = :p4)))', [
97
                    ':p0'=>'kurt%',':p1' =>'%cobain', ':p2' => 'joe', ':p3' => 'paul', ':p4' => 10
98
                ]]
99
            ]
100
101
        ];
102
    }
103
104
105
    public function testRules()
106
    {
107
        foreach ($this->rulesProvider() as $rule) {
108
            $translator = new Translator($rule[0]);
109
            $expected = $rule[1];
110
111
            $this->assertEquals($expected[0], $translator->where());
112
113
            $params = $translator->params();
114
            foreach ($expected[1] as $key => $value) {
115
                $this->assertArrayHasKey($key, $params);
116
                $this->assertEquals($value, $params[$key]);
117
            }
118
        }
119
    }
120
}
121