This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
RoboFile.php
494 lines (426 loc) · 11 KB
/
RoboFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* @package Joomla.Site
* @subpackage RoboFile
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
use Robo\Tasks;
/**
* This is joomla project's console command file for Robo.li task runner.
*
* Do a `$ composer install` afterwards you will be able to execute robo like
* `$ ./libraries/vendor/bin/robo` to see a list of commands
*
* @see http://robo.li/
*/
require_once __DIR__ . '/libraries/vendor/autoload.php';
if (!defined('JPATH_BASE'))
{
define('JPATH_BASE', __DIR__);
}
/**
* System Test (Codeception) test execution for Joomla!
*
* @package RoboFile
*
* @since 3.7.3
*/
class RoboFile extends Tasks
{
use JoomlaRobo\Tasks;
/**
* Path to the Selenium folder#
*
* @var string
* @since 3.7.3
*/
const SELENIUM_FOLDER = __DIR__ . '/libraries/vendor/joomla-projects/selenium-server-standalone';
/**
* Path to the vendor folder
*
* @var string
* @since 3.7.3
*/
private $vendorPath = 'libraries/vendor/';
/**
* Path to the tests
*
* @var string
* @since 4.0.0
*/
private $testsPath = 'tests/Codeception/';
/**
* @var array | null
* @since 3.7.3
*/
private $suiteConfig;
/**
* RoboFile constructor.
*
* @since 3.7.3
*/
public function __construct()
{
// Set default timezone (so no warnings are generated if it is not set)
date_default_timezone_set('UTC');
}
/**
* Creates a testing Joomla site for running the tests (use it before run:test)
*
* @param bool $useHtaccess (1/0) Rename and enable embedded Joomla .htaccess file
*
* @return void
* @since 3.7.3
*
* @throws Exception
*/
public function createTestingSite($useHtaccess = false)
{
$cmsPath = $this->getSuiteConfig()['modules']['config']['Helper\\Acceptance']['cmsPath'];
$localUser = $this->getSuiteConfig()['modules']['config']['Helper\\Acceptance']['localUser'];
// Clean old testing site
if (is_dir($cmsPath))
{
try
{
$this->taskDeleteDir($cmsPath)->run();
}
catch (Exception $e)
{
// Sorry, we tried :(
$this->say('Sorry, you will have to delete ' . $cmsPath . ' manually.');
exit(1);
}
}
$exclude = [
'.drone',
'.github',
'.git',
'.run',
'.idea',
'build',
'dev',
'node_modules',
'tests',
'test-install',
'.appveyor.yml',
'.babelrc',
'.drone.yml',
'.editorconfig',
'.eslintignore',
'.eslintrc',
'.gitignore',
'.hound.yml',
'.php_cs',
'.travis.yml',
'appveyor-phpunit.xml',
'build.js',
'build.xml',
'codeception.yml',
'composer.json',
'composer.lock',
'configuration.php',
'drone-package.json',
'Gemfile',
'htaccess.txt',
'karma.conf.js',
'package.json',
'package-lock.json',
'phpunit.xml.dist',
'RoboFile.dist.ini',
'RoboFile.php',
'robots.txt.dist',
'scss-lint.yml',
'selenium.log',
'travisci-phpunit.xml',
];
$this->copyJoomla($cmsPath, $exclude);
// Optionally change owner to fix permissions issues
if (!empty($localUser))
{
$this->_exec('chown -R ' . $localUser . ' ' . $cmsPath);
}
// Optionally uses Joomla default htaccess file. Used by TravisCI
if ($useHtaccess == true)
{
$this->say('Renaming htaccess.txt to .htaccess');
$this->_copy('./htaccess.txt', $cmsPath . '/.htaccess');
$this->_exec('sed -e "s,# RewriteBase /,RewriteBase /test-install/joomla-cms,g" -in-place test-install/joomla-cms/.htaccess');
}
}
/**
* Copy the Joomla installation excluding folders
*
* @param string $dst Target folder
* @param array $exclude Exclude list of folders
*
* @throws Exception
*
* @since 3.7.3
*
* @return void
*/
protected function copyJoomla($dst, $exclude = array())
{
$dir = @opendir(".");
if (false === $dir)
{
throw new Exception($this, "Cannot open source directory");
}
if (!is_dir($dst))
{
mkdir($dst, 0755, true);
}
while (false !== ($file = readdir($dir)))
{
if (in_array($file, $exclude))
{
continue;
}
if (($file !== '.') && ($file !== '..'))
{
$srcFile = "." . '/' . $file;
$destFile = $dst . '/' . $file;
if (is_dir($srcFile))
{
$this->_copyDir($srcFile, $destFile);
}
else
{
copy($srcFile, $destFile);
}
}
}
closedir($dir);
}
/**
* Executes all the Selenium System Tests in a suite on your machine
*
* @param array $opts Array of configuration options:
* - 'use-htaccess': renames and enable embedded Joomla .htaccess file
* - 'env': set a specific environment to get configuration from
*
* @return void
* @since 3.7.3
*
* @throws Exception
*/
public function runTests($opts = ['use-htaccess' => false, 'env' => 'desktop'])
{
$this->say("Running tests");
$pathToCodeception = $this->prepareRun($opts);
$suites = [
'acceptance/install/',
'acceptance/administrator/components/com_content',
'acceptance/administrator/components/com_media',
'acceptance/administrator/components/com_menu',
'acceptance/administrator/components/com_users',
];
foreach ($suites as $suite) {
$this->taskCodecept($pathToCodeception)
->arg('--fail-fast')
->arg('--steps')
->arg('--debug')
->env($opts['env'])
->arg($this->testsPath . $suite)
->run()
->stopOnFail();
}
}
/**
* Install only Joomla
*
* @param array $opts Additional options
*
* @return void
* @since 4.0.0
*
* @throws Exception
*/
public function runInstall($opts = ['use-htaccess' => false, 'env' => 'desktop'])
{
$this->say("Running Installation");
$pathToCodeception = $this->prepareRun($opts);
$this->taskCodecept($pathToCodeception)
->arg('--fail-fast')
->arg('--steps')
->arg('--debug')
->env($opts['env'])
->arg($this->testsPath . 'acceptance/install/')
->run()
->stopOnFail();
}
/**
* Prepare the installation
*
* @param array $opts Optional Options
*
* @return string Path to codeception
*
* @since 4.0.0
* @throws Exception
*/
protected function prepareRun($opts = ['use-htaccess' => false, 'env' => 'desktop'])
{
$this->createTestingSite($opts['use-htaccess']);
$this->taskRunSelenium(self::SELENIUM_FOLDER, $this->getWebdriver())->run();
// Wait until the server started
sleep(3);
// Make sure to run the build command to generate AcceptanceTester
if ($this->isWindows())
{
$this->_exec('php ' . $this->getWindowsPath($this->vendorPath . 'bin/codecept') . ' build');
$pathToCodeception = $this->getWindowsPath($this->vendorPath . 'bin/codecept');
}
else
{
$this->_exec('php ' . $this->vendorPath . 'bin/codecept build');
$pathToCodeception = $this->vendorPath . 'bin/codecept';
}
return $pathToCodeception;
}
/**
* Executes a specific Selenium System Tests in your machine
*
* @param string $pathToTestFile Optional name of the test to be run
* @param string $suite Optional name of the suite containing the tests, Acceptance by default.
*
* @since 3.7.3
*
* @throws Exception if test not found
*
* @return void
*/
public function runTest($pathToTestFile = null, $suite = 'acceptance')
{
$this->taskRunSelenium(self::SELENIUM_FOLDER, $this->getWebdriver());
// Make sure to run the build command to generate AcceptanceTester
$path = $this->vendorPath . 'bin/codecept';
$this->_exec('php ' . $this->isWindows() ? $this->getWindowsPath($path) : $path . ' build');
if (!$pathToTestFile)
{
$this->say('Available tests in the system:');
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$this->testsPath . '/' . $suite,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::SELF_FIRST
);
$tests = array();
$i = 1;
$iterator->rewind();
while ($iterator->valid())
{
if (strripos($iterator->getSubPathName(), 'cept.php')
|| strripos($iterator->getSubPathName(), 'cest.php')
|| strripos($iterator->getSubPathName(), '.feature')
)
{
$this->say('[' . $i . '] ' . $iterator->getSubPathName());
$tests[$i] = $iterator->getSubPathName();
$i++;
}
$iterator->next();
}
$this->say('');
$testNumber = $this->ask('Type the number of the test in the list that you want to run...');
$test = $tests[$testNumber];
}
$pathToTestFile = $this->testsPath . '/' . $suite . '/' . $test;
// Loading the class to display the methods in the class
// Logic to fetch the class name from the file name
$fileName = explode("/", $test);
// If the selected file is cest only then we will give the option to execute individual methods, we don't need this in cept or feature files
$i = 1;
if (isset($fileName[1]) && strripos($fileName[1], 'cest'))
{
require $this->testsPath . '/' . $suite . '/' . $test;
$className = explode(".", $fileName[1]);
$class_methods = get_class_methods($className[0]);
$this->say('[' . $i . '] ' . 'All');
$methods[$i] = 'All';
$i++;
foreach ($class_methods as $method_name)
{
$reflect = new ReflectionMethod($className[0], $method_name);
if (!$reflect->isConstructor() && $reflect->isPublic())
{
$this->say('[' . $i . '] ' . $method_name);
$methods[$i] = $method_name;
$i++;
}
}
$this->say('');
$methodNumber = $this->ask('Please choose the method in the test that you would want to run...');
$method = $methods[$methodNumber];
}
if (isset($method) && $method != 'All')
{
$pathToTestFile = $pathToTestFile . ':' . $method;
}
$testPathCodecept = $this->vendorPath . 'bin/codecept';
$this->taskCodecept($this->isWindows() ? $this->getWindowsPath($testPathCodecept) : $testPathCodecept)
->test($pathToTestFile)
->arg('--steps')
->arg('--debug')
->run()
->stopOnFail();
}
/**
* Check if local OS is Windows
*
* @return bool
*
* @since 3.7.3
*/
private function isWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
/**
* Return the correct path for Windows (needed by CMD)
*
* @param string $path Linux path
*
* @return string
*
* @since 3.7.3
*/
private function getWindowsPath($path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
/**
* Detect the correct driver for selenium
*
* @return string the webdriver string to use with selenium
*
* @since 3.7.3
*/
public function getWebdriver()
{
$suiteConfig = $this->getSuiteConfig();
$driver = $suiteConfig['modules']['config']['JoomlaBrowser']['browser'];
return $driver;
}
/**
* Get the suite configuration
*
* @param string $suite Name of the test suite
*
* @return array
*
* @since 3.7.3
*/
private function getSuiteConfig($suite = 'acceptance')
{
if (!$this->suiteConfig)
{
$this->suiteConfig = Symfony\Component\Yaml\Yaml::parse(file_get_contents(__DIR__ . '/tests/Codeception/' . $suite . '.suite.yml'));
}
return $this->suiteConfig;
}
}