* 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\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Sullivan Senechal * * @internal * * @covers \PhpCsFixer\Fixer\Alias\NoMixedEchoPrintFixer */ final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixEchoToPrintCases */ public function testFixEchoToPrint(string $expected, ?string $input = null): void { $this->fixer->configure(['use' => 'print']); $this->doTest($expected, $input); } public static function provideFixEchoToPrintCases(): iterable { yield [ '...", "...", ]; yield [ '', ]; foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) { yield [ sprintf($codeSnippet, 'print'), sprintf($codeSnippet, 'echo'), ]; } } /** * @dataProvider provideFixPrintToEchoCases */ public function testFixPrintToEcho(string $expected, ?string $input = null): void { $this->fixer->configure(['use' => 'echo']); $this->doTest($expected, $input); } public static function provideFixPrintToEchoCases(): iterable { yield [ ' 0 ? \'a\' : \'b\'; switch(print(\'a\')) {} if (1 === print($a)) {} ', ]; yield [ 'fixer->configure([]); self::assertCandidateTokenType(T_PRINT, $this->fixer); } /** * @param array $wrongConfig * * @dataProvider provideWrongConfigCases */ public function testWrongConfig(array $wrongConfig, string $expectedMessage): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches($expectedMessage); $this->fixer->configure($wrongConfig); } public static function provideWrongConfigCases(): iterable { yield [ ['a' => 'b'], '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', ]; yield [ ['a' => 'b', 'b' => 'c'], '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', ]; yield [ [1], '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', ]; yield [ ['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(int $expected, AbstractFixer $fixer): void { $reflectionProperty = new \ReflectionProperty($fixer, 'candidateTokenType'); $reflectionProperty->setAccessible(true); self::assertSame($expected, $reflectionProperty->getValue($fixer)); } /** * @return iterable */ private static function getCodeSnippetsToConvertBothWays(): iterable { yield 'inside of HTML' => '
'; yield 'foreach without curly brackets' => ' '