* 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\Alias; use PhpCsFixer\AbstractFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Sullivan Senechal * @author SpacePossum * * @internal * * @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer */ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase { /** * @param string $expected * @param null|string $input * * @dataProvider provideEchoToPrintFixCases * @dataProvider provideEchoToPrintFixNewCases */ public function testFixEchoToPrint($expected, $input = null) { $this->fixer->configure(['use' => 'print']); $this->doTest($expected, $input); } public function provideEchoToPrintFixCases() { return [ [ '...", "...", ], [ '', ], ]; } public static function provideEchoToPrintFixNewCases() { foreach (self::getCodeSnippetsToConvertBothWays() as $name => $codeSnippet) { yield [ sprintf($codeSnippet, 'print'), sprintf($codeSnippet, 'echo'), ]; } } /** * @param string $expected * @param null|string $input * * @dataProvider providePrintToEchoFixCases * @dataProvider providePrintToEchoFixNewCases */ public function testFixPrintToEcho($expected, $input = null) { $this->fixer->configure(['use' => 'echo']); $this->doTest($expected, $input); } public function providePrintToEchoFixCases() { return [ [ ' 0 ? \'a\' : \'b\'; switch(print(\'a\')) {} if (1 === print($a)) {} ', ], [ ' $codeSnippet) { yield [ sprintf($codeSnippet, 'echo'), sprintf($codeSnippet, 'print'), ]; } } /** * @group legacy */ public function testLegacyDefaultConfig() { $this->expectDeprecation('Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.'); $this->fixer->configure(null); static::assertCandidateTokenType(T_PRINT, $this->fixer); } public function testDefaultConfig() { $this->fixer->configure([]); static::assertCandidateTokenType(T_PRINT, $this->fixer); } /** * @dataProvider provideWrongConfigCases * * @param mixed $wrongConfig * @param string $expectedMessage */ public function testWrongConfig($wrongConfig, $expectedMessage) { $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($expectedMessage); $this->fixer->configure($wrongConfig); } public function provideWrongConfigCases() { return [ [ ['a' => 'b'], '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', ], [ ['a' => 'b', 'b' => 'c'], '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', ], [ [1], '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', ], [ ['use' => '_invalid_'], '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#', ], ]; } private static function assertCandidateTokenType($expected, AbstractFixer $fixer) { $reflectionProperty = new \ReflectionProperty($fixer, 'candidateTokenType'); $reflectionProperty->setAccessible(true); static::assertSame($expected, $reflectionProperty->getValue($fixer)); } private static function getCodeSnippetsToConvertBothWays() { yield 'inside of HTML' => '
'; yield 'foreach without curly brackets' => ' '