This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
ProcessorTest.php
508 lines (436 loc) · 17.7 KB
/
ProcessorTest.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Config;
use Zend\Config\Config,
Zend\Config\Processor\Token as TokenProcessor,
Zend\Config\Processor\Translator as TranslatorProcessor,
Zend\Config\Processor\Filter as FilterProcessor,
Zend\Config\Processor\Constant as ConstantProcessor,
Zend\Config\Processor\Queue as Queue,
Zend\Translator\Translator,
Zend\Translator\Adapter\ArrayAdapter,
Zend\Filter\StringToLower,
Zend\Filter\StringToUpper,
Zend\Filter\PregReplace;
/**
* @category Zend
* @package Zend_Config
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Config
*/
class ProcessorTest extends \PHPUnit_Framework_TestCase
{
protected $nested;
protected $tokenBare, $tokenPrefix, $tokenSuffix, $tokenSurround, $tokenSurroundMixed;
protected $translator, $translatorStrings;
protected $userConstants, $phpConstants;
protected $filter;
public function setUp()
{
// Arrays representing common config configurations
$this->nested = array(
'a' => 1,
'b' => 2,
'c' => array(
'ca' => 3,
'cb' => 4,
'cc' => 5,
'cd' => array(
'cda' => 6,
'cdb' => 7
),
),
'd' => array(
'da' => 8,
'db' => 9
),
'e' => 10
);
$this->tokenBare = array(
'simple' => 'BARETOKEN',
'inside' => 'some text with BARETOKEN inside',
'nested' => array(
'simple' => 'BARETOKEN',
'inside' => 'some text with BARETOKEN inside',
),
);
$this->tokenPrefix = array(
'simple' => '::TOKEN',
'inside' => ':: some text with ::TOKEN inside ::',
'nested' => array(
'simple' => '::TOKEN',
'inside' => ':: some text with ::TOKEN inside ::',
),
);
$this->tokenSuffix = array(
'simple' => 'TOKEN::',
'inside' => ':: some text with TOKEN:: inside ::',
'nested' => array(
'simple' => 'TOKEN::',
'inside' => ':: some text with TOKEN:: inside ::',
),
);
$this->tokenSurround = array(
'simple' => '##TOKEN##',
'inside' => '## some text with ##TOKEN## inside ##',
'nested' => array(
'simple' => '##TOKEN##',
'inside' => '## some text with ##TOKEN## inside ##',
),
);
$this->tokenSurroundMixed = array(
'simple' => '##TOKEN##',
'inside' => '## some text with ##TOKEN## inside ##',
'nested' => array(
'simple' => '@@TOKEN@@',
'inside' => '@@ some text with @@TOKEN@@ inside @@',
),
);
$this->translator = array(
'pages' => array(
array(
'id' => 'oneDog',
'label' => 'one dog',
'route' => 'app-one-dog'
),
array(
'id' => 'twoDogs',
'label' => 'two dogs',
'route' => 'app-two-dogs'
),
)
);
$this->translatorStrings = array(
'one dog' => 'ein Hund',
'two dogs' => 'zwei Hunde'
);
$this->filter = array(
'simple' => 'some MixedCase VALue',
'nested' => array(
'simple' => 'OTHER mixed Case Value',
),
);
if (ArrayAdapter::hasCache()) {
ArrayAdapter::clearCache();
ArrayAdapter::removeCache();
}
$this->userConstants = array(
'simple' => 'SOME_USERLAND_CONSTANT',
'inside' => 'some text with SOME_USERLAND_CONSTANT inside',
'nested' => array(
'simple' => 'SOME_USERLAND_CONSTANT',
'inside' => 'some text with SOME_USERLAND_CONSTANT inside',
),
);
$this->phpConstants = array(
'phpVersion' => 'PHP_VERSION',
'phpVersionInside' => 'Current PHP version is: PHP_VERSION',
'nested' => array(
'phpVersion' => 'PHP_VERSION',
'phpVersionInside' => 'Current PHP version is: PHP_VERSION',
),
);
}
public function testProcessorsQueue()
{
$processor1 = new TokenProcessor();
$processor2 = new TokenProcessor();
$queue = new Queue();
$queue->insert($processor1);
$queue->insert($processor2);
$this->assertInstanceOf('\Zend\Config\Processor\Queue', $queue);
$this->assertEquals(2, $queue->count());
$this->assertTrue($queue->contains($processor1));
$this->assertTrue($queue->contains($processor2));
}
public function testBareTokenPost()
{
$config = new Config($this->tokenBare, true);
$processor = new TokenProcessor();
$processor->addToken('BARETOKEN', 'some replaced value');
$processor->process($config);
$this->assertEquals(array('BARETOKEN' => 'some replaced value'), $processor->getTokens());
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals('some text with some replaced value inside', $config->inside);
$this->assertEquals('some replaced value', $config->nested->simple);
$this->assertEquals('some text with some replaced value inside', $config->nested->inside);
}
public function testAddInvalidToken()
{
$processor = new TokenProcessor();
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot use ' . gettype(array()) . ' as token name.');
$processor->addToken(array(), 'bar');
}
public function testSingleValueToken()
{
$processor = new TokenProcessor();
$processor->addToken('BARETOKEN', 'test');
$data = 'BARETOKEN';
$out = $processor->processValue($data);
$this->assertEquals($out, 'test');
}
public function testTokenReadOnly()
{
$config = new Config($this->tokenBare, false);
$processor = new TokenProcessor();
$processor->addToken('BARETOKEN', 'some replaced value');
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only');
$processor->process($config);
}
public function testTokenPrefix()
{
$config = new Config($this->tokenPrefix, true);
$processor = new TokenProcessor(array('TOKEN' => 'some replaced value'), '::');
$processor->process($config);
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals(':: some text with some replaced value inside ::', $config->inside);
$this->assertEquals('some replaced value', $config->nested->simple);
$this->assertEquals(':: some text with some replaced value inside ::', $config->nested->inside);
}
public function testTokenSuffix()
{
$config = new Config($this->tokenSuffix, true);
$processor = new TokenProcessor(array('TOKEN' => 'some replaced value'), '', '::');
$processor->process($config);
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals(':: some text with some replaced value inside ::', $config->inside);
$this->assertEquals('some replaced value', $config->nested->simple);
$this->assertEquals(':: some text with some replaced value inside ::', $config->nested->inside);
}
/**
* @depends testTokenSuffix
* @depends testTokenPrefix
*/
public function testTokenSurround()
{
$config = new Config($this->tokenSurround, true);
$processor = new TokenProcessor(array('TOKEN' => 'some replaced value'), '##', '##');
$processor->process($config);
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals('## some text with some replaced value inside ##', $config->inside);
$this->assertEquals('some replaced value', $config->nested->simple);
$this->assertEquals('## some text with some replaced value inside ##', $config->nested->inside);
}
/**
* @depends testTokenSurround
*/
public function testTokenChangeParams()
{
$config = new Config($this->tokenSurroundMixed, true);
$processor = new TokenProcessor(array('TOKEN' => 'some replaced value'), '##', '##');
$processor->process($config);
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals('## some text with some replaced value inside ##', $config->inside);
$this->assertEquals('@@TOKEN@@', $config->nested->simple);
$this->assertEquals('@@ some text with @@TOKEN@@ inside @@', $config->nested->inside);
/**
* Now change prefix and suffix on the processor
*/
$processor->setPrefix('@@');
$processor->setSuffix('@@');
/**
* Parse the config again
*/
$processor->process($config);
$this->assertEquals('some replaced value', $config->simple);
$this->assertEquals('## some text with some replaced value inside ##', $config->inside);
$this->assertEquals('some replaced value', $config->nested->simple);
$this->assertEquals('@@ some text with some replaced value inside @@', $config->nested->inside);
}
/**
* @depends testTokenSurround
*/
public function testUserConstants()
{
define('SOME_USERLAND_CONSTANT', 'some constant value');
$config = new Config($this->userConstants, true);
$processor = new ConstantProcessor(false);
$processor->process($config);
$tokens = $processor->getTokens();
$this->assertTrue(is_array($tokens));
$this->assertTrue(in_array('SOME_USERLAND_CONSTANT', $tokens));
$this->assertTrue(!$processor->getUserOnly());
$this->assertEquals('some constant value', $config->simple);
$this->assertEquals('some text with some constant value inside', $config->inside);
$this->assertEquals('some constant value', $config->nested->simple);
$this->assertEquals('some text with some constant value inside', $config->nested->inside);
}
/**
* @depends testUserConstants
*/
public function testUserOnlyConstants()
{
$config = new Config($this->userConstants, true);
$processor = new ConstantProcessor();
$processor->process($config);
$tokens = $processor->getTokens();
$this->assertTrue(is_array($tokens));
$this->assertTrue(in_array('SOME_USERLAND_CONSTANT', $tokens));
$this->assertTrue($processor->getUserOnly());
$this->assertEquals('some constant value', $config->simple);
$this->assertEquals('some text with some constant value inside', $config->inside);
$this->assertEquals('some constant value', $config->nested->simple);
$this->assertEquals('some text with some constant value inside', $config->nested->inside);
}
/**
* @depends testTokenSurround
*/
public function testPHPConstants()
{
$config = new Config($this->phpConstants, true);
$processor = new ConstantProcessor(false);
$processor->process($config);
$this->assertEquals(PHP_VERSION, $config->phpVersion);
$this->assertEquals('Current PHP version is: ' . PHP_VERSION, $config->phpVersionInside);
$this->assertEquals(PHP_VERSION, $config->nested->phpVersion);
$this->assertEquals('Current PHP version is: ' . PHP_VERSION, $config->nested->phpVersionInside);
}
public function testTranslator()
{
$config = new Config($this->translator, true);
$translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE');
$processor = new TranslatorProcessor($translator);
$processor->process($config);
$this->assertEquals('oneDog', $config->pages[0]->id);
$this->assertEquals('ein Hund', $config->pages[0]->label);
$this->assertEquals('twoDogs', $config->pages[1]->id);
$this->assertEquals('zwei Hunde', $config->pages[1]->label);
}
public function testTranslatorReadOnly()
{
$config = new Config($this->translator, false);
$translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE');
$processor = new TranslatorProcessor($translator);
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only');
$processor->process($config);
}
public function testTranslatorSingleValue()
{
$translator = new Translator(Translator::AN_ARRAY, $this->translatorStrings, 'de_DE');
$processor = new TranslatorProcessor($translator);
$word = 'one dog';
$this->assertEquals('ein Hund', $processor->processValue($word));
}
public function testFilter()
{
$config = new Config($this->filter, true);
$filter = new StringToLower();
$processor = new FilterProcessor($filter);
$this->assertTrue($processor->getFilter() instanceof StringToLower);
$processor->process($config);
$this->assertEquals('some mixedcase value', $config->simple);
$this->assertEquals('other mixed case value', $config->nested->simple);
}
public function testFilterReadOnly()
{
$config = new Config($this->filter, false);
$filter = new StringToLower();
$processor = new FilterProcessor($filter);
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only');
$processor->process($config);
}
public function testFilterValue()
{
$filter = new StringToLower();
$processor = new FilterProcessor($filter);
$value = 'TEST';
$this->assertEquals('test', $processor->processValue($value));
}
/**
* @depends testFilter
*/
public function testQueueFIFO()
{
$config = new Config($this->filter, true);
$lower = new StringToLower();
$upper = new StringToUpper();
$lowerProcessor = new FilterProcessor($lower);
$upperProcessor = new FilterProcessor($upper);
/**
* Default queue order (FIFO)
*/
$queue = new Queue();
$queue->insert($upperProcessor);
$queue->insert($lowerProcessor);
$queue->process($config);
$this->assertEquals('some mixedcase value', $config->simple);
$this->assertEquals('other mixed case value', $config->nested->simple);
}
public function testQueueReadOnly()
{
$config = new Config($this->filter, false);
$lower = new StringToLower();
$lowerProcessor = new FilterProcessor($lower);
/**
* Default queue order (FIFO)
*/
$queue = new Queue();
$queue->insert($lowerProcessor);
$this->setExpectedException('Zend\Config\Exception\InvalidArgumentException', 'Cannot parse config because it is read-only');
$queue->process($config);
}
public function testQueueSingleValue()
{
$lower = new StringToLower();
$upper = new StringToUpper();
$lowerProcessor = new FilterProcessor($lower);
$upperProcessor = new FilterProcessor($upper);
/**
* Default queue order (FIFO)
*/
$queue = new Queue();
$queue->insert($upperProcessor);
$queue->insert($lowerProcessor);
$data ='TeSt';
$this->assertEquals('test', $queue->processValue($data));
}
/**
* @depends testQueueFIFO
*/
public function testQueuePriorities()
{
$config = new Config($this->filter, 1);
$lower = new StringToLower();
$upper = new StringToUpper();
$replace = new PregReplace('/[a-z]/', '');
$lowerProcessor = new FilterProcessor($lower);
$upperProcessor = new FilterProcessor($upper);
$replaceProcessor = new FilterProcessor($replace);
$queue = new Queue();
/**
* Insert lower case filter with higher priority
*/
$queue->insert($upperProcessor, 10);
$queue->insert($lowerProcessor, 1000);
$config->simple = 'some MixedCase VALue';
$queue->process($config);
$this->assertEquals('SOME MIXEDCASE VALUE', $config->simple);
/**
* Add even higher priority replace processor that will remove all lowercase letters
*/
$queue->insert($replaceProcessor, 10000);
$config->newValue = 'THIRD mixed CASE value';
$queue->process($config);
$this->assertEquals('THIRD CASE ', $config->newValue);
}
}