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
2 changes: 1 addition & 1 deletion phpstan-extension.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
-
class: BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule
class: BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule\PostConditionsTraitUsedRule
tags:
- phpstan.rules.rule
-
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery;
namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
Expand All @@ -11,6 +11,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Type;
use PHPUnit\Framework\TestCase;
use function in_array;
use function sprintf;

Expand Down Expand Up @@ -91,6 +92,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (!$classReflection->isSubclassOf(TestCase::class)) {
return [];
}

$traitName = 'Mockery\\Adapter\\Phpunit\\MockeryPHPUnitIntegration';
if (!$classReflection->hasTraitUse($traitName)) {
$errors[] = RuleErrorBuilder::message(sprintf('Calling %s without %s trait.', $name, $traitName))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php declare(strict_types = 1);

namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<PostConditionsTraitUsedRule>
*/
class PostConditionsTraitUsedRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new PostConditionsTraitUsedRule(
new RuleLevelHelper( // @phpstan-ignore phpstanApi.constructor (we will just update it when we update phpstan, it's minor impact)
$this->createReflectionProvider(),
true,
true,
true,
true,
true,
true,
true,
),
);
}


public function testTestWithoutMockeryTrait(): void
{
$this->analyse(
[__DIR__ . '/__fixtures__/TestWithoutMockeryTrait.php'],
[
[
'Calling expects without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
14,
],
[
'Calling once without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
16,
],
[
'Calling twice without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
19,
],
[
'Calling times without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
22,
],
[
'Calling zeroOrMoreTimes without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
25,
],
[
'Calling once without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
28,
],
[
'Calling atLeast without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
28,
],
[
'Calling once without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
32,
],
[
'Calling atMost without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
32,
],
[
'Calling never without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
36,
],
[
'Calling shouldHaveReceived without Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait.',
39,
],
],
);
}


public function testTestWithMockeryTrait(): void
{
$this->analyse(
[__DIR__ . '/__fixtures__/TestWithMockeryTrait.php'],
[],
);
}


public function testNonTestClassWithoutMockeryTrait(): void
{
$this->analyse(
[__DIR__ . '/__fixtures__/NonTestClassWithoutMockeryTrait.php'],
[],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule\__fixtures__;

use Mockery;

class NonTestClassWithoutMockeryTrait
{
public function doSomething(): void
{
$mock = Mockery::mock('SomeClass');

$mock->expects('doSomething');

$mock->shouldReceive('doSomething')
->once();

$mock->shouldReceive('doSomething')
->twice();

$mock->shouldReceive('doSomething')
->times(2);

$mock->shouldReceive('doSomething')
->zeroOrMoreTimes();

$mock->shouldReceive('doSomething')
->atLeast()
->once();

$mock->shouldReceive('doSomething')
->atMost()
->once();

$mock->shouldReceive('doSomething')
->never();

$mock->shouldHaveReceived('doSomething');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule\__fixtures__;

use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;

class TestWithMockeryTrait extends TestCase
{
use MockeryPHPUnitIntegration;


public function testSomething(): void
{
$mock = Mockery::mock('SomeClass');

$mock->expects('doSomething');

$mock->shouldReceive('doSomething')
->once();

$mock->shouldReceive('doSomething')
->twice();

$mock->shouldReceive('doSomething')
->times(2);

$mock->shouldReceive('doSomething')
->zeroOrMoreTimes();

$mock->shouldReceive('doSomething')
->atLeast()
->once();

$mock->shouldReceive('doSomething')
->atMost()
->once();

$mock->shouldReceive('doSomething')
->never();

$mock->shouldHaveReceived('doSomething');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\PostConditionsTraitUsedRule\__fixtures__;

use Mockery;
use PHPUnit\Framework\TestCase;

class TestWithoutMockeryTrait extends TestCase
{
public function testSomething(): void
{
$mock = Mockery::mock('SomeClass');

$mock->expects('doSomething');

$mock->shouldReceive('doSomething')
->once();

$mock->shouldReceive('doSomething')
->twice();

$mock->shouldReceive('doSomething')
->times(2);

$mock->shouldReceive('doSomething')
->zeroOrMoreTimes();

$mock->shouldReceive('doSomething')
->atLeast()
->once();

$mock->shouldReceive('doSomething')
->atMost()
->once();

$mock->shouldReceive('doSomething')
->never();

$mock->shouldHaveReceived('doSomething');
}
}
Loading