CheckHelperTest::testCheckLogical()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.069
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function testCheckFieldDirect()
28
    {
29
        $wrapper = $this->prepareWrapper(FALSE);
30
        $this->assertTrue(CheckHelper::check($wrapper, []));
31
        $this->assertTrue(CheckHelper::check($wrapper, ['=','field1', 'value1']));
32
        $this->assertFalse(CheckHelper::check($wrapper, ['=','field1', 'wrongValue']));
33
        $this->assertTrue(CheckHelper::check($wrapper, ['=','notExistField', null]));
34
        $this->assertTrue(CheckHelper::check($wrapper, ['=','arrayObj.count()', 2]));
35
        $this->assertTrue(CheckHelper::check($wrapper, ['>','arrayObj.count()', 1]));
36
    }
37
    
38
    public function testCheckFieldRelated()
39
    {
40
        $wrapper = $this->prepareWrapper(FALSE);
41
        $this->assertTrue(CheckHelper::check($wrapper, ['*=','field1', 'array.field1']));
42
        $this->assertFalse(CheckHelper::check($wrapper, ['*=','field1', 'notExistField']));
43
        $this->assertTrue(CheckHelper::check($wrapper, ['*=','notExistField', 'notExistField2']));
44
        $this->assertTrue(CheckHelper::check($wrapper, ['*=','arrayObj.count()', 'arrayObj.count()']));
45
        $this->assertTrue(CheckHelper::check($wrapper, ['*>=','arrayObj.count()', 'arrayObj.count()']));
46
    }
47
    
48
    public function testCheckLogical()
49
    {
50
        $wrapper = $this->prepareWrapper(FALSE);
51
        $this->assertFalse(CheckHelper::check($wrapper, [
52
            'NOT',
53
            ['*=','field1', 'array.field1']
54
        ]));
55
        $this->assertFalse(CheckHelper::check($wrapper, [
56
            'AND',
57
            ['*=','field1', 'array.field1'],
58
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
59
            ['*=','field1', 'notExistField']
60
        ]));
61
        $this->assertTrue(CheckHelper::check($wrapper, [
62
            'OR',
63
            ['*=','field1', 'array.field1'],
64
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
65
            ['*=','field1', 'notExistField']
66
        ]));
67
        $this->assertFalse(CheckHelper::check($wrapper, [
68
            'OR',
69
            ['*!=','field1', 'array.field1'],
70
            ['*>','arrayObj.count()', 'arrayObj.count()'],
71
            ['*=','field1', 'notExistField']
72
        ]));
73
        $this->assertTrue(CheckHelper::check($wrapper, [
74
            'and',
75
            ['*=','field1', 'array.field1'],
76
            ['*>=','arrayObj.count()', 'arrayObj.count()'],
77
            [
78
                'not',
79
                ['*=','field1', 'notExistField']
80
            ],
81
            [
82
                'or',
83
                ['*=','field1', 'array.field1'],
84
                ['*>=','arrayObj.count()', 'arrayObj.count()'],
85
                ['*=','field1', 'notExistField']
86
            ]
87
        ]));
88
        $this->assertTrue(CheckHelper::check($wrapper, [
89
            '*=field1' => 'array.field1',
90
            '*>=arrayObj.count()' => 'arrayObj.count()',
91
            '*<>field1' => 'notExistField'
92
        ]));
93
        $this->assertTrue(CheckHelper::check($wrapper, [
94
            'field1' => 'value1',
95
            '>=arrayObj.count()' => 1,
96
            '<>field1' => 'notExist'
97
        ]));
98
    }
99
}
100