tests for masort()
This commit is contained in:
parent
ae296aa3b9
commit
ecb9cb2dfe
|
@ -888,9 +888,6 @@ function masort(&$data,$sortby,$rev=0) {
|
|||
# if the array to sort is null or empty
|
||||
if (! $data) return;
|
||||
|
||||
static $CACHE = array();
|
||||
|
||||
if (empty($CACHE[$sortby])) {
|
||||
$code = "\$c=0;\n";
|
||||
|
||||
foreach (explode(',',$sortby) as $key) {
|
||||
|
@ -962,10 +959,7 @@ function masort(&$data,$sortby,$rev=0) {
|
|||
|
||||
$code .= 'return $c;';
|
||||
|
||||
$CACHE[$sortby] = create_function('$a, $b',$code);
|
||||
}
|
||||
|
||||
uasort($data,$CACHE[$sortby]);
|
||||
uasort($data, create_function('$a, $b',$code));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
/*
|
||||
|
||||
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
||||
Copyright (C) 2018 Roland Gruber
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
*/
|
||||
|
||||
define('LIBDIR','lam/templates/3rdParty/pla/lib/');
|
||||
include 'lam/templates/3rdParty/pla/lib/functions.php';
|
||||
|
||||
/**
|
||||
* LAMConfig test case.
|
||||
*
|
||||
* @author Roland Gruber
|
||||
*/
|
||||
class PlaFunctionsTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function test_masort() {
|
||||
$data = array(
|
||||
'a' => array('key1' => '1'),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'c' => array('key1' => '2'),
|
||||
'd' => array('key1' => '3'),
|
||||
);
|
||||
|
||||
masort($data, 'key1', 0);
|
||||
|
||||
$dataWanted = array(
|
||||
'a' => array('key1' => '1'),
|
||||
'c' => array('key1' => '2'),
|
||||
'd' => array('key1' => '3'),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
);
|
||||
|
||||
$this->compareArray($dataWanted, $data);
|
||||
}
|
||||
|
||||
public function test_masortRev() {
|
||||
$data = array(
|
||||
'a' => array('key1' => '1'),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'c' => array('key1' => '2'),
|
||||
'd' => array('key1' => '3'),
|
||||
);
|
||||
|
||||
masort($data, 'key1', true);
|
||||
|
||||
$dataWanted = array(
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'd' => array('key1' => '3'),
|
||||
'c' => array('key1' => '2'),
|
||||
'a' => array('key1' => '1'),
|
||||
);
|
||||
|
||||
$this->compareArray($dataWanted, $data);
|
||||
}
|
||||
|
||||
public function test_masortPartialData() {
|
||||
$data = array(
|
||||
'a' => array('key1' => '1'),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'c' => array('key1' => '2'),
|
||||
'd' => array('key1' => '3'),
|
||||
);
|
||||
|
||||
masort($data, 'key2', 0);
|
||||
|
||||
$dataWanted = array(
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'a' => array('key1' => '1'),
|
||||
'c' => array('key1' => '2'),
|
||||
'd' => array('key1' => '3'),
|
||||
);
|
||||
|
||||
$this->compareArray($dataWanted, $data);
|
||||
}
|
||||
|
||||
public function test_masortMultiSort() {
|
||||
$data = array(
|
||||
'a' => array('key1' => '1', 'key2' => 4),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'c' => array('key1' => '1', 'key2' => 1),
|
||||
'd' => array('key1' => '5', 'key2' => 2),
|
||||
'e' => array('key1' => '6', 'key2' => 2),
|
||||
);
|
||||
|
||||
masort($data, 'key1,key2', 0);
|
||||
|
||||
$dataWanted = array(
|
||||
'c' => array('key1' => '1', 'key2' => 1),
|
||||
'a' => array('key1' => '1', 'key2' => 4),
|
||||
'd' => array('key1' => '5', 'key2' => 2),
|
||||
'b' => array('key1' => '5', 'key2' => 3),
|
||||
'e' => array('key1' => '6', 'key2' => 2),
|
||||
);
|
||||
|
||||
$this->compareArray($dataWanted, $data);
|
||||
}
|
||||
|
||||
public function test_masortObject() {
|
||||
$data = array(
|
||||
'a' => (object) ['key1' => '1'],
|
||||
'b' => (object) ['key1' => '5', 'key2' => 3],
|
||||
'c' => (object) ['key1' => '2'],
|
||||
'd' => (object) ['key1' => '3'],
|
||||
);
|
||||
|
||||
masort($data, 'key1', 0);
|
||||
|
||||
$dataWanted = array(
|
||||
'a' => (object) ['key1' => '1'],
|
||||
'c' => (object) ['key1' => '2'],
|
||||
'd' => (object) ['key1' => '3'],
|
||||
'b' => (object) ['key1' => '5', 'key2' => 3],
|
||||
);
|
||||
|
||||
$this->compareArray($dataWanted, $data);
|
||||
}
|
||||
|
||||
private function compareArray($dataWanted, $dataNew) {
|
||||
$this->assertEquals(sizeof($dataWanted), sizeof($dataNew));
|
||||
$keysWanted = array_keys($dataWanted);
|
||||
$keysNew = array_keys($dataNew);
|
||||
foreach ($keysWanted as $index => $key) {
|
||||
if (is_array($dataWanted[$key])) {
|
||||
$this->compareArray($dataWanted[$key], $dataNew[$keysNew[$index]]);
|
||||
}
|
||||
elseif (is_object($dataWanted[$key])) {
|
||||
$this->compareObject($dataWanted[$key], $dataNew[$keysNew[$index]]);
|
||||
}
|
||||
else {
|
||||
$this->assertEquals($dataWanted[$key], $dataNew[$keysNew[$index]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function compareObject($dataWanted, $dataNew) {
|
||||
$membersWanted = get_object_vars($dataWanted);
|
||||
$membersNew = get_object_vars($dataNew);
|
||||
$this->assertEquals(sizeof($membersWanted), sizeof($membersNew));
|
||||
foreach ($membersWanted as $name => $value) {
|
||||
if (is_array($dataWanted->$name)) {
|
||||
$this->compareArray($dataWanted->$name, $dataNew->$name);
|
||||
}
|
||||
else {
|
||||
$this->assertEquals($dataWanted->$name, $dataNew->$name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue