]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / options-resolver / Symfony / Component / OptionsResolver / Tests / OptionsResolverTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\OptionsResolver\Tests;
13
14 use Symfony\Component\OptionsResolver\OptionsResolver;
15 use Symfony\Component\OptionsResolver\Options;
16
17 class OptionsResolverTest extends \PHPUnit_Framework_TestCase
18 {
19 /**
20 * @var OptionsResolver
21 */
22 private $resolver;
23
24 protected function setUp()
25 {
26 $this->resolver = new OptionsResolver();
27 }
28
29 public function testResolve()
30 {
31 $this->resolver->setDefaults(array(
32 'one' => '1',
33 'two' => '2',
34 ));
35
36 $options = array(
37 'two' => '20',
38 );
39
40 $this->assertEquals(array(
41 'one' => '1',
42 'two' => '20',
43 ), $this->resolver->resolve($options));
44 }
45
46 public function testResolveLazy()
47 {
48 $this->resolver->setDefaults(array(
49 'one' => '1',
50 'two' => function (Options $options) {
51 return '20';
52 },
53 ));
54
55 $this->assertEquals(array(
56 'one' => '1',
57 'two' => '20',
58 ), $this->resolver->resolve(array()));
59 }
60
61 public function testResolveLazyDependencyOnOptional()
62 {
63 $this->resolver->setDefaults(array(
64 'one' => '1',
65 'two' => function (Options $options) {
66 return $options['one'].'2';
67 },
68 ));
69
70 $options = array(
71 'one' => '10',
72 );
73
74 $this->assertEquals(array(
75 'one' => '10',
76 'two' => '102',
77 ), $this->resolver->resolve($options));
78 }
79
80 public function testResolveLazyDependencyOnMissingOptionalWithoutDefault()
81 {
82 $test = $this;
83
84 $this->resolver->setOptional(array(
85 'one',
86 ));
87
88 $this->resolver->setDefaults(array(
89 'two' => function (Options $options) use ($test) {
90 /* @var \PHPUnit_Framework_TestCase $test */
91 $test->assertFalse(isset($options['one']));
92
93 return '2';
94 },
95 ));
96
97 $options = array(
98 );
99
100 $this->assertEquals(array(
101 'two' => '2',
102 ), $this->resolver->resolve($options));
103 }
104
105 public function testResolveLazyDependencyOnOptionalWithoutDefault()
106 {
107 $test = $this;
108
109 $this->resolver->setOptional(array(
110 'one',
111 ));
112
113 $this->resolver->setDefaults(array(
114 'two' => function (Options $options) use ($test) {
115 /* @var \PHPUnit_Framework_TestCase $test */
116 $test->assertTrue(isset($options['one']));
117
118 return $options['one'].'2';
119 },
120 ));
121
122 $options = array(
123 'one' => '10',
124 );
125
126 $this->assertEquals(array(
127 'one' => '10',
128 'two' => '102',
129 ), $this->resolver->resolve($options));
130 }
131
132 public function testResolveLazyDependencyOnRequired()
133 {
134 $this->resolver->setRequired(array(
135 'one',
136 ));
137 $this->resolver->setDefaults(array(
138 'two' => function (Options $options) {
139 return $options['one'].'2';
140 },
141 ));
142
143 $options = array(
144 'one' => '10',
145 );
146
147 $this->assertEquals(array(
148 'one' => '10',
149 'two' => '102',
150 ), $this->resolver->resolve($options));
151 }
152
153 public function testResolveLazyReplaceDefaults()
154 {
155 $test = $this;
156
157 $this->resolver->setDefaults(array(
158 'one' => function (Options $options) use ($test) {
159 /* @var \PHPUnit_Framework_TestCase $test */
160 $test->fail('Previous closure should not be executed');
161 },
162 ));
163
164 $this->resolver->replaceDefaults(array(
165 'one' => function (Options $options, $previousValue) {
166 return '1';
167 },
168 ));
169
170 $this->assertEquals(array(
171 'one' => '1',
172 ), $this->resolver->resolve(array()));
173 }
174
175 /**
176 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
177 */
178 public function testResolveFailsIfNonExistingOption()
179 {
180 $this->resolver->setDefaults(array(
181 'one' => '1',
182 ));
183
184 $this->resolver->setRequired(array(
185 'two',
186 ));
187
188 $this->resolver->setOptional(array(
189 'three',
190 ));
191
192 $this->resolver->resolve(array(
193 'foo' => 'bar',
194 ));
195 }
196
197 /**
198 * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
199 */
200 public function testResolveFailsIfMissingRequiredOption()
201 {
202 $this->resolver->setRequired(array(
203 'one',
204 ));
205
206 $this->resolver->setDefaults(array(
207 'two' => '2',
208 ));
209
210 $this->resolver->resolve(array(
211 'two' => '20',
212 ));
213 }
214
215 public function testResolveSucceedsIfOptionValueAllowed()
216 {
217 $this->resolver->setDefaults(array(
218 'one' => '1',
219 ));
220
221 $this->resolver->setAllowedValues(array(
222 'one' => array('1', 'one'),
223 ));
224
225 $options = array(
226 'one' => 'one',
227 );
228
229 $this->assertEquals(array(
230 'one' => 'one',
231 ), $this->resolver->resolve($options));
232 }
233
234 public function testResolveSucceedsIfOptionValueAllowed2()
235 {
236 $this->resolver->setDefaults(array(
237 'one' => '1',
238 'two' => '2',
239 ));
240
241 $this->resolver->setAllowedValues(array(
242 'one' => '1',
243 'two' => '2',
244 ));
245 $this->resolver->addAllowedValues(array(
246 'one' => 'one',
247 'two' => 'two',
248 ));
249
250 $options = array(
251 'one' => '1',
252 'two' => 'two',
253 );
254
255 $this->assertEquals(array(
256 'one' => '1',
257 'two' => 'two',
258 ), $this->resolver->resolve($options));
259 }
260
261 public function testResolveSucceedsIfOptionalWithAllowedValuesNotSet()
262 {
263 $this->resolver->setRequired(array(
264 'one',
265 ));
266
267 $this->resolver->setOptional(array(
268 'two',
269 ));
270
271 $this->resolver->setAllowedValues(array(
272 'one' => array('1', 'one'),
273 'two' => array('2', 'two'),
274 ));
275
276 $options = array(
277 'one' => '1',
278 );
279
280 $this->assertEquals(array(
281 'one' => '1',
282 ), $this->resolver->resolve($options));
283 }
284
285 /**
286 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
287 */
288 public function testResolveFailsIfOptionValueNotAllowed()
289 {
290 $this->resolver->setDefaults(array(
291 'one' => '1',
292 ));
293
294 $this->resolver->setAllowedValues(array(
295 'one' => array('1', 'one'),
296 ));
297
298 $this->resolver->resolve(array(
299 'one' => '2',
300 ));
301 }
302
303 public function testResolveSucceedsIfOptionTypeAllowed()
304 {
305 $this->resolver->setDefaults(array(
306 'one' => '1',
307 ));
308
309 $this->resolver->setAllowedTypes(array(
310 'one' => 'string',
311 ));
312
313 $options = array(
314 'one' => 'one',
315 );
316
317 $this->assertEquals(array(
318 'one' => 'one',
319 ), $this->resolver->resolve($options));
320 }
321
322 public function testResolveSucceedsIfOptionTypeAllowedPassArray()
323 {
324 $this->resolver->setDefaults(array(
325 'one' => '1',
326 ));
327
328 $this->resolver->setAllowedTypes(array(
329 'one' => array('string', 'bool'),
330 ));
331
332 $options = array(
333 'one' => true,
334 );
335
336 $this->assertEquals(array(
337 'one' => true,
338 ), $this->resolver->resolve($options));
339 }
340
341 public function testResolveSucceedsIfOptionTypeAllowedPassObject()
342 {
343 $this->resolver->setDefaults(array(
344 'one' => '1',
345 ));
346
347 $this->resolver->setAllowedTypes(array(
348 'one' => 'object',
349 ));
350
351 $object = new \stdClass();
352 $options = array(
353 'one' => $object,
354 );
355
356 $this->assertEquals(array(
357 'one' => $object,
358 ), $this->resolver->resolve($options));
359 }
360
361 public function testResolveSucceedsIfOptionTypeAllowedPassClass()
362 {
363 $this->resolver->setDefaults(array(
364 'one' => '1',
365 ));
366
367 $this->resolver->setAllowedTypes(array(
368 'one' => '\stdClass',
369 ));
370
371 $object = new \stdClass();
372 $options = array(
373 'one' => $object,
374 );
375
376 $this->assertEquals(array(
377 'one' => $object,
378 ), $this->resolver->resolve($options));
379 }
380
381 public function testResolveSucceedsIfOptionTypeAllowedAddTypes()
382 {
383 $this->resolver->setDefaults(array(
384 'one' => '1',
385 'two' => '2',
386 ));
387
388 $this->resolver->setAllowedTypes(array(
389 'one' => 'string',
390 'two' => 'bool',
391 ));
392 $this->resolver->addAllowedTypes(array(
393 'one' => 'float',
394 'two' => 'integer',
395 ));
396
397 $options = array(
398 'one' => 1.23,
399 'two' => false,
400 );
401
402 $this->assertEquals(array(
403 'one' => 1.23,
404 'two' => false,
405 ), $this->resolver->resolve($options));
406 }
407
408 public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
409 {
410 $this->resolver->setOptional(array(
411 'one',
412 'two',
413 ));
414
415 $this->resolver->setAllowedTypes(array(
416 'one' => 'string',
417 'two' => 'int',
418 ));
419
420 $options = array(
421 'two' => 1,
422 );
423
424 $this->assertEquals(array(
425 'two' => 1,
426 ), $this->resolver->resolve($options));
427 }
428
429 /**
430 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
431 */
432 public function testResolveFailsIfOptionTypeNotAllowed()
433 {
434 $this->resolver->setDefaults(array(
435 'one' => '1',
436 ));
437
438 $this->resolver->setAllowedTypes(array(
439 'one' => array('string', 'bool'),
440 ));
441
442 $this->resolver->resolve(array(
443 'one' => 1.23,
444 ));
445 }
446
447 /**
448 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
449 */
450 public function testResolveFailsIfOptionTypeNotAllowedMultipleOptions()
451 {
452 $this->resolver->setDefaults(array(
453 'one' => '1',
454 'two' => '2',
455 ));
456
457 $this->resolver->setAllowedTypes(array(
458 'one' => 'string',
459 'two' => 'bool',
460 ));
461
462 $this->resolver->resolve(array(
463 'one' => 'foo',
464 'two' => 1.23,
465 ));
466 }
467
468 /**
469 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
470 */
471 public function testResolveFailsIfOptionTypeNotAllowedAddTypes()
472 {
473 $this->resolver->setDefaults(array(
474 'one' => '1',
475 ));
476
477 $this->resolver->setAllowedTypes(array(
478 'one' => 'string',
479 ));
480 $this->resolver->addAllowedTypes(array(
481 'one' => 'bool',
482 ));
483
484 $this->resolver->resolve(array(
485 'one' => 1.23,
486 ));
487 }
488
489 /**
490 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
491 */
492 public function testSetRequiredFailsIfDefaultIsPassed()
493 {
494 $this->resolver->setRequired(array(
495 'one' => '1',
496 ));
497 }
498
499 /**
500 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
501 */
502 public function testSetOptionalFailsIfDefaultIsPassed()
503 {
504 $this->resolver->setOptional(array(
505 'one' => '1',
506 ));
507 }
508
509 public function testFluidInterface()
510 {
511 $this->resolver->setDefaults(array('one' => '1'))
512 ->replaceDefaults(array('one' => '2'))
513 ->setAllowedValues(array('one' => array('1', '2')))
514 ->addAllowedValues(array('one' => array('3')))
515 ->setRequired(array('two'))
516 ->setOptional(array('three'));
517
518 $options = array(
519 'two' => '2',
520 );
521
522 $this->assertEquals(array(
523 'one' => '2',
524 'two' => '2',
525 ), $this->resolver->resolve($options));
526 }
527
528 public function testKnownIfDefaultWasSet()
529 {
530 $this->assertFalse($this->resolver->isKnown('foo'));
531
532 $this->resolver->setDefaults(array(
533 'foo' => 'bar',
534 ));
535
536 $this->assertTrue($this->resolver->isKnown('foo'));
537 }
538
539 public function testKnownIfRequired()
540 {
541 $this->assertFalse($this->resolver->isKnown('foo'));
542
543 $this->resolver->setRequired(array(
544 'foo',
545 ));
546
547 $this->assertTrue($this->resolver->isKnown('foo'));
548 }
549
550 public function testKnownIfOptional()
551 {
552 $this->assertFalse($this->resolver->isKnown('foo'));
553
554 $this->resolver->setOptional(array(
555 'foo',
556 ));
557
558 $this->assertTrue($this->resolver->isKnown('foo'));
559 }
560
561 public function testRequiredIfRequired()
562 {
563 $this->assertFalse($this->resolver->isRequired('foo'));
564
565 $this->resolver->setRequired(array(
566 'foo',
567 ));
568
569 $this->assertTrue($this->resolver->isRequired('foo'));
570 }
571
572 public function testNotRequiredIfRequiredAndDefaultValue()
573 {
574 $this->assertFalse($this->resolver->isRequired('foo'));
575
576 $this->resolver->setRequired(array(
577 'foo',
578 ));
579 $this->resolver->setDefaults(array(
580 'foo' => 'bar',
581 ));
582
583 $this->assertFalse($this->resolver->isRequired('foo'));
584 }
585
586 public function testNormalizersTransformFinalOptions()
587 {
588 $this->resolver->setDefaults(array(
589 'foo' => 'bar',
590 'bam' => 'baz',
591 ));
592 $this->resolver->setNormalizers(array(
593 'foo' => function (Options $options, $value) {
594 return $options['bam'].'['.$value.']';
595 },
596 ));
597
598 $expected = array(
599 'foo' => 'baz[bar]',
600 'bam' => 'baz',
601 );
602
603 $this->assertEquals($expected, $this->resolver->resolve(array()));
604
605 $expected = array(
606 'foo' => 'boo[custom]',
607 'bam' => 'boo',
608 );
609
610 $this->assertEquals($expected, $this->resolver->resolve(array(
611 'foo' => 'custom',
612 'bam' => 'boo',
613 )));
614 }
615
616 public function testResolveWithoutOptionSucceedsIfRequiredAndDefaultValue()
617 {
618 $this->resolver->setRequired(array(
619 'foo',
620 ));
621 $this->resolver->setDefaults(array(
622 'foo' => 'bar',
623 ));
624
625 $this->assertEquals(array(
626 'foo' => 'bar'
627 ), $this->resolver->resolve(array()));
628 }
629
630 public function testResolveWithoutOptionSucceedsIfDefaultValueAndRequired()
631 {
632 $this->resolver->setDefaults(array(
633 'foo' => 'bar',
634 ));
635 $this->resolver->setRequired(array(
636 'foo',
637 ));
638
639 $this->assertEquals(array(
640 'foo' => 'bar'
641 ), $this->resolver->resolve(array()));
642 }
643
644 public function testResolveSucceedsIfOptionRequiredAndValueAllowed()
645 {
646 $this->resolver->setRequired(array(
647 'one', 'two',
648 ));
649 $this->resolver->setAllowedValues(array(
650 'two' => array('2'),
651 ));
652
653 $options = array(
654 'one' => '1',
655 'two' => '2'
656 );
657
658 $this->assertEquals($options, $this->resolver->resolve($options));
659 }
660
661 public function testClone()
662 {
663 $this->resolver->setDefaults(array('one' => '1'));
664
665 $clone = clone $this->resolver;
666
667 // Changes after cloning don't affect each other
668 $this->resolver->setDefaults(array('two' => '2'));
669 $clone->setDefaults(array('three' => '3'));
670
671 $this->assertEquals(array(
672 'one' => '1',
673 'two' => '2',
674 ), $this->resolver->resolve());
675
676 $this->assertEquals(array(
677 'one' => '1',
678 'three' => '3',
679 ), $clone->resolve());
680 }
681 }