Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/tests-and-bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed Dec 18, 2024
2 parents a8470da + fc2199a commit 9978fc6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ jobs:
- name: Set up StellarWP Arrays
run: |
${SLIC_BIN} use arrays
${SLIC_BIN} composer install --ignore-platform-reqs
${SLIC_BIN} composer install
- name: Run suite wpunit
run: ${SLIC_BIN} run ${{ matrix.suite }} --ext DotReporter
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
}
],
"minimum-stability": "stable",
"require": {},
"require": {
"illuminate/collections": "^8.0"
},
"require-dev": {
"codeception/module-asserts": "^1.0",
"codeception/module-cli": "^1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/Arrays/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static function except( $array, $keys ) {
/**
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param \ArrayAccess|Enumerable|array $array
* @param string|int|float $key
*
* @return bool
Expand Down Expand Up @@ -817,7 +817,7 @@ public static function map_or_discard( $keys, array $map, bool &$found = true )
*
* @return array
*/
function merge_recursive( array &$array1, array &$array2 ): array {
public static function merge_recursive( array &$array1, array &$array2 ): array {
$merged = $array1;

foreach ( $array2 as $key => &$value ) {
Expand Down Expand Up @@ -1206,7 +1206,7 @@ public static function sort_by_priority( $array ): array {
*
* @return int
*/
protected static function sort_by_priority_comparison( $a, $b ): int {
public static function sort_by_priority_comparison( $a, $b ): int {
if ( is_array( $a ) ) {
$a_priority = $a['priority'];
} else {
Expand Down
87 changes: 87 additions & 0 deletions tests/wpunit/UndotTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace StellarWP\Arrays;

use StellarWP\Arrays\Tests\ArraysTestCase;
Expand All @@ -12,4 +13,90 @@ public function test_undot() {
$array = [ 'foo.bar' => 'baz' ];
$this->assertTrue(Arr::has(Arr::undot($array), 'foo.bar'));
}

/**
* @test
*/
public function it_properly_expands_array()
{
$dotted = [
'one.two.three.five.eight' => 'fibonacci',
];

$expected = [
'one' => [
'two' => [
'three' => [
'five' => [
'eight' => 'fibonacci'
]
]
]
],
];

$this->assertSame($expected, Arr::undot($dotted));
}

/**
* @test
*/
public function it_expands_array_with_leaves()
{
$dotted = [
'one.first_leaf' => true,
'one.two.second_leaf' => true,
'one.two.three.third_leaf' => true,
'one.two.three.five.fifth_leaf' => true,
'one.two.three.five.eight' => 'fibonacci',
];

$expected = [
'one' => [
'first_leaf' => true,
'two' => [
'second_leaf' => true,
'three' => [
'third_leaf' => true,
'five' => [
'fifth_leaf' => true,
'eight' => 'fibonacci'
]
]
]
],
];

$this->assertSame($expected, Arr::undot($dotted));
}

/**
* @test
*/
public function it_expands_nested_arrays_with_numerical_keys()
{
$dotted = [
'first_array.0' => 'bacon',
'first_array.1' => 'ham',
'first_array.2' => 'cheese',
'second_array.0' => 'bacon',
'second_array.1' => 'egg',
'second_array.2' => 'cheese',
];

$expected = [
'first_array' => [
'bacon',
'ham',
'cheese',
],
'second_array' => [
'bacon',
'egg',
'cheese'
]
];

$this->assertSame($expected, Arr::undot($dotted));
}
}

0 comments on commit 9978fc6

Please sign in to comment.