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, Expect(class: Error::class, message: 'Missing parameter $a ' )]
17+ public function missing () {
18+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
19+ Routine::pass ($ f , []);
20+ }
21+
22+ #[Test, Expect(class: Error::class, message: 'Missing parameter $b ' )]
23+ public function missing_ordered () {
24+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
25+ Routine::pass ($ f , [1 ]);
26+ }
27+
28+ #[Test]
29+ public function pass_named () {
30+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
31+ Assert::equals ([1 , 2 ], Routine::pass ($ f , ['a ' => 1 , 'b ' => 2 ]));
32+ }
33+
34+ #[Test]
35+ public function pass_named_out_of_order () {
36+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
37+ Assert::equals ([1 , 2 ], Routine::pass ($ f , ['b ' => 2 , 'a ' => 1 ]));
38+ }
39+
40+ #[Test, Expect(class: Error::class, message: 'Missing parameter $b ' )]
41+ public function missing_named () {
42+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
43+ Routine::pass ($ f , ['a ' => 1 ]);
44+ }
45+
46+ #[Test, Expect(class: Error::class, message: 'Unknown named parameter $unknown ' )]
47+ public function unknown_named () {
48+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
49+ Routine::pass ($ f , ['a ' => 1 , 'b ' => 2 , 'unknown ' => null ]);
50+ }
51+
52+ #[Test]
53+ public function pass_named_and_ordered () {
54+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
55+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 'b ' => 2 ]));
56+ }
57+
58+ #[Test]
59+ public function pass_too_many () {
60+ $ f = new ReflectionFunction (fn ($ a , $ b ) => null );
61+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 , 3 ]));
62+ }
63+
64+ #[Test]
65+ public function pass_optional () {
66+ $ f = new ReflectionFunction (fn ($ a , $ b = 0 ) => null );
67+ Assert::equals ([1 , 2 ], Routine::pass ($ f , [1 , 2 ]));
68+ }
69+
70+ #[Test]
71+ public function pass_without_optional () {
72+ $ f = new ReflectionFunction (fn ($ a , $ b = 0 ) => null );
73+ Assert::equals ([1 , 0 ], Routine::pass ($ f , [1 ]));
74+ }
75+ }
0 commit comments