@@ -57,9 +57,10 @@ public function test_calculateAllComponents() {
5757 "plugin,local_codechecker, {$ moodleRoot }/local/codechecker \n" .// All ok.
5858 "plugin,mod_forum, {$ moodleRoot }/mod/forum \n" ; // All ok.
5959
60- vfsStream::create ([
61- 'components.txt ' => $ components ,
62- ]);
60+ vfsStream::create (
61+ ['components.txt ' => $ components ,],
62+ $ vfs
63+ );
6364
6465 // Set codechecker config to point to it.
6566 Config::setConfigData ('moodleComponentsListPath ' , $ vfs ->url () . '/components.txt ' , true );
@@ -108,42 +109,42 @@ public function getMoodleComponentProvider() {
108109 'return ' => ['value ' => null ],
109110 'reset ' => true ,
110111 'selfPath ' => false ,
111- 'requireRealMoodle ' => false ,
112+ 'requireMockMoodle ' => false ,
112113 ],
113114 'moodleComponent_file_without_component_class ' => [
114115 'config ' => ['file ' => dirname (__FILE__ ) . '/fixtures/moodleutil/good/lib/lib.php ' ],
115116 'return ' => ['value ' => null ],
116117 'reset ' => true ,
117118 'selfPath ' => false ,
118- 'requireRealMoodle ' => false ,
119+ 'requireMockMoodle ' => false ,
119120 ],
120121 'moodleComponent_file_valid ' => [
121- 'config ' => ['file ' => __FILE__ ],
122- 'return ' => ['value ' => 'local_codechecker ' ],
122+ 'config ' => ['file ' => ' local/invented/lib.php ' ],
123+ 'return ' => ['value ' => 'local_invented ' ],
123124 'reset ' => false , // Prevent resetting cached information to verify next works.
124125 'selfPath ' => false ,
125- 'requireRealMoodle ' => true ,
126+ 'requireMockMoodle ' => true ,
126127 ],
127128 'moodleComponent_file_already_cached ' => [
128- 'config ' => ['file ' => dirname ( __FILE__ ) . ' /fixtures/moodleutil/good/ lib/lib.php ' ],
129- 'return ' => ['value ' => 'local_codechecker ' ],
129+ 'config ' => ['file ' => ' lib/lib.php ' ],
130+ 'return ' => ['value ' => 'core ' ],
130131 'reset ' => true ,
131132 'selfPath ' => false ,
132- 'requireRealMoodle ' => true ,
133+ 'requireMockMoodle ' => true ,
133134 ],
134135 'moodleComponent_file_cache_cleaned ' => [
135136 'config ' => ['file ' => dirname (__FILE__ ) . '/fixtures/moodleutil/good/lib/lib.php ' ],
136137 'return ' => ['value ' => null ],
137138 'reset ' => true ,
138139 'selfPath ' => false ,
139- 'requireRealMoodle ' => false ,
140+ 'requireMockMoodle ' => false ,
140141 ],
141142 'moodleComponent_file_without_component ' => [
142143 'config ' => ['file ' => dirname (__FILE__ , 5 ) . '/userpix/index.php ' ],
143144 'return ' => ['value ' => null ],
144145 'reset ' => true ,
145146 'selfPath ' => false ,
146- 'requireRealMoodle ' => false ,
147+ 'requireMockMoodle ' => false ,
147148 ],
148149 ];
149150 }
@@ -155,7 +156,7 @@ public function getMoodleComponentProvider() {
155156 * @param array $return expected result of the test.
156157 * @param bool $reset to decide if static caches should be reset before the test.
157158 * @param bool $selfPath to decide if codechecker own path is good to find a valid moodle root.
158- * @param bool $requireRealMoodle Whether a real Moodle root is required for this test.
159+ * @param bool $requireMockMoodle Whether a mock Moodle root is required for this test.
159160 *
160161 * @dataProvider getMoodleComponentProvider
161162 */
@@ -164,10 +165,18 @@ public function test_getMoodleComponent(
164165 array $ return ,
165166 bool $ reset = true ,
166167 bool $ selfPath = true ,
167- bool $ requireRealMoodle = false
168+ bool $ requireMockMoodle = false
168169 ) {
169- if ($ requireRealMoodle ) {
170+ if ($ requireMockMoodle ) {
171+ // We have to mock the passed moodleRoot.
172+ $ vfs = vfsStream::setup ('mocksite ' , null , []);
173+ vfsStream::copyFromFileSystem (__DIR__ . '/fixtures/moodleutil/complete ' , $ vfs );
174+ $ config ['moodleRoot ' ] = $ vfs ->url (); // Let's add it to the standard config and immediately use it.
175+ Config::setConfigData ('moodleRoot ' , $ config ['moodleRoot ' ], true );
170176 $ this ->requireRealMoodleRoot ();
177+
178+ // Also, we need to set the config['file'] to point to the vfs one.
179+ $ config ['file ' ] = $ vfs ->url () . '/ ' . $ config ['file ' ];
171180 }
172181
173182 $ file = null ;
@@ -176,9 +185,9 @@ public function test_getMoodleComponent(
176185 foreach ($ config as $ key => $ value ) {
177186 if ($ key === 'file ' ) {
178187 // We are passing a real File, prepare it.
179- $ config = new Config ();
180- $ ruleset = new Ruleset ($ config );
181- $ file = new File ($ value , $ ruleset , $ config );
188+ $ phpcsConfig = new Config ();
189+ $ phpcsRuleset = new Ruleset ($ phpcsConfig );
190+ $ file = new File ($ value , $ phpcsRuleset , $ phpcsConfig );
182191 } else {
183192 // Normal config.
184193 Config::setConfigData ($ key , $ value , true );
@@ -208,9 +217,7 @@ public function test_getMoodleComponent(
208217 // We need to unset all config options when passed.
209218 if ($ config ) {
210219 foreach ($ config as $ key => $ value ) {
211- if ($ key !== 'file ' ) {
212- Config::setConfigData ($ key , null , true );
213- }
220+ Config::setConfigData ($ key , null , true );
214221 }
215222 }
216223 }
@@ -274,9 +281,9 @@ public function test_getMoodleBranch(array $config, array $return, bool $reset =
274281 foreach ($ config as $ key => $ value ) {
275282 if ($ key === 'file ' ) {
276283 // We are passing a real File, prepare it.
277- $ config = new Config ();
278- $ ruleset = new Ruleset ($ config );
279- $ file = new File ($ value , $ ruleset , $ config );
284+ $ phpcsConfig = new Config ();
285+ $ phpcsRuleset = new Ruleset ($ phpcsConfig );
286+ $ file = new File ($ value , $ phpcsRuleset , $ phpcsConfig );
280287 } else {
281288 // Normal config.
282289 Config::setConfigData ($ key , $ value , true );
@@ -306,9 +313,7 @@ public function test_getMoodleBranch(array $config, array $return, bool $reset =
306313 // We need to unset all config options when passed.
307314 if ($ config ) {
308315 foreach ($ config as $ key => $ value ) {
309- if ($ key !== 'file ' ) {
310- Config::setConfigData ($ key , null , true );
311- }
316+ Config::setConfigData ($ key , null , true );
312317 }
313318 }
314319 }
@@ -330,14 +335,14 @@ public function getMoodleRootProvider() {
330335 'requireRealMoodle ' => false ,
331336 ],
332337 'moodleRoot_valid ' => [
333- 'config ' => ['moodleRoot ' => dirname ( __FILE__ , 5 ) ],
334- 'return ' => ['value ' => dirname ( __FILE__ , 5 ) ],
338+ 'config ' => ['moodleRoot ' => ' some_valid_moodle_root ' ],
339+ 'return ' => ['value ' => ' some_valid_moodle_root ' ],
335340 'requireRealMoodle ' => true ,
336341 'reset ' => false , // Prevent resetting cached information to verify next works.
337342 ],
338343 'moodleRoot_already_cached ' => [
339344 'config ' => ['moodleRoot ' => '/does/not/exist ' ],
340- 'return ' => ['value ' => dirname ( __FILE__ , 5 ) ],
345+ 'return ' => ['value ' => ' some_valid_moodle_root ' ],
341346 'requireRealMoodle ' => true ,
342347 ],
343348 'moodleRoot_cache_cleaned ' => [ // Verify that previous has cleaned cached information.
@@ -358,11 +363,11 @@ public function getMoodleRootProvider() {
358363 'requireRealMoodle ' => false ,
359364 ],
360365
361- // Passing nothing, defaults to this file.
366+ // Passing nothing, defaults to this file, that leads to not valid moodle root .
362367 'moodleRoot_pass_nothing ' => [
363368 'config ' => [],
364- 'return ' => ['value ' => dirname ( __FILE__ , 5 ) ],
365- 'requireRealMoodle ' => true ,
369+ 'return ' => ['value ' => null ],
370+ 'requireRealMoodle ' => false ,
366371 ],
367372 ];
368373 }
@@ -373,20 +378,33 @@ public function getMoodleRootProvider() {
373378 * @param array $config get the Config from provider.
374379 * @param array $return expected result of the test.
375380 * @param bool $reset to decide if static caches should be reset before the test.
376- * @param bool $selfPath to decide if codechecker own path is good to find a valid moodle root.
377- * @param bool $requireRealMoodle Whether a real Moodle root is required for this test.
381+ * @param bool $selfPath to decide if moodle-cs own path is good to find a valid moodle root.
382+ * @param bool $requireMockMoodle Whether a mock Moodle root is required for this test.
378383 *
379384 * @dataProvider getMoodleRootProvider
380385 */
381386 public function test_getMoodleRoot (
382- array $ config ,
383- array $ return ,
384- bool $ requireRealMoodle = false ,
385- bool $ reset = true ,
386- bool $ selfPath = true
387+ array $ config ,
388+ array $ return ,
389+ bool $ requireMockMoodle = false ,
390+ bool $ reset = true ,
391+ bool $ selfPath = true
387392 ) {
388- if ($ requireRealMoodle ) {
389- $ this ->requireRealMoodleRoot ();
393+ if ($ requireMockMoodle ) {
394+ if (isset ($ config ['moodleRoot ' ]) && isset ($ return ['value ' ])) {
395+ // We have to mock the passed moodleRoot.
396+ $ vfs = vfsStream::setup ($ config ['moodleRoot ' ], null , [
397+ 'version.php ' => 'some version contents, not important for this test ' ,
398+ 'config-dist.php ' => 'come config contents, not important for this test ' ,
399+ ]);
400+ $ config ['moodleRoot ' ] = $ vfs ->url (); // Let's add it to the standard config and immediately use it.
401+ Config::setConfigData ('moodleRoot ' , $ config ['moodleRoot ' ], true );
402+ $ this ->requireRealMoodleRoot ();
403+
404+ // We also have to mock the passed expectation for the test.
405+ $ returnVfs = vfsStream::setup ($ return ['value ' ], null , []);
406+ $ return ['value ' ] = $ returnVfs ->url ();
407+ }
390408 }
391409
392410 $ file = null ;
@@ -395,9 +413,9 @@ public function test_getMoodleRoot(
395413 foreach ($ config as $ key => $ value ) {
396414 if ($ key === 'file ' ) {
397415 // We are passing a real File, prepare it.
398- $ config = new Config ();
399- $ ruleset = new Ruleset ($ config );
400- $ file = new File ($ value , $ ruleset , $ config );
416+ $ phpcsConfig = new Config ();
417+ $ phpcsRuleset = new Ruleset ($ phpcsConfig );
418+ $ file = new File ($ value , $ phpcsRuleset , $ phpcsConfig );
401419 } else {
402420 // Normal config.
403421 Config::setConfigData ($ key , $ value , true );
@@ -427,9 +445,7 @@ public function test_getMoodleRoot(
427445 // We need to unset all config options when passed.
428446 if ($ config ) {
429447 foreach ($ config as $ key => $ value ) {
430- if ($ key !== 'file ' ) {
431- Config::setConfigData ($ key , null , true );
432- }
448+ Config::setConfigData ($ key , null , true );
433449 }
434450 }
435451 }
0 commit comments