From b867f832c1b85f609c424cab8cf22e6316ce0983 Mon Sep 17 00:00:00 2001 From: phpnut Date: Sun, 11 Nov 2007 23:49:30 +0000 Subject: [PATCH] Refactoring return of Set::map(); and Set::reverse(); Added new test cases to SetTest. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5979 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/set.php | 85 +++-- cake/tests/cases/libs/set.test.php | 582 ++++++++++++----------------- 2 files changed, 309 insertions(+), 358 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 464b69c3c..aea4b3a5c 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -223,30 +223,32 @@ class Set extends Object { } } else { $ret = new $class; - if ($identity != null) { - $ret->__identity__ = $identity; - } } if (empty($value)) { return $ret; } - $keys = array_keys($value); - foreach ($value as $key => $val) { - if (!is_numeric($key) && strlen($key) > 1) { - if ($key{0} == strtoupper($key{0}) && $key{1} == strtolower($key{1}) && (is_array($val) || is_object($val))) { - if ($key == $keys[0]) { - $ret = Set::__map($val, $class, $key); + + if (!is_null($identity)) { + $key = $identity; + } + $count = count($keys); + + for ($i = 0; $i < $count; $i++) { + if (is_array($value[$keys[$i]])) { + foreach ($value[$keys[$i]] as $key => $val) { + if (is_array($val) || is_object($val)) { + $val = Set::__map($value[$keys[$i]], $class, $key); + $ret->{$keys[$i]} = $val; } else { - $ret->{$key} = Set::__map($val, $class, $key); + $ret->{$keys[$i]}->{$key} = $val; } - } else { - $ret->{$key} = $val; } + } else { + $ret->{$keys[$i]} = $value[$keys[$i]]; } } - return $ret; } /** @@ -695,6 +697,7 @@ class Set extends Object { $path2 = $path1; $path1 = $data; $data = $this->get(); + } elseif (is_a($this, 'set') && is_string($data) && empty($path2)) { $path2 = $path1; $path1 = $data; @@ -715,8 +718,10 @@ class Set extends Object { if (!empty($path2) && is_array($path2)) { $format = array_shift($path2); $vals = Set::format($data, $format, $path2); + } elseif (!empty($path2)) { $vals = Set::extract($data, $path2); + } else { $count = count($keys); for ($i = 0; $i < $count; $i++) { @@ -761,19 +766,16 @@ class Set extends Object { return null; } } - } elseif (is_a($object, 'stdclass') || is_a($object, 'stdClass')) { + } else { $object = get_object_vars($object); $keys = array_keys($object); $count = count($keys); for ($i = 0; $i < $count; $i++) { - if ($keys[$i] == '__identity__') { - $key = $object[$keys[$i]]; - unset($object[$keys[$i]]); - $object[$key] = $object; - } elseif (is_array($object[$keys[$i]])) { + if (is_array($object[$keys[$i]])) { $keys1 = array_keys($object[$keys[$i]]); $count1 = count($keys1); + for ($ii = 0; $ii < $count1; $ii++) { if (is_object($object[$keys[$i]][$keys1[$ii]])) { $merge[$keys[$i]][$keys1[$ii]] = Set::reverse($object[$keys[$i]][$keys1[$ii]]); @@ -782,13 +784,50 @@ class Set extends Object { } } unset($object[$keys[$i]]); + } elseif (is_object($object[$keys[$i]])) { - $merge[$keys[$i]] = Set::reverse($object[$keys[$i]]); - unset($object[$keys[$i]]); + if (!isset($key)) { + $key = $keys[0]; + } + + if (isset($merge[$key][$keys[$i]]) && is_string($merge[$key][$keys[$i]])) { + $merge[$keys[$i]] = Set::reverse($object[$keys[$i]]); + continue; + } else { + $merge[$key][$keys[$i]] = Set::reverse($object[$keys[$i]]); + } + $check = array_keys($merge); + + if($key === $check[0] && !is_numeric($key) && (count($object) === 1 || empty($object)) ) { + $merge = array_shift($merge); + } elseif (count($merge[$key]) > 1 && $key === $check[0] && is_numeric($key)) { + $merge = array_shift($merge); + } elseif ($key === $check[0] && !is_numeric($key)) { + $merge = array_shift($merge); + } + + } elseif (!isset($key) && is_string($object[$keys[$i]]) && $keys[$i] === $keys[0]) { + $merge[$keys[$i]] = $object; + $check = array_keys($object); + $countCheck = count($check); + $string = true; + + if($countCheck > 1) { + for ($ii = 0; $ii < $countCheck; $ii++) { + if(is_a($merge[$keys[$i]][$check[$ii]], 'stdclass')) { + $string = false; + } + } + if($string) { + $merge = array_shift($merge); + } + } + + } elseif (isset($key)) { + $merge[$keys[$i]] = $object[$keys[$i]]; } } } - $return = $object; if (!empty($merge)) { $mergeKeys = array_keys($merge); @@ -823,8 +862,8 @@ class Set extends Object { } $object = Set::pushDiff($change, $merge); } - return $object; } + return $object; } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 15eb16ab6..09b739a2e 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -45,99 +45,40 @@ class SetTest extends UnitTestCase { } function testMerge() { - // Test that passing in just 1 array returns it "as-is" $r = Set::merge(array('foo')); $this->assertIdentical($r, array('foo')); - // Test that passing in a non-array turns it into one $r = Set::merge('foo'); $this->assertIdentical($r, array('foo')); - // Test that this works for 2 strings as well $r = Set::merge('foo', 'bar'); $this->assertIdentical($r, array('foo', 'bar')); - // Test that this works for arguments of mixed types as well $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar'); $this->assertIdentical($r, array('foo', 'user' => 'bob', 'no-bar', 'bar')); - // Test merging two simple numerical indexed arrays $a = array('foo', 'foo2'); $b = array('bar', 'bar2'); $this->assertIdentical(Set::merge($a, $b), array('foo', 'foo2', 'bar', 'bar2')); - // Test merging two simple associative arrays $a = array('foo' => 'bar', 'bar' => 'foo'); $b = array('foo' => 'no-bar', 'bar' => 'no-foo'); $this->assertIdentical(Set::merge($a, $b), array('foo' => 'no-bar', 'bar' => 'no-foo')); - // Test merging two simple nested arrays $a = array('users' => array('bob', 'jim')); $b = array('users' => array('lisa', 'tina')); - $this->assertIdentical(Set::merge($a, $b), array( - 'users' => array('bob', 'jim', 'lisa', 'tina') - )); + $this->assertIdentical(Set::merge($a, $b), array('users' => array('bob', 'jim', 'lisa', 'tina'))); - // Test that merging an key holding a string over an array one causes an overwrite $a = array('users' => array('jim', 'bob')); $b = array('users' => 'none'); $this->assertIdentical(Set::merge($a, $b), array('users' => 'none')); - // Test merging two somewhat complex nested arrays - $a = array( - 'users' => array( - 'lisa' => array( - 'id' => 5, - 'pw' => 'secret' - ) - ), - 'cakephp' - ); - $b = array( - 'users' => array( - 'lisa' => array( - 'pw' => 'new-pass', - 'age' => 23 - ) - ), - 'ice-cream' - ); - $this->assertIdentical(Set::merge($a, $b), array( - 'users' => array( - 'lisa' => array( - 'id' => 5, - 'pw' => 'new-pass', - 'age' => 23 - ) - ), - 'cakephp', - 'ice-cream' - )); + $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp'); + $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream'); + $this->assertIdentical(Set::merge($a, $b), array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream')); - // And now go for the ultimate tripple-play ; ) - $c = array( - 'users' => array( - 'lisa' => array( - 'pw' => 'you-will-never-guess', - 'age' => 25, - 'pet' => 'dog' - ) - ), - 'chocolate' - ); - $expected = array( - 'users' => array( - 'lisa' => array( - 'id' => 5, - 'pw' => 'you-will-never-guess', - 'age' => 25, - 'pet' => 'dog' - ) - ), - 'cakephp', - 'ice-cream', - 'chocolate' - ); + $c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate'); + $expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate'); $this->assertIdentical(Set::merge($a, $b, $c), $expected); $this->assertIdentical(Set::merge($a, $b, array(), $c), $expected); @@ -162,16 +103,9 @@ class SetTest extends UnitTestCase { function testExtract() { $a = array( - array('Article' => array( - 'id' => 1, 'title' => 'Article 1' - )), - array('Article' => array( - 'id' => 2, 'title' => 'Article 2' - )), - array('Article' => array( - 'id' => 3, 'title' => 'Article 3' - )) - ); + array('Article' => array('id' => 1, 'title' => 'Article 1')), + array('Article' => array('id' => 2, 'title' => 'Article 2')), + array('Article' => array('id' => 3, 'title' => 'Article 3'))); $result = Set::extract($a, '{n}.Article.id'); $expected = array( 1, 2, 3 ); @@ -182,77 +116,40 @@ class SetTest extends UnitTestCase { $this->assertIdentical($result, $expected); $a = array( - array('Article' => array( - 'id' => 1, 'title' => 'Article 1', - 'User' => array( - 'id' => 1, 'username' => 'mariano.iglesias' - ) - )), - array('Article' => array( - 'id' => 2, 'title' => 'Article 2', - 'User' => array( - 'id' => 1, 'username' => 'mariano.iglesias' - ) - )), - array('Article' => array( - 'id' => 3, 'title' => 'Article 3', - 'User' => array( - 'id' => 2, 'username' => 'phpnut' - ) - )) - ); + array('Article' => array('id' => 1, 'title' => 'Article 1', + 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))), + array('Article' => array('id' => 2, 'title' => 'Article 2', + 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))), + array('Article' => array('id' => 3, 'title' => 'Article 3', + 'User' => array('id' => 2, 'username' => 'phpnut')))); $result = Set::extract($a, '{n}.Article.User.username'); $expected = array( 'mariano.iglesias', 'mariano.iglesias', 'phpnut' ); $this->assertIdentical($result, $expected); $a = array( - array('Article' => array( - 'id' => 1, 'title' => 'Article 1', + array('Article' => array('id' => 1, 'title' => 'Article 1', 'Comment' => array( array('id' => 10, 'title' => 'Comment 10'), array('id' => 11, 'title' => 'Comment 11'), - array('id' => 12, 'title' => 'Comment 12'), - ) - )), - array('Article' => array( - 'id' => 2, 'title' => 'Article 2', + array('id' => 12, 'title' => 'Comment 12')))), + array('Article' => array('id' => 2, 'title' => 'Article 2', 'Comment' => array( array('id' => 13, 'title' => 'Comment 13'), - array('id' => 14, 'title' => 'Comment 14') - ) - )), - array('Article' => array( - 'id' => 3, 'title' => 'Article 3' - )) - ); + array('id' => 14, 'title' => 'Comment 14')))), + array('Article' => array('id' => 3, 'title' => 'Article 3'))); $result = Set::extract($a, '{n}.Article.Comment.{n}.id'); - $expected = array ( - array(10, 11, 12), - array(13, 14), - null - ); + $expected = array (array(10, 11, 12), array(13, 14), null); $this->assertIdentical($result, $expected); $result = Set::extract($a, '{n}.Article.Comment.{n}.title'); - $expected = array ( - array('Comment 10', 'Comment 11', 'Comment 12'), - array('Comment 13', 'Comment 14'), - null - ); + $expected = array (array('Comment 10', 'Comment 11', 'Comment 12'), array('Comment 13', 'Comment 14'), null); $this->assertIdentical($result, $expected); - $a = array( - array( '1day' => '20 sales' ), - array( '1day' => '2 sales' ) - ); - + $a = array(array( '1day' => '20 sales'),array( '1day' => '2 sales')); $result = Set::extract($a, '{n}.1day'); - $expected = array( - '20 sales', - '2 sales' - ); + $expected = array('20 sales', '2 sales'); $this->assertIdentical($result, $expected); } @@ -284,271 +181,287 @@ class SetTest extends UnitTestCase { function testCombine() { $a = array( - array('User' => array( - 'id' => 2, - 'group_id' => 1, - 'Data' => array( - 'user' => 'mariano.iglesias', - 'name' => 'Mariano Iglesias' - ) - )), - array('User' => array( - 'id' => 14, - 'group_id' => 2, - 'Data' => array( - 'user' => 'phpnut', - 'name' => 'Larry E. Masters' - ) - )), - array('User' => array( - 'id' => 25, - 'group_id' => 1, - 'Data' => array( - 'user' => 'gwoo', - 'name' => 'The Gwoo' - ) - )) - ); - + array('User' => array('id' => 2, 'group_id' => 1, + 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))), + array('User' => array('id' => 14, 'group_id' => 2, + 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))), + array('User' => array('id' => 25, 'group_id' => 1, + 'Data' => array('user' => 'gwoo','name' => 'The Gwoo')))); $result = Set::combine($a, '{n}.User.id'); - $expected = array( - 2 => null, - 14 => null, - 25 => null - ); + $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($result, $expected); $result = Set::combine($a, '{n}.User.id', '{n}.User.Data'); $expected = array( - 2 => array( - 'user' => 'mariano.iglesias', - 'name' => 'Mariano Iglesias' - ), - 14 => array( - 'user' => 'phpnut', - 'name' => 'Larry E. Masters' - ), - 25 => array( - 'user' => 'gwoo', - 'name' => 'The Gwoo' - ) - ); + 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), + 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'), + 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')); $this->assertIdentical($result, $expected); $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name'); $expected = array( 2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', - 25 => 'The Gwoo' - ); + 25 => 'The Gwoo'); $this->assertIdentical($result, $expected); $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id'); $expected = array( 1 => array( - 2 => array( - 'user' => 'mariano.iglesias', - 'name' => 'Mariano Iglesias' - ), - 25 => array( - 'user' => 'gwoo', - 'name' => 'The Gwoo' - ) - ), + 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), + 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')), 2 => array( - 14 => array( - 'user' => 'phpnut', - 'name' => 'Larry E. Masters' - ) - ) - ); + 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))); $this->assertIdentical($result, $expected); $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id'); $expected = array( 1 => array( 2 => 'Mariano Iglesias', - 25 => 'The Gwoo' - ), + 25 => 'The Gwoo'), 2 => array( - 14 => 'Larry E. Masters' - ) - ); + 14 => 'Larry E. Masters')); $this->assertIdentical($result, $expected); $Set =& new Set($a); $result = $Set->combine('{n}.User.id'); - $expected = array( - 2 => null, - 14 => null, - 25 => null - ); + $expected = array(2 => null, 14 => null, 25 => null); $this->assertIdentical($result, $expected); $result = $Set->combine('{n}.User.id', '{n}.User.Data'); $expected = array( - 2 => array( - 'user' => 'mariano.iglesias', - 'name' => 'Mariano Iglesias' - ), - 14 => array( - 'user' => 'phpnut', - 'name' => 'Larry E. Masters' - ), - 25 => array( - 'user' => 'gwoo', - 'name' => 'The Gwoo' - ) - ); + 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), + 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'), + 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')); $this->assertIdentical($result, $expected); $result = $Set->combine('{n}.User.id', '{n}.User.Data.name'); - $expected = array( - 2 => 'Mariano Iglesias', - 14 => 'Larry E. Masters', - 25 => 'The Gwoo' - ); + $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo'); $this->assertIdentical($result, $expected); $result = $Set->combine('{n}.User.id', '{n}.User.Data', '{n}.User.group_id'); $expected = array( 1 => array( - 2 => array( - 'user' => 'mariano.iglesias', - 'name' => 'Mariano Iglesias' - ), - 25 => array( - 'user' => 'gwoo', - 'name' => 'The Gwoo' - ) - ), + 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'), + 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')), 2 => array( - 14 => array( - 'user' => 'phpnut', - 'name' => 'Larry E. Masters' - ) - ) - ); + 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))); $this->assertIdentical($result, $expected); $result = $Set->combine('{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id'); $expected = array( 1 => array( 2 => 'Mariano Iglesias', - 25 => 'The Gwoo' - ), + 25 => 'The Gwoo'), 2 => array( - 14 => 'Larry E. Masters' - ) - ); + 14 => 'Larry E. Masters')); $this->assertIdentical($result, $expected); $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id'); $expected = array ( 1 => array ( 2 => 'mariano.iglesias: Mariano Iglesias', - 25 => 'gwoo: The Gwoo' - ), - 2 => array (14 => 'phpnut: Larry E. Masters') - ); + 25 => 'gwoo: The Gwoo'), + 2 => array (14 => 'phpnut: Larry E. Masters')); $this->assertIdentical($result, $expected); } function testMapReverse() { - $expected = array('Array1' => array( - 'Array1Data1' => 'Array1Data1 value 1', - 'Array1Data2' => 'Array1Data2 value 2', - 'Array1Data3' => 'Array1Data3 value 3', - 'Array1Data4' => 'Array1Data4 value 4', - 'Array1Data5' => 'Array1Data5 value 5', - 'Array1Data6' => 'Array1Data6 value 6', - 'Array1Data7' => 'Array1Data7 value 7', - 'Array1Data8' => 'Array1Data8 value 8'), - + $expected = array( + 'Array1' => array( + 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4', + 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'), 'Array2' => array( - 0 => array( - 'Array2Data1' => 1, - 'Array2Data2' => 'Array2Data2 value 2', - 'Array2Data3' => 'Array2Data3 value 2', - 'Array2Data4' => 'Array2Data4 value 4'), - 1 => array( - 'Array2Data1' => 2, - 'Array2Data2' => 'Array2Data2 value 2', - 'Array2Data3' => 'Array2Data3 value 2', - 'Array2Data4' => 'Array2Data4 value 4'), - 2 => array( - 'Array2Data1' => 3, - 'Array2Data2' => 'Array2Data2 value 2', - 'Array2Data3' => 'Array2Data3 value 2', - 'Array2Data4' => 'Array2Data4 value 4'), - 3 => array( - 'Array2Data1' => 4, - 'Array2Data2' => 'Array2Data2 value 2', - 'Array2Data3' => 'Array2Data3 value 2', - 'Array2Data4' => 'Array2Data4 value 4'), - 4 => array( - 'Array2Data1' => 5, - 'Array2Data2' => 'Array2Data2 value 2', - 'Array2Data3' => 'Array2Data3 value 2', - 'Array2Data4' => 'Array2Data4 value 4')), - + 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')), 'Array3' => array( - 0 => array( - 'Array3Data1' => 1, - 'Array3Data2' => 'Array3Data2 value 2', - 'Array3Data3' => 'Array3Data3 value 2', - 'Array3Data4' => 'Array3Data4 value 4'), - 1 => array( - 'Array3Data1' => 2, - 'Array3Data2' => 'Array3Data2 value 2', - 'Array3Data3' => 'Array3Data3 value 2', - 'Array3Data4' => 'Array3Data4 value 4'), - 2 => array( - 'Array3Data1' => 3, - 'Array3Data2' => 'Array3Data2 value 2', - 'Array3Data3' => 'Array3Data3 value 2', - 'Array3Data4' => 'Array3Data4 value 4'), - 3 => array( - 'Array3Data1' => 4, - 'Array3Data2' => 'Array3Data2 value 2', - 'Array3Data3' => 'Array3Data3 value 2', - 'Array3Data4' => 'Array3Data4 value 4'), - 4 => array( - 'Array3Data1' => 5, - 'Array3Data2' => 'Array3Data2 value 2', - 'Array3Data3' => 'Array3Data3 value 2', - 'Array3Data4' => 'Array3Data4 value 4'))); - + 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'))); $map = Set::map($expected); $result = Set::reverse($map); $this->assertIdentical($result, $expected); + + $expected = array( + 'Array1' => array( + 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4', + 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'), + 'string' => 1, + 'another' => 'string', + 'some' => 'thing else', + 'Array2' => array( + 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')), + 'Array3' => array( + 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'))); + $map = Set::map($expected); + $result = Set::reverse($map); + $this->assertIdentical($result, $expected); + + $expected = array( + 'Array1' => array( + 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4', + 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'), + 'string' => 1, + 'another' => 'string', + 'some' => 'thing else', + 'Array2' => array( + 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'), + 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')), + 'string2' => 1, + 'another2' => 'string', + 'some2' => 'thing else', + 'Array3' => array( + 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'), + 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')), + 'string3' => 1, + 'another3' => 'string', + 'some3' => 'thing else'); + $map = Set::map($expected); + $result = Set::reverse($map); + $this->assertIdentical($result, $expected); + + $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851))); + $map = Set::map($expected); + $result = Set::reverse($map); + $this->assertIdentical($result, $expected); + + $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851))); + $class = new stdClass; + $class->User = new stdClass; + $class->User->psword = 'whatever'; + $class->User->Icon = new stdClass; + $class->User->Icon->id = 851; + $result = Set::reverse($class); + $this->assertIdentical($result, $expected); + + $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'))); + $class = new stdClass; + $class->User = new stdClass; + $class->User->psword = 'whatever'; + $class->User->Icon = new stdClass; + $class->User->Icon->id = 851; + $class->User->Profile = new stdClass; + $class->User->Profile->name = 'Some Name'; + $class->User->Profile->address = 'Some Address'; + + $result = Set::reverse($class); + $this->assertIdentical($result, $expected); + + $expected = array('User' => array('psword'=> 'whatever', + 'Icon' => array('id'=> 851), + 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'), + 'Comment' => array( + array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'), + array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31')))); + + $class = new stdClass; + $class->User = new stdClass; + $class->User->psword = 'whatever'; + $class->User->Icon = new stdClass; + $class->User->Icon->id = 851; + $class->User->Profile = new stdClass; + $class->User->Profile->name = 'Some Name'; + $class->User->Profile->address = 'Some Address'; + $class->User->Comment = new stdClass; + $class->User->Comment->{0} = new stdClass; + $class->User->Comment->{0}->id = 1; + $class->User->Comment->{0}->article_id = 1; + $class->User->Comment->{0}->user_id = 1; + $class->User->Comment->{0}->comment = 'First Comment for First Article'; + $class->User->Comment->{0}->published = 'Y'; + $class->User->Comment->{0}->created = '2007-03-18 10:47:23'; + $class->User->Comment->{0}->updated = '2007-03-18 10:49:31'; + $class->User->Comment->{1} = new stdClass; + $class->User->Comment->{1}->id = 2; + $class->User->Comment->{1}->article_id = 1; + $class->User->Comment->{1}->user_id = 2; + $class->User->Comment->{1}->comment = 'Second Comment for First Article'; + $class->User->Comment->{1}->published = 'Y'; + $class->User->Comment->{1}->created = '2007-03-18 10:47:23'; + $class->User->Comment->{1}->updated = '2007-03-18 10:49:31'; + + $result = Set::reverse($class); + $this->assertIdentical($result, $expected); + + $expected = array('User' => array('psword'=> 'whatever', + 'Icon' => array('id'=> 851), + 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'), + 'Comment' => array( + array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'), + array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31')))); + + $class = new stdClass; + $class->User = new stdClass; + $class->User->psword = 'whatever'; + $class->User->Icon = new stdClass; + $class->User->Icon->id = 851; + $class->User->Profile = new stdClass; + $class->User->Profile->name = 'Some Name'; + $class->User->Profile->address = 'Some Address'; + $class->User->Comment = array(); + $comment = new stdClass; + $comment->id = 1; + $comment->article_id = 1; + $comment->user_id = 1; + $comment->comment = 'First Comment for First Article'; + $comment->published = 'Y'; + $comment->created = '2007-03-18 10:47:23'; + $comment->updated = '2007-03-18 10:49:31'; + $comment2 = new stdClass; + $comment2->id = 2; + $comment2->article_id = 1; + $comment2->user_id = 2; + $comment2->comment = 'Second Comment for First Article'; + $comment2->published = 'Y'; + $comment2->created = '2007-03-18 10:47:23'; + $comment2->updated = '2007-03-18 10:49:31'; + $class->User->Comment = array($comment, $comment2); + $result = Set::reverse($class); + $this->assertIdentical($result, $expected); + + uses('model'.DS.'model'); + $model = new Model(false, false); + $expected = array( + '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, + '__behaviorMethods' => array(), 'recursive' => 1, 'order' => null, '__exists' => null, + '__associationKeys' => array( + 'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'), + 'hasOne' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'dependent'), + 'hasMany' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'), + '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), '_log' => null); + $result = Set::reverse($model); + $this->assertIdentical($result, $expected); } function testFormatting() { $data = array( - array('Person' => array( - 'first_name' => 'Nate', - 'last_name' => 'Abele', - 'city' => 'Boston', - 'state' => 'MA', - 'something' => '42' - )), - array('Person' => array( - 'first_name' => 'Larry', - 'last_name' => 'Masters', - 'city' => 'Boondock', - 'state' => 'TN', - 'something' => '{0}' - )), - array('Person' => array( - 'first_name' => 'Garrett', - 'last_name' => 'Woodworth', - 'city' => 'Venice Beach', - 'state' => 'CA', - 'something' => '{1}' - )) - ); + array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')), + array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')), + array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}'))); $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name')); $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett'); @@ -569,48 +482,47 @@ class SetTest extends UnitTestCase { $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}'); $this->assertEqual($result, $expected); } - + function testCountDim() { $data = array('one', '2', 'three'); $result = Set::countDim($data); $this->assertEqual($result, 1); - + $data = array('1' => '1.1', '2', '3'); $result = Set::countDim($data); $this->assertEqual($result, 1); - + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEqual($result, 2); - + $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data); $this->assertEqual($result, 1); - + $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1')); $result = Set::countDim($data, true); $this->assertEqual($result, 2); - + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data); $this->assertEqual($result, 2); - + $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 3); - + $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 4); - + $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 5); - + $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))); $result = Set::countDim($data, true); $this->assertEqual($result, 5); } } - ?> \ No newline at end of file