@@ -516,6 +516,106 @@ public function testGet()
516
516
$ this ->assertSame ('bar ' , Arr::get (['' => ['' => 'bar ' ]], '. ' ));
517
517
}
518
518
519
+ public function testItGetsAString ()
520
+ {
521
+ $ test_array = ['string ' => 'foo bar ' , 'integer ' => 1234 ];
522
+
523
+ // Test string values are returned as strings
524
+ $ this ->assertSame (
525
+ 'foo bar ' , Arr::string ($ test_array , 'string ' )
526
+ );
527
+
528
+ // Test that default string values are returned for missing keys
529
+ $ this ->assertSame (
530
+ 'default ' , Arr::string ($ test_array , 'missing_key ' , 'default ' )
531
+ );
532
+
533
+ // Test that an exception is raised if the value is not a string
534
+ $ this ->expectException (InvalidArgumentException::class);
535
+ $ this ->expectExceptionMessageMatches ('#^Array value for key \[integer\] must be a string, (.*) found.# ' );
536
+ Arr::string ($ test_array , 'integer ' );
537
+ }
538
+
539
+ public function testItGetsAnInteger ()
540
+ {
541
+ $ test_array = ['string ' => 'foo bar ' , 'integer ' => 1234 ];
542
+
543
+ // Test integer values are returned as integers
544
+ $ this ->assertSame (
545
+ 1234 , Arr::integer ($ test_array , 'integer ' )
546
+ );
547
+
548
+ // Test that default integer values are returned for missing keys
549
+ $ this ->assertSame (
550
+ 999 , Arr::integer ($ test_array , 'missing_key ' , 999 )
551
+ );
552
+
553
+ // Test that an exception is raised if the value is not an integer
554
+ $ this ->expectException (InvalidArgumentException::class);
555
+ $ this ->expectExceptionMessageMatches ('#^Array value for key \[string\] must be an integer, (.*) found.# ' );
556
+ Arr::integer ($ test_array , 'string ' );
557
+ }
558
+
559
+ public function testItGetsAFloat ()
560
+ {
561
+ $ test_array = ['string ' => 'foo bar ' , 'float ' => 12.34 ];
562
+
563
+ // Test float values are returned as floats
564
+ $ this ->assertSame (
565
+ 12.34 , Arr::float ($ test_array , 'float ' )
566
+ );
567
+
568
+ // Test that default float values are returned for missing keys
569
+ $ this ->assertSame (
570
+ 56.78 , Arr::float ($ test_array , 'missing_key ' , 56.78 )
571
+ );
572
+
573
+ // Test that an exception is raised if the value is not a float
574
+ $ this ->expectException (InvalidArgumentException::class);
575
+ $ this ->expectExceptionMessageMatches ('#^Array value for key \[string\] must be a float, (.*) found.# ' );
576
+ Arr::float ($ test_array , 'string ' );
577
+ }
578
+
579
+ public function testItGetsABoolean ()
580
+ {
581
+ $ test_array = ['string ' => 'foo bar ' , 'boolean ' => true ];
582
+
583
+ // Test boolean values are returned as booleans
584
+ $ this ->assertSame (
585
+ true , Arr::boolean ($ test_array , 'boolean ' )
586
+ );
587
+
588
+ // Test that default boolean values are returned for missing keys
589
+ $ this ->assertSame (
590
+ true , Arr::boolean ($ test_array , 'missing_key ' , true )
591
+ );
592
+
593
+ // Test that an exception is raised if the value is not a boolean
594
+ $ this ->expectException (InvalidArgumentException::class);
595
+ $ this ->expectExceptionMessageMatches ('#^Array value for key \[string\] must be a boolean, (.*) found.# ' );
596
+ Arr::boolean ($ test_array , 'string ' );
597
+ }
598
+
599
+ public function testItGetsAnArray ()
600
+ {
601
+ $ test_array = ['string ' => 'foo bar ' , 'array ' => ['foo ' , 'bar ' ]];
602
+
603
+ // Test array values are returned as arrays
604
+ $ this ->assertSame (
605
+ ['foo ' , 'bar ' ], Arr::array ($ test_array , 'array ' )
606
+ );
607
+
608
+ // Test that default array values are returned for missing keys
609
+ $ this ->assertSame (
610
+ [1 , 'two ' ], Arr::array ($ test_array , 'missing_key ' , [1 , 'two ' ])
611
+ );
612
+
613
+ // Test that an exception is raised if the value is not an array
614
+ $ this ->expectException (InvalidArgumentException::class);
615
+ $ this ->expectExceptionMessageMatches ('#^Array value for key \[string\] must be an array, (.*) found.# ' );
616
+ Arr::array ($ test_array , 'string ' );
617
+ }
618
+
519
619
public function testHas ()
520
620
{
521
621
$ array = ['products.desk ' => ['price ' => 100 ]];
0 commit comments