Adding documentation to show result of Set::reverse() with non-objects. Fixing commented out test for Set class and adding more tests.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6682 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mariano.iglesias 2008-04-17 22:01:59 +00:00
parent 2262e5803c
commit 9af317b1b4
2 changed files with 65 additions and 70 deletions

View file

@ -979,9 +979,10 @@ class Set extends Object {
return array_combine($keys, $vals);
}
/**
* Converts an object into an array
* Converts an object into an array. If $object is no object, reverse
* will return the same value.
*
* @param object $object
* @param object $object Object to reverse
* @return array
*/
function reverse($object) {
@ -1029,8 +1030,7 @@ class Set extends Object {
}
}
}
} else {
if (is_object($object)) {
} else if (is_object($object)) {
$keys = get_object_vars($object);
if (isset($keys['_name_'])) {
$identity = $keys['_name_'];
@ -1056,8 +1056,6 @@ class Set extends Object {
} else {
$out = $object;
}
}
return $out;
}
/**

View file

@ -35,7 +35,6 @@ App::import('Core', 'Set');
* @subpackage cake.tests.cases.libs
*/
class SetTest extends UnitTestCase {
function testNumericKeyExtraction() {
$data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
$this->assertIdentical(Set::extract($data, '{n}'), array(1, 'whatever'));
@ -182,7 +181,6 @@ class SetTest extends UnitTestCase {
}
function testSort() {
// ascending
$a = array(
0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
@ -195,7 +193,6 @@ class SetTest extends UnitTestCase {
$a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
$this->assertIdentical($a, $b);
// descending
$b = array(
0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
@ -208,7 +205,6 @@ class SetTest extends UnitTestCase {
$a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
$this->assertIdentical($a, $b);
// ascending (with different key)
$a = array(
0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
@ -222,8 +218,6 @@ class SetTest extends UnitTestCase {
$a = Set::sort($a, '{n}.Person.name', 'asc');
$this->assertIdentical($a, $b);
// given a path to an array as the sort path, sort by lowest or highest value in array
$a = array(
array(7,6,4),
array(3,4,5),
@ -239,8 +233,6 @@ class SetTest extends UnitTestCase {
$a = Set::sort($a, '{n}.{n}', 'asc');
$this->assertIdentical($a, $b);
// as per above, but nested arrays
$a = array(
array(7,6,4),
array(3,4,5),
@ -256,8 +248,6 @@ class SetTest extends UnitTestCase {
$a = Set::sort($a, '{n}', 'asc');
$this->assertIdentical($a, $b);
// if every element doesn't have the matching key, the one without is compared as empty
$a = array(
0 => array('Person' => array('name' => 'Jeff')),
1 => array('Shirt' => array('color' => 'black'))
@ -808,6 +798,12 @@ class SetTest extends UnitTestCase {
}
function testMapReverse() {
$result = Set::reverse(null);
$this->assertEqual($result, null);
$result = Set::reverse(false);
$this->assertEqual($result, false);
$expected = array(
'Array1' => array(
'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
@ -830,7 +826,6 @@ class SetTest extends UnitTestCase {
$result = Set::reverse($map);
$this->assertIdentical($result, $expected);
$expected = array(
'Post' => array('id'=> 1, 'title' => 'First Post'),
'Comment' => array(
@ -1003,10 +998,11 @@ class SetTest extends UnitTestCase {
uses('model'.DS.'model');
$model = new Model(array('id' => false, 'name' => 'Model', 'table' => false));
$expected = array(
'Behaviors' => array('modelName' => 'Model', '_attached' => array(), '_disabled' => array(), '__methods' => array(), '__mappedMethods' => array(), '_log' => null),
'useDbConfig' => 'default', 'useTable' => false, 'displayField' => null, 'id' => false, 'data' => array(), 'table' => false, 'primaryKey' => 'id', '_schema' => null, 'validate' => array(),
'validationErrors' => array(), 'tablePrefix' => null, 'name' => 'Model', 'alias' => 'Model', 'tableToModel' => array(), 'logTransactions' => false, 'transactional' => false, 'cacheQueries' => false,
'belongsTo' => array(), 'hasOne' => array(), 'hasMany' => array(), 'hasAndBelongsToMany' => array(), 'actsAs' => null, 'behaviors' => array(), 'whitelist' => array(), 'cacheSources' => true,
'findQueryType' => null, '__behaviorMethods' => array(), 'recursive' => 1, 'order' => null, '__exists' => null,
'belongsTo' => array(), 'hasOne' => array(), 'hasMany' => array(), 'hasAndBelongsToMany' => array(), 'actsAs' => null, 'whitelist' => array(), 'cacheSources' => true,
'findQueryType' => null, 'recursive' => 1, 'order' => null, '__exists' => null,
'__associationKeys' => array(
'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'),
'hasOne' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'dependent'),
@ -1014,11 +1010,12 @@ class SetTest extends UnitTestCase {
'hasAndBelongsToMany' => array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery')),
'__associations' => array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'), '__backAssociation' => array(), '__insertID' => null, '__numRows' => null, '__affectedRows' => null,
'__findMethods' => array('all' => true, 'first' => true, 'count' => true, 'neighbors' => true, 'list' => true), '_log' => null);
//$result = Set::reverse($model);
$result = Set::reverse($model);
//ksort($result);
//ksort($expected);
//$this->assertIdentical($result, $expected);
ksort($result);
ksort($expected);
$this->assertIdentical($result, $expected);
$class = new stdClass;
$class->User = new stdClass;