From b91606ada9c7c9705898764db848a7f6a86b1749 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sun, 15 Sep 2019 20:14:23 +0200 Subject: [PATCH] Increase Mutation Score Indicator (MSI) by creating stronger tests of Arrays class --- src/Utilities/Arrays.php | 38 +- tests/Utilities/ArraysTest.php | 896 ++++++++++++++++++++++++++++++--- 2 files changed, 853 insertions(+), 81 deletions(-) diff --git a/src/Utilities/Arrays.php b/src/Utilities/Arrays.php index e3d57f9..0a77a3f 100644 --- a/src/Utilities/Arrays.php +++ b/src/Utilities/Arrays.php @@ -116,7 +116,7 @@ class Arrays * @param string $separator (optional) Separator used between values. Default: ",". * @return null|string */ - public static function values2csv(array $array, $separator = ',') + public static function values2csv(array $array, string $separator = ','): ?string { // No elements? Nothing to do if (empty($array)) { @@ -157,9 +157,9 @@ class Arrays * first element is returned (first of the First array). * @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; } @@ -175,21 +175,21 @@ class Arrays * first element is returned (first of the first array). * @return mixed */ - public static function getFirstElement(array $array, $firstLevelOnly = true) + public static function getFirstElement(array $array, bool $firstLevelOnly = true) { // No elements? Nothing to do if (empty($array)) { return null; } - $firstKey = self::getFirstKey($array); - $first = $array[$firstKey]; + $firstKey = static::getFirstKey($array); + $result = $array[$firstKey]; - if (!$firstLevelOnly && is_array($first)) { - $first = self::getFirstElement($first, $firstLevelOnly); + if (!$firstLevelOnly && is_array($result)) { + $result = static::getFirstElement($result, $firstLevelOnly); } - return $first; + return $result; } /** @@ -219,9 +219,9 @@ class Arrays * last element is returned (last of the latest array). * @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; } @@ -237,7 +237,7 @@ class Arrays * last element is returned (last of the latest array). * @return mixed */ - public static function getLastElement(array $array, $firstLevelOnly = true) + public static function getLastElement(array $array, bool $firstLevelOnly = true) { // No elements? Nothing to do if (empty($array)) { @@ -247,7 +247,7 @@ class Arrays $last = end($array); if (!$firstLevelOnly && is_array($last)) { - $last = self::getLastElement($last, $firstLevelOnly); + $last = static::getLastElement($last, $firstLevelOnly); } return $last; @@ -290,30 +290,30 @@ class Arrays * @param array $array The array to get the last row of * @return mixed */ - public static function getLastRow(array $array) + public static function getLastRow(array $array): ?array { // No elements? Nothing to do if (empty($array)) { return null; } - $effect = []; + $result = []; $last = end($array); if (is_array($last)) { // 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? * Let's use the previous candidate */ - if (!is_array($effect) || self::isEmptyArray($effect)) { - $effect = $last; + if (!is_array($result) || static::isEmptyArray($result)) { + $result = $last; } } - return $effect; + return $result; } /** diff --git a/tests/Utilities/ArraysTest.php b/tests/Utilities/ArraysTest.php index 82e0c3e..385fc7a 100644 --- a/tests/Utilities/ArraysTest.php +++ b/tests/Utilities/ArraysTest.php @@ -128,7 +128,7 @@ class ArraysTest extends BaseTestCase * * @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: // @@ -137,16 +137,7 @@ class ArraysTest extends BaseTestCase // 1,2,3,45 - actual Locale::setLocale(LC_ALL, 'en', 'US'); - self::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' - ); + static::assertSame($expected, Arrays::values2csv($array, $separator), $description); } public function testGetFirstKey() @@ -166,43 +157,100 @@ class ArraysTest extends BaseTestCase self::assertEquals('amet', Arrays::getLastKey($this->complexArray)); } - public function testGetFirstElement() - { - // Negative cases - self::assertNull(Arrays::getFirstElement([])); + /** + * @param string $description + * @param $expected + * @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 - self::assertEquals('Lorem', Arrays::getFirstElement($this->simpleArray)); - self::assertEquals('Lorem', Arrays::getFirstElement($this->simpleArray)); - self::assertEquals('lorem', Arrays::getFirstElement($this->twoDimensionsArray, false)); - self::assertEquals('sit', Arrays::getFirstElement($this->complexArray, false)); + return; + } + + static::assertSame($expected, Arrays::getFirstElement($array, $firstLevelOnly), $description); } - public function testIsFirstElement() - { - self::assertTrue(Arrays::isFirstElement($this->simpleArray, 'Lorem')); - self::assertFalse(Arrays::isFirstElement($this->simpleArray, 'dolor')); - self::assertFalse(Arrays::isFirstElement($this->simpleArray, ' ')); - self::assertFalse(Arrays::isFirstElement($this->simpleArray, null)); + /** + * @param string $description + * @param bool $expected + * @param array $array + * @param $element + * @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() - { - // Negative cases - self::assertNull(Arrays::getLastElement([])); + /** + * @param string $description + * @param $expected + * @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 - self::assertEquals('amet', Arrays::getLastElement($this->simpleArray)); - self::assertEquals('eleifend', Arrays::getLastElement($this->twoDimensionsArray, false)); - self::assertEquals('primis', Arrays::getLastElement($this->complexArray, false)); + return; + } + + static::assertSame($expected, Arrays::getLastElement($array, $firstLevelOnly), $description); } - public function testIsLastElement() - { - self::assertTrue(Arrays::isLastElement($this->simpleArray, 'amet')); - self::assertFalse(Arrays::isLastElement($this->simpleArray, 'ipsum')); - self::assertFalse(Arrays::isLastElement($this->simpleArray, '')); - self::assertFalse(Arrays::isLastElement($this->simpleArray, null)); + /** + * @param string $description + * @param bool $expected + * @param array $array + * @param $element + * @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() @@ -213,26 +261,16 @@ class ArraysTest extends BaseTestCase 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 - 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)); + static::assertSame($expected, Arrays::getLastRow($array), $description); } /** @@ -898,7 +936,7 @@ letsTest[2] = value_2;'; self::assertEquals($effect, Arrays::setPositions($this->twoDimensionsArray)); - // Positive case - multi-level array + // Positive case - multidimensional array $array = [ 'lorem', 'ipsum' => [ @@ -1102,7 +1140,7 @@ letsTest[2] = value_2;'; // Empty array self::assertNull(Arrays::implodeSmart([], $separator)); - // Simple, one-dimension array + // Simple, One-dimensional array self::assertEquals(implode($separator, $this->simpleArray), Arrays::implodeSmart($this->simpleArray, $separator)); // An array with elements that contain separator @@ -2140,7 +2178,7 @@ letsTest[2] = value_2;'; ]; } - public function provideArrayValues2csv() + public function provideArrayValues2csv(): ?Generator { yield[ 'An empty array', @@ -2321,6 +2359,22 @@ letsTest[2] = value_2;'; ], ' // ', ]; + + yield[ + 'With HTML code', + "
abc
,def,
ghi
\nc,d", + [ + [ + '<div>abc</div>', + 'def', + '<div>ghi</div>', + ], + [ + 'c', + 'd', + ], + ], + ]; } 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} */