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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ $this->realService = new Service();
:+1:


<br>

### RequireAtLeastOneRule

Disallow `atLeast(0)` on mock expectations, as it matches any number of calls (including zero) and provides no real verification. Require a value of `1` or higher.

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

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

:x:

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

:+1:

<br>

### NoMockOnlyTestRule
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\RequireAtLeastOneRule

# better alternative than mocks
- Rector\Mockstan\Rules\NoDocumentMockingRule
Expand Down
2 changes: 2 additions & 0 deletions src/Enum/RuleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ final class RuleIdentifier
public const string FORBIDDEN_CLASS_TO_MOCK = 'mockstan.forbiddenClassToMock';

public const string AVOID_ANY_EXPECTS = 'mockstan.avoidAnyExpects';

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

declare(strict_types=1);

namespace Rector\Mockstan\Rules;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\Int_;
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\RequireAtLeastOneRule\RequireAtLeastOneRuleTest
*/
final class RequireAtLeastOneRule implements Rule
{
public const string ERROR_MESSAGE = 'Using $this->atLeast(0) is meaningless, as it matches any number of calls. Use 1 or higher';

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

/**
* @param MethodCall $node
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (! TestClassDetector::isTestClass($scope)) {
return [];
}

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

$args = $node->getArgs();
if ($args === []) {
return [];
}

$firstArgValue = $args[0]->value;
if (! $firstArgValue instanceof Int_) {
return [];
}

if ($firstArgValue->value >= 1) {
return [];
}

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

return [$identifierRuleError];
}
}
17 changes: 17 additions & 0 deletions tests/Rules/RequireAtLeastOneRule/Fixture/AtLeastZero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use PHPUnit\Framework\TestCase;

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

$mock->expects($this->atLeast(0))
->method('someMethod')
->willReturn('value');
}
}
22 changes: 22 additions & 0 deletions tests/Rules/RequireAtLeastOneRule/Fixture/SkipAtLeastOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

use PHPUnit\Framework\TestCase;

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

$mock->expects($this->atLeast(1))
->method('someMethod')
->willReturn('value');

$anotherMock = $this->createMock(\stdClass::class);
$anotherMock->expects($this->atLeast(3))
->method('anotherMethod')
->willReturn('value');
}
}
37 changes: 37 additions & 0 deletions tests/Rules/RequireAtLeastOneRule/RequireAtLeastOneRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\Mockstan\Tests\Rules\RequireAtLeastOneRule;

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

final class RequireAtLeastOneRuleTest 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/AtLeastZero.php', [[RequireAtLeastOneRule::ERROR_MESSAGE, 13]]];
yield [__DIR__ . '/Fixture/SkipAtLeastOne.php', []];
}

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