Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yus-ham committed May 20, 2024
1 parent 80eb301 commit ff4a7fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ public static function toArray(mixed $object, array $properties = [], bool $recu
* @var Closure|string $name
*/
foreach ($properties[$className] as $key => $name) {
if ($name instanceof Closure) {
$result[$key] = $name($object);
continue;

Check warning on line 116 in src/ArrayHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ foreach ($properties[$className] as $key => $name) { if ($name instanceof Closure) { $result[$key] = $name($object); - continue; + break; } if (is_int($key)) { $result[$name] = $object->{$name};
}
if (is_int($key)) {
$result[(string)$name] = $object->$name;
$result[$name] = $object->$name;
} else {
$result[$key] = $name instanceof Closure ? $name($object) : self::getValue($object, $name);
$result[$key] = self::getValue($object, $name);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ArrayHelper/GetValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public function testGetValueByMatcher(): void

$taggedPost = ArrayHelper::getValue($posts, fn ($post) => isset($post->tag));
$this->assertEquals('hello test', $taggedPost->title);

$default = ArrayHelper::getValue([], fn ($post) => isset($post->tag), 'default');
$this->assertEquals($default, 'default');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Matcher cannot be applied to an object');
$taggedPost = ArrayHelper::getValue(new StaticObject(), fn ($post) => isset($post->tag));
}

public function dataProviderGetValueByPathFromArray(): array
Expand Down

0 comments on commit ff4a7fa

Please sign in to comment.