-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDefinitionIteratorTest.php
339 lines (274 loc) · 12.3 KB
/
DefinitionIteratorTest.php
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Test;
use Laminas\Http\Response;
use Magento\Upward\Context;
use Magento\Upward\Definition;
use Magento\Upward\DefinitionIterator;
use Magento\Upward\Resolver\ResolverInterface;
use Magento\Upward\ResolverFactory;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use function BeBat\Verify\verify;
class DefinitionIteratorTest extends TestCase
{
use MockeryPHPUnitIntegration;
public function testClone(): void
{
$context = new Context([]);
$definition = new Definition([]);
$original = new DefinitionIterator($definition, $context);
verify($original->getContext())->is()->sameAs($context);
verify($original->getRootDefinition())->is()->sameAs($definition);
$clone = clone $original;
verify($clone->getContext())->is()->equalTo($context);
verify($clone->getContext())->isNot()->sameAs($context);
verify($clone->getRootDefinition())->is()->sameAs($definition);
}
public function testDefinitionLoop(): void
{
$context = new Context([]);
$definition = new Definition([
'key1' => 'key2',
'key2' => 'key3',
'key3' => 'key1',
]);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$resolverFactory->shouldReceive('get')
->with(Mockery::any())
->andReturnNull();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Definition appears to contain a loop');
$iterator->get('key1');
}
public function testDefinitionNumericKeys(): void
{
$context = new Context([]);
$definition = new Definition([
'statuses' => [
'ok',
'missing',
'error',
],
'ok' => 200,
'missing' => 404,
'error' => 500,
]);
$iterator = new DefinitionIterator($definition, $context);
verify($iterator->get('statuses'))->is()->sameAs([200, 404, 500]);
}
public function testDefinitionValueIsBuiltin(): void
{
$context = new Context([]);
$definition = new Definition(['key' => true]);
$iterator = new DefinitionIterator($definition, $context);
verify($iterator->get('key'))->is()->true();
// Key has been added to context
verify($context->get('key'))->is()->true();
verify($iterator->get('child-key', new Definition(['child-key' => false])))->is()->false();
// Key was not added to context
verify($context->has('child-key'))->is()->false();
}
public function testIsContextFullyPopulated(): void
{
$context = new Context([
'context-only' => 'value',
'static-value' => 'some value',
'complex-definition' => ['x', 'y'],
'simple-definition' => ['a', 'b'],
'unfinished-list' => ['fist'],
'complete-list' => ['first', 'second', 'third'],
]);
$definition = new Definition([
'static-value' => 'some definition',
'simple-definition' => 'some.lookup.value',
'complex-definition' => [
'resolver' => 'something',
'param' => 'value',
'key' => 'another value',
],
'unfinished-list' => ['first', 'second'],
'complete-list' => ['first', 'second', 'third'],
]);
$iterator = new DefinitionIterator($definition, $context);
verify($iterator->isContextFullyPopulated('missing-key'))->is()->false();
verify($iterator->isContextFullyPopulated('context-only'))->is()->true();
verify($iterator->isContextFullyPopulated('static-value'))->is()->true();
verify($iterator->isContextFullyPopulated('simple-definition'))->is()->true();
verify($iterator->isContextFullyPopulated('complex-definition'))->is()->true();
verify($iterator->isContextFullyPopulated('unfinished-list'))->is()->false();
verify($iterator->isContextFullyPopulated('complete-list'))->is()->true();
}
public function testIteratingDefinitionTree(): void
{
$context = new Context([]);
$definition = new Definition([
'key1' => 'key2',
'key2' => true,
'key4' => false,
]);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$resolverFactory->shouldReceive('get')
->with(Mockery::any())
->andReturnNull();
verify($iterator->get('key1'))->is()->true();
// Both values added to context
verify($context->get('key1'))->is()->true();
verify($context->get('key2'))->is()->true();
// Child definition is an address for a value in the root definition
verify($iterator->get('key3', new Definition(['key3' => 'key4'])))->is()->false();
// Only value from root definition is added to context
verify($context->has('key3'))->is()->false();
verify($context->get('key4'))->is()->false();
}
public function testLookupInContext(): void
{
$context = new Context(['key' => 'context value']);
$definition = new Definition([]);
$iterator = new DefinitionIterator($definition, $context);
$definitionWithSameKey = new Definition(['key' => true]);
verify($iterator->get('key'))->is()->sameAs('context value');
verify($iterator->get('key', $definitionWithSameKey))->is()->sameAs(true);
}
public function testParentResolver(): void
{
$context = new Context([]);
$definition = new Definition(['key' => 'resolver-definition']);
$childDefinition = new Definition(['child-key' => '200']);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with('resolver-definition')
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('isValid')
->with($definition)
->andReturn(true);
$mockResolver->shouldReceive('resolve')
->with('resolver-definition')
->andReturn($childDefinition);
verify($iterator->get('key.child-key'))->is()->sameAs('200');
}
public function testParentResolverIsNotArray(): void
{
$context = new Context([]);
$definition = new Definition(['key' => 'resolver-definition']);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with('resolver-definition')
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('isValid')
->with($definition)
->andReturn(true);
$mockResolver->shouldReceive('resolve')
->with('resolver-definition')
->andReturn('200');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Could not get nested value key.child-key from value of type string');
$iterator->get('key.child-key');
}
public function testParentResolverIsResponse(): void
{
$context = new Context([]);
$definition = new Definition(['key' => 'resolver-definition']);
$response = new Response();
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with('resolver-definition')
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('isValid')
->with($definition)
->andReturn(true);
$mockResolver->shouldReceive('resolve')
->with('resolver-definition')
->andReturn($response);
verify($iterator->get('key.child-key'))->is()->sameAs($response);
}
public function testResolverValueDefinition(): void
{
$context = new Context([]);
$definition = new Definition(['key' => 'resolver-definition']);
$childDefinition = new Definition(['child-key' => true]);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with('resolver-definition')
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('isValid')
->with($definition)
->andReturn(true);
$mockResolver->shouldReceive('resolve')
->with('resolver-definition')
->andReturn($childDefinition);
verify($iterator->get('key'))->is()->sameAs(['child-key' => true]);
verify($context->get('key.child-key'))->is()->true();
// Intermediate value was not added to context
verify($context->has('child-key'))->is()->false();
}
public function testResolverValueInvalidDefinition(): void
{
$context = new Context([]);
$definition = new Definition(['key' => ['child1' => 'value1']]);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with(Definition::class)
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('isValid')
->andReturn(false);
$mockResolver->shouldNotHaveReceived('resolve');
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Definition {"child1":"value1"} is not valid for');
$iterator->get('key');
}
public function testScalarResolverValue(): void
{
$context = new Context([]);
$definition = new Definition(['key' => 'resolver-definition']);
$childDefinition = new Definition(['child-key' => 'resolver-for-child']);
$iterator = new DefinitionIterator($definition, $context);
$resolverFactory = Mockery::mock('alias:' . ResolverFactory::class);
$mockResolver = Mockery::mock(ResolverInterface::class);
$resolverFactory->shouldReceive('get')
->with('resolver-definition')
->andReturn($mockResolver);
$resolverFactory->shouldReceive('get')
->with('resolver-for-child')
->andReturn($mockResolver);
$mockResolver->shouldReceive('setIterator')
->with($iterator);
$mockResolver->shouldReceive('resolve')
->with('resolver-definition')
->andReturn('resolver value');
$mockResolver->shouldReceive('resolve')
->with('resolver-for-child')
->andReturn('child resolver value');
verify($iterator->get('key'))->is()->sameAs('resolver value');
verify($context->get('key'))->is()->sameAs('resolver value');
verify($iterator->get('child-key', $childDefinition))->is()->sameAs('child resolver value');
verify($context->has('child-key'))->is()->false();
}
}