Skip to content

Commit 6e718a5

Browse files
committed
Add tests for the AbstractModel rules
1 parent 29a8e9a commit 6e718a5

File tree

7 files changed

+127
-4
lines changed

7 files changed

+127
-4
lines changed

src/bitExpert/PHPStan/Magento/Rules/AbstractModelRetrieveCollectionViaFactoryRule.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
use PHPStan\Rules\Rule;
1919
use PHPStan\ShouldNotHappenException;
2020
use PHPStan\Type\ObjectType;
21+
use PHPStan\Type\VerbosityLevel;
2122

2223
/**
2324
* Since 101.0.0 because collections should be used directly via factory
25+
*
26+
* @implements Rule<MethodCall>
2427
*/
2528
class AbstractModelRetrieveCollectionViaFactoryRule implements Rule
2629
{
@@ -35,7 +38,7 @@ public function getNodeType(): string
3538
/**
3639
* @param Node $node
3740
* @param Scope $scope
38-
* @return array
41+
* @return (string|\PHPStan\Rules\RuleError)[] errors
3942
* @throws ShouldNotHappenException
4043
*/
4144
public function processNode(Node $node, Scope $scope): array
@@ -61,7 +64,7 @@ public function processNode(Node $node, Scope $scope): array
6164
return [
6265
sprintf(
6366
'Collections should be used directly via factory, not via %s::%s() method',
64-
$type->describe(VerbosityLevel::type()),
67+
$type->describe(VerbosityLevel::typeOnly()),
6568
$node->name->name
6669
)
6770
];

src/bitExpert/PHPStan/Magento/Rules/AbstractModelUseServiceContractRule.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
use PHPStan\Rules\Rule;
1919
use PHPStan\ShouldNotHappenException;
2020
use PHPStan\Type\ObjectType;
21+
use PHPStan\Type\VerbosityLevel;
2122

2223
/**
2324
* Since 100.1.0 entities must not be responsible for their own loading, service contracts should persist entities.
25+
*
26+
* @implements Rule<MethodCall>
2427
*/
2528
class AbstractModelUseServiceContractRule implements Rule
2629
{
@@ -35,7 +38,7 @@ public function getNodeType(): string
3538
/**
3639
* @param Node $node
3740
* @param Scope $scope
38-
* @return array
41+
* @return (string|\PHPStan\Rules\RuleError)[] errors
3942
* @throws ShouldNotHappenException
4043
*/
4144
public function processNode(Node $node, Scope $scope): array
@@ -61,7 +64,7 @@ public function processNode(Node $node, Scope $scope): array
6164
return [
6265
sprintf(
6366
'Use service contracts to persist entities in favour of %s::%s() method',
64-
$type->describe(VerbosityLevel::type()),
67+
$type->describe(VerbosityLevel::typeOnly()),
6568
$node->name->name
6669
)
6770
];
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Rules;
14+
15+
use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel;
16+
use PHPStan\Rules\Rule;
17+
use PHPStan\Testing\RuleTestCase;
18+
19+
/**
20+
* @extends \PHPStan\Testing\RuleTestCase<AbstractModelRetrieveCollectionViaFactoryRule>
21+
*/
22+
class AbstractModelRetrieveCollectionViaFactoryRuleUnitTest extends RuleTestCase
23+
{
24+
protected function getRule(): Rule
25+
{
26+
return new AbstractModelRetrieveCollectionViaFactoryRule();
27+
}
28+
29+
public function testCheckCaughtException(): void
30+
{
31+
$this->analyse([__DIR__ . '/Helper/collection.php'], [
32+
[
33+
'Collections should be used directly via factory, not via ' .
34+
SampleModel::class . '::getCollection() method',
35+
04
36+
]
37+
]);
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Rules;
14+
15+
use bitExpert\PHPStan\Magento\Rules\Helper\SampleModel;
16+
use PHPStan\Rules\Rule;
17+
use PHPStan\Testing\RuleTestCase;
18+
19+
/**
20+
* @extends \PHPStan\Testing\RuleTestCase<AbstractModelUseServiceContractRule>
21+
*/
22+
class AbstractModelUseServiceContractRuleUnitTest extends RuleTestCase
23+
{
24+
protected function getRule(): Rule
25+
{
26+
return new AbstractModelUseServiceContractRule();
27+
}
28+
29+
public function testCheckCaughtException(): void
30+
{
31+
$this->analyse([__DIR__ . '/Helper/service_contract.php'], [
32+
[
33+
'Use service contracts to persist entities in favour of ' . SampleModel::class . '::save() method',
34+
04,
35+
],
36+
[
37+
'Use service contracts to persist entities in favour of ' . SampleModel::class . '::delete() method',
38+
05,
39+
],
40+
[
41+
'Use service contracts to persist entities in favour of ' . SampleModel::class . '::load() method',
42+
06,
43+
],
44+
]);
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the phpstan-magento package.
5+
*
6+
* (c) bitExpert AG
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
declare(strict_types=1);
12+
13+
namespace bitExpert\PHPStan\Magento\Rules\Helper;
14+
15+
use Magento\Framework\Model\AbstractModel;
16+
17+
class SampleModel extends AbstractModel
18+
{
19+
public function __construct()
20+
{
21+
}
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
$model = new \bitExpert\PHPStan\Magento\Rules\Helper\SampleModel();
4+
$model->getCollection();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
$model = new \bitExpert\PHPStan\Magento\Rules\Helper\SampleModel();
4+
$model->save();
5+
$model->delete();
6+
$model->load(1);

0 commit comments

Comments
 (0)