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); } public function test_masortObjectMultiSort() { $data = array( 'a' => (object) ['key1' => '1'], 'b' => (object) ['key1' => '5', 'key2' => 3], 'c' => (object) ['key1' => '2'], 'd' => (object) ['key1' => '5', 'key2' => 1], ); masort($data, 'key1,key2', 0); $dataWanted = array( 'a' => (object) ['key1' => '1'], 'c' => (object) ['key1' => '2'], 'd' => (object) ['key1' => '5', 'key2' => 1], '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); } } } } ?>