Skip to content

Commit

Permalink
Support tests using StrictMocking trait / extending our test case a…
Browse files Browse the repository at this point in the history
…nd using `DisableReturnValueGenerationForTestDoubles` (#67)
  • Loading branch information
ben-challis authored Apr 18, 2024
1 parent d6ac7ef commit 5337285
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Phpstan/Rule/ForbidLooseMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\Attributes\DisableReturnValueGenerationForTestDoubles;
use PHPUnit\Framework\TestCase;

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

if ($reflection->getNativeReflection()->getAttributes(DisableReturnValueGenerationForTestDoubles::class) !== []) {
return [];
}

return [RuleErrorBuilder::message('Forbidden call to "createMock", use "createStrictMock" instead.')->build()];
}
}
12 changes: 12 additions & 0 deletions tests/phpstan/Rule/ForbidLooseMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ public function does_not_report_test_using_disabling_of_return_value_generation_
$this->analyse([__DIR__.'/../data/TestCaseWithAttributeTest.php'], []);
}

#[Test]
public function does_not_report_test_extending_our_test_case_using_disabling_of_return_value_generation_attribute(): void
{
$this->analyse([__DIR__.'/../data/OurTestCaseExtendingAndUsingAttributeTest.php'], []);
}

#[Test]
public function does_not_report_test_using_trait_and_using_disabling_of_return_value_generation_attribute(): void
{
$this->analyse([__DIR__.'/../data/OurTraitUsingAndUsingAttributeTest.php'], []);
}

protected function getRule(): ForbidLooseMock
{
return new ForbidLooseMock();
Expand Down
19 changes: 19 additions & 0 deletions tests/phpstan/data/OurTestCaseExtendingAndUsingAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Tests\Phpstan\Lendable\PHPUnitExtensions\data;

use Lendable\PHPUnitExtensions\TestCase;
use PHPUnit\Framework\Attributes\DisableReturnValueGenerationForTestDoubles;
use PHPUnit\Framework\Attributes\Test;

#[DisableReturnValueGenerationForTestDoubles]
class OurTestCaseExtendingAndUsingAttributeTest extends TestCase
{
#[Test]
public function loose_mock(): void
{
$this->createMock(\ArrayAccess::class);
}
}
22 changes: 22 additions & 0 deletions tests/phpstan/data/OurTraitUsingAndUsingAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Tests\Phpstan\Lendable\PHPUnitExtensions\data;

use Lendable\PHPUnitExtensions\StrictMocking;
use PHPUnit\Framework\Attributes\DisableReturnValueGenerationForTestDoubles;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[DisableReturnValueGenerationForTestDoubles]
class OurTraitUsingAndUsingAttributeTest extends TestCase
{
use StrictMocking;

#[Test]
public function loose_mock(): void
{
$this->createMock(\ArrayAccess::class);
}
}

0 comments on commit 5337285

Please sign in to comment.