Skip to content

Commit caf9ff7

Browse files
committed
Add tests for constant closure parameter and return validation in RuntimeTypeChecker
1 parent 0364d9d commit caf9ff7

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

‎tests/Internal/Ast/NodeBuilderTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,48 @@
9999
->and($call->args[4]->value->value)->toBe(12)
100100
;
101101
});
102+
103+
test('createConstantClosureParamCheckCall creates FuncCall node for RuntimeTypeChecker::checkConstantClosureParam', function () {
104+
$paramVar = new Node\Expr\Variable('id');
105+
106+
$call = NodeBuilder::createConstantClosureParamCheckCall(
107+
'App\Config::Formatter',
108+
'id',
109+
$paramVar,
110+
'positive-int',
111+
15
112+
);
113+
114+
expect($call)->toBeInstanceOf(Node\Expr\FuncCall::class)
115+
->and($call->name->toString())->toBe('TypePHP\Internal\RuntimeTypeChecker::checkConstantClosureParam')
116+
->and($call->args)->toHaveCount(6)
117+
->and($call->args[0]->value->value)->toBe('App\Config::Formatter')
118+
->and($call->args[1]->value->value)->toBe('id')
119+
->and($call->args[2]->value)->toBe($paramVar)
120+
->and($call->args[3]->value->value)->toBe('positive-int')
121+
->and($call->args[4]->value)->toBeInstanceOf(Node\Scalar\MagicConst\File::class)
122+
->and($call->args[5]->value->value)->toBe(15)
123+
;
124+
});
125+
126+
test('createConstantClosureReturnCheckCall creates FuncCall node for RuntimeTypeChecker::checkConstantClosureReturn', function () {
127+
$valueExpr = new Node\Scalar\String_('test');
128+
129+
$call = NodeBuilder::createConstantClosureReturnCheckCall(
130+
'App\Config::Callback',
131+
$valueExpr,
132+
'int',
133+
20
134+
);
135+
136+
expect($call)->toBeInstanceOf(Node\Expr\FuncCall::class)
137+
->and($call->name->toString())->toBe('TypePHP\Internal\RuntimeTypeChecker::checkConstantClosureReturn')
138+
->and($call->args)->toHaveCount(5)
139+
->and($call->args[0]->value->value)->toBe('App\Config::Callback')
140+
->and($call->args[1]->value)->toBe($valueExpr)
141+
->and($call->args[2]->value->value)->toBe('int')
142+
->and($call->args[3]->value)->toBeInstanceOf(Node\Scalar\MagicConst\File::class)
143+
->and($call->args[4]->value->value)->toBe(20)
144+
;
145+
});
102146
});

‎tests/Internal/RuntimeTypeCheckerTest.php‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,74 @@ function vendorAction(): void
790790

791791
expect($res)->toBe(['user' => 'User']);
792792
});
793+
794+
test('checkConstantClosureParam validates arguments and respects config toggles and ignore tags', function () {
795+
$valid = RuntimeTypeChecker::checkConstantClosureParam(
796+
'App\Config::Formatter',
797+
'id',
798+
10,
799+
'positive-int',
800+
__FILE__,
801+
15
802+
);
803+
expect($valid)->toBe(10);
804+
805+
$invalid = RuntimeTypeChecker::checkConstantClosureParam(
806+
'App\Config::Formatter',
807+
'id',
808+
-5,
809+
'positive-int',
810+
__FILE__,
811+
15
812+
);
813+
expect($invalid)->toBeInstanceOf(ErrorMessage::class)
814+
->and($invalid->getMessage())->toContain('App\Config::Formatter(): Argument $id must be of type positive-int')
815+
;
816+
817+
Config::set(['params' => false]);
818+
expect(RuntimeTypeChecker::checkConstantClosureParam('App\Config::Formatter', 'id', -5, 'positive-int'))->toBe(-5);
819+
820+
Config::set(['enabled' => false, 'params' => true]);
821+
expect(RuntimeTypeChecker::checkConstantClosureParam('App\Config::Formatter', 'id', -5, 'positive-int'))->toBe(-5);
822+
823+
Config::set(['enabled' => true, 'params' => true]);
824+
$ignored = RuntimeCheckerIgnoredCaller::run(
825+
fn () => RuntimeTypeChecker::checkConstantClosureParam('App\Config::Formatter', 'id', -5, 'positive-int')
826+
);
827+
expect($ignored)->toBe(-5);
828+
});
829+
830+
test('checkConstantClosureReturn validates return values and respects config toggles and ignore tags', function () {
831+
$valid = RuntimeTypeChecker::checkConstantClosureReturn(
832+
'App\Config::Callback',
833+
42,
834+
'int',
835+
__FILE__,
836+
25
837+
);
838+
expect($valid)->toBe(42);
839+
840+
$invalid = RuntimeTypeChecker::checkConstantClosureReturn(
841+
'App\Config::Callback',
842+
'not_an_int',
843+
'int',
844+
__FILE__,
845+
25
846+
);
847+
expect($invalid)->toBeInstanceOf(ErrorMessage::class)
848+
->and($invalid->getMessage())->toContain('App\Config::Callback(): Return value must be of type int, string \'not_an_int\' returned')
849+
;
850+
851+
Config::set(['returns' => false]);
852+
expect(RuntimeTypeChecker::checkConstantClosureReturn('App\Config::Callback', 'not_an_int', 'int'))->toBe('not_an_int');
853+
854+
Config::set(['enabled' => false, 'returns' => true]);
855+
expect(RuntimeTypeChecker::checkConstantClosureReturn('App\Config::Callback', 'not_an_int', 'int'))->toBe('not_an_int');
856+
857+
Config::set(['enabled' => true, 'returns' => true]);
858+
$ignored = RuntimeCheckerIgnoredCaller::run(
859+
fn () => RuntimeTypeChecker::checkConstantClosureReturn('App\Config::Callback', 'not_an_int', 'int')
860+
);
861+
expect($ignored)->toBe('not_an_int');
862+
});
793863
});

0 commit comments

Comments
 (0)