1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @copyright (c) 2020 Mendel <[email protected]> |
4
|
|
|
* @license see license.txt |
5
|
|
|
*/ |
6
|
|
|
namespace drycart\data\tests; |
7
|
|
|
use drycart\data\CheckHelper; |
8
|
|
|
use drycart\data\DataWrapper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author mendel |
12
|
|
|
*/ |
13
|
|
|
class CheckHelperTest extends \PHPUnit\Framework\TestCase |
14
|
|
|
{ |
15
|
|
|
protected function prepareWrapper(bool $safe) : DataWrapper |
16
|
|
|
{ |
17
|
|
|
$data = [ |
18
|
|
|
'field1'=>'value1', |
19
|
|
|
'field2'=>'value2', |
20
|
|
|
'obj'=>(object) ['field1'=>'value1','field2'=>'value2'], |
21
|
|
|
'array'=>['field1'=>'value1','field2'=>'value2'], |
22
|
|
|
'arrayObj'=> new \ArrayObject(['field1'=>'value1','field2'=>'value2']) |
23
|
|
|
]; |
24
|
|
|
return new DataWrapper($data, $safe); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testCheckFieldDirect() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$wrapper = $this->prepareWrapper(FALSE); |
30
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['=','field1', 'value1'])); |
31
|
|
|
$this->assertFalse(CheckHelper::check($wrapper, ['=','field1', 'wrongValue'])); |
32
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['=','notExistField', null])); |
33
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['=','arrayObj.count()', 2])); |
34
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['>','arrayObj.count()', 1])); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testCheckFieldRelated() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$wrapper = $this->prepareWrapper(FALSE); |
40
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['*=','field1', 'array.field1'])); |
41
|
|
|
$this->assertFalse(CheckHelper::check($wrapper, ['*=','field1', 'notExistField'])); |
42
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['*=','notExistField', 'notExistField2'])); |
43
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['*=','arrayObj.count()', 'arrayObj.count()'])); |
44
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, ['*>=','arrayObj.count()', 'arrayObj.count()'])); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCheckLogical() |
48
|
|
|
{ |
49
|
|
|
$wrapper = $this->prepareWrapper(FALSE); |
50
|
|
|
$this->assertFalse(CheckHelper::check($wrapper, [ |
51
|
|
|
'NOT', |
52
|
|
|
['*=','field1', 'array.field1'] |
53
|
|
|
])); |
54
|
|
|
$this->assertFalse(CheckHelper::check($wrapper, [ |
55
|
|
|
'AND', |
56
|
|
|
['*=','field1', 'array.field1'], |
57
|
|
|
['*>=','arrayObj.count()', 'arrayObj.count()'], |
58
|
|
|
['*=','field1', 'notExistField'] |
59
|
|
|
])); |
60
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, [ |
61
|
|
|
'OR', |
62
|
|
|
['*=','field1', 'array.field1'], |
63
|
|
|
['*>=','arrayObj.count()', 'arrayObj.count()'], |
64
|
|
|
['*=','field1', 'notExistField'] |
65
|
|
|
])); |
66
|
|
|
$this->assertFalse(CheckHelper::check($wrapper, [ |
67
|
|
|
'OR', |
68
|
|
|
['*!=','field1', 'array.field1'], |
69
|
|
|
['*>','arrayObj.count()', 'arrayObj.count()'], |
70
|
|
|
['*=','field1', 'notExistField'] |
71
|
|
|
])); |
72
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, [ |
73
|
|
|
'and', |
74
|
|
|
['*=','field1', 'array.field1'], |
75
|
|
|
['*>=','arrayObj.count()', 'arrayObj.count()'], |
76
|
|
|
[ |
77
|
|
|
'not', |
78
|
|
|
['*=','field1', 'notExistField'] |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
'or', |
82
|
|
|
['*=','field1', 'array.field1'], |
83
|
|
|
['*>=','arrayObj.count()', 'arrayObj.count()'], |
84
|
|
|
['*=','field1', 'notExistField'] |
85
|
|
|
] |
86
|
|
|
])); |
87
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, [ |
88
|
|
|
'*=field1' => 'array.field1', |
89
|
|
|
'*>=arrayObj.count()' => 'arrayObj.count()', |
90
|
|
|
'*<>field1' => 'notExistField' |
91
|
|
|
])); |
92
|
|
|
$this->assertTrue(CheckHelper::check($wrapper, [ |
93
|
|
|
'field1' => 'value1', |
94
|
|
|
'>=arrayObj.count()' => 1, |
95
|
|
|
'<>field1' => 'notExist' |
96
|
|
|
])); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.