1+ <?php namespace lang \reflection \unittest ;
2+
3+ use ReflectionFunction ;
4+ use lang \Error ;
5+ use lang \reflection \Routine ;
6+ use test \{Assert , Expect , Test };
7+
8+ class ArgumentPassingTest {
9+
10+ #[Test]
11+ public function pass_ordered () {
12+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
13+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 ]));
14+ }
15+
16+ #[Test]
17+ public function pass_ordered_null () {
18+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
19+ Assert::equals ([null , 2 ], Routine::pass ($ f , [null , 2 ]));
20+ }
21+
22+ #[Test, Expect(class: Error::class, message: 'Missing parameter $a ' )]
23+ public function missing () {
24+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
25+ Routine::pass ($ f , []);
26+ }
27+
28+ #[Test, Expect(class: Error::class, message: 'Missing parameter $b ' )]
29+ public function missing_ordered () {
30+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
31+ Routine::pass ($ f , [1 ]);
32+ }
33+
34+ #[Test]
35+ public function pass_named () {
36+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
37+ Assert::equals ([1 , 2 ], Routine::pass ($ f , ['a ' => 1 , 'b ' => 2 ]));
38+ }
39+
40+ #[Test]
41+ public function pass_named_null () {
42+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
43+ Assert::equals ([null , 2 ], Routine::pass ($ f , ['a ' => null , 'b ' => 2 ]));
44+ }
45+
46+ #[Test]
47+ public function pass_named_out_of_order () {
48+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
49+ Assert::equals ([1 , 2 ], Routine::pass ($ f , ['b ' => 2 , 'a ' => 1 ]));
50+ }
51+
52+ #[Test]
53+ public function omit_optional () {
54+ $ f = new ReflectionFunction (fn ($ a , $ b = 0 , $ c = 0 ) => null );
55+ Assert::equals ([1 , 0 , 2 ], Routine::pass ($ f , ['a ' => 1 , 'c ' => 2 ]));
56+ }
57+
58+ #[Test, Expect(class: Error::class, message: 'Missing parameter $b ' )]
59+ public function missing_named () {
60+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
61+ Routine::pass ($ f , ['a ' => 1 ]);
62+ }
63+
64+ #[Test, Expect(class: Error::class, message: 'Unknown named parameter $unknown ' )]
65+ public function unknown_named () {
66+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
67+ Routine::pass ($ f , ['a ' => 1 , 'b ' => 2 , 'unknown ' => null ]);
68+ }
69+
70+ #[Test]
71+ public function pass_named_and_ordered () {
72+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
73+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 'b ' => 2 ]));
74+ }
75+
76+ #[Test]
77+ public function pass_too_many () {
78+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
79+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 , 3 ]));
80+ }
81+
82+ #[Test]
83+ public function pass_optional () {
84+ $ f = new ReflectionFunction (fn ($ a , $ b = 0 ) => null );
85+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 ]));
86+ }
87+
88+ #[Test]
89+ public function pass_without_optional () {
90+ $ f = new ReflectionFunction (fn ($ a , $ b = 0 ) => null );
91+ Assert::equals ([1 , 0 ], Routine::pass ($ f , [1 ]));
92+ }
93+
94+ #[Test]
95+ public function pass_variadic () {
96+ $ f = new ReflectionFunction (fn (... $ a ) => null );
97+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 ]));
98+ }
99+
100+ #[Test]
101+ public function pass_variadic_after () {
102+ $ f = new ReflectionFunction (fn ($ a , ... $ b ) => null );
103+ Assert::equals ([1 , 2 , 3 ], Routine::pass ($ f , [1 , 2 , 3 ]));
104+ }
105+ }
0 commit comments