Skip to content

Commit bfbf5ec

Browse files
committed
[code-quality] Add WithCallbackIdenticalToStandaloneAssertsRector
1 parent 6960a96 commit bfbf5ec

File tree

13 files changed

+608
-0
lines changed

13 files changed

+608
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
4646
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
4747
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
48+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
4849
use Rector\PHPUnit\CodeQuality\Rector\StmtsAwareInterface\DeclareStrictTypesTestsRector;
4950
use Rector\PHPUnit\PHPUnit60\Rector\MethodCall\GetMockBuilderGetMockToCreateMockRector;
5051
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ReplaceAtMethodWithDesiredMatcherRector;
@@ -121,6 +122,7 @@
121122

122123
FinalizeTestCaseClassRector::class,
123124
DeclareStrictTypesTestsRector::class,
125+
WithCallbackIdenticalToStandaloneAssertsRector::class,
124126

125127
// prefer simple mocking
126128
GetMockBuilderGetMockToCreateMockRector::class,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipYieldFromExpr extends TestCase
8+
{
9+
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
10+
public function test(string $val1, string $val2): void
11+
{
12+
}
13+
14+
public static function dataProvider(): iterable
15+
{
16+
yield from self::someData();
17+
}
18+
19+
public static function someData(): iterable
20+
{
21+
yield ['value1', 'value2'];
22+
yield ['value3', 'value4'];
23+
yield ['value5', 'value6'];
24+
yield ['value7', 'value8'];
25+
}
26+
}
27+
28+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class ClosureInstanceOf extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 && $args[0] instanceof \stdClass;
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class ClosureInstanceOf extends TestCase
30+
{
31+
public function test()
32+
{
33+
$someMock = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$someMock->expects($this->any())
36+
->method('trans')
37+
->with($this->callback(function ($args): void {
38+
$this->assertInstanceOf(\stdClass::class, $args[0]);
39+
$this->assertCount(5, $args);
40+
}));
41+
}
42+
}
43+
44+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipMultipleStmts extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
$result = false;
17+
return count($args) === 5 && $args[0] === 'some_value';
18+
}));
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class SkipNoArgs extends TestCase
10+
{
11+
public function test()
12+
{
13+
$someMock = $this->getMockBuilder('AnyType')->getMock();
14+
15+
$someMock->expects($this->any())
16+
->method('trans')
17+
->with($this->callback(function (): bool {
18+
$args= [1, 2, 3];
19+
return count($args) === 5 && $args[0] === 'some_value';
20+
}));
21+
}
22+
}
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\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipOr extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 || $args[0] === 'some_value';
17+
}));
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class WithArrowFunction extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with(
16+
$this->callback(
17+
fn ($args): bool => count($args) === 5 && $args[0] instanceof \stdClass
18+
)
19+
);
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
28+
29+
use PHPUnit\Framework\TestCase;
30+
31+
final class WithArrowFunction extends TestCase
32+
{
33+
public function test()
34+
{
35+
$someMock = $this->getMockBuilder('AnyType')->getMock();
36+
37+
$someMock->expects($this->any())
38+
->method('trans')
39+
->with(
40+
$this->callback(
41+
function ($args): void {
42+
$this->assertInstanceOf(\stdClass::class, $args[0]);
43+
$this->assertCount(5, $args);
44+
}
45+
)
46+
);
47+
}
48+
}
49+
50+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class WithCallbackAssert extends TestCase
8+
{
9+
public function test()
10+
{
11+
$someMock = $this->getMockBuilder('AnyType')->getMock();
12+
13+
$someMock->expects($this->any())
14+
->method('trans')
15+
->with($this->callback(function ($args): bool {
16+
return count($args) === 5 && $args[0] === 'some_value';
17+
}));
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;
26+
27+
use PHPUnit\Framework\TestCase;
28+
29+
final class WithCallbackAssert extends TestCase
30+
{
31+
public function test()
32+
{
33+
$someMock = $this->getMockBuilder('AnyType')->getMock();
34+
35+
$someMock->expects($this->any())
36+
->method('trans')
37+
->with($this->callback(function ($args): void {
38+
$this->assertSame('some_value', $args[0]);
39+
$this->assertCount(5, $args);
40+
}));
41+
}
42+
}
43+
44+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class WithCallbackIdenticalToStandaloneAssertsRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(WithCallbackIdenticalToStandaloneAssertsRector::class);
10+
};

0 commit comments

Comments
 (0)