Skip to content

Commit

Permalink
Unit test ArrayHelper::remove method
Browse files Browse the repository at this point in the history
  • Loading branch information
nmolham-godaddy committed Aug 26, 2024
1 parent 86eb78c commit 332b89a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
53 changes: 47 additions & 6 deletions tests/unit/Helpers/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,52 @@ public function providerExcept(): Generator
}

/**
* Tests that can remove items from an array.
*
* @covers ::remove()
* @dataProvider providerCanRemove
*
* @param array<string, mixed> $input
* @param string|string[] $keysToRemove
* @param array $expected
*/
public function testCanRemove(): void
public function testCanRemoveItemsFromArray(array $input, $keysToRemove, array $expected) : void
{
$this->markTestIncomplete('TODO');
ArrayHelper::remove($input, $keysToRemove);
$this->assertEquals($expected, $input);
}

/** @see testCanRemoveItemsFromArray */
public function providerCanRemove() : Generator
{
yield 'remove 1 key from one item array' => [
'input' => ['test' => 1],
'keysToRemove' => 'test',
'expected' => [],
];

yield 'remove 1 key from two item array' => [
'input' => ['test' => 2, 'second' => ['nested' => 3]],
'keysToRemove' => 'second',
'expected' => ['test' => 2],
];

yield 'remove 2 keys' => [
'input' => ['test' => 2, 'second' => ['nested' => 3], 'third' => 4],
'keysToRemove' => ['second', 'third'],
'expected' => ['test' => 2],
];

yield 'remove 2 keys that are one level deep' => [
'input' => [
'resource' => [
'first' => 1,
'second' => 2,
],
],
'keysToRemove' => ['resource.first', 'resource.second'],
'expected' => ['resource' => []],
];
}

/**
Expand Down Expand Up @@ -215,22 +256,22 @@ protected function getArrayAccessObject(): ArrayAccess
return new class implements ArrayAccess
{

public function offsetExists($offset)
public function offsetExists($offset) : bool
{
return true;
}

public function offsetGet($offset)
public function offsetGet($offset) : int
{
return 1;
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value) : void
{
// TODO: Implement offsetSet() method.
}

public function offsetUnset($offset)
public function offsetUnset($offset) : void
{
// TODO: Implement offsetUnset() method.
}
Expand Down
2 changes: 1 addition & 1 deletion woocommerce/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function except(array $array, $keys) : array
* @param array $array
* @param array|string $keys
*/
public static function remove(array &$array, $keys)
public static function remove(array &$array, $keys) : void
{
$original = &$array;

Expand Down

0 comments on commit 332b89a

Please sign in to comment.