Skip to content

Commit

Permalink
add StaticArrayUtilTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ericges committed Mar 26, 2024
1 parent 1fcefc6 commit bec2a5e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/StaticUtil/StaticArrayUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public static function insertBeforeKey(array &$array, array|string $keys, string
}
}


/**
* Insert a value into an existing array by key name.
*
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/StaticUtil/SUtilsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace StaticUtil;
namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\StaticUtil\StaticArrayUtil;
Expand Down
77 changes: 77 additions & 0 deletions tests/StaticUtil/StaticArrayUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\StaticUtil\StaticArrayUtil;
use PHPUnit\Framework\MockObject\MockBuilder;

class StaticArrayUtilTest extends ContaoTestCase
{
public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilder = null): StaticArrayUtil
{
return new StaticArrayUtil();
}

public function testInsertBeforeKey(): void
{
$instance = $this->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);
}
}
2 changes: 1 addition & 1 deletion tests/Util/Type/ArrayUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit bec2a5e

Please sign in to comment.