Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue when trying to check if a multidimensional array is dirty… #19272

80 changes: 62 additions & 18 deletions tests/framework/helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1437,26 +1437,70 @@ public function testArrayAccessWithMagicProperty()
$this->assertEquals('ta-da', ArrayHelper::getValue($model, 'moreMagic'));
}

public function testRecursiveSort()
/**
* @dataProvider dataProviderRecursiveSort
*
* @return void
*/
public function testRecursiveSort($expected_result, $input_array)
{
// empty array
$empty_array = [];
ArrayHelper::recursiveSort($empty_array);
$this->assertEquals([], $empty_array);

// assoc array
$assoc_array = ['foo' => ['bar' => 1, 'baz' => 2]];
ArrayHelper::recursiveSort($assoc_array);
$this->assertEquals(['foo' => ['baz' => 2, 'bar' => 1]], $assoc_array);
$actual = ArrayHelper::recursiveSort($input_array);
$this->assertEquals($expected_result, $actual);
}

if (PHP_VERSION_ID < 70000) {
$this->markTestSkipped('Can not be tested on PHP < 7.0');
} else {
// index array
$index_array = [['bar' => 1], ['baz' => 2]];
ArrayHelper::recursiveSort($index_array);
$this->assertEquals([['baz' => 2], ['bar' => 1]], $index_array);
}
/**
* Data provider for [[testRecursiveSort()]].
* @return array test data
*/
public function dataProviderRecursiveSort()
{
return [
//Normal index array
[
[1, 2, 3, 4],
[4, 1, 3, 2]
],
//Normal associative array
[
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
['b' => 2, 'a' => 1, 'd' => 4, 'c' => 3],
],
//Normal index array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate

Copy link
Contributor

@WinterSilence WinterSilence Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use something like yii2-faker to generate random data. add mixed arrays in provider, you can add your db data

[
[1, 2, 3, 4],
[4, 1, 3, 2]
],
//Multidimensional associative array
[
[
'a' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
'b' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
'c' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
'd' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
],
[
'b' => ['a' => 1, 'd' => 4, 'b' => 2, 'c' => 3],
'd' => ['b' => 2, 'c' => 3, 'a' => 1, 'd' => 4],
'c' => ['c' => 3, 'a' => 1, 'd' => 4, 'b' => 2],
'a' => ['d' => 4, 'b' => 2, 'c' => 3, 'a' => 1],
],
],
//Multidimensional associative array
[
[
'a' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]],
'b' => ['a' => 1, 'b' => 2, 'c' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], 'd' => 4],
'c' => ['a' => 1, 'b' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], 'c' => 3, 'd' => 4],
'd' => ['a' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], 'b' => 2, 'c' => 3, 'd' => 4],
],
[
'b' => ['a' => 1, 'd' => 4, 'b' => 2, 'c' => ['b' => 2, 'c' => 3, 'a' => 1, 'd' => 4]],
'd' => ['b' => 2, 'c' => 3, 'a' => ['a' => 1, 'd' => 4, 'b' => 2, 'c' => 3], 'd' => 4],
'c' => ['c' => 3, 'a' => 1, 'd' => 4, 'b' => ['c' => 3, 'a' => 1, 'd' => 4, 'b' => 2]],
'a' => ['d' => ['d' => 4, 'b' => 2, 'c' => 3, 'a' => 1], 'b' => 2, 'c' => 3, 'a' => 1],
]
],
];
}
}

Expand Down