Completed
Push — master ( d5c275...27397e )
by
unknown
15s
created

testNullValueCanBeCheckedOnlyWithIsOperator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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\Exception\UQLInterpreterException;
16
use Netdudes\DataSourceryBundle\UQL\InterpreterFactory;
17
18
class InterpreterTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var ContextFactory
22
     */
23
    private $contextFactoryProphecy;
24
25
    /**
26
     * @var UqlExtensionContainer
27
     */
28
    private $extensionContainerProphecy;
29
30
    /**
31
     * Test the filter construction against a typical complex multilevel situation.
32
     */
33
    public function testBuildFilter()
34
    {
35
        $astSubtree = new ASTGroup(
36
            'T_LOGIC_AND',
37
            [
38
                new ASTGroup(
39
                    'T_LOGIC_OR',
40
                    [
41
                        new ASTAssertion(
42
                            'test_dse_1',
43
                            'T_OP_EQ',
44
                            'value1'
45
                        ),
46
                        new ASTAssertion(
47
                            'test_dse_2',
48
                            'T_OP_LT',
49
                            'value2'
50
                        ),
51
                    ]
52
                ),
53
                new ASTAssertion(
54
                    'test_dse_3',
55
                    'T_OP_NEQ',
56
                    'value3'
57
                ),
58
            ]
59
        );
60
61
        $filterDefinition1 = new Filter();
62
        $filterDefinition1->setConditionType('OR');
63
        $filter1 = new FilterCondition('test_dse_1', FilterCondition::METHOD_NUMERIC_EQ, 'value1', 'value1');
64
        $filter2 = new FilterCondition('test_dse_2', FilterCondition::METHOD_NUMERIC_LT, 'value2', 'value2');
65
        $filterDefinition1[] = $filter1;
66
        $filterDefinition1[] = $filter2;
67
        $filter3 = new FilterCondition('test_dse_3', FilterCondition::METHOD_NUMERIC_NEQ, 'value3', 'value3');
68
        $filterDefinition2 = new Filter();
69
        $filterDefinition2->setConditionType('AND');
70
        $filterDefinition2[] = $filterDefinition1;
71
        $filterDefinition2[] = $filter3;
72
73
        $expectedFilter = $filterDefinition2;
74
75
        $dataSourceElements = [
76
            new Field('test_dse_1', '', '', new NumberDataType()),
77
            new Field('test_dse_2', '', '', new NumberDataType()),
78
            new Field('test_dse_3', '', '', new NumberDataType()),
79
        ];
80
81
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
82
        $dataSourceProphecy->getFields()->willReturn($dataSourceElements);
83
84
        $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...
85
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
86
87
        $actualFilter = $interpreter->buildFilter($astSubtree);
88
        $this->assertEquals($expectedFilter, $actualFilter);
89
    }
90
91 View Code Duplication
    public function testTranslateOperator()
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...
92
    {
93
        // Manually set the data source element, mimic what the data source would do
94
        $dataSourceElement = new Field(
95
            'test_data_source_element_name',
96
            '',
97
            '',
98
            new StringDataType()
99
        );
100
101
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
102
        $dataSourceProphecy->getFields()->willReturn([$dataSourceElement]);
103
104
        $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...
105
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
106
107
        // LIKE is valid for String type, should return LIKE
108
        $this->assertEquals(FilterCondition::METHOD_STRING_LIKE, $interpreter->translateOperator('T_OP_LIKE', $dataSourceElement), 'Failed to translate T_OP_LIKE into STRING_LIKE for type String');
109
110
        // EQ is valid for String, and should choose STRING_EQ as it's the default for the type
111
        $this->assertEquals(FilterCondition::METHOD_STRING_EQ, $interpreter->translateOperator('T_OP_EQ', $dataSourceElement), 'Failed to translate T_OP_EQ into STRING_EQ for type String');
112
    }
113
114 View Code Duplication
    public function testInStatementsOnlyAcceptArraysAsValues()
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...
115
    {
116
        $astSubtree = new ASTAssertion('field', 'T_OP_IN', '123');
117
        $field = new Field('field', '', '', new NumberDataType());
118
119
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
120
        $dataSourceProphecy->getFields()->willReturn([$field]);
121
122
        $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...
123
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
124
125
        $this->expectException(UQLInterpreterException::class);
126
        $this->expectExceptionMessage('Only arrays are valid arguments for IN / NOT IN statements');
127
        $interpreter->buildFilter($astSubtree);
128
    }
129
130 View Code Duplication
    public function testNullValueCanBeCheckedOnlyWithIsOperator()
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...
131
    {
132
        $astSubtree = new ASTAssertion('field', 'T_OP_GT', null);
133
        $field = new Field('field', '', '', new NumberDataType());
134
135
        $dataSourceProphecy = $this->prophesize(DataSourceInterface::class);
136
        $dataSourceProphecy->getFields()->willReturn([$field]);
137
138
        $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...
139
        $interpreter = $interpreterFactory->create($dataSourceProphecy->reveal());
140
141
        $this->expectException(UQLInterpreterException::class);
142
        $this->expectExceptionMessage('Only IS / IS NOT operator can be used to compare against null value');
143
        $interpreter->buildFilter($astSubtree);
144
    }
145
146
    protected function setUp()
147
    {
148
        $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...
149
        $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...
150
    }
151
}
152