2018-04-17 23:58:51 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Ely\CS\Test\Fixer\Whitespace;
|
|
|
|
|
|
|
|
use Ely\CS\Fixer\Whitespace\BlankLineAroundClassBodyFixer;
|
2022-09-20 05:07:57 +05:30
|
|
|
use PhpCsFixer\AbstractFixer;
|
2018-04-17 23:58:51 +05:30
|
|
|
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
|
|
|
|
use PhpCsFixer\WhitespacesFixerConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author ErickSkrauch <erickskrauch@ely.by>
|
|
|
|
*
|
|
|
|
* @covers \Ely\CS\Fixer\Whitespace\BlankLineAroundClassBodyFixer
|
|
|
|
*/
|
|
|
|
final class BlankLineAroundClassBodyFixerTest extends AbstractFixerTestCase {
|
|
|
|
|
|
|
|
private static $configurationDoNotApplyForAnonymousClasses = ['apply_to_anonymous_classes' => false];
|
|
|
|
|
|
|
|
private static $configurationTwoEmptyLines = ['blank_lines_count' => 2];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFixCases
|
|
|
|
*/
|
2022-09-20 05:07:57 +05:30
|
|
|
public function testFix(string $expected, ?string $input = null, array $configuration = null): void {
|
|
|
|
if ($configuration !== null) {
|
2018-04-17 23:58:51 +05:30
|
|
|
$this->fixer->configure($configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doTest($expected, $input);
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:07:57 +05:30
|
|
|
public function provideFixCases(): iterable {
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Also blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Too many whitespaces
|
|
|
|
*/
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Too many whitespaces
|
|
|
|
*/
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}',
|
|
|
|
];
|
|
|
|
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
interface Good
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod();
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
interface Good
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Also blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod();
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
trait Good
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also no blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod() {}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
trait Good
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Also no blank line before DocBlock
|
|
|
|
*/
|
|
|
|
public function firstMethod() {}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo\bar;
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo\bar;
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo\bar;
|
|
|
|
use Foo\baz;
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo\bar;
|
|
|
|
use Foo\baz;
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo, Bar {
|
|
|
|
Bar::smallTalk insteadof A;
|
|
|
|
Foo::bigTalk insteadof B;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
use Foo, Bar {
|
|
|
|
Bar::smallTalk insteadof A;
|
|
|
|
Foo::bigTalk insteadof B;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}',
|
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{
|
|
|
|
public function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
}',
|
|
|
|
self::$configurationTwoEmptyLines,
|
|
|
|
];
|
|
|
|
|
|
|
|
// check if some fancy whitespaces aren't modified
|
2022-09-20 05:07:57 +05:30
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
class Good
|
|
|
|
{public
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function firstMethod()
|
|
|
|
{
|
|
|
|
//code here
|
|
|
|
}
|
|
|
|
|
|
|
|
}',
|
|
|
|
];
|
|
|
|
|
2022-09-20 05:07:57 +05:30
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
$class = new class extends \Foo {
|
|
|
|
|
|
|
|
public $field;
|
|
|
|
|
|
|
|
public function firstMethod() {}
|
|
|
|
|
|
|
|
};',
|
|
|
|
'<?php
|
|
|
|
$class = new class extends \Foo {
|
|
|
|
public $field;
|
|
|
|
|
|
|
|
public function firstMethod() {}
|
|
|
|
};',
|
|
|
|
];
|
2022-09-20 05:07:57 +05:30
|
|
|
|
|
|
|
yield [
|
2018-04-17 23:58:51 +05:30
|
|
|
'<?php
|
|
|
|
$class = new class extends \Foo {
|
|
|
|
public $field;
|
|
|
|
|
|
|
|
public function firstMethod() {}
|
|
|
|
};',
|
|
|
|
'<?php
|
|
|
|
$class = new class extends \Foo {
|
|
|
|
|
|
|
|
public $field;
|
|
|
|
|
|
|
|
public function firstMethod() {}
|
|
|
|
|
|
|
|
};',
|
|
|
|
self::$configurationDoNotApplyForAnonymousClasses,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideMessyWhitespacesCases
|
|
|
|
*/
|
2022-09-20 05:07:57 +05:30
|
|
|
public function testMessyWhitespaces(string $expected, ?string $input = null): void {
|
2018-04-17 23:58:51 +05:30
|
|
|
/** @var \PhpCsFixer\Fixer\WhitespacesAwareFixerInterface $fixer */
|
|
|
|
$fixer = $this->fixer;
|
|
|
|
$fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
|
|
|
|
|
|
|
|
$this->doTest($expected, $input);
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:07:57 +05:30
|
|
|
public function provideMessyWhitespacesCases(): iterable {
|
|
|
|
yield [
|
|
|
|
"<?php\nclass Foo\n{\r\n\r\n public function bar() {}\r\n\r\n}",
|
|
|
|
"<?php\nclass Foo\n{\n public function bar() {}\n}",
|
|
|
|
];
|
|
|
|
|
|
|
|
yield [
|
|
|
|
"<?php\nclass Foo\n{\r\n\r\n public function bar() {}\r\n\r\n}",
|
|
|
|
"<?php\nclass Foo\n{\r\n\r\n\n\n public function bar() {}\n\n\n\n}",
|
2018-04-17 23:58:51 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:07:57 +05:30
|
|
|
protected function createFixer(): AbstractFixer {
|
2018-04-17 23:58:51 +05:30
|
|
|
return new BlankLineAroundClassBodyFixer();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|