* 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\Comment; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\Tokenizer\Tokens; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Comment\HeaderCommentFixer */ final class HeaderCommentFixerTest extends AbstractFixerTestCase { /** * @param string $expected * @param string $input * * @dataProvider provideFixCases */ public function testFix(array $configuration, $expected, $input) { $this->fixer->configure($configuration); $this->doTest($expected, $input); } public function provideFixCases() { return [ [ ['header' => ''], ' 'tmp', 'location' => 'after_declare_strict', ], ' 'tmp', 'location' => 'after_declare_strict', 'separate' => 'bottom', 'commentType' => 'PHPDoc', ], ' 'tmp', 'location' => 'after_open', ], ' 'new', 'commentType' => 'comment', ], ' 'new', 'commentType' => 'PHPDoc', ], ' 'def', 'commentType' => 'PHPDoc', ], ' 'xyz'], ' 'xyz123', 'separate' => 'none', ], ' 'abc', 'commentType' => 'PHPDoc', ], ' 'ghi', 'separate' => 'both', ], ' 'ghi', 'separate' => 'top', ], ' 'tmp', 'location' => 'after_declare_strict', ], ' 'Foo'], ' 'x'], ' "a\na"], 'fixer->configure(['header' => 'a']); $this->doTest( 'expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class); $this->expectExceptionMessage('[header_comment] '.$exceptionMessage); $this->fixer->configure($configuration); } public function provideMisconfigurationCases() { return [ [[], 'Missing required configuration: The required option "header" is missing.'], [ ['header' => 1], 'Invalid configuration: The option "header" with value 1 is expected to be of type "string", but is of type "integer".', ], [ [ 'header' => '', 'commentType' => 'foo', ], 'Invalid configuration: The option "commentType" with value "foo" is invalid. Accepted values are: "PHPDoc", "comment".', ], [ [ 'header' => '', 'commentType' => new \stdClass(), ], 'Invalid configuration: The option "commentType" with value stdClass is invalid. Accepted values are: "PHPDoc", "comment".', ], [ [ 'header' => '', 'location' => new \stdClass(), ], 'Invalid configuration: The option "location" with value stdClass is invalid. Accepted values are: "after_open", "after_declare_strict".', ], [ [ 'header' => '', 'separate' => new \stdClass(), ], 'Invalid configuration: The option "separate" with value stdClass is invalid. Accepted values are: "both", "top", "bottom", "none".', ], ]; } /** * @param string $expected * @param string $header * @param string $type * * @dataProvider provideHeaderGenerationCases */ public function testHeaderGeneration($expected, $header, $type) { $this->fixer->configure([ 'header' => $header, 'commentType' => $type, ]); $this->doTest( 'fixer->configure($config); $method = new \ReflectionMethod($this->fixer, 'findHeaderCommentInsertionIndex'); $method->setAccessible(true); $this->assertSame($expected, $method->invoke($this->fixer, $tokens)); } public function provideFindHeaderCommentInsertionIndexCases() { $config = ['header' => '']; $cases = [ [1, '', $config, ]; return $cases; } /** * @param string $expected * * @dataProvider provideDoNotTouchCases */ public function testDoNotTouch($expected) { $this->fixer->configure([ 'header' => '', ]); $this->doTest($expected); } public function provideDoNotTouchCases() { return [ ["\n
"], [" '], ['\nexpectException(\PhpCsFixer\ConfigurationException\RequiredFixerConfigurationException::class); $this->doTest('fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->fixer->configure($configuration); $this->doTest($expected, $input); } public function provideMessyWhitespacesCases() { return [ [ [ 'header' => 'whitemess', 'location' => 'after_declare_strict', 'separate' => 'bottom', 'commentType' => 'PHPDoc', ], "fixer->configure(['header' => 'Foo']); $this->doTest( "fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n")); $this->doTest( "fixer->configure(['header' => 'Bar']); $this->doTest( "fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\n")); $this->doTest( "