php-code-style/tests/Fixer/Whitespace/LineBreakAfterStatementsFixerTest.php

593 lines
9.1 KiB
PHP
Raw Normal View History

2018-04-17 23:58:51 +05:30
<?php
declare(strict_types=1);
namespace Ely\CS\Test\Fixer\Whitespace;
use Ely\CS\Fixer\Whitespace\LineBreakAfterStatementsFixer;
use PhpCsFixer\AbstractFixer;
2018-04-17 23:58:51 +05:30
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @covers \Ely\CS\Fixer\Whitespace\LineBreakAfterStatementsFixer
*
* @author ErickSkrauch <erickskrauch@ely.by>
*/
class LineBreakAfterStatementsFixerTest extends AbstractFixerTestCase {
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void {
2018-04-17 23:58:51 +05:30
$this->doTest($expected, $input);
}
public function provideFixCases(): iterable {
2018-04-17 23:58:51 +05:30
// Simple cases
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
} else {
// another code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b") {
// code
} else {
// another code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++) {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str) {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
while ($i < 10) {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
while ($i < 10) {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
do {
// code
} while ($i < 10);
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
do {
// code
} while ($i < 10);
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a":
break;
case "b":
break;
default:
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a":
break;
case "b":
break;
default:
// code
}
$a = "next statement";
}
}',
];
2018-04-17 23:58:51 +05:30
// Extended cases
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} else if ("a" === "c") {
// code
} else if ("a" === "d") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} else if ("a" === "c") {
// code
} else if ("a" === "d") {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} elseif ("a" === "c") {
// code
} elseif ("a" === "d") {
// code
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b") {
// code
} elseif ("a" === "c") {
// code
} elseif ("a" === "d") {
// code
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str) {
if ($str === "foo") {
// code
}
}
}
}',
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str) {
if ($str === "foo") {
// code
}
}
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a": {
break;
}
case "b": {
break;
}
default: {
// code
}
}
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
switch ("str") {
case "a": {
break;
}
case "b": {
break;
}
default: {
// code
}
}
$a = "next statement";
}
}',
];
yield [
2018-04-17 23:58:51 +05:30
'<?php
$a = "prev statement";
foreach ($coordinates as $coordinate) {
2018-08-07 14:55:47 +05:30
$points = explode(",", $coordinate);
2018-04-17 23:58:51 +05:30
}
',
];
// Issue 5
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
else
$this->baz();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
if ("a" === "b")
$this->bar();
else
$this->baz();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
for ($i = 0; $i < 3; $i++)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
foreach (["foo", "bar"] as $str)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
while ($i < 10)
$this->bar();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
while ($i < 10)
$this->bar();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function foo()
{
do
$this->bar();
while ($i < 10);
$a = "next statement";
}
}',
'<?php
class Foo
{
public function foo()
{
do
$this->bar();
while ($i < 10);
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
if ("a" === "b")
$this->foo();
else if ("a" === "c")
$this->bar();
else if ("a" === "d")
$this->baz();
$a = "next statement";
}
}',
'<?php
class Foo
{
public function bar()
{
if ("a" === "b")
$this->foo();
else if ("a" === "c")
$this->bar();
else if ("a" === "d")
$this->baz();
$a = "next statement";
}
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str)
if ($str === "foo")
$this->bar();
return 3;
}
}',
'<?php
class Foo
{
public function bar()
{
foreach (["foo", "bar"] as $str)
if ($str === "foo")
$this->bar();
return 3;
}
}',
];
yield [
'<?php
do {
$a = 123;
} while ($value > 10); // comment here
',
];
2018-04-17 23:58:51 +05:30
}
protected function createFixer(): AbstractFixer {
2018-04-17 23:58:51 +05:30
return new LineBreakAfterStatementsFixer();
}
}