33
44use JmesPath \AstRuntime ;
55use JmesPath \CompilerRuntime ;
6+ use JmesPath \SyntaxErrorException ;
67use PHPUnit \Framework \TestCase ;
78
89class CompilerRuntimeTest extends TestCase
@@ -29,7 +30,7 @@ public function testFunctionNameUsesVersionedSalt(): void
2930 {
3031 $ this ->assertSame (
3132 'jmespath_ ' . md5 (
32- 'jmespath: ' . PHP_MAJOR_VERSION . '. ' . PHP_MINOR_VERSION . ':4 :foo.bar '
33+ 'jmespath: ' . PHP_MAJOR_VERSION . '. ' . PHP_MINOR_VERSION . ':5 :foo.bar '
3334 ),
3435 CompilerRuntime ::functionName ('foo.bar ' )
3536 );
@@ -162,6 +163,35 @@ public function testCompiledRuntimeSortsNumerically(): void
162163 }
163164 }
164165
166+ public function testRejectsLiteralFunctionName (): void
167+ {
168+ $ dir = $ this ->createTempDir ();
169+
170+ try {
171+ $ runtime = new CompilerRuntime ($ dir );
172+ $ runtime ('`"not_a_function"`(@) ' , []);
173+ $ this ->fail ('Expected SyntaxErrorException ' );
174+ } catch (SyntaxErrorException $ e ) {
175+ $ this ->assertSame ([], glob ($ dir . '/* ' ) ?: []);
176+ } finally {
177+ $ this ->removeTempDir ($ dir );
178+ }
179+ }
180+
181+ public function testStillCompilesValidFunctions (): void
182+ {
183+ $ dir = $ this ->createTempDir ();
184+
185+ try {
186+ $ runtime = new CompilerRuntime ($ dir );
187+
188+ $ this ->assertSame (3 , $ runtime ('length(@) ' , [1 , 2 , 3 ]));
189+ $ this ->assertTrue ($ runtime ("contains(@, 'b') " , ['a ' , 'b ' , 'c ' ]));
190+ } finally {
191+ $ this ->removeTempDir ($ dir );
192+ }
193+ }
194+
165195 private function createTempDir ()
166196 {
167197 $ dir = sys_get_temp_dir () . '/jmespath-compiler- ' . bin2hex (random_bytes (12 ));
0 commit comments