Skip to content

Commit ec0a687

Browse files
committed
Added commands to test, check and fix code
1 parent 7c3b7a0 commit ec0a687

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

cli.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ function (&$item) {
7777
$options->registerCommand('cleanLang',
7878
'Clean language files from unused language strings. Detecting which strings are truly in use may ' .
7979
'not always correctly work. Use with caution.');
80+
81+
$options->registerCommand('test', 'Run the unit tests for this extension.');
82+
83+
$options->registerCommand('check', 'Check for code style violations.');
84+
$options->registerArgument('files...', 'The files to check. Defaults to the whole extension.', false, 'check');
85+
86+
$options->registerCommand('fix', 'Fix code style violations and refactor outdated code.');
87+
$options->registerArgument('files...', 'The files to check. Defaults to the whole extension.', false, 'fix');
8088
}
8189

8290
/** @inheritDoc */
@@ -112,6 +120,12 @@ protected function main(Options $options)
112120
return $this->cmdCleanSVG($args, $keep);
113121
case 'cleanLang':
114122
return $this->cmdCleanLang();
123+
case 'test':
124+
return $this->cmdTest();
125+
case 'check':
126+
return $this->cmdCheck($args);
127+
case 'fix':
128+
return $this->cmdFix();
115129
default:
116130
$this->error('Unknown command');
117131
echo $options->help();
@@ -429,5 +443,113 @@ protected function cmdCleanLang()
429443
return 0;
430444
}
431445

446+
/**
447+
* @return int
448+
*/
449+
protected function cmdTest()
450+
{
451+
$dir = fullpath(getcwd());
452+
[$base, $type] = $this->getTypedNameFromDir($dir);
453+
454+
if ($this->colors->isEnabled()) {
455+
$colors = 'always';
456+
} else {
457+
$colors = 'never';
458+
}
459+
460+
$args = [
461+
fullpath(__DIR__ . '/../../../_test/vendor/bin/phpunit'),
462+
'--verbose',
463+
"--colors=$colors",
464+
'--configuration', fullpath(__DIR__ . '/../../../_test/phpunit.xml'),
465+
'--group', $type . '_' . $base,
466+
];
467+
$cmd = join(' ', array_map('escapeshellarg', $args));
468+
$this->info("Running $cmd");
469+
470+
$result = 0;
471+
passthru($cmd, $result);
472+
return $result;
473+
}
474+
475+
/**
476+
* @return int
477+
*/
478+
protected function cmdCheck($files = [])
479+
{
480+
$dir = fullpath(getcwd());
481+
482+
$args = [
483+
fullpath(__DIR__ . '/../../../_test/vendor/bin/phpcs'),
484+
'--standard=' . fullpath(__DIR__ . '/../../../_test/phpcs.xml'),
485+
($this->colors->isEnabled()) ? '--colors' : '--no-colors',
486+
'--',
487+
];
488+
489+
if ($files) {
490+
$args = array_merge($args, $files);
491+
} else {
492+
$args[] = fullpath($dir);
493+
}
494+
495+
$cmd = join(' ', array_map('escapeshellarg', $args));
496+
$this->info("Running $cmd");
497+
498+
$result = 0;
499+
passthru($cmd, $result);
500+
return $result;
501+
}
502+
503+
/**
504+
* @return int
505+
*/
506+
protected function cmdFix($files = [])
507+
{
508+
$dir = fullpath(getcwd());
509+
510+
// first run rector to refactor outdated code
511+
$args = [
512+
fullpath(__DIR__ . '/../../../_test/vendor/bin/rector'),
513+
($this->colors->isEnabled()) ? '--ansi' : '--no-ansi',
514+
'--config=' . fullpath(__DIR__ . '/../../../_test/rector.php'),
515+
'--no-diffs',
516+
'process',
517+
];
518+
519+
if ($files) {
520+
$args = array_merge($args, $files);
521+
} else {
522+
$args[] = fullpath($dir);
523+
}
524+
525+
$cmd = join(' ', array_map('escapeshellarg', $args));
526+
$this->info("Running $cmd");
527+
528+
$result = 0;
529+
passthru($cmd, $result);
530+
if($result !== 0) return $result;
531+
532+
// now run phpcbf to clean up code style
533+
$args = [
534+
fullpath(__DIR__ . '/../../../_test/vendor/bin/phpcbf'),
535+
'--standard=' . fullpath(__DIR__ . '/../../../_test/phpcs.xml'),
536+
($this->colors->isEnabled()) ? '--colors' : '--no-colors',
537+
'--',
538+
];
539+
540+
if ($files) {
541+
$args = array_merge($args, $files);
542+
} else {
543+
$args[] = fullpath($dir);
544+
}
545+
546+
$cmd = join(' ', array_map('escapeshellarg', $args));
547+
$this->info("Running $cmd");
548+
549+
$result = 0;
550+
passthru($cmd, $result);
551+
return $result;
552+
}
553+
432554
//endregion
433555
}

0 commit comments

Comments
 (0)