Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed May 27, 2022
1 parent 47130d7 commit dd04ea3
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/PHPStan/Node/AttributeArgRuleTest.php
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Testing\RuleTestCase;

class AttributeArgRuleTest extends RuleTestCase
{

public const ERROR_MESSAGE = 'Found Arg';

protected function getRule(): Rule
{
return new class() implements Rule {

public function getNodeType(): string
{
return Node\Arg::class;
}

/**
* @param Node\Arg $node
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [AttributeArgRuleTest::ERROR_MESSAGE];
}

};
}

public function dataRule(): iterable
{
yield [
__DIR__ . '/data/attributes.php',
self::ERROR_MESSAGE,
[8, 16, 20, 23, 26, 27, 34, 40],
];
}

/**
* @param int[] $lines
* @dataProvider dataRule
*/
public function testRule(string $file, string $expectedError, array $lines): void
{
$errors = [];
foreach ($lines as $line) {
$errors[] = [$expectedError, $line];
}
$this->analyse([$file], $errors);
}

}
59 changes: 59 additions & 0 deletions tests/PHPStan/Node/AttributeGroupRuleTest.php
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Testing\RuleTestCase;

class AttributeGroupRuleTest extends RuleTestCase
{

public const ERROR_MESSAGE = 'Found AttributeGroup';

protected function getRule(): Rule
{
return new class() implements Rule {

public function getNodeType(): string
{
return Node\AttributeGroup::class;
}

/**
* @param Node\AttributeGroup $node
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [AttributeGroupRuleTest::ERROR_MESSAGE];
}

};
}

public function dataRule(): iterable
{
yield [
__DIR__ . '/data/attributes.php',
self::ERROR_MESSAGE,
[8, 16, 20, 23, 26, 27, 34, 40],
];
}

/**
* @param int[] $lines
* @dataProvider dataRule
*/
public function testRule(string $file, string $expectedError, array $lines): void
{
$errors = [];
foreach ($lines as $line) {
$errors[] = [$expectedError, $line];
}
$this->analyse([$file], $errors);
}

}
59 changes: 59 additions & 0 deletions tests/PHPStan/Node/AttributeRuleTest.php
@@ -0,0 +1,59 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Testing\RuleTestCase;

class AttributeRuleTest extends RuleTestCase
{

public const ERROR_MESSAGE = 'Found Attribute';

protected function getRule(): Rule
{
return new class() implements Rule {

public function getNodeType(): string
{
return Node\Attribute::class;
}

/**
* @param Node\Attribute $node
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [AttributeRuleTest::ERROR_MESSAGE];
}

};
}

public function dataRule(): iterable
{
yield [
__DIR__ . '/data/attributes.php',
self::ERROR_MESSAGE,
[8, 16, 20, 23, 26, 27, 34, 40],
];
}

/**
* @param int[] $lines
* @dataProvider dataRule
*/
public function testRule(string $file, string $expectedError, array $lines): void
{
$errors = [];
foreach ($lines as $line) {
$errors[] = [$expectedError, $line];
}
$this->analyse([$file], $errors);
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Node/data/attributes.php
@@ -0,0 +1,41 @@
<?php

namespace NodeCallbackCalled;

use ClassAttributes\AttributeWithConstructor;
use FunctionAttributes\Baz;

#[\Attribute(flags: \Attribute::TARGET_ALL)]
class UniversalAttribute
{
public function __construct(int $foo)
{
}
}

#[UniversalAttribute(1)]
class MyClass
{

#[UniversalAttribute(2)]
private const MY_CONST = 'const';

#[UniversalAttribute(3)]
private string $myProperty;

#[UniversalAttribute(4)]
public function myMethod(#[UniversalAttribute(5)] string $arg): void
{

}

}

#[UniversalAttribute(6)]
interface MyInterface {}

#[UniversalAttribute(7)]
trait MyTrait {}

#[UniversalAttribute(8)]
function myFunction() {}

0 comments on commit dd04ea3

Please sign in to comment.