Skip to content

Commit

Permalink
cleaning up some Set::extract, adding tests for empty data and result…
Browse files Browse the repository at this point in the history
…s. Removing some checks from Cache engine.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8097 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
gwoo committed Mar 14, 2009
1 parent b184d07 commit 1985480
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
4 changes: 0 additions & 4 deletions cake/libs/cache.php
Expand Up @@ -142,10 +142,6 @@ function config($name = null, $settings = array()) {
* @static
*/
function engine($name = 'File', $settings = array()) {
if (!$name || Configure::read('Cache.disable')) {
return false;
}

$cacheClass = $name . 'Engine';
$_this =& Cache::getInstance();
if (!isset($_this->_Engine[$name])) {
Expand Down
18 changes: 9 additions & 9 deletions cake/libs/set.php
Expand Up @@ -369,16 +369,16 @@ function format($data, $format, $keys) {
* @static
*/
function extract($path, $data = null, $options = array()) {
if (empty($data) && is_string($path) && $path{0} === '/') {
return array();
if (is_string($data)) {
$tmp = $data;
$data = $path;
$path = $tmp;
}
if (is_string($data) && $data{0} === '/') {
$tmp = $path;
$path = $data;
$data = $tmp;
if (empty($data)) {
return array();
}
if (is_array($path) || empty($data) || is_object($path) || empty($path)) {
return Set::classicExtract($path, $data);
if (strpos($path, '/') === false) {
return Set::classicExtract($data, $path);
}
if ($path === '/') {
return $data;
Expand Down Expand Up @@ -1090,7 +1090,7 @@ function __flatten($results, $key = null) {
function sort($data, $path, $dir) {
$result = Set::__flatten(Set::extract($data, $path));
list($keys, $values) = array(Set::extract($result, '{n}.id'), Set::extract($result, '{n}.value'));

$dir = strtolower($dir);
if ($dir === 'asc') {
$dir = SORT_ASC;
Expand Down
33 changes: 26 additions & 7 deletions cake/tests/cases/libs/set.test.php
Expand Up @@ -887,7 +887,7 @@ function testExtract() {
$expected = array('Neo', 'Morpheus');
$r = Set::extract('/User/name', $mixedKeys);
$this->assertEqual($r, $expected);

$single = array(
array(
'CallType' => array(
Expand All @@ -898,11 +898,11 @@ function testExtract() {
)
)
);

$expected = array(7);
$r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
$this->assertEqual($r, $expected);

$multiple = array(
array(
'CallType' => array(
Expand Down Expand Up @@ -933,7 +933,7 @@ function testExtract() {
$expected = array(7,2,1);
$r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
$this->assertEqual($r, $expected);

$f = array(
array(
'file' => array(
Expand Down Expand Up @@ -961,16 +961,16 @@ function testExtract() {
'error' => 0,
'size' => '21324'
)
)
)
);
$expected = array(array('name' => 'zipfile2.zip','type' => 'application/x-zip-compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
$r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
$this->assertEqual($r, $expected);

$expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
$r = Set::extract('/file/.[type=application/zip]', $f);
$this->assertEqual($r, $expected);

}
/**
* testMatches method
Expand Down Expand Up @@ -1042,6 +1042,25 @@ function testMatches() {
$this->assertEqual(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published'));
$this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user'));


}
/**
* testSetExtractReturnsEmptyArray method
*
* @access public
* @return void
*/
function testSetExtractReturnsEmptyArray() {

$this->assertEqual(Set::extract(array(), '/Post/id'), array());

$this->assertEqual(Set::extract('/Post/id', array()), array());

$this->assertEqual(Set::extract('/Post/id', array(
array('Post' => array('name' => 'bob')),
array('Post' => array('name' => 'jim'))
)), array());

}
/**
* testClassicExtract method
Expand Down

0 comments on commit 1985480

Please sign in to comment.