@@ -671,6 +671,61 @@ public function testItReturnsEmptyArrayForMissingKeyByDefault()
671671 $ this ->assertSame ([], Arr::array ($ data , 'missing_key ' ));
672672 }
673673
674+ public function test_it_throws_item_not_found_exception_when_array_is_empty_and_throw_on_not_found_is_true ()
675+ {
676+ $ this ->expectException (ItemNotFoundException::class);
677+ $ this ->expectExceptionMessage ('Array key [roles] not found. ' );
678+
679+ $ data = [];
680+
681+ Arr::array ($ data , 'roles ' , [], true );
682+ }
683+
684+ public function test_it_throws_item_not_found_exception_when_key_is_missing_and_throw_on_not_found_is_true ()
685+ {
686+ $ this ->expectException (ItemNotFoundException::class);
687+ $ this ->expectExceptionMessage ('Array key [roles] not found. ' );
688+
689+ $ data = ['name ' => 'Taylor ' ];
690+
691+ Arr::array ($ data , 'roles ' , [], true );
692+ }
693+
694+ public function test_it_supports_nested_keys_with_dot_notation ()
695+ {
696+ $ data = ['user ' => ['roles ' => ['admin ' ]]];
697+
698+ $ result = Arr::array ($ data , 'user.roles ' );
699+
700+ $ this ->assertSame (['admin ' ], $ result );
701+ }
702+
703+ public function test_it_throws_for_nested_non_array_value ()
704+ {
705+ $ this ->expectException (InvalidArgumentException::class);
706+ $ this ->expectExceptionMessageMatches (
707+ '#^Array value for key \[user.name\] must be an array, string found.# '
708+ );
709+
710+ $ data = ['user ' => ['name ' => 'Taylor ' ]];
711+
712+ Arr::array ($ data , 'user.name ' );
713+ }
714+
715+ public function test_it_should_not_throw_when_key_exists_even_if_equal_to_default ()
716+ {
717+ $ data = ['roles ' => []];
718+
719+ // In the current (buggy) version without Arr::has(),
720+ // this will wrongly throw ItemNotFoundException
721+ // because $value === $default ([] === []).
722+ $ result = Arr::array ($ data , 'roles ' , [], true );
723+
724+ $ this ->assertSame ([], $ result ); // expected to return the actual empty array
725+ }
726+
727+
728+
674729 public function testHas ()
675730 {
676731 $ array = ['products.desk ' => ['price ' => 100 ]];
0 commit comments