2
2
3
3
namespace Handlebars \Tests ;
4
4
5
+ use Handlebars \Handlebars ;
5
6
use Handlebars \DefaultRegistry ;
6
7
use Handlebars \Compiler \PhpCompiler ;
8
+ use Handlebars \Compiler \Runtime as CompilerRuntime ;
9
+ use Handlebars \VM \Runtime as VMRuntime ;
7
10
8
11
class HandlebarsExportTest extends Common
9
12
{
@@ -29,22 +32,22 @@ public function __construct($name = null, array $data = array(), $dataName = '')
29
32
}
30
33
31
34
/**
35
+ * @param SpecTestModel $test
32
36
* @dataProvider specProvider
33
37
*/
34
- public function testCompiler ($ test )
38
+ public function testCompiler (SpecTestModel $ test )
35
39
{
36
- if ( in_array ($ test[ ' name ' ] , self ::$ skipTests ) ) {
40
+ if ( in_array ($ test-> name , self ::$ skipTests ) ) {
37
41
$ this ->markTestIncomplete ();
38
42
}
39
- $ test = $ this ->prepareTestData ($ test );
40
43
41
- if ( ! empty ( $ test[ ' exception ' ]) ) {
44
+ if ( $ test-> exception ) {
42
45
$ this ->setExpectedException ('\\Handlebars \\Exception ' );
43
46
}
44
47
45
48
$ handlebars = $ this ->handlebarsFactory ($ test , 'compiler ' );
46
49
$ compiler = new PhpCompiler ();
47
- $ templateSpecStr = $ compiler ->compile ($ this -> convertContext ( $ test [ ' opcodes ' ] ), $ test[ ' compileOptions ' ] );
50
+ $ templateSpecStr = $ compiler ->compile ($ test -> getOpcodes ( ), $ test-> compileOptions );
48
51
49
52
if ( false ) {
50
53
$ file = tempnam (sys_get_temp_dir (), 'HandlebarsTestsCache ' );
@@ -57,37 +60,35 @@ public function testCompiler($test)
57
60
};
58
61
}
59
62
60
- $ fn = new \ Handlebars \ Compiler \ Runtime ($ handlebars , $ templateSpec );
61
- $ actual = $ fn ($ test[ ' data ' ] , $ test[ ' options ' ] );
62
- $ this ->assertEquals ($ test[ ' expected ' ] , $ actual );
63
+ $ fn = new CompilerRuntime ($ handlebars , $ templateSpec );
64
+ $ actual = $ fn ($ test-> getData () , $ test-> getOptions () );
65
+ $ this ->assertEquals ($ test-> expected , $ actual );
63
66
}
64
67
65
68
/**
69
+ * @param SpecTestModel $test
66
70
* @dataProvider specProvider
67
71
*/
68
- public function testLegacyVM ($ test )
72
+ public function testLegacyVM (SpecTestModel $ test )
69
73
{
70
- if ( in_array ($ test[ ' name ' ] , self ::$ skipTests ) || in_array ($ test[ ' name ' ] , self ::$ skipLegacyVMTests ) ) {
74
+ if ( in_array ($ test-> name , self ::$ skipTests ) || in_array ($ test-> name , self ::$ skipLegacyVMTests ) ) {
71
75
$ this ->markTestIncomplete ();
72
76
}
73
- if ( $ test[ ' description ' ] === 'decorators ' ||
74
- !empty ($ test[ ' decorators ' ] ) ||
75
- $ test[ ' description ' ] === 'inline partials ' ) {
77
+ if ( $ test-> description === 'decorators ' ||
78
+ !empty ($ test-> decorators ) ||
79
+ $ test-> description === 'inline partials ' ) {
76
80
$ this ->markTestIncomplete ("The VM does not support decorators in export mode - requires custom compiler option " );
77
81
}
78
- $ test = $ this ->prepareTestData ($ test );
79
82
80
- if ( ! empty ( $ test[ ' exception ' ]) ) {
83
+ if ( $ test-> exception ) {
81
84
$ this ->setExpectedException ('\\Handlebars \\Exception ' );
82
85
}
83
86
84
87
$ handlebars = $ this ->handlebarsFactory ($ test , 'vm ' );
85
88
86
- $ allOptions = array_merge ($ test ['compileOptions ' ], $ test ['options ' ]);
87
-
88
- $ vm = new \Handlebars \VM \Runtime ($ handlebars , $ this ->convertContext ($ test ['opcodes ' ]));
89
- $ actual = $ vm ($ test ['data ' ], $ allOptions );
90
- $ this ->assertEquals ($ test ['expected ' ], $ actual );
89
+ $ vm = new VMRuntime ($ handlebars , $ test ->getOpcodes ());
90
+ $ actual = $ vm ($ test ->getData (), $ test ->getAllOptions ());
91
+ $ this ->assertEquals ($ test ->expected , $ actual );
91
92
}
92
93
93
94
// Note: New VM doesn't have a way of specifying opcodes (yet)
@@ -113,7 +114,7 @@ public function specProvider($testName)
113
114
$ test ['suiteName ' ] = $ suiteName ;
114
115
$ test ['number ' ] = $ i ;
115
116
$ test ['name ' ] = $ name = sprintf ('%s - %s - %s ' , $ test ['suiteName ' ], $ test ['description ' ], $ test ['it ' ]);
116
- $ tests [$ name ] = array ($ test );
117
+ $ tests [$ name ] = array (new SpecTestModel ( $ test) );
117
118
}
118
119
}
119
120
return $ tests ;
@@ -136,74 +137,31 @@ protected function getDataSetAsString($includeData = true)
136
137
return $ out ;
137
138
}
138
139
139
- protected function handlebarsFactory ($ test , $ mode = null )
140
+ protected function handlebarsFactory (SpecTestModel $ test , $ mode = null )
140
141
{
141
- $ globalHelpers = (array ) $ this ->convertCode ($ test ['globalHelpers ' ]);
142
- $ globalDecorators = (array ) $ this ->convertCode ($ test ['globalDecorators ' ]);
143
- $ helpers = (array ) $ this ->convertCode ($ test ['helpers ' ]);
144
- $ decorators = (array ) $ this ->convertCode ($ test ['decorators ' ]);
145
-
146
- $ pr = new DefaultRegistry ();
147
- $ handlebars = \Handlebars \Handlebars::factory (array (
142
+ $ partialRegistry = new DefaultRegistry ();
143
+ $ handlebars = Handlebars::factory (array (
148
144
'mode ' => $ mode ,
149
- 'helpers ' => new DefaultRegistry (array_merge ( $ globalHelpers , $ helpers )),
150
- 'partials ' => $ pr ,
151
- 'decorators ' => new DefaultRegistry (array_merge ( $ globalDecorators , $ decorators )),
145
+ 'helpers ' => new DefaultRegistry ($ test -> getAllHelpers ( )),
146
+ 'partials ' => $ partialRegistry ,
147
+ 'decorators ' => new DefaultRegistry ($ test -> getAllDecorators ( )),
152
148
));
153
149
if ( $ mode === 'compiler ' ) {
154
150
$ compiler = new PhpCompiler ();
155
- foreach ( $ test ['globalPartialOpcodes ' ] as $ name => $ partialOpcode ) {
156
- $ pr [$ name ] = new \Handlebars \Compiler \Runtime (
157
- $ handlebars ,
158
- eval ('return ' . $ compiler ->compile ($ this ->convertContext ($ partialOpcode ), $ test ['compileOptions ' ]) . '; ' )
159
- );
160
- }
161
- foreach ( $ test ['partialOpcodes ' ] as $ name => $ partialOpcode ) {
162
- $ pr [$ name ] = new \Handlebars \Compiler \Runtime (
151
+ foreach ( $ test ->getAllPartialOpcodes () as $ name => $ partialOpcodes ) {
152
+ $ partialRegistry [$ name ] = new CompilerRuntime (
163
153
$ handlebars ,
164
- eval ('return ' . $ compiler ->compile ($ this -> convertContext ( $ partialOpcode ) , $ test[ ' compileOptions ' ] ) . '; ' )
154
+ eval ('return ' . $ compiler ->compile ($ partialOpcodes , $ test-> compileOptions ) . '; ' )
165
155
);
166
156
}
167
157
} else if ( $ mode === 'vm ' ) {
168
- foreach ( $ test ['globalPartialOpcodes ' ] as $ name => $ partialOpcode ) {
169
- $ pr [$ name ] = new \Handlebars \VM \Runtime ($ handlebars , $ this ->convertContext ($ partialOpcode ));
170
- }
171
- foreach ( $ test ['partialOpcodes ' ] as $ name => $ partialOpcode ) {
172
- $ pr [$ name ] = new \Handlebars \VM \Runtime ($ handlebars , $ this ->convertContext ($ partialOpcode ));
158
+ foreach ( $ test ->getAllPartialOpcodes () as $ name => $ partialOpcodes ) {
159
+ $ partialRegistry [$ name ] = new VMRuntime ($ handlebars , $ partialOpcodes );
173
160
}
174
161
} else {
175
162
throw new \Exception ('Unknown mode: ' . $ mode );
176
163
}
177
- $ handlebars ->setPartials ($ pr );
164
+ $ handlebars ->setPartials ($ partialRegistry );
178
165
return $ handlebars ;
179
166
}
180
-
181
- protected function prepareTestData ($ test )
182
- {
183
- $ test = array_merge (array (
184
- 'data ' => null ,
185
- 'helpers ' => array (),
186
- 'partials ' => array (),
187
- 'decorators ' => array (),
188
- 'globalHelpers ' => array (),
189
- 'globalPartials ' => array (),
190
- 'globalDecorators ' => array (),
191
- 'exception ' => false ,
192
- 'message ' => null ,
193
- 'compileOptions ' => array (),
194
- 'options ' => array (),
195
- 'opcodes ' => array (),
196
- 'partialOpcodes ' => array (),
197
- 'globalPartialOpcodes ' => array (),
198
- ), $ test );
199
- $ test ['compileOptions ' ]['data ' ] = true ; // @todo fix
200
- $ test ['data ' ] = $ this ->convertCode ($ test ['data ' ]);
201
- $ test ['helpers ' ] = (array ) $ this ->convertCode ($ test ['helpers ' ]);
202
- $ test ['partials ' ] = (array ) $ this ->convertCode ($ test ['partials ' ]);
203
- $ test ['decorators ' ] = (array ) $ this ->convertCode ($ test ['decorators ' ]);
204
- if ( isset ($ test ['options ' ]['data ' ]) ) {
205
- $ test ['options ' ]['data ' ] = $ this ->convertCode ($ test ['options ' ]['data ' ]);
206
- }
207
- return $ test ;
208
- }
209
167
}
0 commit comments