Skip to content

Commit cd14c9a

Browse files
committed
add compat test
1 parent bc17200 commit cd14c9a

17 files changed

Lines changed: 356 additions & 6 deletions

.github/workflows/compat_test.yaml

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
# see https://github.com/rectorphp/rector/issues/9416
2+
# compat tests project lives in /compat-tests
23
name: PHPUnit and PHPStan Compat Test
34

45
on:
56
push:
67
branches:
78
- main
89
pull_request: null
10+
schedule:
11+
- cron: '0 6 * * *'
912

1013
jobs:
1114
compat_test:
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
actions:
19+
-
20+
name: 'Rector dev + PHPUnit 10'
21+
run: composer require "phpunit/phpunit:10.*" -W
22+
php: 8.2
23+
-
24+
name: 'Rector dev + PHPUnit 11'
25+
run: composer require "phpunit/phpunit:11.*" -W
26+
php: 8.2
27+
-
28+
name: 'Rector dev + PHPUnit 12'
29+
run: composer require "phpunit/phpunit:12.*" -W
30+
php: 8.3
31+
32+
name: ${{ matrix.actions.name }}
33+
1234
runs-on: ubuntu-latest
1335

36+
defaults:
37+
run:
38+
working-directory: compat-tests
39+
1440
steps:
41+
- uses: actions/checkout@v4
42+
1543
-
1644
uses: shivammathur/setup-php@v2
1745
with:
18-
php-version: 8.3
46+
php-version: ${{ matrix.actions.php }}
1947
coverage: none
2048

21-
- run: composer create-project "rector/rector-compat-tests:dev-main" .
49+
-
50+
uses: "ramsey/composer-install@v4"
51+
with:
52+
working-directory: compat-tests
2253

23-
- run: vendor/bin/phpunit tests/PHPStan
54+
- run: ${{ matrix.actions.run }}
2455

25-
- run: vendor/bin/phpunit tests/Rector
56+
- run: vendor/bin/phpunit tests/Rector
2657

27-
- run: vendor/bin/phpunit tests
58+
- run: vendor/bin/phpunit tests/PHPStan
2859

29-
- run: vendor/bin/phpunit
60+
- run: vendor/bin/phpunit

compat-tests/.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
indent_style = space
9+
indent_size = 4

compat-tests/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
composer.lock
2+
/vendor
3+
4+
.phpunit.result.cache

compat-tests/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Rector Compatibility Tests
2+
3+
A small test harness that verifies [Rector](https://github.com/rectorphp/rector) keeps working alongside other tools in the PHP static-analysis ecosystem.
4+
5+
It runs a minimal real-world setup — a custom Rector rule, a custom PHPStan rule, and a PHPUnit test suite — against multiple combinations of dependencies in CI, to catch breakage early.
6+
7+
## What it checks
8+
9+
* A custom **Rector** rule (`MakeClassFinalRector`, `UseGetArgRector`) loads and applies correctly.
10+
* A custom **PHPStan** rule (`CustomPHPStanRule`) loads and runs alongside Rector.
11+
* **PHPUnit 10, 11, and 12** all work with Rector's preloaded dependencies.
12+
* Manually including **`nikic/php-parser`** in user code does not conflict with the copy Rector ships.
13+
* `rector-laravel` + `nikic/php-parser` upgrade scenarios run cleanly (see [rectorphp/rector#9470](https://github.com/rectorphp/rector/issues/9470)).
14+
15+
This project lives inside `rector/rector-src` (under `compat-tests/`) and pulls in `rector/rector:dev-main` as a downstream dependency, so the compat suite is maintained in one place. It mirrors the standalone [rectorphp/rector-compat-tests](https://github.com/rectorphp/rector-compat-tests).
16+
17+
## How it runs
18+
19+
GitHub Actions runs the matrix daily (`0 6 * * *`) and on every push/PR, from the repository root `.github/workflows`:
20+
21+
* `compat_test.yaml` — Rector dev + PHPUnit 10 / 11 / 12
22+
* `rector_laravel_rector_dev.yaml` — Rector + rector-laravel + php-parser

compat-tests/composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "rector/rector-compat-tests",
3+
"license": "MIT",
4+
"description": "Tests for compatibility with PHPStand and PHPUnit preload magic",
5+
"require-dev": {
6+
"php": "^8.2",
7+
"phpunit/phpunit": "10.*|11.*|12.*",
8+
"nikic/php-parser": "5.4.*|5.5.*|5.6.*|5.7.*",
9+
"rector/rector": "dev-main as 2.4.4",
10+
"phpstan/phpstan": "^2.2",
11+
"driftingly/rector-laravel": "^2.4"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"Rector\\RectorCompatTests\\": "src"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Rector\\RectorCompatTests\\Tests\\": "tests",
21+
"Fixture\\": "fixture"
22+
}
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Fixture;
4+
5+
final class FixtureWithFuncCall
6+
{
7+
public function run(): int
8+
{
9+
return strlen('hello');
10+
}
11+
}

compat-tests/phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<testsuites>
10+
<testsuite name="Project Tests">
11+
<directory>tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

compat-tests/rector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\RectorCompatTests\Rector\UseGetArgRector;
7+
8+
return RectorConfig::configure()
9+
->withPaths([
10+
__DIR__ . '/src',
11+
__DIR__ . '/tests',
12+
__DIR__ . '/fixture',
13+
])
14+
->withRules([UseGetArgRector::class])
15+
->withTypeCoverageLevel(0)
16+
->withDeadCodeLevel(0)
17+
->withCodeQualityLevel(0);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\RectorCompatTests\PHPStan;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleError;
12+
use PHPStan\Rules\RuleErrorBuilder;
13+
14+
/**
15+
* @implements Rule<Class_>
16+
*/
17+
final class CustomPHPStanRule implements Rule
18+
{
19+
public const ERROR_MESSAGE = 'Class "%s" is not final.';
20+
21+
public function getNodeType(): string
22+
{
23+
return Class_::class;
24+
}
25+
26+
/**
27+
* @param Class_ $node
28+
* @return RuleError[]
29+
*/
30+
public function processNode(Node $node, Scope $scope): array
31+
{
32+
if ($node->isFinal()) {
33+
return [];
34+
}
35+
36+
$ruleErrorMessage = RuleErrorBuilder::message(
37+
sprintf(self::ERROR_MESSAGE, $node->name->toString())
38+
)->build();
39+
40+
return [$ruleErrorMessage];
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\RectorCompatTests\Rector;
6+
7+
use PhpParser\Modifiers;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Stmt\Class_;
10+
use Rector\Rector\AbstractRector;
11+
12+
final class MakeClassFinalRector extends AbstractRector
13+
{
14+
/**
15+
* @return array<class-string<Class_>>
16+
*/
17+
public function getNodeTypes(): array
18+
{
19+
return [Class_::class];
20+
}
21+
22+
/**
23+
* @param Class_ $node
24+
*/
25+
public function refactor(Node $node)
26+
{
27+
if ($node->isFinal()) {
28+
return null;
29+
}
30+
31+
$node->flags |= Modifiers::FINAL;
32+
33+
return $node;
34+
}
35+
}

0 commit comments

Comments
 (0)