Increase Mutation Score Indicator (MSI) by creating stronger tests of Arrays class

This commit is contained in:
Meritoo
2019-09-15 20:14:23 +02:00
parent 1f45d38ab8
commit b91606ada9
2 changed files with 853 additions and 81 deletions

View File

@@ -116,7 +116,7 @@ class Arrays
* @param string $separator (optional) Separator used between values. Default: ",". * @param string $separator (optional) Separator used between values. Default: ",".
* @return null|string * @return null|string
*/ */
public static function values2csv(array $array, $separator = ',') public static function values2csv(array $array, string $separator = ','): ?string
{ {
// No elements? Nothing to do // No elements? Nothing to do
if (empty($array)) { if (empty($array)) {
@@ -157,9 +157,9 @@ class Arrays
* first element is returned (first of the First array). * first element is returned (first of the First array).
* @return bool * @return bool
*/ */
public static function isFirstElement(array $array, $element, $firstLevelOnly = true) public static function isFirstElement(array $array, $element, bool $firstLevelOnly = true): bool
{ {
$firstElement = self::getFirstElement($array, $firstLevelOnly); $firstElement = static::getFirstElement($array, $firstLevelOnly);
return $element === $firstElement; return $element === $firstElement;
} }
@@ -175,21 +175,21 @@ class Arrays
* first element is returned (first of the first array). * first element is returned (first of the first array).
* @return mixed * @return mixed
*/ */
public static function getFirstElement(array $array, $firstLevelOnly = true) public static function getFirstElement(array $array, bool $firstLevelOnly = true)
{ {
// No elements? Nothing to do // No elements? Nothing to do
if (empty($array)) { if (empty($array)) {
return null; return null;
} }
$firstKey = self::getFirstKey($array); $firstKey = static::getFirstKey($array);
$first = $array[$firstKey]; $result = $array[$firstKey];
if (!$firstLevelOnly && is_array($first)) { if (!$firstLevelOnly && is_array($result)) {
$first = self::getFirstElement($first, $firstLevelOnly); $result = static::getFirstElement($result, $firstLevelOnly);
} }
return $first; return $result;
} }
/** /**
@@ -219,9 +219,9 @@ class Arrays
* last element is returned (last of the latest array). * last element is returned (last of the latest array).
* @return bool * @return bool
*/ */
public static function isLastElement(array $array, $element, $firstLevelOnly = true) public static function isLastElement(array $array, $element, bool $firstLevelOnly = true): bool
{ {
$lastElement = self::getLastElement($array, $firstLevelOnly); $lastElement = static::getLastElement($array, $firstLevelOnly);
return $element === $lastElement; return $element === $lastElement;
} }
@@ -237,7 +237,7 @@ class Arrays
* last element is returned (last of the latest array). * last element is returned (last of the latest array).
* @return mixed * @return mixed
*/ */
public static function getLastElement(array $array, $firstLevelOnly = true) public static function getLastElement(array $array, bool $firstLevelOnly = true)
{ {
// No elements? Nothing to do // No elements? Nothing to do
if (empty($array)) { if (empty($array)) {
@@ -247,7 +247,7 @@ class Arrays
$last = end($array); $last = end($array);
if (!$firstLevelOnly && is_array($last)) { if (!$firstLevelOnly && is_array($last)) {
$last = self::getLastElement($last, $firstLevelOnly); $last = static::getLastElement($last, $firstLevelOnly);
} }
return $last; return $last;
@@ -290,30 +290,30 @@ class Arrays
* @param array $array The array to get the last row of * @param array $array The array to get the last row of
* @return mixed * @return mixed
*/ */
public static function getLastRow(array $array) public static function getLastRow(array $array): ?array
{ {
// No elements? Nothing to do // No elements? Nothing to do
if (empty($array)) { if (empty($array)) {
return null; return null;
} }
$effect = []; $result = [];
$last = end($array); $last = end($array);
if (is_array($last)) { if (is_array($last)) {
// We've got an array, so looking for the last row of array will be done recursively // We've got an array, so looking for the last row of array will be done recursively
$effect = self::getLastRow($last); $result = static::getLastRow($last);
/* /*
* The last row is not an array or it's an empty array? * The last row is not an array or it's an empty array?
* Let's use the previous candidate * Let's use the previous candidate
*/ */
if (!is_array($effect) || self::isEmptyArray($effect)) { if (!is_array($result) || static::isEmptyArray($result)) {
$effect = $last; $result = $last;
} }
} }
return $effect; return $result;
} }
/** /**

View File

@@ -128,7 +128,7 @@ class ArraysTest extends BaseTestCase
* *
* @dataProvider provideArrayValues2csv * @dataProvider provideArrayValues2csv
*/ */
public function testValues2csv($description, $expected, array $array, $separator = ',') public function testValues2csv(string $description, ?string $expected, array $array, string $separator = ','): void
{ {
// Required to avoid failure: // Required to avoid failure:
// //
@@ -137,16 +137,7 @@ class ArraysTest extends BaseTestCase
// 1,2,3,45 - actual // 1,2,3,45 - actual
Locale::setLocale(LC_ALL, 'en', 'US'); Locale::setLocale(LC_ALL, 'en', 'US');
self::assertSame($expected, Arrays::values2csv($array, $separator), $description); static::assertSame($expected, Arrays::values2csv($array, $separator), $description);
self::assertSame('', Arrays::values2csv($this->simpleArray), 'Simple array');
self::assertSame(
"lorem,ipsum,dolor,sit,amet\n"
. "consectetur,adipiscing,elit\n"
. 'donec,sagittis,fringilla,eleifend',
Arrays::values2csv($this->twoDimensionsArray),
'Two dimensions array'
);
} }
public function testGetFirstKey() public function testGetFirstKey()
@@ -166,43 +157,100 @@ class ArraysTest extends BaseTestCase
self::assertEquals('amet', Arrays::getLastKey($this->complexArray)); self::assertEquals('amet', Arrays::getLastKey($this->complexArray));
} }
public function testGetFirstElement() /**
{ * @param string $description
// Negative cases * @param $expected
self::assertNull(Arrays::getFirstElement([])); * @param array $array
* @param null|bool $firstLevelOnly
*
* @dataProvider provideFirstElement
*/
public function testGetFirstElement(
string $description,
$expected,
array $array,
?bool $firstLevelOnly = null
): void {
if (null === $firstLevelOnly) {
static::assertSame($expected, Arrays::getFirstElement($array), $description);
// Positive cases return;
self::assertEquals('Lorem', Arrays::getFirstElement($this->simpleArray)); }
self::assertEquals('Lorem', Arrays::getFirstElement($this->simpleArray));
self::assertEquals('lorem', Arrays::getFirstElement($this->twoDimensionsArray, false)); static::assertSame($expected, Arrays::getFirstElement($array, $firstLevelOnly), $description);
self::assertEquals('sit', Arrays::getFirstElement($this->complexArray, false));
} }
public function testIsFirstElement() /**
{ * @param string $description
self::assertTrue(Arrays::isFirstElement($this->simpleArray, 'Lorem')); * @param bool $expected
self::assertFalse(Arrays::isFirstElement($this->simpleArray, 'dolor')); * @param array $array
self::assertFalse(Arrays::isFirstElement($this->simpleArray, ' ')); * @param $element
self::assertFalse(Arrays::isFirstElement($this->simpleArray, null)); * @param bool $firstLevelOnly
*
* @dataProvider provideIsFirstElement
*/
public function testIsFirstElement(
string $description,
bool $expected,
array $array,
$element,
?bool $firstLevelOnly = null
): void {
if (null === $firstLevelOnly) {
static::assertSame($expected, Arrays::isFirstElement($array, $element), $description);
return;
}
static::assertSame($expected, Arrays::isFirstElement($array, $element, $firstLevelOnly), $description);
} }
public function testGetLastElement() /**
{ * @param string $description
// Negative cases * @param $expected
self::assertNull(Arrays::getLastElement([])); * @param array $array
* @param null|bool $firstLevelOnly
*
* @dataProvider provideLastElement
*/
public function testGetLastElement(
string $description,
$expected,
array $array,
?bool $firstLevelOnly = null
): void {
if (null === $firstLevelOnly) {
static::assertSame($expected, Arrays::getLastElement($array), $description);
// Positive cases return;
self::assertEquals('amet', Arrays::getLastElement($this->simpleArray)); }
self::assertEquals('eleifend', Arrays::getLastElement($this->twoDimensionsArray, false));
self::assertEquals('primis', Arrays::getLastElement($this->complexArray, false)); static::assertSame($expected, Arrays::getLastElement($array, $firstLevelOnly), $description);
} }
public function testIsLastElement() /**
{ * @param string $description
self::assertTrue(Arrays::isLastElement($this->simpleArray, 'amet')); * @param bool $expected
self::assertFalse(Arrays::isLastElement($this->simpleArray, 'ipsum')); * @param array $array
self::assertFalse(Arrays::isLastElement($this->simpleArray, '')); * @param $element
self::assertFalse(Arrays::isLastElement($this->simpleArray, null)); * @param null|bool $firstLevelOnly
*
* @dataProvider provideIsLastElement
*/
public function testIsLastElement(
string $description,
bool $expected,
array $array,
$element,
?bool $firstLevelOnly = null
): void {
if (null === $firstLevelOnly) {
static::assertSame($expected, Arrays::isLastElement($array, $element), $description);
return;
}
static::assertSame($expected, Arrays::isLastElement($array, $element, $firstLevelOnly), $description);
} }
public function testGetLastElementBreadCrumb() public function testGetLastElementBreadCrumb()
@@ -213,26 +261,16 @@ class ArraysTest extends BaseTestCase
self::assertEquals('amet/1/primis', Arrays::getLastElementBreadCrumb($this->complexArray)); self::assertEquals('amet/1/primis', Arrays::getLastElementBreadCrumb($this->complexArray));
} }
public function testGetLastRow(): void /**
* @param string $description
* @param null|array $expected
* @param array $array
*
* @dataProvider provideLastRow
*/
public function testGetLastRow(string $description, ?array $expected, array $array): void
{ {
// Negative cases static::assertSame($expected, Arrays::getLastRow($array), $description);
self::assertNull(Arrays::getLastRow([]));
// Positive cases
self::assertEquals([], Arrays::getLastRow($this->simpleArray));
self::assertEquals([], Arrays::getLastRow($this->simpleArrayWithKeys));
self::assertEquals([
'donec',
'sagittis',
'fringilla',
'eleifend',
], Arrays::getLastRow($this->twoDimensionsArray));
self::assertEquals([
'iaculis',
'primis',
], Arrays::getLastRow($this->complexArray));
} }
/** /**
@@ -898,7 +936,7 @@ letsTest[2] = value_2;';
self::assertEquals($effect, Arrays::setPositions($this->twoDimensionsArray)); self::assertEquals($effect, Arrays::setPositions($this->twoDimensionsArray));
// Positive case - multi-level array // Positive case - multidimensional array
$array = [ $array = [
'lorem', 'lorem',
'ipsum' => [ 'ipsum' => [
@@ -1102,7 +1140,7 @@ letsTest[2] = value_2;';
// Empty array // Empty array
self::assertNull(Arrays::implodeSmart([], $separator)); self::assertNull(Arrays::implodeSmart([], $separator));
// Simple, one-dimension array // Simple, One-dimensional array
self::assertEquals(implode($separator, $this->simpleArray), Arrays::implodeSmart($this->simpleArray, $separator)); self::assertEquals(implode($separator, $this->simpleArray), Arrays::implodeSmart($this->simpleArray, $separator));
// An array with elements that contain separator // An array with elements that contain separator
@@ -2140,7 +2178,7 @@ letsTest[2] = value_2;';
]; ];
} }
public function provideArrayValues2csv() public function provideArrayValues2csv(): ?Generator
{ {
yield[ yield[
'An empty array', 'An empty array',
@@ -2321,6 +2359,22 @@ letsTest[2] = value_2;';
], ],
' // ', ' // ',
]; ];
yield[
'With HTML code',
"<div>abc</div>,def,<div>ghi</div>\nc,d",
[
[
'&lt;div&gt;abc&lt;/div&gt;',
'def',
'&lt;div&gt;ghi&lt;/div&gt;',
],
[
'c',
'd',
],
],
];
} }
public function provideArrayValues2string() public function provideArrayValues2string()
@@ -2923,6 +2977,724 @@ letsTest[2] = value_2;';
]; ];
} }
public function provideIsFirstElement(): ?Generator
{
yield[
'An empty array (first level only)',
false,
[],
'',
];
yield[
'An empty array',
false,
[],
'',
false,
];
yield[
'Non-existing integer in array with integers (first level only)',
false,
[
1,
2,
3,
],
4,
];
yield[
'Existing integer in array with integers (first level only)',
true,
[
1,
2,
3,
],
1,
];
yield[
'Existing integer in array with integers',
true,
[
1,
2,
3,
],
1,
false,
];
yield[
'Non-existing integer in multidimensional array with integers (first level only)',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
9,
];
yield[
'Non-existing integer in multidimensional array with integers',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
9,
false,
];
yield[
'Existing integer in multidimensional array with integers, but first level only checked',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
1,
];
yield[
'Existing integer in multidimensional array with integers',
true,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
1,
false,
];
yield[
'Non-existing element in multidimensional array (first level only)',
false,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
9,
];
yield[
'Existing element in multidimensional array, but first level only checked',
false,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
'abc',
];
yield[
'Existing element in multidimensional array',
true,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
'abc',
false,
];
}
public function provideFirstElement(): ?Generator
{
yield[
'An empty array (first level only)',
null,
[],
];
yield[
'An empty array',
null,
[],
false,
];
yield[
'Multidimensional array (first level only)',
[
[
'abc',
2,
'def',
],
4,
],
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
];
yield[
'Multidimensional array',
'abc',
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
false,
];
}
public function provideIsLastElement(): ?Generator
{
yield[
'An empty array (first level only)',
false,
[],
'',
];
yield[
'An empty array',
false,
[],
'',
false,
];
yield[
'Non-existing integer in array with integers (first level only)',
false,
[
1,
2,
3,
],
4,
];
yield[
'Existing integer in array with integers (first level only)',
true,
[
1,
2,
3,
],
3,
];
yield[
'Existing integer in array with integers',
true,
[
1,
2,
3,
],
3,
false,
];
yield[
'Non-existing integer in multidimensional array with integers (first level only)',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
11,
];
yield[
'Non-existing integer in multidimensional array with integers',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
11,
false,
];
yield[
'Existing integer in multidimensional array with integers, but first level only checked',
false,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
10,
];
yield[
'Existing integer in multidimensional array with integers',
true,
[
[
[
1,
2,
3,
],
4,
],
5,
[
6,
7,
[
8,
9,
10,
],
],
],
10,
false,
];
yield[
'Non-existing element in multidimensional array (first level only)',
false,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
9,
];
yield[
'Existing element in multidimensional array, but first level only checked',
false,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
10,
'...',
'jkl',
],
],
],
'jkl',
];
yield[
'Existing element in multidimensional array',
true,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
10,
'...',
'jkl',
],
],
],
'jkl',
false,
];
}
public function provideLastElement(): ?Generator
{
yield[
'An empty array (first level only)',
null,
[],
];
yield[
'An empty array',
null,
[],
false,
];
yield[
'One-dimensional array (first level only)',
3,
[
1,
2,
3,
],
];
yield[
'One-dimensional array (first level only)',
3,
[
1,
2,
3,
],
false,
];
yield[
'Multidimensional array (first level only)',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
];
yield[
'Multidimensional array',
10,
[
[
[
'abc',
2,
'def',
],
4,
],
'---',
[
'ghi',
7,
[
'jkl',
'...',
10,
],
],
],
false,
];
}
public function provideLastRow(): ?Generator
{
yield[
'An empty array',
null,
[],
];
yield[
'One-dimensional array',
[],
[
'a',
'b',
1,
2,
],
];
yield[
'Multidimensional array with scalar as last element',
[],
[
'a',
[
'b',
'c',
],
[
'e',
'f',
],
1,
2,
],
];
yield[
'Multidimensional array with an empty array as last element',
[],
[
'a',
[
'b',
'c',
],
1,
2,
[],
],
];
yield[
'Multidimensional array',
[
'e',
'f',
],
[
'a',
[
'b',
'c',
],
1,
2,
[
'e',
'f',
],
],
];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */