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()); |
|
|
|
|
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() |
|
|
|
|
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()); |
|
|
|
|
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() |
|
|
|
|
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()); |
|
|
|
|
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() |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
149
|
|
|
$this->contextFactoryProphecy = $this->prophesize(ContextFactory::class); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.