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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ $dateTime = new \DateTime();

<br>

### NoWithOnStubRule

Disallow `with()` on stubs (mocks without `expects()`). PHPUnit deprecates `with*()` on test stubs because they silently swallow argument mismatches.

```yaml
rules:
- Rector\Mockstan\Rules\NoWithOnStubRule
```

```php
$someMock = $this->createMock(Service::class);
$someMock->method('calculate')
->with(10)
->willReturn(20);
```

:x:

```php
$someMock = $this->createMock(Service::class);
$someMock->expects($this->once())
->method('calculate')
->with(10)
->willReturn(20);
```

:+1:

<br>

### NoDocumentMockingRule

Prevent mocking of Doctrine ODM document classes. Use real instances instead.
Expand Down
1 change: 1 addition & 0 deletions config/phpunit-mocks-rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ rules:
# explicit expects()
- Rector\Mockstan\Rules\ExplicitExpectsMockMethodRule
- Rector\Mockstan\Rules\AvoidAnyExpectsRule
- Rector\Mockstan\Rules\NoWithOnStubRule
- Rector\Mockstan\Rules\RequireAtLeastOneRule

# better alternative than mocks
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<exclude>tests/Rules/ClassNameRespectsParentSuffixRule/Fixture/</exclude>
<exclude>tests/Rules/ExplicitExpectsMockMethodRule/Fixture</exclude>
<exclude>tests/Rules/NoMockOnlyTestRule/Fixture</exclude>
<exclude>tests/Rules/NoWithOnStubRule/Fixture</exclude>
</testsuite>
</phpunit>
2 changes: 2 additions & 0 deletions src/Enum/RuleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ final class RuleIdentifier

public const string AVOID_ANY_EXPECTS = 'mockstan.avoidAnyExpects';

public const string NO_WITH_ON_STUB = 'mockstan.noWithOnStub';

public const string REQUIRE_AT_LEAST_ONE = 'mockstan.requireAtLeastOne';
}
75 changes: 75 additions & 0 deletions src/Rules/NoWithOnStubRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Rector\Mockstan\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Rector\Mockstan\Enum\RuleIdentifier;
use Rector\Mockstan\Helper\NamingHelper;
use Rector\Mockstan\PHPUnit\TestClassDetector;

/**
* @implements Rule<MethodCall>
*
* @see \Rector\Mockstan\Tests\Rules\NoWithOnStubRule\NoWithOnStubRuleTest
*/
final class NoWithOnStubRule implements Rule
{
public const string ERROR_MESSAGE = 'Using with() on a stub is misleading and deprecated by PHPUnit. Use explicit expects() to turn it into a mock, or drop with()';

public function getNodeType(): string
{
return MethodCall::class;
}

/**
* @param MethodCall $node
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (! NamingHelper::isName($node->name, 'with')) {
return [];
}

if (! TestClassDetector::isTestClass($scope)) {
return [];
}

if (! $node->var instanceof MethodCall) {
return [];
}

$methodCall = $node->var;
if (! NamingHelper::isName($methodCall->name, 'method')) {
return [];
}

if ($methodCall->var instanceof MethodCall && NamingHelper::isName($methodCall->var->name, 'expects')) {
return [];
}

if (! $methodCall->var instanceof Variable && ! $methodCall->var instanceof PropertyFetch) {
return [];
}

$callerType = $scope->getType($methodCall->var);
if (! $callerType->hasMethod('expects')->yes()) {
return [];
}

$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(RuleIdentifier::NO_WITH_ON_STUB)
->build();

return [$identifierRuleError];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Mockstan\Tests\Rules\NoWithOnStubRule\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipMockWithExpectsAndWith extends TestCase
{
public function test(): void
{
$mock = $this->createMock(\stdClass::class);

$mock->expects($this->once())
->method('someMethod')
->with('arg')
->willReturn('value');
}
}
17 changes: 17 additions & 0 deletions tests/Rules/NoWithOnStubRule/Fixture/StubWithWith.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Mockstan\Tests\Rules\NoWithOnStubRule\Fixture;

use PHPUnit\Framework\TestCase;

final class StubWithWith extends TestCase
{
public function test(): void
{
$mock = $this->createMock(\stdClass::class);

$mock->method('someMethod')
->with('arg')
->willReturn('value');
}
}
38 changes: 38 additions & 0 deletions tests/Rules/NoWithOnStubRule/NoWithOnStubRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Mockstan\Tests\Rules\NoWithOnStubRule;

use Iterator;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Mockstan\Rules\NoWithOnStubRule;

final class NoWithOnStubRuleTest extends RuleTestCase
{
/**
* @param array<int, array<string|int>> $expectedErrorsWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorsWithLines): void
{
$this->analyse([$filePath], $expectedErrorsWithLines);
}

/**
* @return Iterator<array<array<int, mixed>, mixed>>
*/
public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/StubWithWith.php', [[NoWithOnStubRule::ERROR_MESSAGE, 13]]];

yield [__DIR__ . '/Fixture/SkipMockWithExpectsAndWith.php', []];
}

protected function getRule(): Rule
{
return new NoWithOnStubRule();
}
}
Loading