* 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\Fixer\LanguageConstruct\IsNullFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Vladimir Reznichenko * * @internal * * @covers \PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer */ final class IsNullFixerTest extends AbstractFixerTestCase { public function testConfigurationWrongOption() { $fixer = new IsNullFixer(); $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class); $this->expectExceptionMessage('[is_null] Invalid configuration: The option "yoda" does not exist.'); $fixer->configure(['yoda' => true]); } /** * @group legacy * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead. */ public function testConfigurationWrongValue() { $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('#^\[is_null\] Invalid configuration: The option "use_yoda_style" with value -1 is expected to be of type "bool", but is of type "(int|integer)"\.$#'); $this->fixer->configure(['use_yoda_style' => -1]); } /** * @group legacy * @expectedDeprecation Option "use_yoda_style" for rule "is_null" is deprecated and will be removed in version 3.0. Use "yoda_style" fixer instead. */ public function testCorrectConfiguration() { $this->fixer->configure(['use_yoda_style' => false]); $reflectionProperty = new \ReflectionProperty($this->fixer, 'configuration'); $reflectionProperty->setAccessible(true); $configuration = $reflectionProperty->getValue($this->fixer); static::assertFalse($configuration['use_yoda_style']); } /** * @dataProvider provideFixCases * * @param string $expected * @param null|string $input */ public function testFix($expected, $input = null) { $this->doTest($expected, $input); } public function provideFixCases() { $multiLinePatternToFix = <<<'FIX' is_null(json_decode($x));'], ['', "", ], [ '', ], // edge cases: is_null wrapped into a binary operations [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ ' (null === $a)); ?>', ' is_null($a)); ?>', ], [ ' false); ?>', ' false); ?>', ], [ '', '', ], // check with logical operator [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], [ '', '', ], // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses [ 'fixer->configure(['use_yoda_style' => false]); $this->doTest($expected, $input); } public function provideNonYodaFixCases() { return [ [ 'getNextMaxId()) === null);', 'getNextMaxId()));', ], ]; } /** * @requires PHP 7.0 */ public function testFix70() { $this->doTest( 'doTest($expected, $input); } public function provideFix73Cases() { return [ [ '', '', ], // edge cases: $isContainingDangerousConstructs, $wrapIntoParentheses [ '