88use PHPUnit \Framework \TestCase ;
99
1010/**
11- * @phpstan-type RenderTest array{template: string, options?: Options, expected: string}
12- * @phpstan-type ErrorCase array{template: string, options: Options, expected: string|null }
11+ * @phpstan-type RenderTest array{template: string, options?: Options, data?: array<mixed>, expected: string}
12+ * @phpstan-type ErrorCase array{template: string, options? : Options, expected: string}
1313 */
1414class ErrorTest extends TestCase
1515{
1616 /**
17- * @param RenderTest $test
17+ * @param array<mixed> $data
1818 */
1919 #[DataProvider("renderErrorProvider " )]
20- public function testRenderingException (array $ test ): void
20+ public function testRenderingException (string $ template , string $ expected , ? Options $ options = null , array $ data = [] ): void
2121 {
22- $ php = Handlebars ::precompile ($ test [ ' template ' ] , $ test [ ' options ' ] ?? new Options ());
22+ $ php = Handlebars ::precompile ($ template , $ options ?? new Options ());
2323 $ renderer = Handlebars ::template ($ php );
2424 try {
25- $ renderer ();
26- $ this ->fail ("Expected to throw exception: {$ test [ ' expected ' ] }. CODE: $ php " );
25+ $ renderer ($ data );
26+ $ this ->fail ("Expected to throw exception: {$ expected }. CODE: $ php " );
2727 } catch (\Exception $ e ) {
28- $ this ->assertEquals ($ test [ ' expected ' ] , $ e ->getMessage (), $ php );
28+ $ this ->assertEquals ($ expected , $ e ->getMessage (), $ php );
2929 }
3030 }
3131
3232 /**
33- * @return list<array{ RenderTest}>
33+ * @return RenderTest[]
3434 */
3535 public static function renderErrorProvider (): array
3636 {
37- $ errorCases = [
37+ return [
3838 [
3939 'template ' => "{{#> testPartial}} \n {{#> innerPartial}} \n {{> @partial-block}} \n {{/innerPartial}} \n{{/testPartial}} " ,
4040 'options ' => new Options (
@@ -49,20 +49,44 @@ public static function renderErrorProvider(): array
4949 'template ' => '{{> @partial-block}} ' ,
5050 'expected ' => "Runtime: the partial @partial-block could not be found " ,
5151 ],
52+ [
53+ 'template ' => '{{foo.bar}} ' ,
54+ 'options ' => new Options (strict: true ),
55+ 'data ' => ['foo ' => []],
56+ 'expected ' => '"foo.bar" not defined ' ,
57+ ],
5258 [
5359 'template ' => '{{foo}} ' ,
5460 'options ' => new Options (strict: true ),
55- 'expected ' => 'Runtime: foo does not exist ' ,
61+ 'expected ' => '"foo" not defined ' ,
62+ ],
63+ [
64+ // strict mode should override helperMissing
65+ 'template ' => '{{foo}} ' ,
66+ 'options ' => new Options (
67+ strict: true ,
68+ helpers: ['helperMissing ' => fn () => 'bad ' ],
69+ ),
70+ 'expected ' => '"foo" not defined ' ,
71+ ],
72+ [
73+ // strict mode should override blockHelperMissing
74+ 'template ' => '{{#foo}}OK{{/foo}} ' ,
75+ 'options ' => new Options (
76+ strict: true ,
77+ helpers: ['blockHelperMissing ' => fn () => 'bad ' ],
78+ ),
79+ 'expected ' => '"foo" not defined ' ,
5680 ],
5781 [
5882 'template ' => '{{#foo}}OK{{/foo}} ' ,
5983 'options ' => new Options (strict: true ),
60- 'expected ' => 'Runtime: foo does not exist ' ,
84+ 'expected ' => '" foo" not defined ' ,
6185 ],
6286 [
6387 'template ' => '{{{foo}}} ' ,
6488 'options ' => new Options (strict: true ),
65- 'expected ' => 'Runtime: foo does not exist ' ,
89+ 'expected ' => '" foo" not defined ' ,
6690 ],
6791 [
6892 'template ' => '{{foo}} ' ,
@@ -73,7 +97,7 @@ public static function renderErrorProvider(): array
7397 },
7498 ],
7599 ),
76- 'expected ' => 'Runtime: call custom helper \'foo \' error: Expect the unexpected ' ,
100+ 'expected ' => 'Custom helper \'foo \' error: Expect the unexpected ' ,
77101 ],
78102 // ensure that callable strings in data aren't treated as functions
79103 [
@@ -82,22 +106,13 @@ public static function renderErrorProvider(): array
82106 'expected ' => '"foo.bar" is not a block helper function ' ,
83107 ],
84108 ];
85-
86- return array_map (fn ($ i ) => [$ i ], $ errorCases );
87109 }
88110
89111 #[DataProvider("errorProvider " )]
90- public function testErrors (string $ template , ? string $ expected , Options $ options ): void
112+ public function testErrors (string $ template , string $ expected , ? Options $ options = null ): void
91113 {
92- if ($ expected === null ) {
93- // should compile without error
94- $ code = Handlebars ::precompile ($ template , $ options );
95- $ this ->assertNotEmpty ($ code );
96- return ;
97- }
98-
99114 try {
100- Handlebars ::precompile ($ template , $ options );
115+ Handlebars ::precompile ($ template , $ options ?? new Options () );
101116 $ this ->fail ("Expected to throw exception: {$ expected }" );
102117 } catch (\Exception $ e ) {
103118 $ this ->assertSame ($ expected , $ e ->getMessage ());
@@ -109,14 +124,11 @@ public function testErrors(string $template, ?string $expected, Options $options
109124 */
110125 public static function errorProvider (): array
111126 {
112- $ errorCases = [
127+ return [
113128 [
114129 'template ' => '{{typeof hello}} ' ,
115130 'expected ' => 'Missing helper: "typeof" ' ,
116131 ],
117- [
118- 'template ' => '{{#with items}}OK!{{/with}} ' ,
119- ],
120132 [
121133 'template ' => '{{#with}}OK!{{/with}} ' ,
122134 'expected ' => '#with requires exactly one argument ' ,
@@ -164,26 +176,10 @@ public static function errorProvider(): array
164176 'template ' => '{{> (foo) bar}} ' ,
165177 'expected ' => 'Missing helper: "foo" ' ,
166178 ],
167- [
168- 'template ' => '{{log}} ' ,
169- ],
170179 [
171180 'template ' => '{{#*help me}}{{/help}} ' ,
172181 'expected ' => 'Unknown decorator: "help" ' ,
173182 ],
174- [
175- 'template ' => '{{#*inline}}{{/inline}} ' ,
176- ],
177183 ];
178-
179- return array_map (function ($ i ) {
180- if (!isset ($ i ['options ' ])) {
181- $ i ['options ' ] = new Options ();
182- }
183- if (!isset ($ i ['expected ' ])) {
184- $ i ['expected ' ] = null ;
185- }
186- return $ i ;
187- }, $ errorCases );
188184 }
189185}
0 commit comments