* 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\FunctionNotation; use PhpCsFixer\ConfigurationException\InvalidConfigurationException; use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Andreas Möller * * @internal * * @covers \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer */ final class NativeFunctionInvocationFixerTest extends AbstractFixerTestCase { public function testConfigureRejectsUnknownConfigurationKey(): void { $key = 'foo'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(sprintf( '[native_function_invocation] Invalid configuration: The option "%s" does not exist.', $key )); $this->fixer->configure([ $key => 'bar', ]); } /** * @dataProvider provideInvalidConfigurationElementCases * * @param mixed $element */ public function testConfigureRejectsInvalidConfigurationElement($element): void { $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage(sprintf( 'Each element must be a non-empty, trimmed string, got "%s" instead.', \is_object($element) ? \get_class($element) : \gettype($element) )); $this->fixer->configure([ 'exclude' => [ $element, ], ]); } public function provideInvalidConfigurationElementCases(): array { return [ 'null' => [null], 'false' => [false], 'true' => [false], 'int' => [1], 'array' => [[]], 'float' => [0.1], 'object' => [new \stdClass()], 'not-trimmed' => [' is_string '], ]; } /** * @param string[] $include * * @dataProvider provideConfigureIncludeSetsCases */ public function testConfigureIncludeSets( array $include, ?string $expectedExceptionClass = null, ?string $expectedExceptionMessage = null ): void { if (null !== $expectedExceptionClass) { $this->expectException($expectedExceptionClass); $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($expectedExceptionMessage, '#'))); } $this->fixer->configure(['include' => $include]); if (null === $expectedExceptionClass) { $this->addToAssertionCount(1); } } public function provideConfigureIncludeSetsCases(): array { return [ [['foo', 'bar']], [[NativeFunctionInvocationFixer::SET_ALL]], [[NativeFunctionInvocationFixer::SET_ALL, 'bar']], [ ['@xxx'], InvalidFixerConfigurationException::class, '[native_function_invocation] Invalid configuration: Unknown set "@xxx", known sets are "@all", "@internal", "@compiler_optimized".', ], [ [' x '], InvalidFixerConfigurationException::class, '[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.', ], ]; } public function testConfigureResetsExclude(): void { $this->fixer->configure([ 'exclude' => [ 'is_string', ], ]); $before = <<<'PHP' 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 function provideFixWithDefaultConfigurationCases(): array { return [ [ ' [ 'fixer->configure([ 'exclude' => [ 'is_string', ], ]); $this->doTest($expected, $input); } public function provideFixWithConfiguredExcludeCases(): array { return [ [ 'fixer->configure(['scope' => $scope]); $this->doTest($expected, $input); } public function provideFixWithNamespaceConfigurationCases(): array { return [ [ ' ', ' ', ], [ 'fixer->configure($configuration); $this->doTest($expected, $input); } public function provideFixWithConfiguredIncludeCases(): \Generator { yield from [ 'include set + 1, exclude 1' => [ ' [NativeFunctionInvocationFixer::SET_INTERNAL, 'some_other'], 'exclude' => ['strlen'], ], ], 'include @all' => [ ' [NativeFunctionInvocationFixer::SET_ALL], ], ], 'include @compiler_optimized' => [ ' [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED], ], ], [ ' [ ' 'namespaced', 'strict' => true, ], ], [ ' [NativeFunctionInvocationFixer::SET_ALL], ], ], ]; if (\PHP_VERSION_ID < 80000) { yield 'include @compiler_optimized with strict enabled' => [ ' [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED], 'strict' => true, ], ]; } } /** * @requires PHP 7.3 */ public function testFix73(): void { $this->doTest( 'doTest( 'fixer->configure($config); $this->doTest($expected, $input); } public function provideFix80Cases(): \Generator { yield 'attribute and strict' => [ ' true], ]; yield 'null safe operator' => ['count();']; yield 'multiple function-calls-like in attribute' => [ ' ['@all']], ]; } }