-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
49 changes: 49 additions & 0 deletions
49
utils/phpstan/src/FactoriesTraitMissing/AbstractFactoriesTraitMissingInTestCaseRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPStan\FactoriesTraitMissing; | ||
|
||
use phpDocumentor\Reflection\ProjectFactory; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\TypeWithClassName; | ||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
/** | ||
* @template T of Node\Expr\MethodCall|Node\Expr\StaticCall | ||
* @implements Rule<T> | ||
*/ | ||
abstract class AbstractFactoriesTraitMissingInTestCaseRule implements Rule | ||
{ | ||
/** | ||
* @param T $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$scopeClassReflection = $scope->getClassReflection(); | ||
|
||
if ( | ||
!$scopeClassReflection?->is(TestCase::class) | ||
|| $scopeClassReflection->hasMethod('_bootFoundry') | ||
|| !$this->isFoundryCall($node, $scope) | ||
) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf('You must use the trait "%s" in order to use Foundry in a test.', Factories::class)) | ||
->identifier('foundry.FactoriesTraitMissing') | ||
->build(), | ||
]; | ||
} | ||
|
||
/** | ||
* @param T $node | ||
*/ | ||
abstract protected function isFoundryCall(Node $node, Scope $scope): bool; | ||
} |
28 changes: 28 additions & 0 deletions
28
...s/phpstan/src/FactoriesTraitMissing/FactoriesTraitMissingInTestCaseWithMethodCallRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPStan\FactoriesTraitMissing; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\TypeWithClassName; | ||
use Zenstruck\Foundry\Factory; | ||
|
||
/** | ||
* @extends AbstractFactoriesTraitMissingInTestCaseRule<Node\Expr\MethodCall> | ||
*/ | ||
final class FactoriesTraitMissingInTestCaseWithMethodCallRule extends AbstractFactoriesTraitMissingInTestCaseRule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\MethodCall::class; | ||
} | ||
|
||
protected function isFoundryCall(Node $node, Scope $scope): bool | ||
{ | ||
$reflection = $scope->getType($node->var)->getObjectClassReflections()[0] ?? null; | ||
|
||
return (bool) $reflection?->is(Factory::class); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...tan/src/FactoriesTraitMissing/FactoriesTraitMissingInTestCaseWithStaticMethodCallRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\PHPStan\FactoriesTraitMissing; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use Zenstruck\Foundry\Factory; | ||
|
||
/** | ||
* @extends AbstractFactoriesTraitMissingInTestCaseRule<Node\Expr\StaticCall> | ||
*/ | ||
final class FactoriesTraitMissingInTestCaseWithStaticMethodCallRule extends AbstractFactoriesTraitMissingInTestCaseRule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\StaticCall::class; | ||
} | ||
|
||
protected function isFoundryCall(Node $node, Scope $scope): bool | ||
{ | ||
if (!$node->class instanceof Node\Name) { | ||
return false; | ||
} | ||
|
||
$reflection = $scope->resolveTypeByName($node->class)->getObjectClassReflections()[0] ?? null; | ||
|
||
return (bool)$reflection?->is(Factory::class); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
utils/phpstan/tests/FactoriesTraitMissingInTestCaseWithMethodCallRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Zenstruck\Foundry\PHPStan\FactoriesTraitMissing\FactoriesTraitMissingInTestCaseWithMethodCallRule; | ||
|
||
/** | ||
* @extends RuleTestCase<FactoriesTraitMissingInTestCaseWithMethodCallRule> | ||
*/ | ||
final class FactoriesTraitMissingInTestCaseWithMethodCallRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
return new FactoriesTraitMissingInTestCaseWithMethodCallRule(); | ||
} | ||
|
||
/** | ||
* @dataProvider ruleProvider | ||
*/ | ||
public function test_rule(string $file, array $errors): void | ||
{ | ||
$this->analyse([__DIR__."/data/FactoriesTraitMissing/{$file}"], $errors,); | ||
Check failure on line 35 in utils/phpstan/tests/FactoriesTraitMissingInTestCaseWithMethodCallRuleTest.php
|
||
} | ||
|
||
public static function ruleProvider(): iterable | ||
{ | ||
$errorMessage = 'You must use the trait "Zenstruck\Foundry\Test\Factories" in order to use Foundry in a test.'; | ||
|
||
yield ['NotUsingFactoriesTraitInTest.php', [[$errorMessage, 15]]]; | ||
yield ['UsingFactoriesTraitInTest.php', []]; | ||
yield ['NotInTest.php', []]; | ||
yield ['WithAbstractClassUsingFactoriesTraitTest.php', []]; | ||
yield ['WithTraitWithFactoriesTraitTest.php', []]; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
utils/phpstan/tests/FactoriesTraitMissingInTestCaseWithStaticMethodCallRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use Zenstruck\Foundry\PHPStan\FactoriesTraitMissing\FactoriesTraitMissingInTestCaseWithStaticMethodCallRule; | ||
|
||
/** | ||
* @extends RuleTestCase<FactoriesTraitMissingInTestCaseWithStaticMethodCallRule> | ||
*/ | ||
final class FactoriesTraitMissingInTestCaseWithStaticMethodCallRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
return new FactoriesTraitMissingInTestCaseWithStaticMethodCallRule(); | ||
} | ||
|
||
/** | ||
* @dataProvider ruleProvider | ||
*/ | ||
public function test_rule(string $file, array $errors): void | ||
{ | ||
$this->analyse([__DIR__ . "/data/FactoriesTraitMissing/{$file}"], $errors); | ||
Check failure on line 35 in utils/phpstan/tests/FactoriesTraitMissingInTestCaseWithStaticMethodCallRuleTest.php
|
||
} | ||
|
||
public static function ruleProvider(): iterable | ||
{ | ||
$errorMessage = 'You must use the trait "Zenstruck\Foundry\Test\Factories" in order to use Foundry in a test.'; | ||
|
||
yield ['NotUsingFactoriesTraitInTest.php', [[$errorMessage, 14], [$errorMessage, 15]]]; | ||
yield ['UsingFactoriesTraitInTest.php', []]; | ||
yield ['NotInTest.php', []]; | ||
yield ['WithAbstractClassUsingFactoriesTraitTest.php', []]; | ||
yield ['WithTraitWithFactoriesTraitTest.php', []]; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
utils/phpstan/tests/data/FactoriesTraitMissing/AbstractClassUsingFactoriesTraitTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
abstract class AbstractClassUsingFactoriesTraitTestCase extends TestCase | ||
{ | ||
use Factories; | ||
} |
16 changes: 16 additions & 0 deletions
16
utils/phpstan/tests/data/FactoriesTraitMissing/NotInTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; | ||
|
||
final class NotInTest | ||
{ | ||
public function someMethod(): void | ||
{ | ||
ContactFactory::createOne(); | ||
ContactFactory::new()->create(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
utils/phpstan/tests/data/FactoriesTraitMissing/NotUsingFactoriesTraitInTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; | ||
|
||
final class NotUsingFactoriesTraitInTest extends TestCase | ||
{ | ||
public function testSomething(): void | ||
{ | ||
ContactFactory::createOne(); | ||
ContactFactory::new()->create(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
utils/phpstan/tests/data/FactoriesTraitMissing/TraitWithFactoriesTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use Zenstruck\Foundry\Test\Factories; | ||
|
||
trait TraitWithFactoriesTrait | ||
{ | ||
use Factories; | ||
} |
20 changes: 20 additions & 0 deletions
20
utils/phpstan/tests/data/FactoriesTraitMissing/UsingFactoriesTraitInTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; | ||
|
||
final class UsingFactoriesTraitInTest extends TestCase | ||
{ | ||
use Factories; | ||
|
||
public function testSomething(): void | ||
{ | ||
ContactFactory::createOne(); | ||
ContactFactory::new()->create(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
utils/phpstan/tests/data/FactoriesTraitMissing/WithAbstractClassUsingFactoriesTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; | ||
|
||
final class WithAbstractClassUsingFactoriesTraitTest extends AbstractClassUsingFactoriesTraitTestCase | ||
{ | ||
public function testSomething(): void | ||
{ | ||
ContactFactory::createOne(); | ||
ContactFactory::new()->create(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
utils/phpstan/tests/data/FactoriesTraitMissing/WithTraitWithFactoriesTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\PHPStan\data\FactoriesTraitMissing; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\Contact\ContactFactory; | ||
|
||
final class WithTraitWithFactoriesTraitTest extends TestCase | ||
{ | ||
use TraitWithFactoriesTrait; | ||
|
||
public function testSomething(): void | ||
{ | ||
ContactFactory::createOne(); | ||
ContactFactory::new()->create(); | ||
} | ||
} |