@@ -54,49 +54,41 @@ public function testSpecs(array $spec): void
5454 }
5555 self ::addDataHelpers ($ spec );
5656
57- // setup helpers
57+ $ options = new Options (
58+ compat: $ spec ['compileOptions ' ]['compat ' ] ?? false ,
59+ knownHelpers: $ spec ['compileOptions ' ]['knownHelpers ' ] ?? [],
60+ knownHelpersOnly: $ spec ['compileOptions ' ]['knownHelpersOnly ' ] ?? false ,
61+ strict: $ spec ['compileOptions ' ]['strict ' ] ?? false ,
62+ assumeObjects: $ spec ['compileOptions ' ]['assumeObjects ' ] ?? false ,
63+ preventIndent: $ spec ['compileOptions ' ]['preventIndent ' ] ?? false ,
64+ ignoreStandalone: $ spec ['compileOptions ' ]['ignoreStandalone ' ] ?? false ,
65+ explicitPartialContext: $ spec ['compileOptions ' ]['explicitPartialContext ' ] ?? false ,
66+ );
67+
5868 $ helpers = [];
59- $ helpersList = '[ ' ;
69+ $ helpersList = [];
70+ $ partials = [];
71+
6072 foreach ($ spec ['helpers ' ] as $ name => $ func ) {
6173 if (!isset ($ func ['php ' ])) {
6274 continue ;
6375 }
6476 $ helper = self ::patchHelperCode ($ func ['php ' ]);
65- $ helpersList .= "\n ' $ name' => $ helper, \n" ;
77+ $ helpersList[ $ name] = $ helper ;
6678 eval ('$helpers[ \'' . $ name . '\'] = ' . $ helper . '; ' );
6779 }
68- $ helpersList .= '] ' ;
6980
70- // Convert "!code" partials (callable PHP strings) into actual callables.
71- $ partials = [];
72- $ stringPartials = [];
7381 foreach ($ spec ['partials ' ] as $ name => $ partial ) {
82+ // Convert "!code" partials (callable PHP strings) into actual callables.
7483 if (is_array ($ partial ) && isset ($ partial ['!code ' ], $ partial ['php ' ])) {
7584 $ partials [$ name ] = eval ('return ' . $ partial ['php ' ] . '; ' );
7685 } else {
77- $ stringPartials [$ name ] = $ partial ;
86+ $ partials [$ name ] = Handlebars:: compile ( $ partial, $ options ) ;
7887 }
7988 }
8089
8190 try {
82- $ knownHelpersOnly = $ spec ['compileOptions ' ]['knownHelpersOnly ' ] ?? false ;
83- $ strict = $ spec ['compileOptions ' ]['strict ' ] ?? false ;
84- $ assumeObjects = $ spec ['compileOptions ' ]['assumeObjects ' ] ?? false ;
85- $ preventIndent = $ spec ['compileOptions ' ]['preventIndent ' ] ?? false ;
86- $ ignoreStandalone = $ spec ['compileOptions ' ]['ignoreStandalone ' ] ?? false ;
87- $ explicitPartialContext = $ spec ['compileOptions ' ]['explicitPartialContext ' ] ?? false ;
88-
89- $ php = Handlebars::precompile ($ spec ['template ' ], new Options (
90- compat: $ spec ['compileOptions ' ]['compat ' ] ?? false ,
91- knownHelpers: $ spec ['compileOptions ' ]['knownHelpers ' ] ?? [],
92- knownHelpersOnly: $ knownHelpersOnly ,
93- strict: $ strict ,
94- assumeObjects: $ assumeObjects ,
95- preventIndent: $ preventIndent ,
96- ignoreStandalone: $ ignoreStandalone ,
97- explicitPartialContext: $ explicitPartialContext ,
98- partials: $ stringPartials ,
99- ));
91+ $ php = Handlebars::precompile ($ spec ['template ' ], $ options );
10092 } catch (\Exception $ e ) {
10193 if ($ spec ['exception ' ]) {
10294 $ this ->expectNotToPerformAssertions ();
@@ -127,20 +119,27 @@ public function testSpecs(array $spec): void
127119 $ this ->expectNotToPerformAssertions ();
128120 return ;
129121 }
130- $ this ->fail ("Rendering error: {$ e ->getMessage ()}\n\n" . self ::getSpecDetails ($ php , $ helpersList ));
122+ $ this ->fail ("Rendering error: {$ e ->getMessage ()}\n\n" . self ::getSpecDetails ($ php , $ helpersList, $ spec [ ' partials ' ] ));
131123 }
132124
133125 if ($ spec ['exception ' ]) {
134126 $ details = $ spec ['exception ' ] === true ? '. ' : ": {$ spec ['exception ' ]}\n" ;
135- $ this ->fail ("Expected exception {$ details }\nResult: $ result \n\n" . self ::getSpecDetails ($ php , $ helpersList ));
127+ $ this ->fail ("Expected exception {$ details }\nResult: $ result \n\n" . self ::getSpecDetails ($ php , $ helpersList, $ spec [ ' partials ' ] ));
136128 }
137129
138- $ this ->assertSame ($ spec ['expected ' ], $ result , self ::getSpecDetails ($ php , $ helpersList ));
130+ $ this ->assertSame ($ spec ['expected ' ], $ result , self ::getSpecDetails ($ php , $ helpersList, $ spec [ ' partials ' ] ));
139131 }
140132
141- private static function getSpecDetails (string $ code , string $ helpers ): string
133+ /**
134+ * @param array<mixed> $helpers
135+ * @param array<mixed> $partials
136+ */
137+ private static function getSpecDetails (string $ code , array $ helpers , array $ partials ): string
142138 {
143- return "Helpers: $ helpers \nPHP code: \n$ code " ;
139+ $ flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ;
140+ $ partials = json_encode ($ partials , $ flags );
141+ $ helpers = json_encode ($ helpers , $ flags );
142+ return "Partials: $ partials \nHelpers: $ helpers \nPHP code: \n$ code " ;
144143 }
145144
146145 /**
0 commit comments