11<?php
22
3+ namespace DevTheorem \Handlebars \Test ;
4+
35use DevTheorem \Handlebars \Handlebars ;
46use DevTheorem \Handlebars \Options ;
57use PHPUnit \Framework \Attributes \DataProvider ;
68use PHPUnit \Framework \TestCase ;
79
8- /**
9- * Used by vendor/jbboehr/handlebars-spec/spec/data.json
10- */
11- class Utils
12- {
13- public static function createFrame (mixed $ data ): mixed
14- {
15- if (is_array ($ data )) {
16- $ r = [];
17- foreach ($ data as $ k => $ v ) {
18- $ r [$ k ] = $ v ;
19- }
20- return $ r ;
21- }
22- return $ data ;
23- }
24- }
25-
2610/**
2711 * @phpstan-type JsonSpec array{
28- * file: string, no: int, message: string|null, data: null|int|bool|string|array<mixed>|stdClass,
12+ * file: string, no: int, message: string|null, data: null|int|bool|string|array<mixed>|\ stdClass,
2913 * it: string, description: string, expected: string|null, helpers: array<mixed>,
3014 * partials: array<mixed>, compileOptions: array<mixed>, template: string,
3115 * exception: string|null, runtimeOptions: array<mixed>, number: string|null,
@@ -82,11 +66,7 @@ public function testSpecs(array $spec): void
8266 if (!isset ($ func ['php ' ])) {
8367 $ this ->markTestIncomplete ("No PHP helper code provided for [ {$ spec ['file ' ]}# {$ spec ['description ' ]}]# {$ spec ['no ' ]}" );
8468 }
85- $ helper = self ::patchSafeString ($ func ['php ' ]);
86- $ helper = str_replace ('$options[ \'name \'] ' , '$options->name ' , $ helper );
87- $ helper = str_replace ('$options[ \'data \'] ' , '$options->data ' , $ helper );
88- $ helper = str_replace ('$options[ \'hash \'] ' , '$options->hash ' , $ helper );
89- $ helper = str_replace ('$arguments[count($arguments)-1][ \'name \']; ' , '$arguments[count($arguments)-1]->name; ' , $ helper );
69+ $ helper = self ::patchHelperCode ($ func ['php ' ]);
9070 $ helpersList .= "\n ' $ name' => $ helper, \n" ;
9171 eval ('$helpers[ \'' . $ name . '\'] = ' . $ helper . '; ' );
9272 }
@@ -182,7 +162,7 @@ public static function jsonSpecProvider(): array
182162
183163 $ files = glob ('vendor/jbboehr/handlebars-spec/spec/*.json ' );
184164 if ($ files === false ) {
185- throw new Exception ("Failed to read JSON spec files " );
165+ throw new \ Exception ("Failed to read JSON spec files " );
186166 }
187167
188168 foreach ($ files as $ file ) {
@@ -192,7 +172,7 @@ public static function jsonSpecProvider(): array
192172 }
193173 $ contents = file_get_contents ($ file );
194174 if ($ contents === false ) {
195- throw new Exception ("Failed to read JSON spec file {$ file }" );
175+ throw new \ Exception ("Failed to read JSON spec file {$ file }" );
196176 }
197177 $ i = 0 ;
198178 $ json = json_decode ($ contents , true );
@@ -254,10 +234,14 @@ private static function evalNestedCode(array &$data): void
254234 }
255235 }
256236
257- private static function patchSafeString (string $ code ): string
237+ private static function patchHelperCode (string $ code ): string
258238 {
259- $ classname = '\\DevTheorem \\Handlebars \\SafeString ' ;
260- return preg_replace ('/ ( \\\Handlebars \\\)?SafeString(\s*\(.*?\))?/ ' , ' ' . $ classname . '$2 ' , $ code )
261- ?? throw new Exception ("Failed to patch SafeString in $ code " );
239+ $ code = preg_replace ('/ ( \\\Handlebars \\\)?SafeString(\s*\(.*?\))?/ ' , ' \\DevTheorem \\Handlebars \\SafeString$2 ' , $ code )
240+ ?? throw new \Exception ("Failed to patch SafeString in $ code " );
241+ $ code = str_replace ('Utils::createFrame( ' , '\DevTheorem\Handlebars\Handlebars::createFrame( ' , $ code );
242+ $ code = str_replace ('$options[ \'name \'] ' , '$options->name ' , $ code );
243+ $ code = str_replace ('$options[ \'data \'] ' , '$options->data ' , $ code );
244+ $ code = str_replace ('$options[ \'hash \'] ' , '$options->hash ' , $ code );
245+ return str_replace ('$arguments[count($arguments)-1][ \'name \']; ' , '$arguments[count($arguments)-1]->name; ' , $ code );
262246 }
263247}
0 commit comments