* 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\ClassNotation; use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer */ final class ClassAttributesSeparationFixerTest extends AbstractFixerTestCase { /** * @param int $expected * @param string $code * @param int $index * * @dataProvider provideCommentBlockStartDetectionCases */ public function testCommentBlockStartDetection($expected, $code, $index) { Tokens::clearCache(); $tokens = Tokens::fromCode($code); $method = new \ReflectionMethod($this->fixer, 'findCommentBlockStart'); $method->setAccessible(true); $result = $method->invoke($this->fixer, $tokens, $index); static::assertSame( $expected, $result, sprintf('Expected index %d (%s) got index %d (%s).', $expected, $tokens[$expected]->toJson(), $result, $tokens[$result]->toJson()) ); } public function provideCommentBlockStartDetectionCases() { return [ [ 4, 'doTest($expected, $input); } public function provideFixClassesCases() { $cases = []; $cases[] = ['filter( function (\SplFileInfo $file) use ($files) { return !in_array($file->getRelativePathname(), $files, true); } ); } } private $a; public static function method145() { } abstract protected function method245(); // comment final private function method345() { } } function some1(){ echo 1;} function some2(){ echo 2;}', 'filter( function (\SplFileInfo $file) use ($files) { return !in_array($file->getRelativePathname(), $files, true); } ); } } private $a; public static function method145() { } abstract protected function method245(); // comment final private function method345() { } } function some1(){ echo 1;} function some2(){ echo 2;}', ]; $cases[] = [ ' * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Linter; /** * Dummy linter. No linting is performed. No error is raised. * * @author Dariusz Rumiński * * @internal */ final class NullLinter implements LinterInterface { /** * {@inheritdoc} */ public function lintFile($path) { unset($path); } /** * {@inheritdoc} */ public function lintSource($source) { unset($source); } } ', ]; // do not touch anonymous functions (since PHP doesn't allow // for class attributes being functions :(, we only have to test // those used within methods) $cases[] = [ 'doTest($expected, $input); } public function provideFixTraitsCases() { $cases = []; // do not touch well formatted traits $cases[] = [ 'doTest($expected, $input); } public function provideFixInterfaceCases() { $cases = []; $cases[] = [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } public function provideMessyWhitespacesCases() { return [ [ "fixer->configure($config); $this->doTest($expected, $input); } public function provideConfigCases() { return [ [ ' ['property' => ClassAttributesSeparationFixer::SPACING_ONE]], ], [ ' ['property' => ClassAttributesSeparationFixer::SPACING_NONE]], ], [ ' ['const' => ClassAttributesSeparationFixer::SPACING_ONE]], ], [ ' ['const' => ClassAttributesSeparationFixer::SPACING_NONE]], ], [ ' ['method' => ClassAttributesSeparationFixer::SPACING_ONE]], ], [ ' ['method' => ClassAttributesSeparationFixer::SPACING_NONE]], ], ]; } /** * @param string $expected * @param null|string $input * * @dataProvider provideDeprecatedConfigCases * @group legacy * @expectedDeprecation A list of elements is deprecated, use a dictionary of `const|method|property` => `none|one` instead. */ public function testWithDeprecatedConfig($expected, $input = null, array $config = []) { $this->fixer->configure($config); $this->doTest($expected, $input); } public function provideDeprecatedConfigCases() { return [ [ ' ['property']], ], [ ' ['method']], ], [ ' ['const']], ], ]; } /** * @param string $expected * @param null|string $input * * @dataProvider provideFix70Cases * @requires PHP 7.0 */ public function testFix70($expected, $input = null) { $this->doTest($expected, $input); } public function provideFix70Cases() { $to = $from = 'fixer->configure([ 'elements' => ['method' => ClassAttributesSeparationFixer::SPACING_ONE, 'const' => ClassAttributesSeparationFixer::SPACING_ONE], ]); $this->doTest($expected, $input); } public function provideFix71Cases() { return [ [ 'doTest($expected, $input); } public function provideFix74Cases() { yield [ 'doTest($expected, $input); } public function provideFixPhp80Cases() { yield 'attributes' => [ ' "The email {{ value }} is not a valid email."])] private $email; #[Assert\String()] private $name; }', ' "The email {{ value }} is not a valid email."])] private $email; #[Assert\String()] private $name; }', ]; yield 'attributes minimal' => [ ' [ ' "Foo"])] private $email; }', ' "Foo"])] private $email; }', ]; yield 'constructor property promotion' => [ ' [ '