* 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\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Andreas Möller * @author SpacePossum * * @internal * * @covers \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer */ final class NativeFunctionInvocationFixerTest extends AbstractFixerTestCase { public function testConfigureRejectsUnknownConfigurationKey() { $key = 'foo'; $this->expectException(\PhpCsFixer\ConfigurationException\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) { $this->expectException(\PhpCsFixer\ConfigurationException\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, ], ]); } /** * @return array */ public function provideInvalidConfigurationElementCases() { return [ 'null' => [null], 'false' => [false], 'true' => [false], 'int' => [1], 'array' => [[]], 'float' => [0.1], 'object' => [new \stdClass()], 'not-trimmed' => [' json_encode '], ]; } /** * @param string[] $include * @param null|string $expectedExceptionClass * @param null|string $expectedExceptionMessage * * @dataProvider provideConfigureIncludeSetsCases */ public function testConfigureIncludeSets( array $include, $expectedExceptionClass = null, $expectedExceptionMessage = null ) { 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() { return [ [['foo', 'bar']], [['@all']], [['@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() { $this->fixer->configure([ 'exclude' => [ 'json_encode', ], ]); $before = <<<'PHP' doTest($before); $this->fixer->configure([]); $this->doTest($after, $before); } /** * @dataProvider provideFixWithDefaultConfigurationCases * * @param string $expected * @param null|string $input */ public function testFixWithDefaultConfiguration($expected, $input = null) { $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithDefaultConfigurationCases() { return [ [ 'fixer->configure([ 'exclude' => [ 'json_encode', ], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithConfiguredExcludeCases() { return [ [ 'fixer->configure(['scope' => $scope]); $this->doTest($expected, $input); } public function provideFixWithNamespaceConfigurationCases() { return [ [ ' ', ' ', ], [ 'fixer->configure($configuration); $this->doTest($expected, $input); } public function provideFixWithConfiguredIncludeCases() { $tests = [ 'include set + 1, exclude 1' => [ ' ['@internal', 'some_other'], 'exclude' => ['strlen'], ], ], 'include @all' => [ ' ['@all'], ], ], 'include @compiler_optimized' => [ ' ['@compiler_optimized'], ], ], [ ' [ ' 'namespaced', 'strict' => true, ], ], [ ' ['@all'], ], ], ]; foreach ($tests as $index => $test) { yield $index => $test; } if (\PHP_VERSION_ID < 80000) { yield 'include @compiler_optimized with strict enabled' => [ ' ['@compiler_optimized'], 'strict' => true, ], ]; } } /** * @requires PHP 7.3 */ public function testFix73() { $this->doTest( 'doTest( '