diff --git a/PHPCSUtils/BackCompat/Helper.php b/PHPCSUtils/BackCompat/Helper.php index 7a122105..aeb0cf5a 100644 --- a/PHPCSUtils/BackCompat/Helper.php +++ b/PHPCSUtils/BackCompat/Helper.php @@ -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. @@ -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); } diff --git a/Tests/BackCompat/Helper/ConfigDataTest.php b/Tests/BackCompat/Helper/ConfigDataTest.php index f570e0f2..9cd2a74e 100644 --- a/Tests/BackCompat/Helper/ConfigDataTest.php +++ b/Tests/BackCompat/Helper/ConfigDataTest.php @@ -10,6 +10,7 @@ namespace PHPCSUtils\Tests\BackCompat\Helper; +use PHP_CodeSniffer\Config; use PHPCSUtils\BackCompat\Helper; use PHPUnit\Framework\TestCase; @@ -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'; @@ -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); } } diff --git a/Tests/BackCompat/Helper/GetCommandLineDataTest.php b/Tests/BackCompat/Helper/GetCommandLineDataTest.php index 7ce9c868..9c143091 100644 --- a/Tests/BackCompat/Helper/GetCommandLineDataTest.php +++ b/Tests/BackCompat/Helper/GetCommandLineDataTest.php @@ -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); } } @@ -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); } /**