ArrayHelperTest.php 4.34 KB
Newer Older
1 2
<?php

3
namespace yiiunit\framework\helpers;
4

5
use yii\base\Object;
Qiang Xue committed
6
use yii\helpers\ArrayHelper;
Alexander Makarov committed
7
use yii\test\TestCase;
Qiang Xue committed
8
use yii\data\Sort;
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
class Post1
{
	public $id = 23;
	public $title = 'tt';
}

class Post2 extends Object
{
	public $id = 123;
	public $content = 'test';
	private $secret = 's';
	public function getSecret()
	{
		return $this->secret;
	}
}

Alexander Makarov committed
27
class ArrayHelperTest extends TestCase
28
{
29
	public function testToArray()
30
	{
31 32 33 34
		$object = new Post1;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
		$object = new Post2;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
35

36 37 38 39 40 41 42 43 44
		$object1 = new Post1;
		$object2 = new Post2;
		$this->assertEquals(array(
			get_object_vars($object1),
			get_object_vars($object2),
		), ArrayHelper::toArray(array(
			$object1,
			$object2,
		)));
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59
		$object = new Post2;
		$this->assertEquals(array(
			'id' => 123,
			'secret' => 's',
			'_content' => 'test',
			'length' => 4,
		), ArrayHelper::toArray($object, array(
			$object->className() => array(
				'id', 'secret',
				'_content' => 'content',
				'length' => function ($post) {
					return strlen($post->content);
				}
		))));
60 61
	}

62
	public function testRemove()
63 64
	{
		$array = array('name' => 'b', 'age' => 3);
65
		$name = ArrayHelper::remove($array, 'name');
66 67 68 69 70 71

		$this->assertEquals($name, 'b');
		$this->assertEquals($array, array('age' => 3));
	}


72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	public function testMultisort()
	{
		// single key
		$array = array(
			array('name' => 'b', 'age' => 3),
			array('name' => 'a', 'age' => 1),
			array('name' => 'c', 'age' => 2),
		);
		ArrayHelper::multisort($array, 'name');
		$this->assertEquals(array('name' => 'a', 'age' => 1), $array[0]);
		$this->assertEquals(array('name' => 'b', 'age' => 3), $array[1]);
		$this->assertEquals(array('name' => 'c', 'age' => 2), $array[2]);

		// multiple keys
		$array = array(
			array('name' => 'b', 'age' => 3),
			array('name' => 'a', 'age' => 2),
			array('name' => 'a', 'age' => 1),
		);
		ArrayHelper::multisort($array, array('name', 'age'));
		$this->assertEquals(array('name' => 'a', 'age' => 1), $array[0]);
		$this->assertEquals(array('name' => 'a', 'age' => 2), $array[1]);
		$this->assertEquals(array('name' => 'b', 'age' => 3), $array[2]);

		// case-insensitive
		$array = array(
			array('name' => 'a', 'age' => 3),
			array('name' => 'b', 'age' => 2),
100
			array('name' => 'B', 'age' => 4),
101 102
			array('name' => 'A', 'age' => 1),
		);
103

slavcopost committed
104
		ArrayHelper::multisort($array, array('name', 'age'), false, array(SORT_STRING, SORT_REGULAR));
105 106 107 108 109
		$this->assertEquals(array('name' => 'A', 'age' => 1), $array[0]);
		$this->assertEquals(array('name' => 'B', 'age' => 4), $array[1]);
		$this->assertEquals(array('name' => 'a', 'age' => 3), $array[2]);
		$this->assertEquals(array('name' => 'b', 'age' => 2), $array[3]);

slavcopost committed
110
		ArrayHelper::multisort($array, array('name', 'age'), false, array(SORT_STRING, SORT_REGULAR), false);
111 112 113
		$this->assertEquals(array('name' => 'A', 'age' => 1), $array[0]);
		$this->assertEquals(array('name' => 'a', 'age' => 3), $array[1]);
		$this->assertEquals(array('name' => 'b', 'age' => 2), $array[2]);
114
		$this->assertEquals(array('name' => 'B', 'age' => 4), $array[3]);
115
	}
slavcopost committed
116 117 118 119

	public function testMultisortUseSort()
	{
		// single key
Qiang Xue committed
120 121 122 123
		$sort = new Sort(array(
			'attributes' => array('name', 'age'),
			'defaultOrder' => array('name' => Sort::ASC),
		));
slavcopost committed
124 125 126 127 128 129 130 131 132 133 134 135 136
		$orders = $sort->getOrders();

		$array = array(
			array('name' => 'b', 'age' => 3),
			array('name' => 'a', 'age' => 1),
			array('name' => 'c', 'age' => 2),
		);
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
		$this->assertEquals(array('name' => 'a', 'age' => 1), $array[0]);
		$this->assertEquals(array('name' => 'b', 'age' => 3), $array[1]);
		$this->assertEquals(array('name' => 'c', 'age' => 2), $array[2]);

		// multiple keys
Qiang Xue committed
137 138 139 140
		$sort = new Sort(array(
			'attributes' => array('name', 'age'),
			'defaultOrder' => array('name' => Sort::ASC, 'age' => Sort::DESC),
		));
slavcopost committed
141 142 143 144 145 146 147 148 149 150 151 152
		$orders = $sort->getOrders();

		$array = array(
			array('name' => 'b', 'age' => 3),
			array('name' => 'a', 'age' => 2),
			array('name' => 'a', 'age' => 1),
		);
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
		$this->assertEquals(array('name' => 'a', 'age' => 2), $array[0]);
		$this->assertEquals(array('name' => 'a', 'age' => 1), $array[1]);
		$this->assertEquals(array('name' => 'b', 'age' => 3), $array[2]);
	}
153
}