-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConfigurableProducerTest.php
More file actions
217 lines (177 loc) · 7.8 KB
/
ConfigurableProducerTest.php
File metadata and controls
217 lines (177 loc) · 7.8 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
<?php
namespace Smartbox\Integration\FrameworkBundle\Tests\Functional\Producers;
use Smartbox\CoreBundle\Type\SerializableArray;
use Smartbox\Integration\FrameworkBundle\Components\WebService\ConfigurableWebserviceProtocol;
use Smartbox\Integration\FrameworkBundle\Configurability\ConfigurableServiceHelper;
use Smartbox\Integration\FrameworkBundle\Core\Endpoints\Endpoint;
use Smartbox\Integration\FrameworkBundle\Core\Exchange;
use Smartbox\Integration\FrameworkBundle\Core\Messages\Message;
use Smartbox\Integration\FrameworkBundle\Core\Producers\AbstractConfigurableProducer;
use Smartbox\Integration\FrameworkBundle\Core\Producers\ConfigurableProducerInterface;
use Smartbox\Integration\FrameworkBundle\Tests\Functional\BaseTestCase;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Smartbox\Integration\FrameworkBundle\Components\WebService\Exception\UnrecoverableExternalSystemException;
use Smartbox\Integration\FrameworkBundle\Components\WebService\Exception\RecoverableExternalSystemException;
class ConfigurableProducerTest extends BaseTestCase
{
/** @var AbstractConfigurableProducer|\PHPUnit_Framework_MockObject_MockObject */
protected $configurableProducer;
/** @var OptionsResolver */
protected $optionsResolver;
/** @var ConfigurableWebserviceProtocol */
protected $protocol;
protected $defaultOptions = [
'x' => 1,
'y' => 2,
'z' => [1, 2, 3],
];
protected $simpleMethodsConfig = [
'methodA' => [
ConfigurableServiceHelper::KEY_DESCRIPTION => 'Description here',
ConfigurableProducerInterface::CONF_STEPS => [
[ConfigurableServiceHelper::STEP_DEFINE => [
'x' => 'eval: 1 + 2',
'val' => 'eval: msg.getBody().get("value")',
]],
[ConfigurableServiceHelper::STEP_DEFINE => [
'result' => 'eval: x + val',
]],
],
ConfigurableProducerInterface::CONF_VALIDATIONS => [
[
'rule' => 'eval: x == 3',
'message' => 'Define does not work!',
'recoverable' => true,
],
[
'rule' => 'eval: val != 666',
'message' => 'Ugly number!!',
'recoverable' => true,
],
[
'rule' => 'eval: val != 1313666',
'message' => 'Too ugly number!!',
'recoverable' => false,
],
],
ConfigurableProducerInterface::CONF_RESPONSE => [
'body' => ['result' => 'eval: 1 + 2 + msg.getBody().get(\'value\') + 10'],
],
],
];
public function setUp(): void
{
parent::setUp();
$this->configurableProducer = $this->getMockBuilder(AbstractConfigurableProducer::class)->setMethods(null)->getMock();
$confHelper = new ConfigurableServiceHelper();
$confHelper->setSerializer($this->getContainer()->get('jms_serializer'));
$confHelper->setEvaluator($this->getContainer()->get('smartesb.util.evaluator'));
$this->configurableProducer->setConfHelper($confHelper);
$this->configurableProducer->setEvaluator($this->getContainer()->get('smartesb.util.evaluator'));
$this->configurableProducer->setSerializer($this->getContainer()->get('jms_serializer'));
$this->configurableProducer->setOptions($this->defaultOptions);
$this->configurableProducer->setMethodsConfiguration($this->simpleMethodsConfig);
$this->optionsResolver = new OptionsResolver();
$this->protocol = new ConfigurableWebserviceProtocol();
$this->protocol->configureOptionsResolver($this->optionsResolver);
}
public function testDefaultOptionsShouldBeSet()
{
$defaults = $this->configurableProducer->getOptions();
foreach ($this->defaultOptions as $defaultKey => $defaultValue) {
$this->assertArrayHasKey($defaultKey, $defaults);
$this->assertEquals($defaults[$defaultKey], $defaultValue);
}
}
public function testExecuteStepDefine()
{
$context = [
'x' => 1,
'y' => 2,
];
$actionParams = [
'r1' => 'eval: x + y',
'r2' => [
'sub1' => [
'a' => 'eval: x*10',
'b' => 'eval: y*10',
],
'sub2' => [
'a' => 'eval: x+10',
'b' => 'eval: y+10',
],
],
];
$options = [];
$this->configurableProducer->executeStep('define', $actionParams, $options, $context);
$this->assertEquals(3, $context['vars']['r1']);
$this->assertEquals([
'sub1' => [
'a' => '10',
'b' => '20',
],
'sub2' => [
'a' => '11',
'b' => '12',
],
], $context['vars']['r2']);
}
public function testSendWorks()
{
$exchange = new Exchange(
new Message(new SerializableArray(['value' => 5]))
);
$opts = $this->optionsResolver->resolve([
ConfigurableWebserviceProtocol::OPTION_METHOD => 'methodA',
ConfigurableWebserviceProtocol::OPTION_EXCHANGE_PATTERN => ConfigurableWebserviceProtocol::EXCHANGE_PATTERN_IN_OUT,
]);
$endpoint = new Endpoint('xxx', $opts, $this->protocol);
$this->configurableProducer->send($exchange, $endpoint);
$this->assertInstanceOf(SerializableArray::class, $exchange->getResult()->getBody());
$this->assertEquals(
(3 + 5 + 10),
$exchange->getResult()->getBody()->get('result')
);
}
public function testSendWithExchangePatternInOnlyRespectsMessage()
{
$in = new Message(new SerializableArray(['value' => 5]));
$exchange = new Exchange($in);
$opts = $this->optionsResolver->resolve([
ConfigurableWebserviceProtocol::OPTION_METHOD => 'methodA',
ConfigurableWebserviceProtocol::OPTION_EXCHANGE_PATTERN => ConfigurableWebserviceProtocol::EXCHANGE_PATTERN_IN_ONLY,
]);
$endpoint = new Endpoint('xxx', $opts, $this->protocol);
$this->configurableProducer->send($exchange, $endpoint);
$this->assertEquals(
$in,
$exchange->getResult()
);
}
public function testValidationWorksWithUnrecoverableException()
{
$this->expectException(UnrecoverableExternalSystemException::class);
$exchange = new Exchange(
new Message(new SerializableArray(['value' => 1313666]))
);
$opts = $this->optionsResolver->resolve([
ConfigurableWebserviceProtocol::OPTION_METHOD => 'methodA',
ConfigurableWebserviceProtocol::OPTION_EXCHANGE_PATTERN => ConfigurableWebserviceProtocol::EXCHANGE_PATTERN_IN_OUT,
]);
$endpoint = new Endpoint('xxx', $opts, $this->protocol);
$this->configurableProducer->send($exchange, $endpoint);
}
public function testValidationWorksWithRecoverableException()
{
$this->expectException(RecoverableExternalSystemException::class);
$exchange = new Exchange(
new Message(new SerializableArray(['value' => 666]))
);
$opts = $this->optionsResolver->resolve([
ConfigurableWebserviceProtocol::OPTION_METHOD => 'methodA',
ConfigurableWebserviceProtocol::OPTION_EXCHANGE_PATTERN => ConfigurableWebserviceProtocol::EXCHANGE_PATTERN_IN_OUT,
]);
$endpoint = new Endpoint('xxx', $opts, $this->protocol);
$this->configurableProducer->send($exchange, $endpoint);
}
}