-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExpressionEvaluatorTest.php
More file actions
303 lines (287 loc) · 10.7 KB
/
ExpressionEvaluatorTest.php
File metadata and controls
303 lines (287 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace Smartbox\Integration\FrameworkBundle\Tests\Functional\Tools\Evaluator;
use Smartbox\CoreBundle\Type\SerializableArray;
use Smartbox\Integration\FrameworkBundle\Core\Exchange;
use Smartbox\Integration\FrameworkBundle\Core\Messages\Message;
use Smartbox\Integration\FrameworkBundle\Tools\Evaluator\ExpressionEvaluator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* Class ExpressionEvaluatorTest.
*
* @coversDefaultClass \Smartbox\Integration\FrameworkBundle\Tools\Evaluator\ExpressionEvaluator
*/
class ExpressionEvaluatorTest extends KernelTestCase
{
/**
* @var ExpressionEvaluator
*/
protected $evaluator;
protected function setUp(): void
{
$this->bootKernel();
$this->evaluator = static::$kernel->getContainer()->get('smartesb.util.evaluator');
}
protected function tearDown(): void
{
$this->evaluator = null;
}
/**
* @return array
*/
public function dataProviderForExpressionsEvaluatedWithVars()
{
return [
'Simple expression' => [
'expected' => 2,
'expression' => '1 + 1',
'vars' => [],
],
'Checking hasKey expression for existing key' => [
'expected' => true,
'expression' => 'hasKey("existing_key", value)',
'vars' => [
'value' => ['existing_key' => 'This key exists'],
],
],
'Checking hasKey expression for not existing key' => [
'expected' => false,
'expression' => 'hasKey("not_existing_key", value)',
'vars' => [
'value' => ['existing_key' => 'This key exists'],
],
],
'Check substr returns a substring' => [
'expected' => 'abc',
'expression' => 'substr("abcdef", 0, 3)',
'vars' => [],
],
'Check substr returns a blank string' => [
'expected' => '',
'expression' => 'substr("abcdef", 10, 3)',
'vars' => [],
],
'Check mb_substr returns a substring' => [
'expected' => 'ábé',
'expression' => 'mb_substr("ábédef", 0, 3)',
'vars' => [],
],
'Check mb_substr returns a blank string' => [
'expected' => '',
'expression' => 'substr("abcdef", 10, 3)',
'vars' => [],
],
'Check md5 returns a hash' => [
'expected' => '187ef4436122d1cc2f40dc2b92f0eba0',
'expression' => 'md5("ab")',
'var' => [],
],
'Check numberFormat returns null if null is passed' => [
'expected' => null,
'expression' => 'numberFormat(null)',
'vars' => [],
],
'Check numberFormat returns a main land number format to 2 decimals and no thousands separator' => [
'expected' => '123456,00',
'expression' => 'numberFormat( 123456.001, 2, ",", "" )',
'vars' => [],
],
'Check count returns the number of elements in an array' => [
'expected' => 2,
'expression' => 'count(array)',
'vars' => [
'array' => [
1,
2,
],
],
],
'Check slice getting elements from the array starting from position 2 until the end' => [
'expected' => ['c', 'd', 'e'],
'expression' => 'slice(["a", "b", "c", "d", "e"], 2)',
'vars' => [],
],
'Check slice getting 1 element from the array stating 2 positions from the end' => [
'expected' => ['d'],
'expression' => 'slice(["a", "b", "c", "d", "e"], -2, 1)',
'vars' => [],
],
'Check slice getting 3 elements from the array starting from the beginning' => [
'expected' => ['a', 'b', 'c'],
'expression' => 'slice(["a", "b", "c", "d", "e"], 0, 3)',
'vars' => [],
],
'Check explode array' => [
'expected' => ['this', 'is', 'a', 'test'],
'expression' => 'explode(",","this,is,a,test")',
'vars' => [],
],
'Check explode array with single element' => [
'expected' => ['this'],
'expression' => 'explode(",","this")',
'vars' => [],
],
'Check explode array with single element and trailing delimiter' => [
'expected' => ['this', ''],
'expression' => 'explode(",","this,")',
'vars' => [],
],
'Check explode when passed null return an array with 1 empty element' => [
'expected' => [''],
'expression' => 'explode(",",null)',
'vars' => [],
],
'Check implode returns a correctly formatted string' => [
'expected' => 'this_is_a_test',
'expression' => 'implode("_",["this","is","a","test"])',
'vars' => [],
],
'Check implode returns a correctly formatted string when passed an array with only one element' => [
'expected' => 'this',
'expression' => 'implode("_",["this"])',
'vars' => [],
],
'Check removeNewLines removes any new lines in a string' => [
'expected' => 'Line 1 Line 2',
'expression' => 'removeNewLines("\nLine 1\nLine 2\n")',
'vars' => [],
],
'Check strtoupper replaces lower case with upper case' => [
'expected' => 'SUPER AWESOME',
'expression' => 'strtoupper("Super awesome")',
'vars' => [],
],
'Check strtoupper does nothing on upper case text' => [
'expected' => 'SUPER AWESOME',
'expression' => 'strtoupper("SUPER AWESOME")',
'vars' => [],
],
'Checking getKey expression for existing KEY index' => [
'expected' => 'VALUE_WITH_KEY',
'expression' => 'getKey(array)',
'vars' => [
'array' => ['key' => 'VALUE_WITH_KEY'],
],
],
'Checking getValue expression for existing VALUE index' => [
'expected' => 'VALUE_WITH_VALUE_KEY',
'expression' => 'getValue(array)',
'vars' => [
'array' => ['value' => 'VALUE_WITH_VALUE_KEY'],
],
],
];
}
/**
* @return array
*/
public function dataProviderForFailedExpressionsEvaluatedWithVars()
{
return [
'Checking getKey expression for not existing KEY index' => [
'exception' => \RuntimeException::class,
'message' => 'The argument passed to "getKey" should have a index called "key"',
'expression' => 'getKey(array)',
'vars' => [
'array' => ['VALUE_WITH_NO_KEY'],
],
],
'Checking getValue expression for existing VALUE index' => [
'exception' => \RuntimeException::class,
'message' => 'The argument passed to "getValue" should have a index called "value"',
'expression' => 'getValue(array)',
'vars' => [
'array' => ['VALUE_WITH_NO_VALUE_KEY'],
],
],
];
}
/**
* @covers ::evaluateWithVars
* @dataProvider dataProviderForExpressionsEvaluatedWithVars
*
* @param $expected
* @param $expression
* @param array $vars
*/
public function testEvaluateWithVars($expected, $expression, array $vars)
{
$this->assertEquals($expected, $this->evaluator->evaluateWithVars($expression, $vars));
}
/**
* @covers ::evaluateWithVars
* @dataProvider dataProviderForFailedExpressionsEvaluatedWithVars
*
* @param $exception
* @param $message
* @param $expression
* @param array $vars
*
* @throws \Exception
*/
public function testFailedEvaluateWithVars($exception, $message, $expression, array $vars)
{
$this->expectException($exception);
$this->expectExceptionMessage($message);
$this->evaluator->evaluateWithVars($expression, $vars);
}
/**
* @return array
*/
public function dataProviderForExpressionsEvaluatedWithExchange()
{
$body = new SerializableArray(
[
'a' => 1,
'b' => 2,
'c' => 3,
]
);
$messageHeaders = [
Message::HEADER_EXPIRES => 3,
Message::HEADER_QUEUE => 'queue_uri',
Message::HEADER_FROM => 'from_uri',
];
$message = new Message(
$body,
$messageHeaders
);
$exchange = new Exchange($message);
$exchange->setHeader(Exchange::HEADER_HANDLER, 'handler');
$exchange->setHeader(Exchange::HEADER_PARENT_EXCHANGE, 'parent_exchange');
$exchange->setHeader(Exchange::HEADER_FROM, 'from_uri');
return [
'Simple expression to extract exchange' => [
'expected' => $exchange,
'expression' => 'exchange',
'exchange' => $exchange,
],
'Simple expression to extract message' => [
'expected' => $message,
'expression' => 'msg',
'exchange' => $exchange,
],
'Simple expression to extract headers' => [
'expected' => $messageHeaders,
'expression' => 'headers',
'exchange' => $exchange,
],
'Simple expression to extract body' => [
'expected' => $message->getBody(),
'expression' => 'body',
'exchange' => $exchange,
],
];
}
/**
* @covers ::evaluateWithExchange
* @dataProvider dataProviderForExpressionsEvaluatedWithExchange
*
* @param $expected
* @param $expression
* @param Exchange $exchange
*/
public function testEvaluateWithExchange($expected, $expression, Exchange $exchange)
{
$this->assertEquals($expected, $this->evaluator->evaluateWithExchange($expression, $exchange));
}
}