Skip to content

Commit 921381a

Browse files
authored
[CodeQuality] Extend FlipAssertRector to more assert methods and expected value shapes (#787)
1 parent 69ce6b2 commit 921381a

16 files changed

Lines changed: 462 additions & 7 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class ArrayLiteral extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result, $model, $response, $transformations)
8+
{
9+
$this->assertSame($response->json(), ['id' => 1, 'status' => SomeEnum::Active]);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
18+
19+
final class ArrayLiteral extends \PHPUnit\Framework\TestCase
20+
{
21+
public function test($result, $model, $response, $transformations)
22+
{
23+
$this->assertSame(['id' => 1, 'status' => SomeEnum::Active], $response->json());
24+
}
25+
}
26+
27+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class ArrayLiteralWithVariable extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result, $model, $response, $transformations)
8+
{
9+
$this->assertEquals($model->value, ['transformations' => $transformations]);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
18+
19+
final class ArrayLiteralWithVariable extends \PHPUnit\Framework\TestCase
20+
{
21+
public function test($result, $model, $response, $transformations)
22+
{
23+
$this->assertEquals(['transformations' => $transformations], $model->value);
24+
}
25+
}
26+
27+
?>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class CanonicalizingAndDelta extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result, $model, $response, $transformations)
8+
{
9+
$this->assertEqualsCanonicalizing($result, [1, 2]);
10+
$this->assertNotEqualsCanonicalizing($result, [3, 4]);
11+
$this->assertEqualsIgnoringCase($result, 'expected');
12+
$this->assertNotEqualsIgnoringCase($result, 'other');
13+
$this->assertEqualsWithDelta($result, 1.5, 0.01);
14+
$this->assertNotEqualsWithDelta($result, 2.5, 0.01);
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
23+
24+
final class CanonicalizingAndDelta extends \PHPUnit\Framework\TestCase
25+
{
26+
public function test($result, $model, $response, $transformations)
27+
{
28+
$this->assertEqualsCanonicalizing([1, 2], $result);
29+
$this->assertNotEqualsCanonicalizing([3, 4], $result);
30+
$this->assertEqualsIgnoringCase('expected', $result);
31+
$this->assertNotEqualsIgnoringCase('other', $result);
32+
$this->assertEqualsWithDelta(1.5, $result, 0.01);
33+
$this->assertNotEqualsWithDelta(2.5, $result, 0.01);
34+
}
35+
}
36+
37+
?>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class ConcatLiteral extends \PHPUnit\Framework\TestCase
6+
{
7+
private const PREFIX = 'item';
8+
9+
public function test($result)
10+
{
11+
$this->assertSame($result->getName(), 'foo' . 'bar');
12+
$this->assertSame($result->getPath(), __DIR__ . '/fixture.json');
13+
$this->assertSame($result->getId(), self::PREFIX . '-1');
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
22+
23+
final class ConcatLiteral extends \PHPUnit\Framework\TestCase
24+
{
25+
private const PREFIX = 'item';
26+
27+
public function test($result)
28+
{
29+
$this->assertSame('foo' . 'bar', $result->getName());
30+
$this->assertSame(__DIR__ . '/fixture.json', $result->getPath());
31+
$this->assertSame(self::PREFIX . '-1', $result->getId());
32+
}
33+
}
34+
35+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class EnumValue extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result)
8+
{
9+
$this->assertSame($result->status, Status::Active->value);
10+
$this->assertSame($result->getStatusName(), Status::Active->name);
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
19+
20+
final class EnumValue extends \PHPUnit\Framework\TestCase
21+
{
22+
public function test($result)
23+
{
24+
$this->assertSame(Status::Active->value, $result->status);
25+
$this->assertSame(Status::Active->name, $result->getStatusName());
26+
}
27+
}
28+
29+
?>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class NegativeNumber extends \PHPUnit\Framework\TestCase
6+
{
7+
private const LIMIT = 10;
8+
9+
public function test($result, $model, $response, $transformations)
10+
{
11+
$this->assertSame($result->getOffset(), -1);
12+
$this->assertSame($result->getMin(), -PHP_INT_MAX);
13+
$this->assertSame($result->getLimit(), -self::LIMIT);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
22+
23+
final class NegativeNumber extends \PHPUnit\Framework\TestCase
24+
{
25+
private const LIMIT = 10;
26+
27+
public function test($result, $model, $response, $transformations)
28+
{
29+
$this->assertSame(-1, $result->getOffset());
30+
$this->assertSame(-PHP_INT_MAX, $result->getMin());
31+
$this->assertSame(-self::LIMIT, $result->getLimit());
32+
}
33+
}
34+
35+
?>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class NewWithLiteralArgs extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result)
8+
{
9+
$this->assertEquals($result, new Money(100, Currency::EUR));
10+
$this->assertEquals($result->getPeriod(), new Period(['from' => 1, 'to' => -1]));
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
19+
20+
final class NewWithLiteralArgs extends \PHPUnit\Framework\TestCase
21+
{
22+
public function test($result)
23+
{
24+
$this->assertEquals(new Money(100, Currency::EUR), $result);
25+
$this->assertEquals(new Period(['from' => 1, 'to' => -1]), $result->getPeriod());
26+
}
27+
}
28+
29+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class SkipActualArrayLiteral extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result, $model, $response, $transformations)
8+
{
9+
$this->assertSame($result, [$model->x, $model->y]);
10+
$this->assertSame($result, [$model->getX()]);
11+
}
12+
}
13+
14+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class SkipBothArrayLiteral extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test($result, $model, $response, $transformations)
8+
{
9+
$this->assertSame(['id' => 1], [1, 2]);
10+
}
11+
}
12+
13+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\FlipAssertRector\Fixture;
4+
5+
final class SkipComputedExpectedAgainstBuiltArray extends \PHPUnit\Framework\TestCase
6+
{
7+
private array $expectedIds = [];
8+
9+
private static array $fixtures = [];
10+
11+
public function test($first, $second, array $expected, $user, $team, $result)
12+
{
13+
$this->assertSame($this->expectedIds, [$first->id, $second->id]);
14+
$this->assertSame($expected['ids'], [$user->id, $team->id]);
15+
$this->assertSame(static::$fixtures, [$result->name]);
16+
}
17+
}
18+
19+
?>

0 commit comments

Comments
 (0)