Skip to content

Commit db0d1d5

Browse files
authored
[phpunit 12] Add AllowMockObjectsWithoutExpectationsAttributeRector (#619)
* [phpunit 12] Add AllowMockObjectsWithoutExpectationsAttributeRector * add fixtures to handle * addd attribute if msising method
1 parent b2a2a04 commit db0d1d5

File tree

10 files changed

+441
-0
lines changed

10 files changed

+441
-0
lines changed

config/sets/phpunit120.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414

1515
// stubs over mocks
1616
CreateStubOverCreateMockArgRector::class,
17+
18+
// experimental, from PHPUnit 12.5.2
19+
// @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
20+
// AllowMockObjectsWithoutExpectationsAttributeRector::class,
1721
]);
1822
};
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\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AllowMockObjectsWithoutExpectationsAttributeRectorTest 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipIfAllTestsMethodsDefineExpectations extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someMock = $this->createMock(\stdClass::class);
14+
}
15+
16+
public function testOne()
17+
{
18+
$this->someMock->method('doSomething')->willReturn('value');
19+
}
20+
21+
public function testTwo()
22+
{
23+
$this->someMock->method('doSomethingElse')->willReturn('another value');
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipIfMockNotUsedIn2TestMethods extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someMock = $this->createMock(\stdClass::class);
14+
}
15+
16+
public function testOne()
17+
{
18+
}
19+
20+
public function testTwo()
21+
{
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipOnlySingleTest extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someMock = $this->createMock(\stdClass::class);
14+
}
15+
16+
public function testOne()
17+
{
18+
}
19+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SomeClass extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someMock = $this->createMock(\stdClass::class);
14+
}
15+
16+
public function testOne()
17+
{
18+
$this->someMock->method('doSomething')->willReturn('value');
19+
}
20+
21+
public function testTwo()
22+
{
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\Fixture;
31+
32+
use PHPUnit\Framework\TestCase;
33+
34+
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
35+
final class SomeClass extends TestCase
36+
{
37+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
38+
39+
protected function setUp(): void
40+
{
41+
$this->someMock = $this->createMock(\stdClass::class);
42+
}
43+
44+
public function testOne()
45+
{
46+
$this->someMock->method('doSomething')->willReturn('value');
47+
}
48+
49+
public function testTwo()
50+
{
51+
}
52+
}
53+
54+
?>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([AllowMockObjectsWithoutExpectationsAttributeRector::class]);

0 commit comments

Comments
 (0)