ArrayHelperTest.php 7.69 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;
7
use yiiunit\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
{
Carsten Brandt committed
43 44 45 46 47 48
	protected function setUp()
	{
		parent::setUp();
		$this->mockApplication();
	}

49
	public function testToArray()
50
	{
51 52 53 54
		$object = new Post1;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
		$object = new Post2;
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object));
55

56 57
		$object1 = new Post1;
		$object2 = new Post2;
Alexander Makarov committed
58
		$this->assertEquals([
59 60
			get_object_vars($object1),
			get_object_vars($object2),
Alexander Makarov committed
61
		], ArrayHelper::toArray([
62 63
			$object1,
			$object2,
Alexander Makarov committed
64
		]));
65

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

		$object = new Post3();
Alexander Makarov committed
83 84
		$this->assertEquals(get_object_vars($object), ArrayHelper::toArray($object, [], false));
		$this->assertEquals([
85
			'id' => 33,
Alexander Makarov committed
86
			'subObject' => [
87 88
				'id' => 123,
				'content' => 'test',
Alexander Makarov committed
89 90
			],
		], ArrayHelper::toArray($object));
91 92
	}

93
	public function testRemove()
94
	{
Alexander Makarov committed
95
		$array = ['name' => 'b', 'age' => 3];
96
		$name = ArrayHelper::remove($array, 'name');
97 98

		$this->assertEquals($name, 'b');
Alexander Makarov committed
99
		$this->assertEquals($array, ['age' => 3]);
100 101 102

		$default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue');
		$this->assertEquals('defaultValue', $default);
103 104 105
	}


106 107 108
	public function testMultisort()
	{
		// single key
Alexander Makarov committed
109 110 111 112 113
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 1],
			['name' => 'c', 'age' => 2],
		];
114
		ArrayHelper::multisort($array, 'name');
Alexander Makarov committed
115 116 117
		$this->assertEquals(['name' => 'a', 'age' => 1], $array[0]);
		$this->assertEquals(['name' => 'b', 'age' => 3], $array[1]);
		$this->assertEquals(['name' => 'c', 'age' => 2], $array[2]);
118 119

		// multiple keys
Alexander Makarov committed
120 121 122 123 124 125 126 127 128
		$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]);
129 130

		// case-insensitive
Alexander Makarov committed
131 132 133 134 135 136 137
		$array = [
			['name' => 'a', 'age' => 3],
			['name' => 'b', 'age' => 2],
			['name' => 'B', 'age' => 4],
			['name' => 'A', 'age' => 1],
		];

138
		ArrayHelper::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING, SORT_REGULAR]);
Alexander Makarov committed
139 140 141 142 143
		$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]);

144
		ArrayHelper::multisort($array, ['name', 'age'], SORT_ASC, [SORT_STRING | SORT_FLAG_CASE, SORT_REGULAR]);
Alexander Makarov committed
145 146 147 148
		$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]);
149
	}
slavcopost committed
150 151 152 153

	public function testMultisortUseSort()
	{
		// single key
Alexander Makarov committed
154 155
		$sort = new Sort([
			'attributes' => ['name', 'age'],
156
			'defaultOrder' => ['name' => SORT_ASC],
Alexander Makarov committed
157
		]);
slavcopost committed
158 159
		$orders = $sort->getOrders();

Alexander Makarov committed
160 161 162 163 164
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 1],
			['name' => 'c', 'age' => 2],
		];
slavcopost committed
165
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
Alexander Makarov committed
166 167 168
		$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
169 170

		// multiple keys
Alexander Makarov committed
171 172
		$sort = new Sort([
			'attributes' => ['name', 'age'],
173
			'defaultOrder' => ['name' => SORT_ASC, 'age' => SORT_DESC],
Alexander Makarov committed
174
		]);
slavcopost committed
175 176
		$orders = $sort->getOrders();

Alexander Makarov committed
177 178 179 180 181
		$array = [
			['name' => 'b', 'age' => 3],
			['name' => 'a', 'age' => 2],
			['name' => 'a', 'age' => 1],
		];
slavcopost committed
182
		ArrayHelper::multisort($array, array_keys($orders), array_values($orders));
Alexander Makarov committed
183 184 185
		$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
186
	}
187 188 189

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

		$result = ArrayHelper::merge($a, $b, $c);
Alexander Makarov committed
221
		$expected = [
222 223
			'name' => 'Yii',
			'version' => '2.0',
Alexander Makarov committed
224
			'options' => [
225 226
				'namespace' => true,
				'unittest' => true,
Alexander Makarov committed
227 228
			],
			'features' => [
229 230 231
				'mvc',
				'gii',
				'debug',
Alexander Makarov committed
232 233
			],
		];
234 235 236 237 238 239

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

	public function testIndex()
	{
Alexander Makarov committed
240 241 242 243
		$array = [
			['id' => '123', 'data' => 'abc'],
			['id' => '345', 'data' => 'def'],
		];
244
		$result = ArrayHelper::index($array, 'id');
Alexander Makarov committed
245 246 247 248
		$this->assertEquals([
			'123' => ['id' => '123', 'data' => 'abc'],
			'345' => ['id' => '345', 'data' => 'def'],
		], $result);
249 250 251 252

		$result = ArrayHelper::index($array, function ($element) {
			return $element['data'];
		});
Alexander Makarov committed
253 254 255 256
		$this->assertEquals([
			'abc' => ['id' => '123', 'data' => 'abc'],
			'def' => ['id' => '345', 'data' => 'def'],
		], $result);
257 258 259 260
	}

	public function testGetColumn()
	{
Alexander Makarov committed
261 262 263 264
		$array = [
			'a' => ['id' => '123', 'data' => 'abc'],
			'b' => ['id' => '345', 'data' => 'def'],
		];
265
		$result = ArrayHelper::getColumn($array, 'id');
Alexander Makarov committed
266
		$this->assertEquals(['a' => '123', 'b' => '345'], $result);
267
		$result = ArrayHelper::getColumn($array, 'id', false);
Alexander Makarov committed
268
		$this->assertEquals(['123', '345'], $result);
269 270 271 272

		$result = ArrayHelper::getColumn($array, function ($element) {
			return $element['data'];
		});
Alexander Makarov committed
273
		$this->assertEquals(['a' => 'abc', 'b' => 'def'], $result);
274 275 276
		$result = ArrayHelper::getColumn($array, function ($element) {
			return $element['data'];
		}, false);
Alexander Makarov committed
277
		$this->assertEquals(['abc', 'def'], $result);
278 279 280 281
	}

	public function testMap()
	{
Alexander Makarov committed
282 283 284 285 286
		$array = [
			['id' => '123', 'name' => 'aaa', 'class' => 'x'],
			['id' => '124', 'name' => 'bbb', 'class' => 'x'],
			['id' => '345', 'name' => 'ccc', 'class' => 'y'],
		];
287 288

		$result = ArrayHelper::map($array, 'id', 'name');
Alexander Makarov committed
289
		$this->assertEquals([
290 291 292
			'123' => 'aaa',
			'124' => 'bbb',
			'345' => 'ccc',
Alexander Makarov committed
293
		], $result);
294 295

		$result = ArrayHelper::map($array, 'id', 'name', 'class');
Alexander Makarov committed
296 297
		$this->assertEquals([
			'x' => [
298 299
				'123' => 'aaa',
				'124' => 'bbb',
Alexander Makarov committed
300 301
			],
			'y' => [
302
				'345' => 'ccc',
Alexander Makarov committed
303 304
			],
		], $result);
305
	}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

	public function testKeyExists()
	{
		$array = [
			'a' => 1,
			'B' => 2,
		];
		$this->assertTrue(ArrayHelper::keyExists('a', $array));
		$this->assertFalse(ArrayHelper::keyExists('b', $array));
		$this->assertTrue(ArrayHelper::keyExists('B', $array));
		$this->assertFalse(ArrayHelper::keyExists('c', $array));

		$this->assertTrue(ArrayHelper::keyExists('a', $array, false));
		$this->assertTrue(ArrayHelper::keyExists('b', $array, false));
		$this->assertTrue(ArrayHelper::keyExists('B', $array, false));
		$this->assertFalse(ArrayHelper::keyExists('c', $array, false));
	}
323
}