From 43bae363e5b1dd71f9e3648757a89480fbec10f4 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sat, 18 Dec 2021 17:01:18 -0500 Subject: [PATCH] Change all instances of options to use CliOptions --- PhpcsChanged/Cli.php | 45 ++++---- PhpcsChanged/FullReporter.php | 7 +- PhpcsChanged/GitWorkflow.php | 51 +++++---- PhpcsChanged/JsonReporter.php | 3 +- PhpcsChanged/PhpcsMessagesHelpers.php | 2 +- PhpcsChanged/Reporter.php | 3 +- PhpcsChanged/XmlReporter.php | 3 +- bin/phpcs-changed | 55 +++++----- tests/FullReporterTest.php | 15 +-- tests/GitWorkflowTest.php | 74 +++++++------ tests/JsonReporterTest.php | 15 +-- tests/SvnWorkflowTest.php | 149 +++++++++++++++----------- tests/XmlReporterTest.php | 15 +-- 13 files changed, 233 insertions(+), 204 deletions(-) diff --git a/PhpcsChanged/Cli.php b/PhpcsChanged/Cli.php index 349b3e4..7907d5e 100644 --- a/PhpcsChanged/Cli.php +++ b/PhpcsChanged/Cli.php @@ -4,6 +4,7 @@ namespace PhpcsChanged\Cli; use PhpcsChanged\NoChangesException; +use PhpcsChanged\CliOptions; use PhpcsChanged\Reporter; use PhpcsChanged\JsonReporter; use PhpcsChanged\FullReporter; @@ -193,11 +194,13 @@ function runManualWorkflow(string $diffFile, string $phpcsOldFile, string $phpcs return $messages; } -function runSvnWorkflow(array $svnFiles, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { +function runSvnWorkflow(CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { $svn = getenv('SVN') ?: 'svn'; $phpcs = getenv('PHPCS') ?: 'phpcs'; $cat = getenv('CAT') ?: 'cat'; + $svnFiles = $options->files; + try { $debug('validating executables'); $shell->validateExecutableExists('svn', $svn); @@ -221,12 +224,12 @@ function runSvnWorkflow(array $svnFiles, array $options, ShellOperator $shell, C return PhpcsMessages::merge($phpcsMessages); } -function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { +function runSvnWorkflowForFile(string $svnFile, CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { $svn = getenv('SVN') ?: 'svn'; $phpcs = getenv('PHPCS') ?: 'phpcs'; $cat = getenv('CAT') ?: 'cat'; - $phpcsStandard = $options['standard'] ?? null; + $phpcsStandard = $options->phpcsStandard; $phpcsStandardOption = $phpcsStandard ? ' --standard=' . escapeshellarg($phpcsStandard) : ''; try { @@ -285,19 +288,21 @@ function runSvnWorkflowForFile(string $svnFile, array $options, ShellOperator $s ); } -function runGitWorkflow(array $gitFiles, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { +function runGitWorkflow(CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { $git = getenv('GIT') ?: 'git'; $phpcs = getenv('PHPCS') ?: 'phpcs'; $cat = getenv('CAT') ?: 'cat'; + $gitFiles = $options->files; + try { $debug('validating executables'); $shell->validateExecutableExists('git', $git); $shell->validateExecutableExists('phpcs', $phpcs); $shell->validateExecutableExists('cat', $cat); $debug('executables are valid'); - if (isset($options['git-base']) && ! empty($options['git-base'])) { - $options['git-base'] = getGitMergeBase($git, [$shell, 'executeCommand'], $options, $debug); + if ($options->mode === 'git-base' && ! empty($options->gitBase)) { + $options->gitBase = getGitMergeBase($git, [$shell, 'executeCommand'], $options, $debug); } } catch(\Exception $err) { $shell->printError($err->getMessage()); @@ -316,12 +321,12 @@ function runGitWorkflow(array $gitFiles, array $options, ShellOperator $shell, C return PhpcsMessages::merge($phpcsMessages); } -function runGitWorkflowForFile(string $gitFile, array $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { +function runGitWorkflowForFile(string $gitFile, CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { $git = getenv('GIT') ?: 'git'; $phpcs = getenv('PHPCS') ?: 'phpcs'; $cat = getenv('CAT') ?: 'cat'; - $phpcsStandard = $options['standard'] ?? null; + $phpcsStandard = $options->phpcsStandard; $phpcsStandardOption = $phpcsStandard ? ' --standard=' . escapeshellarg($phpcsStandard) : ''; try { @@ -382,15 +387,14 @@ function runGitWorkflowForFile(string $gitFile, array $options, ShellOperator $s return getNewPhpcsMessages($unifiedDiff, PhpcsMessages::fromPhpcsJson($oldFilePhpcsOutput, $fileName), PhpcsMessages::fromPhpcsJson($newFilePhpcsOutput, $fileName)); } -function reportMessagesAndExit(PhpcsMessages $messages, string $reportType, array $options): void { - $reporter = getReporter($reportType); +function reportMessagesAndExit(PhpcsMessages $messages, CliOptions $options): void { + $reporter = getReporter($options->reporter); echo $reporter->getFormattedMessages($messages, $options); exit($reporter->getExitCode($messages)); } function fileHasValidExtension(\SplFileInfo $file): bool { // The following logic is copied from PHPCS itself. See https://github.com/squizlabs/PHP_CodeSniffer/blob/2ecd8dc15364cdd6e5089e82ffef2b205c98c412/src/Filters/Filter.php#L161 - // phpcs:disable $AllowedExtensions = [ 'php', 'inc', @@ -410,7 +414,7 @@ function fileHasValidExtension(\SplFileInfo $file): bool { $extensions = []; array_shift($fileParts); - foreach ($fileParts as $part) { + foreach ($fileParts as $part) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $extensions[] = implode('.', $fileParts); array_shift($fileParts); } @@ -420,7 +424,6 @@ function fileHasValidExtension(\SplFileInfo $file): bool { } return true; - // phpcs:enable } function shouldIgnorePath(string $path, string $patternOption = null): bool { @@ -500,17 +503,11 @@ function shouldIgnorePath(string $path, string $patternOption = null): bool { return false; } -function isCachingEnabled(array $options): bool { - if (isset($options['no-cache'])) { - return false; - } - if (isset($options['cache'])) { - return true; - } - return false; +function isCachingEnabled(CliOptions $options): bool { + return $options->useCache; } -function loadCache(CacheManager $cache, ShellOperator $shell, array $options): void { +function loadCache(CacheManager $cache, ShellOperator $shell, CliOptions $options): void { if (isCachingEnabled($options)) { try { $cache->load(); @@ -525,7 +522,7 @@ function loadCache(CacheManager $cache, ShellOperator $shell, array $options): v } } - if (isset($options['clear-cache'])) { + if ($options->clearCache) { $cache->clearCache(); try { $cache->save(); @@ -537,7 +534,7 @@ function loadCache(CacheManager $cache, ShellOperator $shell, array $options): v } } -function saveCache(CacheManager $cache, ShellOperator $shell, array $options): void { +function saveCache(CacheManager $cache, ShellOperator $shell, CliOptions $options): void { if (isCachingEnabled($options)) { try { $cache->save(); diff --git a/PhpcsChanged/FullReporter.php b/PhpcsChanged/FullReporter.php index 9d6123e..089244c 100644 --- a/PhpcsChanged/FullReporter.php +++ b/PhpcsChanged/FullReporter.php @@ -6,10 +6,11 @@ use PhpcsChanged\Reporter; use PhpcsChanged\PhpcsMessages; use PhpcsChanged\LintMessage; +use PhpcsChanged\CliOptions; use function PhpcsChanged\Cli\getLongestString; class FullReporter implements Reporter { - public function getFormattedMessages(PhpcsMessages $messages, array $options): string { + public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string { $files = array_unique(array_map(function(LintMessage $message): string { return $message->getFile() ?? 'STDIN'; }, $messages->getMessages())); @@ -30,7 +31,7 @@ public function getFormattedMessages(PhpcsMessages $messages, array $options): s }, $files))); } - private function getFormattedMessagesForFile(array $messages, string $file, array $options): ?string { + private function getFormattedMessagesForFile(array $messages, string $file, ?CliOptions $options): ?string { $lineCount = count($messages); if ($lineCount < 1) { return null; @@ -52,7 +53,7 @@ private function getFormattedMessagesForFile(array $messages, string $file, arra $formattedLines = implode("\n", array_map(function(LintMessage $message) use ($longestNumber, $options): string { $source = $message->getSource() ?: 'Unknown'; - $sourceString = isset($options['s']) ? " ({$source})" : ''; + $sourceString = !empty($options->showMessageCodes) ? " ({$source})" : ''; return sprintf(" %{$longestNumber}d | %s | %s%s", $message->getLineNumber(), $message->getType(), $message->getMessage(), $sourceString); }, $messages)); diff --git a/PhpcsChanged/GitWorkflow.php b/PhpcsChanged/GitWorkflow.php index 9479170..352b480 100644 --- a/PhpcsChanged/GitWorkflow.php +++ b/PhpcsChanged/GitWorkflow.php @@ -5,6 +5,7 @@ use PhpcsChanged\NoChangesException; use PhpcsChanged\ShellException; +use PhpcsChanged\CliOptions; function validateGitFileExists(string $gitFile, string $git, callable $isReadable, callable $executeCommand, callable $debug, array $options): void { if (isset($options['arc-lint'])) { @@ -23,7 +24,7 @@ function validateGitFileExists(string $gitFile, string $git, callable $isReadabl } } -function isRunFromGitRoot( string $git, callable $executeCommand, callable $debug ): bool { +function isRunFromGitRoot(string $git, callable $executeCommand, callable $debug): bool { static $isRunFromGitRoot; if (null !== $isRunFromGitRoot) { return $isRunFromGitRoot; @@ -39,24 +40,24 @@ function isRunFromGitRoot( string $git, callable $executeCommand, callable $debu return $isRunFromGitRoot; } -function getGitMergeBase(string $git, callable $executeCommand, array $options, callable $debug): string { - if ( empty($options['git-base']) ) { +function getGitMergeBase(string $git, callable $executeCommand, CliOptions $options, callable $debug): string { + if ( empty($options->gitBase) ) { return ''; } - $mergeBaseCommand = "{$git} merge-base " . escapeshellarg($options['git-base']) . ' HEAD'; + $mergeBaseCommand = "{$git} merge-base " . escapeshellarg($options->gitBase) . ' HEAD'; $debug('running merge-base command:', $mergeBaseCommand); $mergeBase = $executeCommand($mergeBaseCommand); if (! $mergeBase) { $debug('merge-base command produced no output'); - return $options['git-base']; + return $options->gitBase; } $debug('merge-base command output:', $mergeBase); return trim($mergeBase); } -function getGitUnifiedDiff(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): string { - $objectOption = isset($options['git-base']) && ! empty($options['git-base']) ? ' ' . escapeshellarg($options['git-base']) . '...' : ''; - $stagedOption = empty( $objectOption ) && ! isset($options['git-unstaged']) ? ' --staged' : ''; +function getGitUnifiedDiff(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): string { + $objectOption = $options->mode === 'git-base' && ! empty($options->gitBase) ? ' ' . escapeshellarg($options->gitBase) . '...' : ''; + $stagedOption = empty( $objectOption ) && $options->mode !== 'git-unstaged' ? ' --staged' : ''; $unifiedDiffCommand = "{$git} diff{$stagedOption}{$objectOption} --no-prefix " . escapeshellarg($gitFile); $debug('running diff command:', $unifiedDiffCommand); $unifiedDiff = $executeCommand($unifiedDiffCommand); @@ -67,16 +68,16 @@ function getGitUnifiedDiff(string $gitFile, string $git, callable $executeComman return $unifiedDiff; } -function isNewGitFile(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool { - if ( isset($options['git-base']) && ! empty($options['git-base']) ) { +function isNewGitFile(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool { + if ( $options->mode === 'git-base' && ! empty($options->gitBase) ) { return isNewGitFileRemote( $gitFile, $git, $executeCommand, $options, $debug ); } else { return isNewGitFileLocal( $gitFile, $git, $executeCommand, $options, $debug ); } } -function isNewGitFileRemote(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool { - $gitStatusCommand = "${git} cat-file -e " . escapeshellarg($options['git-base']) . ':' . escapeshellarg($gitFile) . ' 2>/dev/null'; +function isNewGitFileRemote(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool { + $gitStatusCommand = "${git} cat-file -e " . escapeshellarg($options->gitBase) . ':' . escapeshellarg($gitFile) . ' 2>/dev/null'; $debug('checking status of file with command:', $gitStatusCommand); $return_val = 1; $gitStatusOutput = []; @@ -86,7 +87,7 @@ function isNewGitFileRemote(string $gitFile, string $git, callable $executeComma return 0 !== $return_val; } -function isNewGitFileLocal(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): bool { +function isNewGitFileLocal(string $gitFile, string $git, callable $executeCommand, CliOptions $options, callable $debug): bool { $gitStatusCommand = "${git} status --porcelain " . escapeshellarg($gitFile); $debug('checking git status of file with command:', $gitStatusCommand); $gitStatusOutput = $executeCommand($gitStatusCommand); @@ -100,7 +101,7 @@ function isNewGitFileLocal(string $gitFile, string $git, callable $executeComman return isset($gitStatusOutput[0]) && $gitStatusOutput[0] === 'A'; } -function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, string $phpcsStandardOption, callable $executeCommand, array $options, callable $debug): string { +function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, string $phpcsStandardOption, callable $executeCommand, CliOptions $options, callable $debug): string { $oldFileContents = getOldGitRevisionContentsCommand($gitFile, $git, $options, $executeCommand, $debug); $oldFilePhpcsOutputCommand = "{$oldFileContents} | {$phpcs} --report=json -q" . $phpcsStandardOption . ' --stdin-path=' . escapeshellarg($gitFile) . ' -'; @@ -113,7 +114,7 @@ function getGitBasePhpcsOutput(string $gitFile, string $git, string $phpcs, stri return $oldFilePhpcsOutput; } -function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, string $cat, string $phpcsStandardOption, callable $executeCommand, array $options, callable $debug): string { +function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, string $cat, string $phpcsStandardOption, callable $executeCommand, CliOptions $options, callable $debug): string { $newFileContents = getNewGitRevisionContentsCommand($gitFile, $git, $cat, $options, $executeCommand, $debug); $newFilePhpcsOutputCommand = "{$newFileContents} | {$phpcs} --report=json -q" . $phpcsStandardOption . ' --stdin-path=' . escapeshellarg($gitFile) .' -'; @@ -130,14 +131,14 @@ function getGitNewPhpcsOutput(string $gitFile, string $git, string $phpcs, strin return $newFilePhpcsOutput; } -function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $cat, array $options, callable $executeCommand, callable $debug): string { - if (isset($options['git-base']) && ! empty($options['git-base'])) { +function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $cat, CliOptions $options, callable $executeCommand, callable $debug): string { + if ($options->mode === 'git-base' && ! empty($options->gitBase)) { // for git-base mode, we get the contents of the file from the HEAD version of the file in the current branch if (isRunFromGitRoot($git, $executeCommand, $debug)) { return "{$git} show HEAD:" . escapeshellarg($gitFile); } return "{$git} show HEAD:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ')'; - } else if (isset($options['git-unstaged'])) { + } else if ($options->mode === 'git-unstaged') { // for git-unstaged mode, we get the contents of the file from the current working copy return "{$cat} " . escapeshellarg($gitFile); } @@ -148,11 +149,11 @@ function getNewGitRevisionContentsCommand(string $gitFile, string $git, string $ return "{$git} show :0:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ')'; } -function getOldGitRevisionContentsCommand(string $gitFile, string $git, array $options, callable $executeCommand, callable $debug): string { - if (isset($options['git-base']) && ! empty($options['git-base'])) { +function getOldGitRevisionContentsCommand(string $gitFile, string $git, CliOptions $options, callable $executeCommand, callable $debug): string { + if ($options->mode === 'git-base' && ! empty($options->gitBase)) { // git-base - $rev = escapeshellarg($options['git-base']); - } else if (isset($options['git-unstaged'])) { + $rev = escapeshellarg($options->gitBase); + } else if ($options->mode === 'git-unstaged') { // git-unstaged $rev = ':0'; // :0 in this case means "staged version or HEAD if there is no staged version" } else { @@ -165,9 +166,7 @@ function getOldGitRevisionContentsCommand(string $gitFile, string $git, array $o return "${git} show {$rev}:$(${git} ls-files --full-name " . escapeshellarg($gitFile) . ")"; } - - -function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, array $options, callable $debug): string { +function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, CliOptions $options, callable $debug): string { $fileContents = getNewGitRevisionContentsCommand($gitFile, $git, $cat, $options, $executeCommand, $debug); $command = "{$fileContents} | {$git} hash-object --stdin"; $debug('running new file git hash command:', $command); @@ -179,7 +178,7 @@ function getNewGitFileHash(string $gitFile, string $git, string $cat, callable $ return $hash; } -function getOldGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, array $options, callable $debug): string { +function getOldGitFileHash(string $gitFile, string $git, string $cat, callable $executeCommand, CliOptions $options, callable $debug): string { $fileContents = getOldGitRevisionContentsCommand($gitFile, $git, $options, $executeCommand, $debug); $command = "{$fileContents} | {$git} hash-object --stdin"; $debug('running old file git hash command:', $command); diff --git a/PhpcsChanged/JsonReporter.php b/PhpcsChanged/JsonReporter.php index af3f0f2..8d50f6e 100644 --- a/PhpcsChanged/JsonReporter.php +++ b/PhpcsChanged/JsonReporter.php @@ -7,9 +7,10 @@ use PhpcsChanged\PhpcsMessages; use PhpcsChanged\PhpcsMessagesHelpers; use PhpcsChanged\LintMessage; +use PhpcsChanged\CliOptions; class JsonReporter implements Reporter { - public function getFormattedMessages(PhpcsMessages $messages, array $options): string { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $files = array_unique(array_map(function(LintMessage $message): string { return $message->getFile() ?? 'STDIN'; }, $messages->getMessages())); diff --git a/PhpcsChanged/PhpcsMessagesHelpers.php b/PhpcsChanged/PhpcsMessagesHelpers.php index 5509494..4a14b55 100644 --- a/PhpcsChanged/PhpcsMessagesHelpers.php +++ b/PhpcsChanged/PhpcsMessagesHelpers.php @@ -37,7 +37,7 @@ public static function fromPhpcsJson(string $messages, string $forcedFileName = public static function toPhpcsJson(PhpcsMessages $messages): string { $reporter = new JsonReporter(); - return $reporter->getFormattedMessages($messages, []); + return $reporter->getFormattedMessages($messages, null); } public static function fromArrays(array $messages, string $fileName = null): PhpcsMessages { diff --git a/PhpcsChanged/Reporter.php b/PhpcsChanged/Reporter.php index 3c9d5e0..37fc288 100644 --- a/PhpcsChanged/Reporter.php +++ b/PhpcsChanged/Reporter.php @@ -4,8 +4,9 @@ namespace PhpcsChanged; use PhpcsChanged\PhpcsMessages; +use PhpcsChanged\CliOptions; interface Reporter { - public function getFormattedMessages(PhpcsMessages $messages, array $options): string; + public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string; public function getExitCode(PhpcsMessages $messages): int; } diff --git a/PhpcsChanged/XmlReporter.php b/PhpcsChanged/XmlReporter.php index b5ba77f..79383f8 100644 --- a/PhpcsChanged/XmlReporter.php +++ b/PhpcsChanged/XmlReporter.php @@ -8,9 +8,10 @@ use PhpcsChanged\LintMessage; use PhpcsChanged\UnixShell; use PhpcsChanged\ShellException; +use PhpcsChanged\CliOptions; class XmlReporter implements Reporter { - public function getFormattedMessages(PhpcsMessages $messages, array $options): string { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + public function getFormattedMessages(PhpcsMessages $messages, ?CliOptions $options): string { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable $files = array_unique(array_map(function(LintMessage $message): string { return $message->getFile() ?? 'STDIN'; }, $messages->getMessages())); diff --git a/bin/phpcs-changed b/bin/phpcs-changed index 9fffc45..71a7bb2 100755 --- a/bin/phpcs-changed +++ b/bin/phpcs-changed @@ -23,6 +23,8 @@ use function PhpcsChanged\Cli\{ use PhpcsChanged\UnixShell; use PhpcsChanged\CacheManager; use PhpcsChanged\FileCache; +use PhpcsChanged\CliOptions; +use PhpcsChanged\InvalidOptionException; $optind = 0; $options = getopt( @@ -58,7 +60,7 @@ foreach( $fileNames as $file ) { if ( shouldIgnorePath($file, $options['ignore'] ?? null, '') ) { continue; } - $iterator = new RecursiveIteratorIterator(new RecursiveCallbackFilterIterator(new RecursiveDirectoryIterator($file, (RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS)), function($file, $key, $iterator){ + $iterator = new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator(new \RecursiveDirectoryIterator($file, (\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)), function($file, $key, $iterator){ if ($file->isFile() && !fileHasValidExtension($file)) { return false; } @@ -88,61 +90,58 @@ if (isset($options['i'])) { printInstalledCodingStandards(); } -// --git-branch exists for compatibility, --git-bases supports branches +// --git-branch exists for compatibility, --git-base supports branches if (isset($options['git-branch'])) { $options['git-base'] = $options['git-branch']; unset($options['git-branch']); } +if (count($options) === 0) { + printHelp(); + exit(1); +} + +$options['files'] = $fileNamesExpanded; + $debug = getDebug(isset($options['debug'])); -run($options, $fileNamesExpanded, $debug); +run($options, $debug); + +function run(array $rawOptions, callable $debug): void { + $debug('Options: ' . json_encode($rawOptions)); -function run(array $options, array $fileNamesExpanded, callable $debug): void { - $debug('Running on filenames: ' . implode(', ', $fileNamesExpanded)); - $debug('Options: ' . json_encode($options)); - $reportType = $options['report'] ?? 'full'; - $diffFile = $options['diff'] ?? null; - $phpcsOldFile = $options['phpcs-orig'] ?? null; - $phpcsNewFile = $options['phpcs-new'] ?? null; + try { + $options = CliOptions::fromArray($rawOptions); + } catch ( InvalidOptionException $err ) { + printErrorAndExit($err->getMessage()); + exit(1); + } - if ($diffFile && $phpcsOldFile && $phpcsNewFile) { + if ($options->mode === 'manual') { reportMessagesAndExit( - runManualWorkflow($diffFile, $phpcsOldFile, $phpcsNewFile), - $reportType, + runManualWorkflow($options->diffFile, $options->phpcsOldFile, $options->phpcsNewFile), $options ); return; } - if ((isset($options['svn']) || isset($options['git'])) && empty($fileNamesExpanded)) { - printErrorAndExit('You must supply at least one file or directory to process.'); - return; - } - - if (isset($options['svn'])) { + if ($options->mode === 'svn') { $shell = new UnixShell(); reportMessagesAndExit( - runSvnWorkflow($fileNamesExpanded, $options, $shell, new CacheManager(new FileCache(), $debug), $debug), - $reportType, + runSvnWorkflow($options, $shell, new CacheManager(new FileCache(), $debug), $debug), $options ); return; } - if (isset($options['git'])) { + if ($options->isGitMode()) { $shell = new UnixShell(); reportMessagesAndExit( - runGitWorkflow($fileNamesExpanded, $options, $shell, new CacheManager(new FileCache(), $debug), $debug), - $reportType, + runGitWorkflow($options, $shell, new CacheManager(new FileCache(), $debug), $debug), $options ); return; } - if (count($options) > 0) { - printErrorAndExit('You must use either manual or automatic mode.'); - return; - } printHelp(); exit(1); } diff --git a/tests/FullReporterTest.php b/tests/FullReporterTest.php index 7e62026..b375948 100644 --- a/tests/FullReporterTest.php +++ b/tests/FullReporterTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use PhpcsChanged\PhpcsMessages; use PhpcsChanged\FullReporter; +use PhpcsChanged\CliOptions; final class FullReporterTest extends TestCase { public function testSingleWarning() { @@ -31,7 +32,7 @@ public function testSingleWarning() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -58,7 +59,7 @@ public function testSingleWarningWithShowCodeOption() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => true, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -84,7 +85,7 @@ public function testSingleWarningWithShowCodeOptionAndNoCode() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => true, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -121,7 +122,7 @@ public function testMultipleWarningsWithLongLineNumber() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -198,7 +199,7 @@ public function testMultipleWarningsErrorsAndFiles() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => 1, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -208,7 +209,7 @@ public function testNoWarnings() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -235,7 +236,7 @@ public function testSingleWarningWithNoFilename() { EOF; $reporter = new FullReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } diff --git a/tests/GitWorkflowTest.php b/tests/GitWorkflowTest.php index 49940df..1fab360 100644 --- a/tests/GitWorkflowTest.php +++ b/tests/GitWorkflowTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use PhpcsChanged\PhpcsMessages; +use PhpcsChanged\CliOptions; use PhpcsChanged\ShellException; use PhpcsChanged\CacheManager; use PhpcsChangedTests\TestShell; @@ -30,7 +31,7 @@ public function testIsNewGitFileReturnsTrueForNewFile() { return $this->fixture->getNewFileInfo('foobar.php'); } }; - $this->assertTrue(isNewGitFile($gitFile, $git, $executeCommand, array(), '\PhpcsChangedTests\Debug')); + $this->assertTrue(isNewGitFile($gitFile, $git, $executeCommand, CliOptions::fromArray(['git' => true, 'files' => ['test']]), '\PhpcsChangedTests\Debug')); } public function testIsNewGitFileReturnsFalseForOldFile() { @@ -41,7 +42,7 @@ public function testIsNewGitFileReturnsFalseForOldFile() { return $this->fixture->getModifiedFileInfo('foobar.php'); } }; - $this->assertFalse(isNewGitFile($gitFile, $git, $executeCommand, array(), '\PhpcsChangedTests\Debug')); + $this->assertFalse(isNewGitFile($gitFile, $git, $executeCommand, CliOptions::fromArray(['git' => true, 'files' => ['test']]), '\PhpcsChangedTests\Debug')); } public function testGetGitUnifiedDiff() { @@ -54,7 +55,7 @@ public function testGetGitUnifiedDiff() { } return $diff; }; - $this->assertEquals($diff, getGitUnifiedDiff($gitFile, $git, $executeCommand, [], '\PhpcsChangedTests\Debug')); + $this->assertEquals($diff, getGitUnifiedDiff($gitFile, $git, $executeCommand, CliOptions::fromArray(['git' => true, 'files' => ['test']]), '\PhpcsChangedTests\Debug')); } public function testFullGitWorkflowForOneFileStaged() { @@ -66,10 +67,10 @@ public function testFullGitWorkflowForOneFileStaged() { $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20])->toPhpcsJson()); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20, 21], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -82,10 +83,10 @@ public function testFullGitWorkflowForOneFileUnstaged() { $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [21, 20], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = ['git-unstaged' => '1']; + $options = CliOptions::fromArray(['git-unstaged' => '1', 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -100,17 +101,18 @@ public function testFullGitWorkflowForOneFileUnstagedCachesDataThenUsesCache() { $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php') | git hash-object --stdin", 'previous-file-hash'); $shell->registerCommand("cat 'foobar.php' | git hash-object --stdin", 'new-file-hash'); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = [ + $options = CliOptions::fromArray([ 'git-unstaged' => '1', + 'files' => [$gitFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $cache = new CacheManager( new TestCache(), '\PhpcsChangedTests\Debug' ); $expected = $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'); - runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $shell->resetCommandsCalled(); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("git show :0:$(git ls-files --full-name 'foobar.php') | phpcs")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php' | phpcs")); @@ -127,19 +129,20 @@ public function testFullGitWorkflowForOneFileUnstagedCachesDataThenClearsOldCach $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php') | git hash-object --stdin", 'previous-file-hash'); $shell->registerCommand("cat 'foobar.php' | git hash-object --stdin", 'new-file-hash'); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = [ + $options = CliOptions::fromArray([ 'git-unstaged' => '1', + 'files' => [$gitFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $cache = new CacheManager( new TestCache(), '\PhpcsChangedTests\Debug' ); $expected = $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'); - runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $shell->deregisterCommand("git show :0:$(git ls-files --full-name 'foobar.php') | git hash-object --stdin"); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php') | git hash-object --stdin", 'old-file-hash-2'); $shell->resetCommandsCalled(); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("git show :0:$(git ls-files --full-name 'foobar.php') | phpcs")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php' | phpcs")); @@ -156,19 +159,20 @@ public function testFullGitWorkflowForOneFileUnstagedCachesDataThenClearsNewCach $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php') | git hash-object --stdin", 'previous-file-hash'); $shell->registerCommand("cat 'foobar.php' | git hash-object --stdin", 'new-file-hash'); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = [ + $options = CliOptions::fromArray([ 'git-unstaged' => '1', + 'files' => [$gitFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $cache = new CacheManager( new TestCache(), '\PhpcsChangedTests\Debug' ); $expected = $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'); - runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $shell->deregisterCommand("cat 'foobar.php' | git hash-object --stdin"); $shell->registerCommand("cat 'foobar.php' | git hash-object --stdin", 'new-file-hash-2'); $shell->resetCommandsCalled(); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("git show :0:$(git ls-files --full-name 'foobar.php') | phpcs")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php' | phpcs")); @@ -188,13 +192,13 @@ public function testFullGitWorkflowForMultipleFilesStaged() { $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20, 21], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("git show :0:$(git ls-files --full-name 'baz.php')", $this->phpcs->getResults('STDIN', [20, 21], 'Found unused symbol Baz.')->toPhpcsJson()); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => $gitFiles]); $cache = new CacheManager( new TestCache() ); $expected = PhpcsMessages::merge([ $this->phpcs->getResults('bin/foobar.php', [20], 'Found unused symbol Foobar.'), $this->phpcs->getResults('bin/baz.php', [20], 'Found unused symbol Baz.'), ]); - $messages = runGitWorkflow($gitFiles, $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -206,10 +210,10 @@ public function testFullGitWorkflowForUnchangedFileWithPhpcsMessages() { $shell->registerCommand("git status --porcelain 'foobar.php'", ''); $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20])->toPhpcsJson()); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20])->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = PhpcsMessages::fromArrays([], '/dev/null'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -221,10 +225,10 @@ public function testFullGitWorkflowForUnchangedFileWithoutPhpcsMessages() { $shell->registerCommand("git status --porcelain 'foobar.php'", ''); $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [])->toPhpcsJson()); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [])->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = PhpcsMessages::fromArrays([], '/dev/null'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -237,9 +241,9 @@ public function testFullGitWorkflowForNonGitFile() { $shell->registerCommand("git status --porcelain 'foobar.php'", "?? foobar.php" ); $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'foobar.php')", $this->fixture->getNonGitFileShow('foobar.php'), 128); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [20], 'Found unused symbol Foobar.')->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); - runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); } public function testFullGitWorkflowForNewFile() { @@ -250,10 +254,10 @@ public function testFullGitWorkflowForNewFile() { $shell->registerCommand("git status --porcelain 'foobar.php'", $this->fixture->getNewFileInfo('foobar.php')); $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $this->phpcs->getResults('STDIN', [5, 6], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = $this->phpcs->getResults('bin/foobar.php', [5, 6], 'Found unused symbol Foobar.'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -270,10 +274,10 @@ public function testFullGitWorkflowForEmptyNewFile() { '; $shell->registerCommand("git show :0:$(git ls-files --full-name 'foobar.php')", $fixture, 1); - $options = []; + $options = CliOptions::fromArray(['git' => true, 'files' => [$gitFile]]); $cache = new CacheManager( new TestCache() ); $expected = PhpcsMessages::fromArrays([], '/dev/null'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -290,10 +294,10 @@ function testFullGitWorkflowForInterBranchDiff() { $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'bin/foobar.php') | phpcs --report=json -q --stdin-path='bin/foobar.php' -", $this->phpcs->getResults('\/srv\/www\/wordpress-default\/public_html\/test\/bin\/foobar.php', [6, 7], 'Found unused symbol Foobar.')->toPhpcsJson()); $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'bin/foobar.php') | git hash-object --stdin", 'new-file-hash'); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = [ 'git-base' => 'master' ]; + $options = CliOptions::fromArray([ 'git-base' => 'master', 'files' => [$gitFile] ]); $cache = new CacheManager( new TestCache() ); $expected = $this->phpcs->getResults('bin/foobar.php', [6], 'Found unused symbol Foobar.'); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -310,14 +314,14 @@ function testNameDetectionInFullGitWorkflowForInterBranchDiff() { $shell->registerCommand("git show '0123456789abcdef0123456789abcdef01234567':$(git ls-files --full-name 'test.php') | git hash-object --stdin", 'previous-file-hash'); $shell->registerCommand("git show HEAD:$(git ls-files --full-name 'test.php') | git hash-object --stdin", 'new-file-hash'); $shell->registerCommand("git rev-parse --show-toplevel", 'run-from-git-root'); - $options = [ 'git-base' => 'master' ]; + $options = CliOptions::fromArray([ 'git-base' => 'master', 'files' => [$gitFile] ]); $cache = new CacheManager( new TestCache() ); $expected = PhpcsMessages::merge([ $this->phpcs->getResults('test.php', [6], "Found unused symbol 'Foobar'."), $this->phpcs->getResults('test.php', [7], "Found unused symbol 'Foobar'."), $this->phpcs->getResults('test.php', [8], "Found unused symbol 'Foobar'."), ]); - $messages = runGitWorkflow([$gitFile], $options, $shell, $cache, '\PhpcsChangedTests\Debug'); + $messages = runGitWorkflow($options, $shell, $cache, '\PhpcsChangedTests\Debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } } diff --git a/tests/JsonReporterTest.php b/tests/JsonReporterTest.php index f525fb6..f962360 100644 --- a/tests/JsonReporterTest.php +++ b/tests/JsonReporterTest.php @@ -4,6 +4,7 @@ require_once dirname(__DIR__) . '/index.php'; use PHPUnit\Framework\TestCase; +use PhpcsChanged\CliOptions; use PhpcsChanged\PhpcsMessages; use PhpcsChanged\JsonReporter; @@ -24,7 +25,7 @@ public function testSingleWarning() { {"totals":{"errors":0,"warnings":1,"fixable":0},"files":{"fileA.php":{"errors":0,"warnings":1,"messages":[{"line":15,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -44,7 +45,7 @@ public function testSingleWarningWithShowCodeOption() { {"totals":{"errors":0,"warnings":1,"fixable":0},"files":{"fileA.php":{"errors":0,"warnings":1,"messages":[{"line":15,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => 1, 'git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -63,7 +64,7 @@ public function testSingleWarningWithShowCodeOptionAndNoCode() { {"totals":{"errors":0,"warnings":1,"fixable":0},"files":{"fileA.php":{"errors":0,"warnings":1,"messages":[{"line":15,"type":"WARNING","severity":5,"fixable":false,"column":5,"message":"Found unused symbol Foo."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => 1, 'git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -92,7 +93,7 @@ public function testMultipleWarningsWithLongLineNumber() { {"totals":{"errors":0,"warnings":2,"fixable":0},"files":{"fileA.php":{"errors":0,"warnings":2,"messages":[{"line":133825,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."},{"line":15,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Bar."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -151,7 +152,7 @@ public function testMultipleWarningsErrorsAndFiles() { {"totals":{"errors":2,"warnings":3,"fixable":0},"files":{"fileA.php":{"errors":2,"warnings":2,"messages":[{"line":12,"type":"ERROR","severity":5,"fixable":true,"column":2,"source":"ImportDetection.Imports.RequireImports.Something","message":"Found unused symbol Faa."},{"line":15,"type":"ERROR","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."},{"line":18,"type":"WARNING","severity":5,"fixable":false,"column":8,"source":"ImportDetection.Imports.RequireImports.Boom","message":"Found unused symbol Bar."},{"line":22,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."}]},"fileB.php":{"errors":0,"warnings":1,"messages":[{"line":30,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Zoop","message":"Found unused symbol Hi."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => 1, 'git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -161,7 +162,7 @@ public function testNoWarnings() { {"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -181,7 +182,7 @@ public function testSingleWarningWithNoFilename() { {"totals":{"errors":0,"warnings":1,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":1,"messages":[{"line":15,"type":"WARNING","severity":5,"fixable":false,"column":5,"source":"ImportDetection.Imports.RequireImports.Import","message":"Found unused symbol Foo."}]}}} EOF; $reporter = new JsonReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['git' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } diff --git a/tests/SvnWorkflowTest.php b/tests/SvnWorkflowTest.php index 949d363..f69309e 100644 --- a/tests/SvnWorkflowTest.php +++ b/tests/SvnWorkflowTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use PhpcsChanged\PhpcsMessages; +use PhpcsChanged\CliOptions; use PhpcsChanged\ShellException; use PhpcsChanged\CacheManager; use PhpcsChangedTests\TestShell; @@ -68,9 +69,9 @@ public function testFullSvnWorkflowForOneFile() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -81,9 +82,9 @@ public function testFullSvnWorkflowForOneFileWithNoMessages() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getEmptyResults()->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = $this->phpcs->getEmptyResults(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -94,11 +95,13 @@ public function testFullSvnWorkflowForOneFileWithCachingEnabledButNoCache() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -108,13 +111,15 @@ public function testFullSvnWorkflowForOneFileCached() { $shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getAddedLineDiff('foobar.php', 'use Foobar;')); $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $cache->setEntry('foobar.php', 'old', '188280', '', $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); @@ -127,21 +132,23 @@ public function testFullSvnWorkflowForOneFileUncachedThenCachesBothVersionsOfThe $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $manager = new CacheManager($cache); // Run once to cache results - runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); // Run again to prove results have been cached $shell->deregisterCommand("svn cat 'foobar.php'"); $shell->deregisterCommand("cat 'foobar.php'"); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -152,23 +159,25 @@ public function testFullSvnWorkflowForOneDoesNotUseNewFileCacheWhenHashChanges() $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $manager = new CacheManager($cache); // Run once to cache results - runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); // Run again to prove results have been cached $shell->deregisterCommand("svn cat 'foobar.php'"); $shell->deregisterCommand("cat 'foobar.php'"); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); @@ -176,7 +185,7 @@ public function testFullSvnWorkflowForOneDoesNotUseNewFileCacheWhenHashChanges() $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); $shell->setFileHash('foobar.php', 'different-hash'); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); } @@ -188,9 +197,11 @@ public function testFullSvnWorkflowForOneClearsCacheForFileWhenHashChanges() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $original_hash = $shell->getFileHash('foobar.php'); @@ -198,14 +209,14 @@ public function testFullSvnWorkflowForOneClearsCacheForFileWhenHashChanges() { $manager = new CacheManager($cache); // Run once to cache results - runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); // Run again to prove results have been cached $shell->deregisterCommand("svn cat 'foobar.php'"); $shell->deregisterCommand("cat 'foobar.php'"); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); @@ -213,14 +224,14 @@ public function testFullSvnWorkflowForOneClearsCacheForFileWhenHashChanges() { $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); $shell->setFileHash('foobar.php', 'different-hash'); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); // Run a fourth time, restoring the old hash, and make sure we still don't use the (new file) cache $shell->setFileHash('foobar.php', $original_hash); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); } @@ -232,39 +243,41 @@ public function testFullSvnWorkflowForOneDoesNotClearCacheWhenStandardChanges() $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $manager = new CacheManager($cache); // Run once to cache results - $options['standard'] = 'one'; - runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $options->phpcsStandard = 'one'; + runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertTrue($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); // Run again to prove results have been cached $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); // Run a third time, with the standard changed, and make sure we don't use the cache - $options['standard'] = 'two'; + $options->phpcsStandard = 'two'; $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); // Run a fourth time, restoring the standard, and make sure we do use the cache - $options['standard'] = 'one'; + $options->phpcsStandard = 'one'; $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); @@ -277,15 +290,17 @@ public function testFullSvnWorkflowForOneFileUncachedWhenCachingIsDisabled() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'no-cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $cache->disabled = true; $manager = new CacheManager($cache); - runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); - $messages = runSvnWorkflow([$svnFile], $options, $shell, $manager, '\PhpcsChangedTests\debug'); + runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, $manager, '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -296,14 +311,16 @@ public function testFullSvnWorkflowForOneFileWithOldCacheVersion() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $cache->setCacheVersion('0.1-something-else'); $cache->setEntry('foobar.php', 'old', '188280', '', 'blah'); // This invalid JSON will throw if the cache is used - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); @@ -318,14 +335,16 @@ public function testFullSvnWorkflowForOneFileWithCacheThatHasDifferentStandard() $newFileOutput = $this->phpcs->getResults('STDIN', [20, 21]); $shell->registerCommand("svn cat 'foobar.php'", $oldFileOutput->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $newFileOutput->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false 'standard' => 'TestStandard1', - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); $cache->setEntry('foobar.php', 'old', '188280', 'TestStandard2', 'blah'); // This invalid JSON will throw if the cache is used - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertTrue($shell->wasCommandCalled("cat 'foobar.php'")); @@ -337,21 +356,23 @@ public function testFullSvnWorkflowForOneFileWithCacheOfOldFileVersionDoesNotUse $shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getAddedLineDiff('foobar.php', 'use Foobar;')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = $this->phpcs->getResults('bin/foobar.php', [20]); $cache = new TestCache(); // Set the saved cached revisionId to 1000 $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280', '1000')); - runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); // The revisionId of the previous version of the file will be 188000 $shell->deregisterCommand("svn info 'foobar.php'"); $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280', '188000')); $shell->resetCommandsCalled(); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertTrue($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); @@ -363,13 +384,15 @@ public function testFullSvnWorkflowForUnchangedFileWithCache() { $shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getEmptyFileDiff()); $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php', '188280')); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = [ + $options = CliOptions::fromArray([ + 'svn' => true, + 'files' => [$svnFile], 'cache' => false, // getopt is weird and sets options to false - ]; + ]); $expected = PhpcsMessages::fromArrays([], 'bin/foobar.php'); $cache = new TestCache(); $cache->setEntry('foobar.php', 'old', '188280', '', $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager($cache), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); $this->assertFalse($shell->wasCommandCalled("svn cat 'foobar.php'")); $this->assertFalse($shell->wasCommandCalled("cat 'foobar.php'")); @@ -388,12 +411,12 @@ public function testFullSvnWorkflowForMultipleFiles() { $shell->registerCommand("svn cat 'baz.php'", $this->phpcs->getResults('STDIN', [20, 99], 'Found unused symbol Baz.')->toPhpcsJson()); $shell->registerCommand("cat 'baz.php'", $this->phpcs->getResults('STDIN', [20, 21], 'Found unused symbol Baz.')->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => $svnFiles]); $expected = PhpcsMessages::merge([ $this->phpcs->getResults('bin/foobar.php', [20]), $this->phpcs->getResults('bin/baz.php', [20], 'Found unused symbol Baz.'), ]); - $messages = runSvnWorkflow($svnFiles, $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -404,9 +427,9 @@ public function testFullSvnWorkflowForUnchangedFileWithPhpCsMessages() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = PhpcsMessages::fromArrays([], 'STDIN'); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -417,9 +440,9 @@ public function testFullSvnWorkflowForUnchangedFileWithoutPhpCsMessages() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfo('foobar.php')); $shell->registerCommand("svn cat 'foobar.php'|phpcs", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}'); $shell->registerCommand("cat 'foobar.php'|phpcs", '{"totals":{"errors":0,"warnings":0,"fixable":0},"files":{"STDIN":{"errors":0,"warnings":0,"messages":[]}}}'); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = PhpcsMessages::fromArrays([], 'STDIN'); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -431,8 +454,8 @@ public function testFullSvnWorkflowForNonSvnFile() { $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfoNonSvnFile('foobar.php'), 1); $shell->registerCommand("svn cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 99])->toPhpcsJson()); - $options = []; - runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); + runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); } public function testFullSvnWorkflowForNewFile() { @@ -441,9 +464,9 @@ public function testFullSvnWorkflowForNewFile() { $shell->registerCommand("svn diff 'foobar.php'", $this->fixture->getNewFileDiff('foobar.php')); $shell->registerCommand("svn info 'foobar.php'", $this->fixture->getSvnInfoNewFile('foobar.php')); $shell->registerCommand("cat 'foobar.php'", $this->phpcs->getResults('STDIN', [20, 21])->toPhpcsJson()); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = $this->phpcs->getResults('STDIN', [20, 21]); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } @@ -457,9 +480,9 @@ public function testFullSvnWorkflowForEmptyNewFile() { Run "phpcs --help" for usage information '; $shell->registerCommand( "cat 'foobar.php'", $fixture); - $options = []; + $options = CliOptions::fromArray(['svn' => true, 'files' => [$svnFile]]); $expected = PhpcsMessages::fromArrays([], 'STDIN'); - $messages = runSvnWorkflow([$svnFile], $options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); + $messages = runSvnWorkflow($options, $shell, new CacheManager(new TestCache()), '\PhpcsChangedTests\debug'); $this->assertEquals($expected->getMessages(), $messages->getMessages()); } } diff --git a/tests/XmlReporterTest.php b/tests/XmlReporterTest.php index 90e1808..e61dd11 100644 --- a/tests/XmlReporterTest.php +++ b/tests/XmlReporterTest.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use PhpcsChanged\PhpcsMessages; +use PhpcsChanged\CliOptions; use PhpcsChangedTests\TestXmlReporter; final class XmlReporterTest extends TestCase { @@ -31,7 +32,7 @@ public function testSingleWarning() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -57,7 +58,7 @@ public function testSingleWarningWithShowCodeOption() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => true, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -82,7 +83,7 @@ public function testSingleWarningWithShowCodeOptionAndNoCode() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => true, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -118,7 +119,7 @@ public function testMultipleWarningsWithLongLineNumber() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -189,7 +190,7 @@ public function testMultipleWarningsErrorsAndFiles() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, ['s' => 1]); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['s' => true, 'svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -204,7 +205,7 @@ public function testNoWarnings() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); } @@ -230,7 +231,7 @@ public function testSingleWarningWithNoFilename() { EOF; $reporter = new TestXmlReporter(); - $result = $reporter->getFormattedMessages($messages, []); + $result = $reporter->getFormattedMessages($messages, CliOptions::fromArray(['svn' => true, 'files' => ['test']])); $this->assertEquals($expected, $result); }