Skip to content

Commit 79ee1bc

Browse files
committed
[OptionsResolver] fix counting of options
and make sure the tests are compatible with phpunit strict mode
1 parent 47611ee commit 79ee1bc

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

OptionsResolver.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,8 @@ public function offsetGet($option)
932932
*
933933
* @return bool Whether the option is set
934934
*
935+
* @throws AccessException If accessing this method outside of {@link resolve()}
936+
*
935937
* @see \ArrayAccess::offsetExists()
936938
*/
937939
public function offsetExists($option)
@@ -964,15 +966,23 @@ public function offsetUnset($option)
964966
}
965967

966968
/**
967-
* {@inheritdoc}
969+
* Returns the number of set options.
970+
*
971+
* This may be only a subset of the defined options.
972+
*
973+
* @return int Number of options
974+
*
975+
* @throws AccessException If accessing this method outside of {@link resolve()}
976+
*
977+
* @see \Countable::count()
968978
*/
969979
public function count()
970980
{
971981
if (!$this->locked) {
972982
throw new AccessException('Counting is only supported within closures of lazy options and normalizers.');
973983
}
974984

975-
return count($this->resolved);
985+
return count($this->defaults);
976986
}
977987

978988
/**

Tests/OptionsResolver2Dot6Test.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ public function testResolveSucceedsIfRequiredOptionSet()
256256
$this->resolver->setRequired('foo');
257257
$this->resolver->setDefault('foo', 'bar');
258258

259-
$this->resolver->resolve();
259+
$this->assertNotEmpty($this->resolver->resolve());
260260
}
261261

262262
public function testResolveSucceedsIfRequiredOptionPassed()
263263
{
264264
$this->resolver->setRequired('foo');
265265

266-
$this->resolver->resolve(array('foo' => 'bar'));
266+
$this->assertNotEmpty($this->resolver->resolve(array('foo' => 'bar')));
267267
}
268268

269269
public function testIsRequired()
@@ -504,7 +504,7 @@ public function testResolveSucceedsIfValidType()
504504
$this->resolver->setDefault('foo', 'bar');
505505
$this->resolver->setAllowedTypes('foo', 'string');
506506

507-
$this->resolver->resolve();
507+
$this->assertNotEmpty($this->resolver->resolve());
508508
}
509509

510510
/**
@@ -523,7 +523,7 @@ public function testResolveSucceedsIfValidTypeMultiple()
523523
$this->resolver->setDefault('foo', true);
524524
$this->resolver->setAllowedTypes('foo', array('string', 'bool'));
525525

526-
$this->resolver->resolve();
526+
$this->assertNotEmpty($this->resolver->resolve());
527527
}
528528

529529
/**
@@ -542,7 +542,7 @@ public function testResolveSucceedsIfInstanceOfClass()
542542
$this->resolver->setDefault('foo', new \stdClass());
543543
$this->resolver->setAllowedTypes('foo', '\stdClass');
544544

545-
$this->resolver->resolve();
545+
$this->assertNotEmpty($this->resolver->resolve());
546546
}
547547

548548
////////////////////////////////////////////////////////////////////////////
@@ -587,7 +587,7 @@ public function testResolveSucceedsIfValidAddedType()
587587
$this->resolver->setDefault('foo', 'bar');
588588
$this->resolver->addAllowedTypes('foo', 'string');
589589

590-
$this->resolver->resolve();
590+
$this->assertNotEmpty($this->resolver->resolve());
591591
}
592592

593593
/**
@@ -606,7 +606,7 @@ public function testResolveSucceedsIfValidAddedTypeMultiple()
606606
$this->resolver->setDefault('foo', 'bar');
607607
$this->resolver->addAllowedTypes('foo', array('string', 'bool'));
608608

609-
$this->resolver->resolve();
609+
$this->assertNotEmpty($this->resolver->resolve());
610610
}
611611

612612
public function testAddAllowedTypesDoesNotOverwrite()
@@ -617,7 +617,7 @@ public function testAddAllowedTypesDoesNotOverwrite()
617617

618618
$this->resolver->setDefault('foo', 'bar');
619619

620-
$this->resolver->resolve();
620+
$this->assertNotEmpty($this->resolver->resolve());
621621
}
622622

623623
public function testAddAllowedTypesDoesNotOverwrite2()
@@ -628,7 +628,7 @@ public function testAddAllowedTypesDoesNotOverwrite2()
628628

629629
$this->resolver->setDefault('foo', false);
630630

631-
$this->resolver->resolve();
631+
$this->assertNotEmpty($this->resolver->resolve());
632632
}
633633

634634
////////////////////////////////////////////////////////////////////////////
@@ -1078,7 +1078,7 @@ public function testNormalizerNotCalledForUnsetOptions()
10781078
\PHPUnit_Framework_Assert::fail('Should not be called.');
10791079
});
10801080

1081-
$this->resolver->resolve();
1081+
$this->assertEmpty($this->resolver->resolve());
10821082
}
10831083

10841084
////////////////////////////////////////////////////////////////////////////
@@ -1208,7 +1208,7 @@ public function testFailIfRemoveFromLazyOption()
12081208

12091209
public function testRemoveUnknownOptionIgnored()
12101210
{
1211-
$this->resolver->remove('foo');
1211+
$this->assertNotNull($this->resolver->remove('foo'));
12121212
}
12131213

12141214
////////////////////////////////////////////////////////////////////////////
@@ -1427,8 +1427,10 @@ public function testCount()
14271427
$this->resolver->setDefault('lazy1', function () {});
14281428

14291429
$this->resolver->setDefault('lazy2', function (Options $options) {
1430-
\PHPUnit_Framework_Assert::assertCount(3, $options);
1430+
\PHPUnit_Framework_Assert::assertCount(4, $options);
14311431
});
1432+
1433+
$this->assertCount(4, $this->resolver->resolve(array('required' => 'value')));
14321434
}
14331435

14341436
/**

0 commit comments

Comments
 (0)