* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\LanguageConstruct; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @internal * * @covers \PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer */ final class FunctionToConstantFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $config * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $config = []): void { $this->fixer->configure($config); $this->doTest($expected, $input); } public static function provideFixCases(): iterable { yield 'Minimal case, alternative casing, alternative statement end.' => [ '', '', ]; yield 'With embedded comment.' => [ '', '', ]; yield 'With white space.' => [ ' [ ' [ ' [ ' [ ' [ ' [ ' [ 'phpversion();', ]; yield 'Overridden function.' => [ '', ]; yield 'phpversion only' => [ ' ['phpversion']], ]; yield 'php_sapi_name only' => [ ' ['php_sapi_name']], ]; yield 'php_sapi_name in conditional' => [ ' ['php_sapi_name']], ]; yield 'pi only' => [ ' ['pi']], ]; yield 'multi line pi' => [ ' ['pi']], ]; yield 'phpversion and pi' => [ ' ['pi', 'phpversion']], ]; yield 'diff argument count than native allows' => [ ' T_CLASS' => [ ' [ ' ['get_called_class']], ]; yield [ ' ['get_called_class']], ]; yield 'get_called_class with leading backslash' => [ ' ['get_called_class']], ]; yield 'get_called_class overridden' => [ ' ['get_called_class']], ]; yield [ ' ['get_class_this']], ]; yield [ ' ['get_class_this']], ]; yield [ ' ['get_class_this']], ]; yield [ ' ['get_class_this', 'get_class']], ]; yield [ 'extension)); }} class Foo{ public function Bar(){ echo $reflection = new \ReflectionClass(get_class($this() )); }} ', null, ['functions' => ['get_class_this']], ]; yield [ "expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessage($expectedExceptionMessage); $this->fixer->configure($config); } public static function provideInvalidConfigurationKeysCases(): iterable { yield [ ['functions' => ['a']], '[function_to_constant] Invalid configuration: The option "functions" with value array is invalid.', ]; yield [ ['functions' => [false => 1]], '[function_to_constant] Invalid configuration: The option "functions" with value array is expected to be of type "string[]", but one of the elements is of type "int".', ]; yield [ ['functions' => ['abc' => true]], '[function_to_constant] Invalid configuration: The option "functions" with value array is expected to be of type "string[]", but one of the elements is of type "bool".', ]; } public function testInvalidConfigurationValue(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('#^\[function_to_constant\] Invalid configuration: The option "0" does not exist\. Defined options are: "functions"\.$#'); $this->fixer->configure(['pi123']); } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield 'first callable class' => [ '