ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
RequireJsProcessTest.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of Component Installer.
5 *
6 * (c) Rob Loach (http://robloach.net)
7 *
8 * For the full copyright and license information, please view the LICENSE.md
9 * file that was distributed with this source code.
10 */
11
13
15use Composer\Config;
16
21{
25 protected $process;
26
27 public function setUp()
28 {
29 parent::setUp();
30 $this->process = new RequireJsProcess($this->composer, $this->io);
31 }
32
40 public function testRequireJs(array $json = array(), $expected = '')
41 {
42 $result = $this->process->requireJs($json);
43 $this->assertEquals($expected, $result);
44 }
45
46 public function providerRequireJs()
47 {
48 // Start with a base RequireJS configuration.
49 $js = <<<EOT
50var components = %s;
51if (typeof require !== "undefined" && require.config) {
52 require.config(components);
53} else {
54 var require = components;
55}
56if (typeof exports !== "undefined" && typeof module !== "undefined") {
57 module.exports = components;
58}
59EOT;
60
61 // Tests a basic configuration.
62 $tests[] = array(
63 array('foo' => 'bar'),
64 sprintf($js, "{\n \"foo\": \"bar\"\n}"),
65 );
66
67 return $tests;
68 }
69
78 public function testRequireJson(array $packages, array $config, $expected = null)
79 {
80 $this->composer->getConfig()->merge(array('config' => $config));
81 $this->process->init();
82 $result = $this->process->requireJson($packages);
83 $this->assertEquals($expected, $result);
84 }
85
86 public function providerRequireJson()
87 {
88 // Test a package that doesn't have any extra information.
89 $packageWithoutExtra = array(
90 'name' => 'components/packagewithoutextra',
91 'type' => 'component',
92 );
93 $packages = array($packageWithoutExtra);
94 $expected = array(
95 'baseUrl' => 'components',
96 );
97 $tests[] = array($packages, array(), $expected);
98
99 // Test switching the component directory.
100 $packages = array($packageWithoutExtra);
101 $expected = array(
102 'baseUrl' => 'components',
103 );
104 $tests[] = array($packages, array('component-dir' => 'otherdir'), $expected);
105
106 // Test switching the baseUrl.
107 $packages = array($packageWithoutExtra);
108 $expected = array(
109 'baseUrl' => '/another/path',
110 );
111 $tests[] = array($packages, array('component-baseurl' => '/another/path'), $expected);
112
113 // Test a package with just Scripts.
114 $jquery = array(
115 'name' => 'components/jquery',
116 'type' => 'component',
117 'extra' => array(
118 'component' => array(
119 'scripts' => array(
120 'jquery.js',
121 )
122 )
123 )
124 );
125 $packages = array($jquery);
126 $expected = array(
127 'baseUrl' => 'components',
128 );
129 $tests[] = array($packages, array(), $expected);
130
131 // Test a pckage with just shim information.
132 $underscore = array(
133 'name' => 'components/underscore',
134 'type' => 'component',
135 'extra' => array(
136 'component' => array(
137 'shim' => array(
138 'exports' => '_',
139 ),
140 ),
141 ),
142 'baseUrl' => 'components',
143 );
144 $packages = array($underscore);
145 $expected = array(
146 'shim' => array(
147 'underscore' => array(
148 'exports' => '_',
149 ),
150 ),
151 'baseUrl' => 'components',
152 );
153 $tests[] = array($packages, array(), $expected);
154
155 // Test a package the has everything.
156 $backbone = array(
157 'name' => 'components/backbone',
158 'type' => 'component',
159 'extra' => array(
160 'component' => array(
161 'scripts' => array(
162 'backbone.js'
163 ),
164 'shim' => array(
165 'exports' => 'Backbone',
166 'deps' => array(
167 'underscore',
168 'jquery'
169 ),
170 ),
171 ),
172 ),
173 );
174 $packages = array($backbone);
175 $expected = array(
176 'shim' => array(
177 'backbone' => array(
178 'exports' => 'Backbone',
179 'deps' => array(
180 'underscore',
181 'jquery'
182 ),
183 ),
184 ),
185 'baseUrl' => 'components',
186 );
187 $tests[] = array($packages, array(), $expected);
188
189 // Test multiple packages.
190 $packages = array($backbone, $jquery);
191 $expected = array(
192 'shim' => array(
193 'backbone' => array(
194 'exports' => 'Backbone',
195 'deps' => array(
196 'underscore',
197 'jquery'
198 ),
199 ),
200 ),
201 'baseUrl' => 'components',
202 );
203 $tests[] = array($packages, array(), $expected);
204
205 // Package with a config definition.
206 $packageWithConfig = array(
207 'name' => 'components/packagewithconfig',
208 'type' => 'component',
209 'extra' => array(
210 'component' => array(
211 'config' => array(
212 'color' => 'blue',
213 ),
214 ),
215 ),
216 );
217 $packages = array($packageWithConfig);
218 $expected = array(
219 'config' => array(
220 'packagewithconfig' => array(
221 'color' => 'blue',
222 ),
223 ),
224 'baseUrl' => 'components',
225 );
226 $tests[] = array($packages, array(), $expected);
227
228 // Test building the JavaScript file.
229 $packageWithScripts = array(
230 'name' => 'components/foobar',
231 'type' => 'component',
232 'extra' => array(
233 'component' => array(
234 'scripts' => array(
235 'tests/ComponentInstaller/Test/Resources/test.js',
236 ),
237 ),
238 ),
239 'is-root' => true,
240 );
241 $packages = array($packageWithScripts);
242 $expected = array(
243 'packages' => array(
244 array(
245 'name' => 'foobar',
246 'main' => 'foobar-built.js'
247 ),
248 ),
249 'baseUrl' => 'components',
250 );
251 $tests[] = array($packages, array(), $expected);
252
253 // Test building JavaScript files with glob().
254 $packageWithScripts = array(
255 'name' => 'components/foobar2',
256 'extra' => array(
257 'component' => array(
258 'scripts' => array(
259 'tests/ComponentInstaller/Test/Resources/*.js',
260 ),
261 ),
262 ),
263 'is-root' => true,
264 );
265 $packages = array($packageWithScripts);
266 $expected = array(
267 'packages' => array(
268 array(
269 'name' => 'foobar2',
270 'main' => 'foobar2-built.js'
271 ),
272 ),
273 'baseUrl' => 'components',
274 );
275 $tests[] = array($packages, array(), $expected);
276
277 // Test bringing in config options from the root package.
278 $package = array(
279 'name' => 'components/foobar3',
280 'extra' => array(
281 'component' => array(
282 'config' => array(
283 'color' => 'blue',
284 ),
285 ),
286 ),
287 );
288 $packages = array($package);
289 $config = array(
290 'component' => array(
291 'waitSeconds' => 3,
292 'baseUrl' => 'public',
293 ),
294 );
295 $expected = array(
296 'config' => array(
297 'foobar3' => array(
298 'color' => 'blue',
299 ),
300 ),
301 'baseUrl' => 'public',
302 'waitSeconds' => 3,
303 );
304 $tests[] = array($packages, $config, $expected);
305
306 // Test overriding component config from root packages.
307 $package = array(
308 'name' => 'components/foobar3',
309 'extra' => array(
310 'component' => array(
311 'config' => array(
312 'color' => 'blue',
313 ),
314 ),
315 ),
316 );
317 $packages = array($package);
318 $config = array(
319 'component' => array(
320 'waitSeconds' => 3,
321 'baseUrl' => 'public',
322 'config' => array(
323 'foobar3' => array(
324 'color' => 'red',
325 ),
326 ),
327 ),
328 );
329 $expected = array(
330 'config' => array(
331 'foobar3' => array(
332 'color' => 'red',
333 ),
334 ),
335 'baseUrl' => 'public',
336 'waitSeconds' => 3,
337 );
338 $tests[] = array($packages, $config, $expected);
339
340 return $tests;
341 }
342
343}
sprintf('%.4f', $callTime)
$result
An exception for terminatinating execution or to throw for unit testing.
Builds the require.js configuration.
testRequireJs(array $json=array(), $expected='')
testRequireJs
testRequireJson(array $packages, array $config, $expected=null)
testRequireJson
$js