* 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\ControlStructure; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Dariusz Rumiński * @author SpacePossum * * @internal * * @covers \PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer */ final class YodaStyleFixerTest extends AbstractFixerTestCase { /** * @param string $expected * @param null|string $input * * @dataProvider provideFixCases */ public function testFix($expected, $input = null, array $extraConfig = []) { $this->fixer->configure(['equal' => true, 'identical' => true] + $extraConfig); $this->doTest($expected, $input); } /** * Test with the inverse config. * * @param string $expected * @param null|string $input * * @dataProvider provideFixCases */ public function testFixInverse($expected, $input = null, array $extraConfig = []) { $this->fixer->configure(['equal' => false, 'identical' => false] + $extraConfig); if (null === $input) { $this->doTest($expected); } else { $this->doTest($input, $expected); } } public function provideFixCases() { $tests = [ [ ' true], ], [ ' true], ], [ ' false], ], [ ' true], ], [ 'array[$var]) === $a;'], ['array[$var]);'], ['getStuff() === $myVariable;'], ['getStuff();'], [' 2;'], ['myObject1->{$index}+$b === "";'], ['$a === $foo->$b->$c;'], [' [ ' [ '1&&$c<=10;', '1&&$c<=10;', ], 'Comments.' => [ '', ], [ '', ], [ '', '', ['always_move_variable' => true], ], [ 'myArray[$index];', 'myArray[$index] === "";', ], [ 'myArray[$index]->/*1*//*2*//*3*/a;', 'myArray[$index]->/*1*//*2*//*3*/a === "";', ], [ 'myArray[$index]->a;', 'myArray[$index]->a === "";', ], [ 'myObject2-> {$index};', 'myObject2-> {$index} === "";', ], [ 'myObject3->{$index}->a;', 'myObject3->{$index}->a === "";', ], [ 'myObject4->{$index}->{$index}->a;', 'myObject4->{$index}->{$index}->a === "";', ], [ 'myObject4->$index->a;', 'myObject4->$index->a === "";', ], [ '', '', ], 'Nested case' => [ '{null === $a ? "a" : "b"};', '{$a === null ? "a" : "b"} === null;', ], 'Complex code sample.' => [ 'b) : 0 === $b->a; } else { if ($c === (null === $b)) { return false === $d; } }', 'b === 0) : $b->a === 0; } else { if ($c === ($b === null)) { return $d === false; } }', ], [ 'getStuff();', 'getStuff() === 2;', ], [ 'myObject5->{$index}->/*1*//*2*/b;', 'myObject5->{$index}->/*1*//*2*/b === "";', ], [ ' $value) { false !== uniqid() ? 1 : 2; } false !== uniqid() ? 1 : 2; }', ' $value) { uniqid() !== false ? 1 : 2; } uniqid() !== false ? 1 : 2; }', ], [ 'array[$var]) === $a;', 'array[$var]);', ['always_move_variable' => true], ], [ 'getStuff() === $myVariable;', 'getStuff();', ['always_move_variable' => true], ], [ 'array[$var]) === $a;', 'array[$var]);', ['always_move_variable' => true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' 2 === $k;', ' 2;', ['always_move_variable' => true], ], [ ' true], ], [ '= 2 === $k;', '= 2;', ['always_move_variable' => true], ], [ ' true], ], [ ' true], ], [ '> 2 === $k;', '> 2;', ['always_move_variable' => true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' true], ], [ ' $test) { yield $index => $test; } $template = ' true], ]; } $templateExpected = '>=', '&=', '.=', '/=', '-=', '||', '&&']; foreach ($operators as $operator) { yield [ sprintf($templateExpected, $operator), sprintf($templateInput, $operator), ]; } } /** * @param string $expected * @param string $input * * @dataProvider provideLessGreaterCases */ public function testFixLessGreater($expected, $input) { $this->fixer->configure(['less_and_greater' => true]); $this->doTest($expected, $input); } /** * Test with the inverse config. * * @param string $expected * @param string $input * * @dataProvider provideLessGreaterCases */ public function testFixLessGreaterInverse($expected, $input) { $this->fixer->configure(['less_and_greater' => false]); $this->doTest($input, $expected); } public function provideLessGreaterCases() { return [ [ '= 3;', ], [ ' $b;', ' $b) || $d;', 'fixer->configure([ 'equal' => null, 'identical' => true, 'less_and_greater' => false, ]); $this->doTest( ' 3; ', ' 3; ' ); } /** * @param string $expectedMessage * * @dataProvider provideInvalidConfigurationCases */ public function testInvalidConfig(array $config, $expectedMessage) { $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches("#^\\[{$this->fixer->getName()}\\] {$expectedMessage}$#"); $this->fixer->configure($config); } /** * @return array */ public function provideInvalidConfigurationCases() { return [ [['equal' => 2], 'Invalid configuration: The option "equal" with value 2 is expected to be of type "bool" or "null", but is of type "(int|integer)"\.'], [['_invalid_' => true], 'Invalid configuration: The option "_invalid_" does not exist\. Defined options are: "always_move_variable", "equal", "identical", "less_and_greater"\.'], ]; } public function testDefinition() { static::assertInstanceOf(\PhpCsFixer\FixerDefinition\FixerDefinitionInterface::class, $this->fixer->getDefinition()); } /** * @param string $expected * @param null|string $input * * @dataProvider providePHP70Cases * @requires PHP 7.0 */ public function testPHP70Cases($expected, $input = null) { $this->fixer->configure(['equal' => true, 'identical' => true]); $this->doTest($expected, $input); } /** * Test with the inverse config. * * @param string $expected * @param null|string $input * * @dataProvider providePHP70Cases * @requires PHP 7.0 */ public function testPHP70CasesInverse($expected, $input = null) { $this->fixer->configure(['equal' => false, 'identical' => false]); if (null === $input) { $this->doTest($expected); } else { $this->doTest($input, $expected); } } public function providePHP70Cases() { return [ [' $d;'], [ 'fixer->configure(['equal' => true, 'identical' => true]); $this->doTest($expected, $input); } /** * Test with the inverse config. * * @param string $expected * @param null|string $input * * @dataProvider providePHP71Cases * @requires PHP 7.1 */ public function testPHP71CasesInverse($expected, $input = null) { $this->fixer->configure(['equal' => false, 'identical' => false]); if (null === $input) { $this->doTest($expected); } else { $this->doTest($input, $expected); } } public function providePHP71Cases() { return [ // no fix cases [' $a, "b" => $b, "c" => $c) = $c === array(1) ? $b : $d;'], [' $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points;'], [' list($x1, $y1), "second" => list($x2, $y2)) = $points;'], [' $a, "b" => $b, "c" => $c] = $a[0];'], [' $a, "b" => $b, "c" => $c) = $c === array(1) ? $b : $d;'], [' $a, "b" => $b, "c" => $c) = 1 === $c ? $b : $d;', ' $a, "b" => $b, "c" => $c) = $c === 1 ? $b : $d;', ], [ ' $a, "b" => $b, "c" => $c) = A::B === $c ? $b : $d;', ' $a, "b" => $b, "c" => $c) = $c === A::B ? $b : $d;', ], [ ' $b) = ["a" => 7 === $c ? 5 : 1, "b" => 7];', ' $b) = ["a" => $c === 7 ? 5 : 1, "b" => 7];', ], [ ' $b] = ["a" => 7 === $c ? 5 : 1, "b" => 7];', ' $b] = ["a" => $c === 7 ? 5 : 1, "b" => 7];', ], 'Array destruct by ternary.' => [ 'fixer->configure($config); $this->doTest($expected); } public function provideFixWithConfigCases() { return [ [ [ 'identical' => false, ], ' false, 'identical' => false, ], 'event == \'created\') { foreach ($revision->getModified() as $col => $data) { $model->$col = $data[\'new\']; } } else { foreach ($revision->getModified() as $col => $data) { $model->$col = $data[\'old\']; } }', ], ]; } /** * @param string $expected * @param null|string $input * * @dataProvider provideFixPhp74Cases * @requires PHP 7.4 */ public function testFixPhp74($expected, $input) { $this->doTest($expected, $input); } public function provideFixPhp74Cases() { yield [ 'fixer->configure($configuration); } $this->doTest($expected, $input); } public function providePHP74Cases() { return [ [ ' $c === array(1) ? $b : $d;', null, [ 'less_and_greater' => false, ], ], [ 'doTest($expected, $input); } public function provideFixPrePHP80Cases() { yield [ '$a[1] === $bar[$baz]{1}->$a[1][2][3]->$d[$z]{1};']; yield ['a{2}+1 == 2;']; yield ['doTest($expected, $input); } public function provideFix80Cases() { yield [ '