Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ final class BothAnnotationAndAttributeExists extends TestCase
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ final class OnSelf extends TestCase
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ class WithoutNamespaceTest extends \PHPUnit\Framework\TestCase
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector;
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -31,7 +31,7 @@ final class Fixture extends TestCase
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -31,7 +31,7 @@ final class StaticFixture extends TestCase
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\Fixture;
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ExplicitMockExpectsCallRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
public function testMethod(): void
{
$someClass = $this->createMock(\stdClass::class);

$someClass->method('some');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\Fixture;

use PHPUnit\Framework\TestCase;

final class Fixture extends TestCase
{
public function testMethod(): void
{
$someClass = $this->createMock(\stdClass::class);

$someClass->expects($this->atLeastOnce())->method('some');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector;

return RectorConfig::configure()
->withRules([ExplicitMockExpectsCallRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
/**
* @see https://github.com/sebastianbergmann/phpunit/issues/6053
* @see https://github.com/sebastianbergmann/phpunit/blob/12.0.0/ChangeLog-12.0.md
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest
*
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest
*/
final class AssertIsTypeMethodCallRector extends AbstractRector
{
Expand Down
101 changes: 101 additions & 0 deletions rules/PHPUnit120/Rector/MethodCall/ExplicitMockExpectsCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\PHPUnit120\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ObjectType;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\ExplicitMockExpectsCallRector\ExplicitMockExpectsCallRectorTest
*/
final class ExplicitMockExpectsCallRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add explicit expects() to mocks, to make expectations count explicit',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
public function testMe()
{
$someMock = $this->createMock(\stdClass::class);
$someMock->method('some');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
public function testMe()
{
$someMock = $this->createMock(\stdClass::class);
$someMock->expects($this->atLeastOnce())->method('some');
}
}
CODE_SAMPLE
,
),
],
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): Node|null
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}

if (! $node->var instanceof Variable && ! $node->var instanceof PropertyFetch) {
return null;
}

if (! $this->isName($node->name, 'method')) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType(PHPUnitClassName::MOCK_OBJECT))) {
return null;
}

$node->var = new MethodCall($node->var, 'expects', [
new Arg(new MethodCall(new Variable('this'), 'atLeastOnce')),
]);

return $node;
}
}
1 change: 1 addition & 0 deletions tests/Issues/DoubleAnnotation/Fixture/some_test.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ final class SomeTest extends TestCase
{
}
}