Completed
Push — master ( 48c3ae...6eeb89 )
by
unknown
05:00 queued 02:22
created

InterpreterTest::testBuildFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 63
rs 9.4347
cc 1
eloc 44
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
namespace Netdudes\DataSourceryBundle\Tests\UQL;
3
4
use Netdudes\DataSourceryBundle\DataSource\Configuration\Field;
5
use Netdudes\DataSourceryBundle\DataType\NumberDataType;
6
use Netdudes\DataSourceryBundle\DataType\StringDataType;
7
use Netdudes\DataSourceryBundle\Query\Filter;
8
use Netdudes\DataSourceryBundle\Query\FilterCondition;
9
use Netdudes\DataSourceryBundle\Query\FilterConditionFactory;
10
use Netdudes\DataSourceryBundle\UQL\AST\ASTAssertion;
11
use Netdudes\DataSourceryBundle\UQL\AST\ASTGroup;
12
use Netdudes\DataSourceryBundle\UQL\InterpreterFactory;
13
14
class InterpreterTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * Test the filter construction against a typical complex multilevel situation.
18
     */
19
    public function testBuildFilter()
20
    {
21
        $astSubtree = new ASTGroup(
22
            'T_LOGIC_AND',
23
            [
24
                new ASTGroup(
25
                    'T_LOGIC_OR',
26
                    [
27
                        new ASTAssertion(
28
                            'test_dse_1',
29
                            'T_OP_EQ',
30
                            'value1'
31
                        ),
32
                        new ASTAssertion(
33
                            'test_dse_2',
34
                            'T_OP_LT',
35
                            'value2'
36
                        ),
37
                    ]
38
                ),
39
                new ASTAssertion(
40
                    'test_dse_3',
41
                    'T_OP_NEQ',
42
                    'value3'
43
                ),
44
            ]
45
        );
46
47
        $filterDefinition1 = new Filter();
48
        $filterDefinition1->setConditionType('OR');
49
        $filter1 = new FilterCondition('test_dse_1', FilterCondition::METHOD_NUMERIC_EQ, 'value1', 'value1');
50
        $filter2 = new FilterCondition('test_dse_2', FilterCondition::METHOD_NUMERIC_LT, 'value2', 'value2');
51
        $filterDefinition1[] = $filter1;
52
        $filterDefinition1[] = $filter2;
53
        $filter3 = new FilterCondition('test_dse_3', FilterCondition::METHOD_NUMERIC_NEQ, 'value3', 'value3');
54
        $filterDefinition2 = new Filter();
55
        $filterDefinition2->setConditionType('AND');
56
        $filterDefinition2[] = $filterDefinition1;
57
        $filterDefinition2[] = $filter3;
58
59
        $expectedFilter = $filterDefinition2;
60
61
        $dataSourceElements = [
62
            new Field('test_dse_1', '', '', new NumberDataType()),
63
            new Field('test_dse_2', '', '', new NumberDataType()),
64
            new Field('test_dse_3', '', '', new NumberDataType()),
65
        ];
66
67
        $dataSource = $this->getMock('Netdudes\DataSourceryBundle\DataSource\DataSourceInterface');
68
        $dataSource->expects($this->any())
69
            ->method('getFields')
70
            ->will($this->returnValue($dataSourceElements));
71
72
        $extensionContainer = $this->getMockBuilder('Netdudes\DataSourceryBundle\Extension\UqlExtensionContainer')
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        $interpreterFactory = new InterpreterFactory($extensionContainer, new FilterConditionFactory());
77
        $interpreter = $interpreterFactory->create($dataSource);
78
79
        $actualFilter = $interpreter->buildFilter($astSubtree);
80
        $this->assertEquals($expectedFilter, $actualFilter);
81
    }
82
83
    public function testTranslateOperator()
84
    {
85
        // Manually set the data source element, mimic what the data source would do
86
        $dataSourceElement = new Field(
87
            'test_data_source_element_name',
88
            '',
89
            '',
90
            new StringDataType()
91
        );
92
93
        $dataSource = $this->getMock('Netdudes\DataSourceryBundle\DataSource\DataSourceInterface');
94
        $dataSource->expects($this->any())
95
            ->method('getFields')
96
            ->will($this->returnValue([$dataSourceElement]));
97
98
        $extensionContainer = $this->getMockBuilder('Netdudes\DataSourceryBundle\Extension\UqlExtensionContainer')
99
            ->disableOriginalConstructor()
100
            ->getMock();
101
        $interpreterFactory = new InterpreterFactory($extensionContainer, new FilterConditionFactory());
102
        $interpreter = $interpreterFactory->create($dataSource);
103
104
        // LIKE is valid for String type, should return LIKE
105
        $this->assertEquals(FilterCondition::METHOD_STRING_LIKE, $interpreter->translateOperator('T_OP_LIKE', $dataSourceElement), 'Failed to translate T_OP_LIKE into STRING_LIKE for type String');
106
107
        // EQ is valid for String, and should choose STRING_EQ as it's the default for the type
108
        $this->assertEquals(FilterCondition::METHOD_STRING_EQ, $interpreter->translateOperator('T_OP_EQ', $dataSourceElement), 'Failed to translate T_OP_EQ into STRING_EQ for type String');
109
    }
110
}
111