Skip to content

Commit

Permalink
Fix: Optional method should be called inside closure so that each gen…
Browse files Browse the repository at this point in the history
…erated value is optional
  • Loading branch information
yakobe authored and willdurand committed May 16, 2014
1 parent 09a6a05 commit 5e6ad9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Factory/FormatterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ public static function createClosure($generator, $method, array $parameters = ar
$generator = $generator->unique();
}

if (null !== $optional && $generator instanceof Generator) {
$generator = $generator->optional((double) $optional);
}
return function () use ($generator, $method, $parameters, $optional) {

if (0 === count($parameters)) {
return function () use ($generator, $method) { return $generator->$method(); };
}
if (null !== $optional && $generator instanceof Generator) {
$generator = $generator->optional((double)$optional);
}

return function () use ($generator, $method, $parameters) { return call_user_func_array(array($generator, $method), (array) $parameters); };
return call_user_func_array(array($generator, $method), (array)$parameters);
};
}
}
22 changes: 22 additions & 0 deletions Tests/Factory/FormatterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,35 @@ public function testCreateClosureWithoutParameters()
->expects($this->once())
->method('foo')
;
$generator
->expects($this->never())
->method('optional')
;

$closure = FormatterFactory::createClosure($generator, 'foo');

$this->assertTrue(is_callable($closure));
$closure();
}

public function testCreateClosureWithOptional()
{
$generator = $this->getMock('Faker\Generator', array('foo','optional'));
$generator
->expects($this->once())
->method('foo')
;
$generator
->expects($this->once())
->method('optional')
->willReturn($generator)
;
$closure = FormatterFactory::createClosure($generator, 'foo', array(), null, 0.1);

$this->assertTrue(is_callable($closure));
$closure();
}

public function withParameterProvider()
{
return array(
Expand Down

0 comments on commit 5e6ad9c

Please # to comment.