Skip to content

Commit af5b783

Browse files
JanTvrdikclaude
andcommitted
Add comprehensive test suite with example rule
- Create RuleTestCaseTest to validate RuleTestCase functionality - Add DisallowDivisionByLiteralZeroRule as example PHPStan rule - Implement proper PSR-4 namespace structure for tests - Organize test data in Data directory following convention - Include test data file with expected error comments The test suite demonstrates how to use RuleTestCase and validates that the enhanced testing framework works correctly with error detection and validation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 88521d5 commit af5b783

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonkTests\PHPStanDev\Rule\Data\DisallowDivisionByLiteralZeroRule;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\BinaryOp\Div;
7+
use PhpParser\Node\Scalar\Int_;
8+
use PHPStan\Analyser\Scope;
9+
use PHPStan\Rules\Rule;
10+
use PHPStan\Rules\RuleErrorBuilder;
11+
12+
/**
13+
* @implements Rule<Div>
14+
*/
15+
class DisallowDivisionByLiteralZeroRule implements Rule
16+
{
17+
18+
public function getNodeType(): string
19+
{
20+
return Div::class;
21+
}
22+
23+
public function processNode(Node $node, Scope $scope): array
24+
{
25+
if ($node->right instanceof Int_ && $node->right->value === 0) {
26+
return [
27+
RuleErrorBuilder::message('Division by literal zero is not allowed')
28+
->identifier('shipmonk.divisionByZero')
29+
->build(),
30+
];
31+
}
32+
33+
return [];
34+
}
35+
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DisallowDivisionByLiteralZeroRule;
4+
5+
function testDivision(): void
6+
{
7+
$a = 10;
8+
$b = 0;
9+
10+
$validDivision = $a / 2;
11+
$validDivisionWithVariable = $a / $b;
12+
13+
$invalidDivision = $a / 0; // error: Division by literal zero is not allowed
14+
$anotherInvalidDivision = 5 / 0; // error: Division by literal zero is not allowed
15+
}
16+

tests/RuleTestCaseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ShipMonkTests\PHPStanDev;
4+
5+
use PHPStan\Rules\Rule;
6+
use ShipMonk\PHPStanDev\RuleTestCase;
7+
use ShipMonkTests\PHPStanDev\Rule\Data\DisallowDivisionByLiteralZeroRule\DisallowDivisionByLiteralZeroRule;
8+
9+
/**
10+
* @extends RuleTestCase<DisallowDivisionByLiteralZeroRule>
11+
*/
12+
class RuleTestCaseTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new DisallowDivisionByLiteralZeroRule();
18+
}
19+
20+
public function testRule(): void
21+
{
22+
$this->analyseFile(__DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/code.php');
23+
}
24+
25+
}
26+

0 commit comments

Comments
 (0)