Skip to content

Commit

Permalink
RegisterExternalStandardsTest: simplify check whether PHPCS can run w…
Browse files Browse the repository at this point in the history
…ith the standard

To check whether PHPCS can run with the standard, previously, a scan on a simple file was done, with this file being created on the fly for each test.

This commit replaces that check with running the "explain" command.

Advantages:
* The "explain" command does not need a file to scan and can still confirm that the standard can be read correctly by PHPCS.
* The output for the "explain" command has fewer differences across PHPCS versions, so is simpler to verify.
  • Loading branch information
jrfnl committed Feb 2, 2022
1 parent f1ad86e commit b973ddf
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions tests/IntegrationTest/RegisterExternalStandardsTest.php
Expand Up @@ -96,20 +96,15 @@ public function testRegisterOneStandardGlobal($phpcsVersion)
static::$tempGlobalPath
);

// Make sure there is a PHP file to scan.
$this->createFile(static::$tempGlobalPath . '/test.php');

// Verify that PHPCS can run with the external standard.
$phpcsCommand = '"vendor/bin/phpcs" -psl . --standard=DummySubDir';
// Verify that PHPCS can with the external standard set as the standard.
$phpcsCommand = '"vendor/bin/phpcs" --standard=DummySubDir -e';
$phpcsResult = $this->executeCliCommand($phpcsCommand, static::$tempGlobalPath);

$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS scan did not match 0');
$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS explain did not match 0');
$this->assertMatchesRegularExpression(
// PHPCS 3.x added the "1/1 (100%)" annotation, the new line (+timing) was added early in the 2.x cycle.
'`^\.(?: 1 / 1 \(100%\))?(?:' . \PHP_EOL . '|$)`',
// Progress reporting moved from stdout to stderr in PHPCS 4.x.
($phpcsVersion[0] !== '4') ? trim($phpcsResult['stdout']) : trim($phpcsResult['stderr']),
'Scanning the directory with PHPCS failed.'
'`DummySubDir \(1 sniffs?\)\s+[-]+\s+DummySubDir\.Demo\.Demo(?:[\r\n]+|$)`',
$phpcsResult['stdout'],
'Output of the PHPCS explain command did not match the expectation.'
);
}

Expand Down Expand Up @@ -170,20 +165,15 @@ public function testRegisterOneStandardLocal($phpcsVersion)
static::$tempLocalPath
);

// Make sure there is a PHP file to scan.
$this->createFile(static::$tempLocalPath . '/test.php');

// Verify that PHPCS can run with the external standard.
$phpcsCommand = '"vendor/bin/phpcs" -psl . --standard=DummySubDir';
// Verify that PHPCS can with the external standard set as the standard.
$phpcsCommand = '"vendor/bin/phpcs" --standard=DummySubDir -e';
$phpcsResult = $this->executeCliCommand($phpcsCommand, static::$tempLocalPath);

$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS scan did not match 0');
$this->assertSame(0, $phpcsResult['exitcode'], 'Exitcode for PHPCS explain did not match 0');
$this->assertMatchesRegularExpression(
// PHPCS 3.x added the "1/1 (100%)" annotation, the new line (+timing) was added early in the 2.x cycle.
'`^\.(?: 1 / 1 \(100%\))?(?:' . \PHP_EOL . '|$)`',
// Progress reporting moved from stdout to stderr in PHPCS 4.x.
($phpcsVersion[0] !== '4') ? ltrim($phpcsResult['stdout']) : ltrim($phpcsResult['stderr']),
'Scanning the directory with PHPCS failed.'
'`DummySubDir \(1 sniffs?\)\s+[-]+\s+DummySubDir\.Demo\.Demo(?:[\r\n]+|$)`',
$phpcsResult['stdout'],
'Output of the PHPCS explain command did not match the expectation.'
);
}

Expand Down

0 comments on commit b973ddf

Please sign in to comment.