Completed
Pull Request — master (#15)
by
unknown
02:18
created

InterpreterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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