Completed
Push — master ( f7f008...2d0f6c )
by
unknown
02:21
created

InterpreterTest::testTranslateOperator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 12
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 $contextFactoryProphecy;
23
24
    /**
25
     * @var UqlExtensionContainer
26
     */
27
    private $extensionContainerProphecy;
28
29
    /**
30
     * Test the filter construction against a typical complex multilevel situation.
31
     */
32
    public function testBuildFilter()
33
    {
34
        $astSubtree = new ASTGroup(
35
            'T_LOGIC_AND',
36
            [
37
                new ASTGroup(
38
                    'T_LOGIC_OR',
39
                    [
40
                        new ASTAssertion(
41
                            'test_dse_1',
42
                            'T_OP_EQ',
43
                            'value1'
44
                        ),
45
                        new ASTAssertion(
46
                            'test_dse_2',
47
                            'T_OP_LT',
48
                            'value2'
49
                        ),
50
                    ]
51
                ),
52
                new ASTAssertion(
53
                    'test_dse_3',
54
                    'T_OP_NEQ',
55
                    'value3'
56
                ),
57
            ]
58
        );
59
60
        $filterDefinition1 = new Filter();
61
        $filterDefinition1->setConditionType('OR');
62
        $filter1 = new FilterCondition('test_dse_1', FilterCondition::METHOD_NUMERIC_EQ, 'value1', 'value1');
63
        $filter2 = new FilterCondition('test_dse_2', FilterCondition::METHOD_NUMERIC_LT, 'value2', 'value2');
64
        $filterDefinition1[] = $filter1;
65
        $filterDefinition1[] = $filter2;
66
        $filter3 = new FilterCondition('test_dse_3', FilterCondition::METHOD_NUMERIC_NEQ, 'value3', 'value3');
67
        $filterDefinition2 = new Filter();
68
        $filterDefinition2->setConditionType('AND');
69
        $filterDefinition2[] = $filterDefinition1;
70
        $filterDefinition2[] = $filter3;
71
72
        $expectedFilter = $filterDefinition2;
73
74
        $dataSourceElements = [
75
            new Field('test_dse_1', '', '', new NumberDataType()),
76
            new Field('test_dse_2', '', '', new NumberDataType()),
77
            new Field('test_dse_3', '', '', new NumberDataType()),
78
        ];
79
80
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
81
        $dataSourceProphecy->getFields()->willReturn($dataSourceElements);
82
83
        $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Netdudes\DataSour...\UqlExtensionContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method reveal() does not seem to exist on object<Netdudes\DataSour...tension\ContextFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
85
86
        $actualFilter = $interpreter->buildFilter($astSubtree);
87
        $this->assertEquals($expectedFilter, $actualFilter);
88
    }
89
90
    public function testTranslateOperator()
91
    {
92
        // Manually set the data source element, mimic what the data source would do
93
        $dataSourceElement = new Field(
94
            'test_data_source_element_name',
95
            '',
96
            '',
97
            new StringDataType()
98
        );
99
100
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
101
        $dataSourceProphecy->getFields()->willReturn([$dataSourceElement]);
102
103
        $interpreterFactory = new InterpreterFactory($this->extensionContainerProphecy->reveal(), new FilterConditionFactory(), $this->contextFactoryProphecy->reveal());
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<Netdudes\DataSour...\UqlExtensionContainer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method reveal() does not seem to exist on object<Netdudes\DataSour...tension\ContextFactory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
105
106
        // LIKE is valid for String type, should return LIKE
107
        $this->assertEquals(FilterCondition::METHOD_STRING_LIKE, $interpreter->translateOperator('T_OP_LIKE', $dataSourceElement), 'Failed to translate T_OP_LIKE into STRING_LIKE for type String');
108
109
        // EQ is valid for String, and should choose STRING_EQ as it's the default for the type
110
        $this->assertEquals(FilterCondition::METHOD_STRING_EQ, $interpreter->translateOperator('T_OP_EQ', $dataSourceElement), 'Failed to translate T_OP_EQ into STRING_EQ for type String');
111
    }
112
113
    protected function setUp()
114
    {
115
        $this->extensionContainerProphecy = $this->prophesize(UqlExtensionContainer::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Netdu...ensionContainer::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Netdudes\DataSour...\UqlExtensionContainer> of property $extensionContainerProphecy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
        $this->contextFactoryProphecy = $this->prophesize(ContextFactory::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->prophesize(\Netdu...\ContextFactory::class) of type object<Prophecy\Prophecy\ObjectProphecy> is incompatible with the declared type object<Netdudes\DataSour...tension\ContextFactory> of property $contextFactoryProphecy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
117
    }
118
}
119