Completed
Push — master ( 98eb76...c74b62 )
by Max
01:10
created

CheckHelperTest::testCheckLogical()   A

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 View Code Duplication
    public function testCheckFieldDirect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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