aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php')
-rw-r--r--vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php529
1 files changed, 529 insertions, 0 deletions
diff --git a/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php
new file mode 100644
index 00000000..e24a7647
--- /dev/null
+++ b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php
@@ -0,0 +1,529 @@
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
12namespace Symfony\Component\OptionsResolver\Tests;
13
14use Symfony\Component\OptionsResolver\Options;
15
16class OptionsTest extends \PHPUnit_Framework_TestCase
17{
18 /**
19 * @var Options
20 */
21 private $options;
22
23 protected function setUp()
24 {
25 $this->options = new Options();
26 }
27
28 public function testArrayAccess()
29 {
30 $this->assertFalse(isset($this->options['foo']));
31 $this->assertFalse(isset($this->options['bar']));
32
33 $this->options['foo'] = 0;
34 $this->options['bar'] = 1;
35
36 $this->assertTrue(isset($this->options['foo']));
37 $this->assertTrue(isset($this->options['bar']));
38
39 unset($this->options['bar']);
40
41 $this->assertTrue(isset($this->options['foo']));
42 $this->assertFalse(isset($this->options['bar']));
43 $this->assertEquals(0, $this->options['foo']);
44 }
45
46 public function testCountable()
47 {
48 $this->options->set('foo', 0);
49 $this->options->set('bar', 1);
50
51 $this->assertCount(2, $this->options);
52 }
53
54 /**
55 * @expectedException \OutOfBoundsException
56 */
57 public function testGetNonExisting()
58 {
59 $this->options->get('foo');
60 }
61
62 /**
63 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
64 */
65 public function testSetNotSupportedAfterGet()
66 {
67 $this->options->set('foo', 'bar');
68 $this->options->get('foo');
69 $this->options->set('foo', 'baz');
70 }
71
72 /**
73 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
74 */
75 public function testRemoveNotSupportedAfterGet()
76 {
77 $this->options->set('foo', 'bar');
78 $this->options->get('foo');
79 $this->options->remove('foo');
80 }
81
82 /**
83 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
84 */
85 public function testSetNormalizerNotSupportedAfterGet()
86 {
87 $this->options->set('foo', 'bar');
88 $this->options->get('foo');
89 $this->options->setNormalizer('foo', function () {});
90 }
91
92 public function testSetLazyOption()
93 {
94 $test = $this;
95
96 $this->options->set('foo', function (Options $options) use ($test) {
97 return 'dynamic';
98 });
99
100 $this->assertEquals('dynamic', $this->options->get('foo'));
101 }
102
103 public function testSetDiscardsPreviousValue()
104 {
105 $test = $this;
106
107 // defined by superclass
108 $this->options->set('foo', 'bar');
109
110 // defined by subclass
111 $this->options->set('foo', function (Options $options, $previousValue) use ($test) {
112 /* @var \PHPUnit_Framework_TestCase $test */
113 $test->assertNull($previousValue);
114
115 return 'dynamic';
116 });
117
118 $this->assertEquals('dynamic', $this->options->get('foo'));
119 }
120
121 public function testOverloadKeepsPreviousValue()
122 {
123 $test = $this;
124
125 // defined by superclass
126 $this->options->set('foo', 'bar');
127
128 // defined by subclass
129 $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
130 /* @var \PHPUnit_Framework_TestCase $test */
131 $test->assertEquals('bar', $previousValue);
132
133 return 'dynamic';
134 });
135
136 $this->assertEquals('dynamic', $this->options->get('foo'));
137 }
138
139 public function testPreviousValueIsEvaluatedIfLazy()
140 {
141 $test = $this;
142
143 // defined by superclass
144 $this->options->set('foo', function (Options $options) {
145 return 'bar';
146 });
147
148 // defined by subclass
149 $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
150 /* @var \PHPUnit_Framework_TestCase $test */
151 $test->assertEquals('bar', $previousValue);
152
153 return 'dynamic';
154 });
155
156 $this->assertEquals('dynamic', $this->options->get('foo'));
157 }
158
159 public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
160 {
161 $test = $this;
162
163 // defined by superclass
164 $this->options->set('foo', function (Options $options) use ($test) {
165 $test->fail('Should not be called');
166 });
167
168 // defined by subclass, no $previousValue argument defined!
169 $this->options->overload('foo', function (Options $options) use ($test) {
170 return 'dynamic';
171 });
172
173 $this->assertEquals('dynamic', $this->options->get('foo'));
174 }
175
176 public function testLazyOptionCanAccessOtherOptions()
177 {
178 $test = $this;
179
180 $this->options->set('foo', 'bar');
181
182 $this->options->set('bam', function (Options $options) use ($test) {
183 /* @var \PHPUnit_Framework_TestCase $test */
184 $test->assertEquals('bar', $options->get('foo'));
185
186 return 'dynamic';
187 });
188
189 $this->assertEquals('bar', $this->options->get('foo'));
190 $this->assertEquals('dynamic', $this->options->get('bam'));
191 }
192
193 public function testLazyOptionCanAccessOtherLazyOptions()
194 {
195 $test = $this;
196
197 $this->options->set('foo', function (Options $options) {
198 return 'bar';
199 });
200
201 $this->options->set('bam', function (Options $options) use ($test) {
202 /* @var \PHPUnit_Framework_TestCase $test */
203 $test->assertEquals('bar', $options->get('foo'));
204
205 return 'dynamic';
206 });
207
208 $this->assertEquals('bar', $this->options->get('foo'));
209 $this->assertEquals('dynamic', $this->options->get('bam'));
210 }
211
212 public function testNormalizer()
213 {
214 $this->options->set('foo', 'bar');
215
216 $this->options->setNormalizer('foo', function () {
217 return 'normalized';
218 });
219
220 $this->assertEquals('normalized', $this->options->get('foo'));
221 }
222
223 public function testNormalizerReceivesUnnormalizedValue()
224 {
225 $this->options->set('foo', 'bar');
226
227 $this->options->setNormalizer('foo', function (Options $options, $value) {
228 return 'normalized['.$value.']';
229 });
230
231 $this->assertEquals('normalized[bar]', $this->options->get('foo'));
232 }
233
234 public function testNormalizerCanAccessOtherOptions()
235 {
236 $test = $this;
237
238 $this->options->set('foo', 'bar');
239 $this->options->set('bam', 'baz');
240
241 $this->options->setNormalizer('bam', function (Options $options) use ($test) {
242 /* @var \PHPUnit_Framework_TestCase $test */
243 $test->assertEquals('bar', $options->get('foo'));
244
245 return 'normalized';
246 });
247
248 $this->assertEquals('bar', $this->options->get('foo'));
249 $this->assertEquals('normalized', $this->options->get('bam'));
250 }
251
252 public function testNormalizerCanAccessOtherLazyOptions()
253 {
254 $test = $this;
255
256 $this->options->set('foo', function (Options $options) {
257 return 'bar';
258 });
259 $this->options->set('bam', 'baz');
260
261 $this->options->setNormalizer('bam', function (Options $options) use ($test) {
262 /* @var \PHPUnit_Framework_TestCase $test */
263 $test->assertEquals('bar', $options->get('foo'));
264
265 return 'normalized';
266 });
267
268 $this->assertEquals('bar', $this->options->get('foo'));
269 $this->assertEquals('normalized', $this->options->get('bam'));
270 }
271
272 /**
273 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
274 */
275 public function testFailForCyclicDependencies()
276 {
277 $this->options->set('foo', function (Options $options) {
278 $options->get('bam');
279 });
280
281 $this->options->set('bam', function (Options $options) {
282 $options->get('foo');
283 });
284
285 $this->options->get('foo');
286 }
287
288 /**
289 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
290 */
291 public function testFailForCyclicDependenciesBetweenNormalizers()
292 {
293 $this->options->set('foo', 'bar');
294 $this->options->set('bam', 'baz');
295
296 $this->options->setNormalizer('foo', function (Options $options) {
297 $options->get('bam');
298 });
299
300 $this->options->setNormalizer('bam', function (Options $options) {
301 $options->get('foo');
302 });
303
304 $this->options->get('foo');
305 }
306
307 /**
308 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
309 */
310 public function testFailForCyclicDependenciesBetweenNormalizerAndLazyOption()
311 {
312 $this->options->set('foo', function (Options $options) {
313 $options->get('bam');
314 });
315 $this->options->set('bam', 'baz');
316
317 $this->options->setNormalizer('bam', function (Options $options) {
318 $options->get('foo');
319 });
320
321 $this->options->get('foo');
322 }
323
324 public function testAllInvokesEachLazyOptionOnlyOnce()
325 {
326 $test = $this;
327 $i = 1;
328
329 $this->options->set('foo', function (Options $options) use ($test, &$i) {
330 $test->assertSame(1, $i);
331 ++$i;
332
333 // Implicitly invoke lazy option for "bam"
334 $options->get('bam');
335 });
336 $this->options->set('bam', function (Options $options) use ($test, &$i) {
337 $test->assertSame(2, $i);
338 ++$i;
339 });
340
341 $this->options->all();
342 }
343
344 public function testAllInvokesEachNormalizerOnlyOnce()
345 {
346 $test = $this;
347 $i = 1;
348
349 $this->options->set('foo', 'bar');
350 $this->options->set('bam', 'baz');
351
352 $this->options->setNormalizer('foo', function (Options $options) use ($test, &$i) {
353 $test->assertSame(1, $i);
354 ++$i;
355
356 // Implicitly invoke normalizer for "bam"
357 $options->get('bam');
358 });
359 $this->options->setNormalizer('bam', function (Options $options) use ($test, &$i) {
360 $test->assertSame(2, $i);
361 ++$i;
362 });
363
364 $this->options->all();
365 }
366
367 public function testReplaceClearsAndSets()
368 {
369 $this->options->set('one', '1');
370
371 $this->options->replace(array(
372 'two' => '2',
373 'three' => function (Options $options) {
374 return '2' === $options['two'] ? '3' : 'foo';
375 }
376 ));
377
378 $this->assertEquals(array(
379 'two' => '2',
380 'three' => '3',
381 ), $this->options->all());
382 }
383
384 public function testClearRemovesAllOptions()
385 {
386 $this->options->set('one', 1);
387 $this->options->set('two', 2);
388
389 $this->options->clear();
390
391 $this->assertEmpty($this->options->all());
392
393 }
394
395 /**
396 * @covers Symfony\Component\OptionsResolver\Options::replace
397 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
398 */
399 public function testCannotReplaceAfterOptionWasRead()
400 {
401 $this->options->set('one', 1);
402 $this->options->all();
403
404 $this->options->replace(array(
405 'two' => '2',
406 ));
407 }
408
409 /**
410 * @covers Symfony\Component\OptionsResolver\Options::overload
411 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
412 */
413 public function testCannotOverloadAfterOptionWasRead()
414 {
415 $this->options->set('one', 1);
416 $this->options->all();
417
418 $this->options->overload('one', 2);
419 }
420
421 /**
422 * @covers Symfony\Component\OptionsResolver\Options::clear
423 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
424 */
425 public function testCannotClearAfterOptionWasRead()
426 {
427 $this->options->set('one', 1);
428 $this->options->all();
429
430 $this->options->clear();
431 }
432
433 public function testOverloadCannotBeEvaluatedLazilyWithoutExpectedClosureParams()
434 {
435 $this->options->set('foo', 'bar');
436
437 $this->options->overload('foo', function () {
438 return 'test';
439 });
440
441 $this->assertNotEquals('test', $this->options->get('foo'));
442 $this->assertTrue(is_callable($this->options->get('foo')));
443 }
444
445 public function testOverloadCannotBeEvaluatedLazilyWithoutFirstParamTypeHint()
446 {
447 $this->options->set('foo', 'bar');
448
449 $this->options->overload('foo', function ($object) {
450 return 'test';
451 });
452
453 $this->assertNotEquals('test', $this->options->get('foo'));
454 $this->assertTrue(is_callable($this->options->get('foo')));
455 }
456
457 public function testOptionsIteration()
458 {
459 $this->options->set('foo', 'bar');
460 $this->options->set('foo1', 'bar1');
461 $expectedResult = array('foo' => 'bar', 'foo1' => 'bar1');
462
463 $this->assertEquals($expectedResult, iterator_to_array($this->options, true));
464 }
465
466 public function testHasWithNullValue()
467 {
468 $this->options->set('foo', null);
469
470 $this->assertTrue($this->options->has('foo'));
471 }
472
473 public function testRemoveOptionAndNormalizer()
474 {
475 $this->options->set('foo1', 'bar');
476 $this->options->setNormalizer('foo1', function (Options $options) {
477 return '';
478 });
479 $this->options->set('foo2', 'bar');
480 $this->options->setNormalizer('foo2', function (Options $options) {
481 return '';
482 });
483
484 $this->options->remove('foo2');
485 $this->assertEquals(array('foo1' => ''), $this->options->all());
486 }
487
488 public function testReplaceOptionAndNormalizer()
489 {
490 $this->options->set('foo1', 'bar');
491 $this->options->setNormalizer('foo1', function (Options $options) {
492 return '';
493 });
494 $this->options->set('foo2', 'bar');
495 $this->options->setNormalizer('foo2', function (Options $options) {
496 return '';
497 });
498
499 $this->options->replace(array('foo1' => 'new'));
500 $this->assertEquals(array('foo1' => 'new'), $this->options->all());
501 }
502
503 public function testClearOptionAndNormalizer()
504 {
505 $this->options->set('foo1', 'bar');
506 $this->options->setNormalizer('foo1', function (Options $options) {
507 return '';
508 });
509 $this->options->set('foo2', 'bar');
510 $this->options->setNormalizer('foo2', function (Options $options) {
511 return '';
512 });
513
514 $this->options->clear();
515 $this->assertEmpty($this->options->all());
516 }
517
518 public function testNormalizerWithoutCorrespondingOption()
519 {
520 $test = $this;
521
522 $this->options->setNormalizer('foo', function (Options $options, $previousValue) use ($test) {
523 $test->assertNull($previousValue);
524
525 return '';
526 });
527 $this->assertEquals(array('foo' => ''), $this->options->all());
528 }
529}