* 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\ConstantNotation; use PhpCsFixer\ConfigurationException\InvalidConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Filippo Tessarotto * * @internal * * @covers \PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer */ final class NativeConstantInvocationFixerTest extends AbstractFixerTestCase { public function testConfigureRejectsUnknownConfigurationKey(): void { $key = 'foo'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(\sprintf('[native_constant_invocation] Invalid configuration: The option "%s" does not exist.', $key)); $this->fixer->configure([ $key => 'bar', ]); } /** * @dataProvider provideInvalidConfigurationElementCases * * @param mixed $element */ public function testConfigureRejectsInvalidExcludeConfigurationElement($element, string $expectedExceptionMessage): void { $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage($expectedExceptionMessage); $this->fixer->configure([ 'exclude' => [ $element, ], ]); } /** * @dataProvider provideInvalidConfigurationElementCases * * @param mixed $element */ public function testConfigureRejectsInvalidIncludeConfigurationElement($element, string $expectedExceptionMessage): void { $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage( str_replace('"exclude"', '"include"', $expectedExceptionMessage) ); $this->fixer->configure([ 'include' => [ $element, ], ]); } public static function provideInvalidConfigurationElementCases(): iterable { yield 'null' => [ null, '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "null".', ]; yield 'false' => [ false, '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".', ]; yield 'true' => [ true, '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".', ]; yield 'int' => [ 1, '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "int".', ]; yield 'array' => [ [], '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "array".', ]; yield 'float' => [ 0.1, '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "float".', ]; yield 'object' => [ new \stdClass(), '[native_constant_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "stdClass".', ]; yield 'not-trimmed' => [ ' M_PI ', '[native_constant_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.', ]; } public function testConfigureResetsExclude(): void { $this->fixer->configure([ 'exclude' => [ 'M_PI', ], ]); $before = 'doTest($before); $this->fixer->configure([]); $this->doTest($after, $before); } /** * @dataProvider provideFixWithDefaultConfigurationCases */ public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDefaultConfigurationCases(): iterable { yield ['M_PI(self::M_PI); } }']; yield ['fixer->configure([ 'include' => [ 'FOO_BAR_BAZ', ], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithConfiguredCustomIncludeCases(): iterable { yield [ 'fixer->configure([ 'fix_built_in' => false, 'include' => [ 'M_PI', ], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithConfiguredOnlyIncludeCases(): iterable { yield [ 'fixer->configure([ 'exclude' => [ 'M_PI', ], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithConfiguredExcludeCases(): iterable { yield [ 'fixer->configure([ 'fix_built_in' => false, 'include' => [ 'null', 'false', 'M_PI', 'M_pi', ], 'exclude' => [], ]); $expected = <<<'EOT' doTest($expected, $input); } public function testDoNotIncludeUserConstantsUnlessExplicitlyListed(): void { $uniqueConstantName = uniqid(self::class); $uniqueConstantName = preg_replace('/\W+/', '_', $uniqueConstantName); $uniqueConstantName = strtoupper($uniqueConstantName); $dontFixMe = 'DONTFIXME_'.$uniqueConstantName; $fixMe = 'FIXME_'.$uniqueConstantName; \define($dontFixMe, 1); \define($fixMe, 1); $this->fixer->configure([ 'fix_built_in' => true, 'include' => [ $fixMe, ], 'exclude' => [], ]); $expected = <<doTest($expected, $input); } public function testDoNotFixImportedConstants(): void { $this->fixer->configure([ 'fix_built_in' => false, 'include' => [ 'M_PI', 'M_EULER', ], 'exclude' => [], ]); $expected = <<<'EOT' doTest($expected, $input); } public function testFixScopedOnly(): void { $this->fixer->configure(['scope' => 'namespaced']); $expected = <<<'EOT' doTest($expected, $input); } public function testFixScopedOnlyNoNamespace(): void { $this->fixer->configure(['scope' => 'namespaced']); $expected = <<<'EOT' doTest($expected); } public function testFixStrictOption(): void { $this->fixer->configure(['strict' => true]); $this->doTest( 'doTest( 'fixer->configure(['strict' => true]); $this->doTest($expected); } /** * @return iterable */ public static function provideFixPhp80Cases(): iterable { yield [ 'fixer->configure(['strict' => true]); $this->doTest($expected); } /** * @return iterable */ public static function provideFixPhp81Cases(): iterable { yield [ 'fixer->configure(['strict' => true]); $this->doTest($expected); } /** * @return iterable */ public static function provideFixPhp82Cases(): iterable { yield ['fixer->configure(['strict' => true]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixPhp83Cases(): iterable { yield [ '