* 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 */ 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): void { $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(sprintf( 'Each element must be a non-empty, trimmed string, got "%s" instead.', get_debug_type($element) )); $this->fixer->configure([ 'exclude' => [ $element, ], ]); } /** * @dataProvider provideInvalidConfigurationElementCases * * @param mixed $element */ public function testConfigureRejectsInvalidIncludeConfigurationElement($element): void { $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(sprintf( 'Each element must be a non-empty, trimmed string, got "%s" instead.', get_debug_type($element) )); $this->fixer->configure([ 'include' => [ $element, ], ]); } public static function provideInvalidConfigurationElementCases(): iterable { yield 'null' => [null]; yield 'false' => [false]; yield 'true' => [true]; yield 'int' => [1]; yield 'array' => [[]]; yield 'float' => [0.1]; yield 'object' => [new \stdClass()]; yield 'not-trimmed' => [' M_PI ']; } 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); } public static function provideFixWithDefaultConfigurationCases(): iterable { yield ['M_PI(self::M_PI); } }']; yield ['fixer->configure([ 'include' => [ 'FOO_BAR_BAZ', ], ]); $this->doTest($expected, $input); } public static function provideFixWithConfiguredCustomIncludeCases(): iterable { yield [ 'fixer->configure([ 'fix_built_in' => false, 'include' => [ 'M_PI', ], ]); $this->doTest($expected, $input); } public static function provideFixWithConfiguredOnlyIncludeCases(): iterable { yield [ 'fixer->configure([ 'exclude' => [ 'M_PI', ], ]); $this->doTest($expected, $input); } 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); } public static function provideFixPhp80Cases(): iterable { yield [ '