Skip to content

Commit

Permalink
Merge pull request nelmio#76 from baldurrensch/custom_setter_fix
Browse files Browse the repository at this point in the history
Fix issue when using custom setter
  • Loading branch information
Seldaek committed Dec 27, 2013
2 parents dc5c9f9 + e033cdc commit e3f5a1c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ private function populateObject($instance, $class, $name, $data)
}
} elseif (isset($customSetter)) {
$instance->$customSetter($key, $generatedVal);
$variables[$key] = $generatedVal;
} elseif (is_array($generatedVal) && method_exists($instance, $key)) {
foreach ($generatedVal as $num => $param) {
$generatedVal[$num] = $this->checkTypeHints($instance, $key, $param, $num);
Expand Down
24 changes: 20 additions & 4 deletions tests/Nelmio/Alice/Loader/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase
const CONTACT = 'Nelmio\Alice\fixtures\Contact';

protected $orm;

/**
* @var \Nelmio\Alice\Loader\Base
*/
protected $loader;

protected function loadData(array $data, array $options = array())
Expand Down Expand Up @@ -913,20 +917,27 @@ public function testCurrentInConstructor()

public function testCustomSetFunction()
{
$this->loadData(
$loader = $this->createLoader(
array(
'providers' => array(new FakerProvider())
)
);
$loader->load(
array(
self::USER => array(
'user' => array(
'username' => 'foo',
'fullname' => 'foo bar',
'__set' => 'customSetter'
'__set' => 'customSetter',
'test_variable' => '<noop($username)>',
)
)
)
);

$this->assertEquals('foo set by custom setter', $this->loader->getReference('user')->username);
$this->assertEquals('foo bar set by custom setter', $this->loader->getReference('user')->fullname);
$this->assertEquals('foo set by custom setter', $loader->getReference('user')->username);
$this->assertEquals('foo bar set by custom setter', $loader->getReference('user')->fullname);
$this->assertEquals('foo set by custom setter', $loader->getReference('user')->test_variable);
}

/**
Expand Down Expand Up @@ -960,4 +971,9 @@ public function randomNumber()
{
return mt_rand(0, 9);
}

public function noop($str)
{
return $str;
}
}

0 comments on commit e3f5a1c

Please sign in to comment.