ArrayHelperTest.php 7.03 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;
	}
}

27 28 29 30 31 32 33 34 35 36 37
class Post3 extends Object
{
	public $id = 33;
	public $subObject;

	public function init()
	{
		$this->subObject = new Post2();
	}
}

38 39 40
/**
 * @group helpers
 */
Alexander Makarov committed
41
class ArrayHelperTest extends TestCase
42
{
43
	public function testToArray()
44
	{
45 46 47 48
		$object = new Post1;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
		$object = new Post2;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
49

50 51
		$object1 = new Post1;
		$object2 = new Post2;
Alexander Makarov committed
52
		$this->assertEquals([
53 54
			get_object_vars($object1),
			get_object_vars($object2),
Alexander Makarov committed
55
		], ArrayHelper::toArray([
56 57
			$object1,
			$object2,
Alexander Makarov committed
58
		]));
59

60
		$object = new Post2;
Alexander Makarov committed
61
		$this->assertEquals([
62 63 64 65
			'id' => 123,
			'secret' => 's',
			'_content' => 'test',
			'length' => 4,
Alexander Makarov committed
66 67
		], ArrayHelper::toArray($object, [
			$object->className() => [
68 69 70 71 72
				'id', 'secret',
				'_content' => 'content',
				'length' => function ($post) {
					return strlen($post->content);
				}
Alexander Makarov committed
73 74
			]
		]));
75 76

		$object = new Post3();
Alexander Makarov committed
77 78
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object, [], false));
		$this->assertEquals([
79
			'id' => 33,
Alexander Makarov committed
80
			'subObject' => [
81 82
				'id' => 123,
				'content' => 'test',
Alexander Makarov committed
83 84
			],
		], ArrayHelper::toArray($object));
85 86
	}

87
	public function testRemove()
88
	{
Alexander Makarov committed
89
		$array = ['name' => 'b', 'age' => 3];
90
		$name = ArrayHelper::remove($array, 'name');
91 92

		$this->assertEquals($name, 'b');
Alexander Makarov committed
93
		$this->assertEquals($array, ['age' => 3]);
94 95 96

		$default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue');
		$this->assertEquals('defaultValue', $default);
97 98 99
	}


100 101 102
	public function testMultisort()
	{
		// single key
Alexander Makarov committed
103 104 105 106 107
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 1],
			['name' => 'c', 'age' => 2],
		];
108
		ArrayHelper::multisort($array, 'name');
Alexander Makarov committed
109 110 111
		$this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
		$this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
112 113

		// multiple keys
Alexander Makarov committed
114 115 116 117 118 119 120 121 122
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 2],
			['name' => 'a', 'age' => 1],
		];
		ArrayHelper::multisort($array, ['name', 'age']);
		$this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'a', 'age' => 2], $array[1]);
		$this->assertEquals(['name' => 'b', 'age' => 3], $array[2]);
123 124

		// case-insensitive
Alexander Makarov committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		$array = [
			['name' => 'a', 'age' => 3],
			['name' => 'b', 'age' => 2],
			['name' => 'B', 'age' => 4],
			['name' => 'A', 'age' => 1],
		];

		ArrayHelper::multisort($array, ['name', 'age'], false, [SORT_STRING, SORT_REGULAR]);
		$this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'B', 'age' => 4], $array[1]);
		$this->assertEquals(['name' => 'a', 'age' => 3], $array[2]);
		$this->assertEquals(['name' => 'b', 'age' => 2], $array[3]);

		ArrayHelper::multisort($array, ['name', 'age'], false, [SORT_STRING, SORT_REGULAR], false);
		$this->assertEquals(['name' => 'A', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'a', 'age' => 3], $array[1]);
		$this->assertEquals(['name' => 'b', 'age' => 2], $array[2]);
		$this->assertEquals(['name' => 'B', 'age' => 4], $array[3]);
143
	}
slavcopost committed
144 145 146 147

	public function testMultisortUseSort()
	{
		// single key
Alexander Makarov committed
148 149 150 151
		$sort = new Sort([
			'attributes' => ['name', 'age'],
			'defaultOrder' => ['name' => Sort::ASC],
		]);
slavcopost committed
152 153
		$orders = $sort->getOrders();

Alexander Makarov committed
154 155 156 157 158
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 1],
			['name' => 'c', 'age' => 2],
		];
slavcopost committed
159
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
Alexander Makarov committed
160 161 162
		$this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
		$this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
slavcopost committed
163 164

		// multiple keys
Alexander Makarov committed
165 166 167 168
		$sort = new Sort([
			'attributes' => ['name', 'age'],
			'defaultOrder' => ['name' => Sort::ASC, 'age' => Sort::DESC],
		]);
slavcopost committed
169 170
		$orders = $sort->getOrders();

Alexander Makarov committed
171 172 173 174 175
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 2],
			['name' => 'a', 'age' => 1],
		];
slavcopost committed
176
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
Alexander Makarov committed
177 178 179
		$this->assertEquals(['name' => 'a', 'age' => 2], $array[0]);
		$this->assertEquals(['name' => 'a', 'age' => 1], $array[1]);
		$this->assertEquals(['name' => 'b', 'age' => 3], $array[2]);
slavcopost committed
180
	}
181 182 183

	public function testMerge()
	{
Alexander Makarov committed
184
		$a = [
185 186
			'name' => 'Yii',
			'version' => '1.0',
Alexander Makarov committed
187
			'options' => [
188 189
				'namespace' => false,
				'unittest' => false,
Alexander Makarov committed
190 191
			],
			'features' => [
192
				'mvc',
Alexander Makarov committed
193 194 195
			],
		];
		$b = [
196
			'version' => '1.1',
Alexander Makarov committed
197
			'options' => [
198
				'unittest' => true,
Alexander Makarov committed
199 200
			],
			'features' => [
201
				'gii',
Alexander Makarov committed
202 203 204
			],
		];
		$c = [
205
			'version' => '2.0',
Alexander Makarov committed
206
			'options' => [
207
				'namespace' => true,
Alexander Makarov committed
208 209
			],
			'features' => [
210
				'debug',
Alexander Makarov committed
211 212
			],
		];
213 214

		$result = ArrayHelper::merge($a, $b, $c);
Alexander Makarov committed
215
		$expected = [
216 217
			'name' => 'Yii',
			'version' => '2.0',
Alexander Makarov committed
218
			'options' => [
219 220
				'namespace' => true,
				'unittest' => true,
Alexander Makarov committed
221 222
			],
			'features' => [
223 224 225
				'mvc',
				'gii',
				'debug',
Alexander Makarov committed
226 227
			],
		];
228 229 230 231 232 233

		$this->assertEquals($expected, $result);
	}

	public function testIndex()
	{
Alexander Makarov committed
234 235 236 237
		$array = [
			['id' => '123', 'data' => 'abc'],
			['id' => '345', 'data' => 'def'],
		];
238
		$result = ArrayHelper::index($array, 'id');
Alexander Makarov committed
239 240 241 242
		$this->assertEquals([
			'123' => ['id' => '123', 'data' => 'abc'],
			'345' => ['id' => '345', 'data' => 'def'],
		], $result);
243 244 245 246

		$result = ArrayHelper::index($array, function ($element) {
			return $element['data'];
		});
Alexander Makarov committed
247 248 249 250
		$this->assertEquals([
			'abc' => ['id' => '123', 'data' => 'abc'],
			'def' => ['id' => '345', 'data' => 'def'],
		], $result);
251 252 253 254
	}

	public function testGetColumn()
	{
Alexander Makarov committed
255 256 257 258
		$array = [
			'a' => ['id' => '123', 'data' => 'abc'],
			'b' => ['id' => '345', 'data' => 'def'],
		];
259
		$result = ArrayHelper::getColumn($array, 'id');
Alexander Makarov committed
260
		$this->assertEquals(['a' => '123', 'b' => '345'], $result);
261
		$result = ArrayHelper::getColumn($array, 'id', false);
Alexander Makarov committed
262
		$this->assertEquals(['123', '345'], $result);
263 264 265 266

		$result = ArrayHelper::getColumn($array, function ($element) {
			return $element['data'];
		});
Alexander Makarov committed
267
		$this->assertEquals(['a' => 'abc', 'b' => 'def'], $result);
268 269 270
		$result = ArrayHelper::getColumn($array, function ($element) {
			return $element['data'];
		}, false);
Alexander Makarov committed
271
		$this->assertEquals(['abc', 'def'], $result);
272 273 274 275
	}

	public function testMap()
	{
Alexander Makarov committed
276 277 278 279 280
		$array = [
			['id' => '123', 'name' => 'aaa', 'class' => 'x'],
			['id' => '124', 'name' => 'bbb', 'class' => 'x'],
			['id' => '345', 'name' => 'ccc', 'class' => 'y'],
		];
281 282

		$result = ArrayHelper::map($array, 'id', 'name');
Alexander Makarov committed
283
		$this->assertEquals([
284 285 286
			'123' => 'aaa',
			'124' => 'bbb',
			'345' => 'ccc',
Alexander Makarov committed
287
		], $result);
288 289

		$result = ArrayHelper::map($array, 'id', 'name', 'class');
Alexander Makarov committed
290 291
		$this->assertEquals([
			'x' => [
292 293
				'123' => 'aaa',
				'124' => 'bbb',
Alexander Makarov committed
294 295
			],
			'y' => [
296
				'345' => 'ccc',
Alexander Makarov committed
297 298
			],
		], $result);
299
	}
300
}