Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper::setConfigData(): PHPCS 4.x compatibility #137

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions PHPCSUtils/BackCompat/Helper.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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