Skip to content

Commit

Permalink
Merge 2ffa09b into f29c62f
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 14, 2020
2 parents f29c62f + 2ffa09b commit 97f6bc2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
27 changes: 21 additions & 6 deletions PHPCSUtils/BackCompat/Helper.php
Expand Up @@ -11,6 +11,7 @@
namespace PHPCSUtils\BackCompat;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Exceptions\RuntimeException;

/**
* Utility methods to retrieve (configuration) information from PHP_CodeSniffer.
Expand Down Expand Up @@ -60,21 +61,35 @@ public static function getVersion()
*
* @since 1.0.0
*
* @param string $key The name of the config value.
* @param string|null $value The value to set. If null, the config entry
* is deleted, reverting it to the default value.
* @param bool $temp Set this config data temporarily for this script run.
* This will not write the config data to the config file.
* @param string $key The name of the config value.
* @param string|null $value The value to set. If null, the config entry
* is deleted, reverting it to the default value.
* @param bool $temp Set this config data temporarily for this script run.
* This will not write the config data to the config file.
* @param \PHP_CodeSniffer\Config $config The PHPCS config object.
* This parameter is required for PHPCS 4.x, optional
* for PHPCS 3.x and not possible to pass for PHPCS 2.x.
* Passing the `$phpcsFile->config` property should work
* in PHPCS 3.x and higher.
*
* @return bool Whether the setting of the data was successfull.
*/
public static function setConfigData($key, $value, $temp = false)
public static function setConfigData($key, $value, $temp = false, $config = null)
{
if (\method_exists('\PHP_CodeSniffer\Config', 'setConfigData') === false) {
// PHPCS 2.x.
return \PHP_CodeSniffer::setConfigData($key, $value, $temp);
}

if (isset($config) === true) {
// PHPCS 3.x and 4.x.
return $config->setConfigData($key, $value, $temp);
}

if (version_compare(self::getVersion(), '3.99.99', '>') === true) {
throw new RuntimeException('Passing the $config parameter is required in PHPCS 4.x');
}

// PHPCS 3.x.
return \PHP_CodeSniffer\Config::setConfigData($key, $value, $temp);
}
Expand Down
62 changes: 59 additions & 3 deletions Tests/BackCompat/Helper/ConfigDataTest.php
Expand Up @@ -10,6 +10,7 @@

namespace PHPCSUtils\Tests\BackCompat\Helper;

use PHP_CodeSniffer\Config;
use PHPCSUtils\BackCompat\Helper;
use PHPUnit\Framework\TestCase;

Expand All @@ -27,12 +28,41 @@ class ConfigDataTest extends TestCase
{

/**
* Test the getConfigData() and setConfigData() method.
* Test the getConfigData() and setConfigData() method when used in a cross-version compatible manner.
*
* @return void
*/
public function testConfigData()
public function testConfigData34()
{
if (version_compare(Helper::getVersion(), '2.99.99', '<=') === true) {
$this->markTestSkipped('Test only applicable to PHPCS > 2.x');
}

$config = new Config();
$original = Helper::getConfigData('arbitrary_name');
$expected = 'expected';

$return = Helper::setConfigData('arbitrary_name', $expected, true, $config);
$this->assertTrue($return);

$result = Helper::getConfigData('arbitrary_name');
$this->assertSame($expected, $result);

// Reset the value after the test.
Helper::setConfigData('arbitrary_name', $original, true, $config);
}

/**
* Test the getConfigData() and setConfigData() method when used in a non-PHPCS 4.x compatible manner.
*
* @return void
*/
public function testConfigDataPHPCS23()
{
if (version_compare(Helper::getVersion(), '3.99.99', '>') === true) {
$this->markTestSkipped('Test only applicable to PHPCS < 4.x');
}

$original = Helper::getConfigData('arbitrary_name');
$expected = 'expected';

Expand All @@ -43,6 +73,32 @@ public function testConfigData()
$this->assertSame($expected, $result);

// Reset the value after the test.
$return = Helper::setConfigData('arbitrary_name', $original, true);
Helper::setConfigData('arbitrary_name', $original, true);
}

/**
* Test the getConfigData() and setConfigData() method when used in a non-PHPCS 4.x compatible manner.
*
* @return void
*/
public function testConfigDataPHPCS4Exception()
{
if (version_compare(Helper::getVersion(), '3.99.99', '<=') === true) {
$this->markTestSkipped('Test only applicable to PHPCS 4.x');
}

$msg = 'Passing the $config parameter is required in PHPCS 4.x';
$exception = 'PHP_CodeSniffer\Exceptions\RuntimeException';

if (\method_exists($this, 'expectException')) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($msg);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $msg);
}

Helper::setConfigData('arbitrary_name', 'test', true);
}
}
20 changes: 15 additions & 5 deletions Tests/BackCompat/Helper/GetCommandLineDataTest.php
Expand Up @@ -132,16 +132,21 @@ public function testGetEncodingWithoutPHPCSFile()
$expected = \version_compare(static::$phpcsVersion, '2.99.99', '>') ? 'utf-8' : 'iso-8859-1';
$this->assertSame($expected, $result, 'Failed retrieving the default encoding');

Helper::setConfigData('encoding', 'utf-16', true);
$config = null;
if (isset(self::$phpcsFile->config) === true) {
$config = self::$phpcsFile->config;
}

Helper::setConfigData('encoding', 'utf-16', true, $config);

$result = Helper::getEncoding();
$this->assertSame('utf-16', $result, 'Failed retrieving the custom set encoding');

// Restore defaults before moving to the next test.
if (\version_compare(static::$phpcsVersion, '2.99.99', '>') === true) {
Helper::setConfigData('encoding', 'utf-8', true);
Helper::setConfigData('encoding', 'utf-8', true, $config);
} else {
Helper::setConfigData('encoding', 'iso-8859-1', true);
Helper::setConfigData('encoding', 'iso-8859-1', true, $config);
}
}

Expand Down Expand Up @@ -197,13 +202,18 @@ public function testIgnoreAnnotationsV3SetViaMethod()
$this->markTestSkipped('Test only applicable to PHPCS 3.x');
}

Helper::setConfigData('annotations', false, true);
$config = null;
if (isset(self::$phpcsFile->config) === true) {
$config = self::$phpcsFile->config;
}

Helper::setConfigData('annotations', false, true, $config);

$result = Helper::ignoreAnnotations();
$this->assertTrue($result);

// Restore defaults before moving to the next test.
Helper::setConfigData('annotations', true, true);
Helper::setConfigData('annotations', true, true, $config);
}

/**
Expand Down

0 comments on commit 97f6bc2

Please sign in to comment.