Skip to content

Commit 067c9c2

Browse files
committed
TASK: Add test for SiteAttributeValidationRule
1 parent e089fe5 commit 067c9c2

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed

src/Rule/SiteAttributeValidationRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function getNodeType(): string
2929
return Node\Expr\MethodCall::class;
3030
}
3131

32+
/**
33+
* @param Node\Expr\MethodCall $node
34+
*/
3235
public function processNode(Node $node, Scope $scope): array
3336
{
3437
if (!$node->name instanceof Node\Identifier) {
@@ -42,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
4245

4346
$declaringClass = $methodReflection->getDeclaringClass();
4447

45-
if (!$declaringClass->implementsInterface(Site::class) && $declaringClass->getName() !== Site::class) {
48+
if ($declaringClass->getName() !== Site::class) {
4649
return [];
4750
}
4851

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\SiteAttributeValidationRule\Fixture;
4+
5+
use TYPO3\CMS\Core\Site\Entity\Site;
6+
7+
final class UseSiteWithDefinedAttribute
8+
{
9+
10+
/** @var Site */
11+
private $site;
12+
13+
public function __construct(Site $site)
14+
{
15+
$this->site = $site;
16+
}
17+
18+
public function someMethod(): void
19+
{
20+
$this->site->getAttribute('languages');
21+
22+
$this->site->getLanguageById(1);
23+
24+
$classWithGetAttributeMethod = new class {
25+
26+
public function getAttribute(string $attributeName): string
27+
{
28+
return 'foo';
29+
}
30+
31+
};
32+
33+
$classWithGetAttributeMethod->getAttribute('foo');
34+
35+
$this->site->getAttribute();
36+
}
37+
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\SiteAttributeValidationRule\Fixture;
4+
5+
use TYPO3\CMS\Core\Site\Entity\Site;
6+
7+
final class UseSiteWithUndefinedAttribute
8+
{
9+
10+
/** @var Site */
11+
private $site;
12+
13+
public function __construct(Site $site)
14+
{
15+
$this->site = $site;
16+
}
17+
18+
public function someMethod(): void
19+
{
20+
$this->site->getAttribute('foo');
21+
}
22+
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace SaschaEgerer\PhpstanTypo3\Tests\Unit\Rule\SiteAttributeValidationRule;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use SaschaEgerer\PhpstanTypo3\Rule\SiteAttributeValidationRule;
8+
9+
/**
10+
* @extends RuleTestCase<SiteAttributeValidationRule>
11+
*/
12+
final class SiteAttributeValidationRuleTest extends RuleTestCase
13+
{
14+
15+
public function testRuleWithoutErrors(): void
16+
{
17+
$this->analyse([__DIR__ . '/Fixture/UseSiteWithDefinedAttribute.php'], []);
18+
}
19+
20+
public function testRuleWithErrors(): void
21+
{
22+
$this->analyse(
23+
[__DIR__ . '/Fixture/UseSiteWithUndefinedAttribute.php'],
24+
[
25+
[
26+
'There is no site attribute "foo" configured so we can\'t figure out the exact type to return when calling TYPO3\CMS\Core\Site\Entity\Site::getAttribute',
27+
20,
28+
'You should add custom site attribute to the typo3.siteGetAttributeMapping setting.',
29+
],
30+
]
31+
);
32+
}
33+
34+
protected function getRule(): Rule
35+
{
36+
return new SiteAttributeValidationRule([
37+
'languages' => 'de',
38+
]);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)