Skip to content

Commit

Permalink
pass the path to the recurse method
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Aug 31, 2013
1 parent 2a38544 commit 5dbc3f6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Controller/Crud/Listener/ApiTransformationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function _recurse(&$variable, $key = null) {
}

foreach ($variable as $k => &$value) {
$this->_recurse($value, $key ? "$key.$k" : $k);
$this->_recurse($value, $key === null ? $k : "$key.$k");
}

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,91 @@ public function testRecurseKeySpecificTransform() {
$this->assertSame($expected, $data);
}

public function testRecurseKeySpecificNestedTransform() {
$listener = $this
->getMockBuilder('ApiTransformationListener')
->disableOriginalConstructor()
->getMock();

$callback = function($variable, $key) {
if (preg_match('@^\d+.User\.changeme$@', $key)) {
return 'changed';
}
return $variable;
};

$settings = array(
'changeNesting' => true,
'changeKeys' => false,
'changeTime' => false,
'castNumbers' => false,
'keyMethods' => array(),
'valueMethods' => array($callback),
'replaceMap' => array()
);

$this->setReflectionClassInstance($listener);
$this->setProtectedProperty('_settings', $settings, $listener);

$data = array(
'changeme' => 'no change',
array(
'changeme' => 'no change',
'User' => array(
'id' => '5',
'name' => 'FriendsOfCake',
'changeme' => 'old'
),
'Profile' => array(
'id' => '987',
'twitter' => '@FriendsOfCake'
)
),
array(
'User' => array(
'id' => '6',
'name' => 'FriendsOfCake',
'changeme' => 'old two'
),
'Profile' => array(
'id' => '987',
'twitter' => '@FriendsOfCake'
)
)
);

$expected = array(
'changeme' => 'no change',
array(
'changeme' => 'no change',
'User' => array(
'id' => '5',
'name' => 'FriendsOfCake',
'changeme' => 'changed'
),
'Profile' => array(
'id' => '987',
'twitter' => '@FriendsOfCake'
)
),
array(
'User' => array(
'id' => '6',
'name' => 'FriendsOfCake',
'changeme' => 'changed'
),
'Profile' => array(
'id' => '987',
'twitter' => '@FriendsOfCake'
)
)
);

$this->callProtectedMethod('_recurse', array(&$data), $listener);

$this->assertSame($expected, $data);
}

/**
* testGetReplaceMapFromAssociationsEndlessLoopPrevention
*
Expand Down

0 comments on commit 5dbc3f6

Please sign in to comment.