Skip to content

Commit

Permalink
Add tests for issue #104
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 27, 2012
1 parent ff5e72c commit 8cadac3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/Cake/Test/Case/Utility/Set2Test.php
Expand Up @@ -833,6 +833,45 @@ public function testExtractAttributePattern() {
$this->assertEquals($expected, $result);
}

/**
* Test that uneven keys are handled correctly.
*
* @return void
*/
public function testExtractUnevenKeys() {
$data = array(
'Level1' => array(
'Level2' => array('test1', 'test2'),
'Level2bis' => array('test3', 'test4')
)
);
$this->assertEquals(
array('test1', 'test2'),
Set2::extract($data, 'Level1.Level2')
);
$this->assertEquals(
array('test3', 'test4'),
Set2::extract($data, 'Level1.Level2bis')
);

$data = array(
'Level1' => array(
'Level2bis' => array(
array('test3', 'test4'),
array('test5', 'test6')
)
)
);
$expected = array(
array('test3', 'test4'),
array('test5', 'test6')
);
$this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis'));

$data['Level1']['Level2'] = array('test1', 'test2');
$this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis'));
}

/**
* testSort method
*
Expand Down
8 changes: 8 additions & 0 deletions lib/Cake/Utility/Set2.php
Expand Up @@ -376,6 +376,14 @@ public static function combine(array $data, $keyPath, $valuePath = null, $groupP
/**
* Returns a formated series of values extracted from `$data`, using
* `$format` as the format and `$paths` as the values to extract.
*
* Usage:
*
* {{{
* $result = Set::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s');
* }}}
*
* The `$format` string can use any format options that `vsprintf()` and `sprintf()` do.
*
* @param array $data Source array from which to extract the data
* @param string $paths An array containing one or more Set2::extract()-style key paths
Expand Down

0 comments on commit 8cadac3

Please sign in to comment.