From bec2a5e0d288df676033bdb9dbfee41f2a6cbbfc Mon Sep 17 00:00:00 2001 From: Eric Gesemann Date: Tue, 26 Mar 2024 09:09:07 +0100 Subject: [PATCH] add StaticArrayUtilTest --- src/StaticUtil/StaticArrayUtil.php | 3 +- tests/StaticUtil/SUtilsTest.php | 2 +- tests/StaticUtil/StaticArrayUtilTest.php | 77 ++++++++++++++++++++++++ tests/Util/Type/ArrayUtilTest.php | 2 +- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/StaticUtil/StaticArrayUtilTest.php diff --git a/src/StaticUtil/StaticArrayUtil.php b/src/StaticUtil/StaticArrayUtil.php index ade02c30..35527399 100644 --- a/src/StaticUtil/StaticArrayUtil.php +++ b/src/StaticUtil/StaticArrayUtil.php @@ -35,7 +35,6 @@ public static function insertBeforeKey(array &$array, array|string $keys, string } } - /** * Insert a value into an existing array by key name. * @@ -67,7 +66,7 @@ public static function insertAfterKey(array &$array, string $key, mixed $value, if (false === $index && false === $options['attachMissingKey']) { return; } - $pos = false === $index ? \count($array) : $index + 1; + $pos = false === $index ? count($array) : $index + 1; $pos = $pos + $options['offset']; if ($newKey) { diff --git a/tests/StaticUtil/SUtilsTest.php b/tests/StaticUtil/SUtilsTest.php index 7edea4db..bddaba0d 100644 --- a/tests/StaticUtil/SUtilsTest.php +++ b/tests/StaticUtil/SUtilsTest.php @@ -1,6 +1,6 @@ getTestInstance(); + $array = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $instance::insertBeforeKey($array, 'e', 'f', 'F'); + $this->assertSame( + ['a' => 'A', 'b' => 'B', 'c' => 'C', 'f' => 'F'], $array); + + $instance::insertBeforeKey($array, ['z'], 'h', 'H'); + $this->assertSame( + ['a' => 'A', 'b' => 'B', 'c' => 'C', 'f' => 'F', 'h' => 'H'], $array); + + $instance::insertBeforeKey($array, ['f', 'h'], 'd', 'D'); + $this->assertSame( + ['a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D', 'f' => 'F', 'h' => 'H'], $array); + } + + public function testInsertAfterKey() + { + $arrayUtil = $this->getTestInstance(); + + $target = ['hello' => 'world']; + $arrayUtil->insertAfterKey($target, 'foo', 'bar'); + $this->assertSame(['hello' => 'world', 0 => 'bar'], $target); + + $target = ['hello' => 'world']; + $arrayUtil->insertAfterKey($target, 'foo', 'bar', 'foobar'); + $this->assertSame(['hello' => 'world', 'foobar' => 'bar'], $target); + + $target = ['hello' => 'world']; + $arrayUtil->insertAfterKey($target, 'foo', 'bar', null, ['attachMissingKey' => false]); + $this->assertSame(['hello' => 'world'], $target); + + $target = ['hello' => 'world', 'foo' => 'bar', 'heimrich' => 'hannot']; + $arrayUtil->insertAfterKey($target, 'hello', 'camp', 'contao', ['offset' => 1]); + $this->assertSame(['hello' => 'world', 'foo' => 'bar', 'contao' => 'camp', 'heimrich' => 'hannot'], $target); + + $target = ['1' => 'world', 'foo' => 'bar', 'heimrich' => 'hannot']; + $arrayUtil->insertAfterKey($target, 1, 'camp', 'contao', ['strict' => false]); + $this->assertSame(['1' => 'world', 'contao' => 'camp', 'foo' => 'bar', 'heimrich' => 'hannot'], $target); + + $target = ['1' => 'world', 'foo' => 'bar', 'heimrich' => 'hannot']; + $arrayUtil->insertAfterKey($target, 1, 'camp', 'contao', ['strict' => true]); + $this->assertSame(['1' => 'world', 'foo' => 'bar', 'heimrich' => 'hannot', 'contao' => 'camp'], $target); + } + + public function testRemoveValue() + { + $arrayUtil = $this->getTestInstance(); + + $array = [0 => 0, 1 => 1, 2 => 2]; + $result = $arrayUtil->removeValue(1, $array); + $this->assertTrue($result); + $this->assertCount(2, $array); + $this->assertArrayHasKey(0, $array); + $this->assertArrayHasKey(2, $array); + + $result = $arrayUtil->removeValue(1, $array); + $this->assertFalse($result); + } +} \ No newline at end of file diff --git a/tests/Util/Type/ArrayUtilTest.php b/tests/Util/Type/ArrayUtilTest.php index da878b00..b6cd3b1b 100644 --- a/tests/Util/Type/ArrayUtilTest.php +++ b/tests/Util/Type/ArrayUtilTest.php @@ -14,7 +14,7 @@ class ArrayUtilTest extends AbstractUtilsTestCase { - public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilder = null) + public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilder = null): ArrayUtil { return new ArrayUtil(); }