55use Error ;
66use Koded \Stdlib \Arguments ;
77use Koded \Stdlib \Config ;
8+ use Koded \Stdlib \ExtendedArguments ;
89use Koded \Stdlib \Tapped ;
910use PHPUnit \Framework \TestCase ;
1011use function Koded \Stdlib \tap ;
@@ -15,9 +16,9 @@ class TapFunctionTest extends TestCase
1516
1617 public function test_object_with_callback ()
1718 {
18- $ conf = tap (new Config , function (Config $ c ) {
19- $ c ->silent (true );
20- $ c ->foo = 'bar ' ;
19+ $ conf = tap (new Config , function (Config $ conf ) {
20+ $ conf ->silent (true );
21+ $ conf ->foo = 'bar ' ;
2122 });
2223
2324 $ this ->assertInstanceOf (Config::class, $ conf );
@@ -34,19 +35,41 @@ public function test_object_without_callback()
3435 $ this ->assertSame ('bar ' , $ conf ->foo );
3536 }
3637
38+ public function test_extended_arguments ()
39+ {
40+ $ args = tap (new ExtendedArguments , function (ExtendedArguments $ args ) {
41+ $ args
42+ ->import ((array )null )
43+ ->set ('foo ' , 'bar ' );
44+ });
45+
46+ $ this ->assertSame ('bar ' , $ args ->get ('foo ' ));
47+ }
48+
3749 public function test_with_array_value ()
3850 {
39- $ arr = tap ([], function (&$ data ) {
40- $ data ['foo ' ] = 'bar ' ;
51+ $ arr = tap ([], function (&$ arr ) {
52+ $ arr ['foo ' ] = 'bar ' ;
4153 });
4254
4355 $ this ->assertSame (['foo ' => 'bar ' ], $ arr );
4456 }
4557
58+ public function test_array_value_without_callback ()
59+ {
60+ $ original = [];
61+ $ arr = tap ($ original );
62+
63+ $ original ['foo ' ] = 'bar ' ;
64+
65+ $ this ->assertSame (['foo ' => 'bar ' ], $ original );
66+ $ this ->assertInstanceOf (Tapped::class, $ arr );
67+ }
68+
4669 public function test_chainable_methods ()
4770 {
48- $ data = tap (new Arguments (['foo ' => 'bar ' ]), function ($ args ) {
49- $ args
71+ $ data = tap (new Arguments (['foo ' => 'bar ' ]), function ($ data ) {
72+ $ data
5073 ->set ('bar ' , [1 , 2 , 3 ])
5174 ->delete ('bar.1 ' )
5275 ->import ([
@@ -68,19 +91,18 @@ public function test_chainable_methods()
6891 );
6992 }
7093
71- public function test_with_primitive_value ()
94+ public function test_primitive_by_value ()
7295 {
73- $ val1 = tap (42 , function ($ v ) {
74- $ v = 'fubar ' ;
75- });
76- $ this ->assertSame (42 , $ val1 ,
77- 'The tapped value is not changed ' );
96+ $ value = tap (42 , function ($ value ) { $ value = 'fubar ' ; });
97+ $ this ->assertSame (42 , $ value ,
98+ 'The tapped value is not changed (passed by value) ' );
99+ }
78100
79- $ val2 = tap ( 42 , function (& $ v ) {
80- $ v = ' fubar ' ;
81- });
82- $ this ->assertSame ('fubar ' , $ val2 ,
83- 'The tapped value is changed (passed by reference) ' );
101+ public function test_primitive_by_reference ()
102+ {
103+ $ value = tap ( 42 , function (& $ value ) { $ value = ' fubar ' ; });
104+ $ this ->assertSame ('fubar ' , $ value ,
105+ 'The tapped value is changed (passed as reference) ' );
84106 }
85107
86108 public function test_unreasonable_use ()
0 commit comments