mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
Updated Rector to commit bc29f1bb5041f0e6b7eee4bb1b5e4aba2723b83e
bc29f1bb50
Update how_to_add_test_for_rector_rule.md (#473)
This commit is contained in:
parent
f679476193
commit
ff3d08c8e0
@ -1,11 +1,52 @@
|
|||||||
# How to Add Test for Rector Rule
|
# How to Add Test for Rector Rule
|
||||||
|
|
||||||
## 1. Detect the Rector Rule
|
## 1. Create an environment for testing
|
||||||
|
|
||||||
|
When using Rector to update your own code, you will typically be using release repository installed by composer, however, when adding tests, you will need to use the development repository as shown:
|
||||||
|
|
||||||
|
- Fork https://github.com/rectorphp/rector-src
|
||||||
|
- Clone it locally
|
||||||
|
- Install dependencies by executing `composer install`
|
||||||
|
- Tests your installation by executing `composer fix-cs` and `composer phpstan`
|
||||||
|
- Create a new branch for your test
|
||||||
|
- Add your test as described below. Note that the rector binary is located at `bin/rector` instead of the typical `vendor/bin/rector`
|
||||||
|
- Push the branch and create a new pull request to https://github.com/rectorphp/rector-src
|
||||||
|
|
||||||
|
Alternatively, the above may be performed on the CLI
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Authenticate
|
||||||
|
gh auth login
|
||||||
|
|
||||||
|
# Fork and clone the repository
|
||||||
|
gh repo fork https://github.com/rectorphp/rector-src
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
cd rector-src
|
||||||
|
composer install
|
||||||
|
|
||||||
|
# Test installation
|
||||||
|
composer fix-cs
|
||||||
|
composer phpstan
|
||||||
|
|
||||||
|
# Create and checkout a branch
|
||||||
|
git checkout -b your_branch_name
|
||||||
|
|
||||||
|
# Create test as described below
|
||||||
|
|
||||||
|
# Push the branch
|
||||||
|
git push -u origin your_branch_name
|
||||||
|
|
||||||
|
# create new pull request to https://github.com/rectorphp/rector-src
|
||||||
|
gh pr create --title "Your title text" --body "Your body text"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Detect the Rector Rule
|
||||||
|
|
||||||
Run Rector only on 1 directory, or better 1 file.
|
Run Rector only on 1 directory, or better 1 file.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
vendor/bin/rector process /some-file.php
|
bin/rector process /some-file.php
|
||||||
```
|
```
|
||||||
|
|
||||||
See "Applied rules" under the diff:
|
See "Applied rules" under the diff:
|
||||||
@ -16,7 +57,7 @@ Our rule in this example is: `Rector\Privatization\Rector\Class_\FinalizeClasses
|
|||||||
|
|
||||||
This rule's job is to add `final` to every class that has no children and is not a Doctrine entity = everywhere it can without breaking our code.
|
This rule's job is to add `final` to every class that has no children and is not a Doctrine entity = everywhere it can without breaking our code.
|
||||||
|
|
||||||
## 2. Detect the Minimal File
|
## 3. Detect the Minimal File
|
||||||
|
|
||||||
Usually, the Rector diff output is long and contains many other errors related to other rules. It's a mess; we can't use that for a test fixture. We need to find **1 responsible line**.
|
Usually, the Rector diff output is long and contains many other errors related to other rules. It's a mess; we can't use that for a test fixture. We need to find **1 responsible line**.
|
||||||
|
|
||||||
@ -33,12 +74,12 @@ class StaticEasyPrefixer
|
|||||||
Then rerun Rector to confirm:
|
Then rerun Rector to confirm:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
vendor/bin/rector process app/SomeFile.php
|
bin/rector process app/SomeFile.php
|
||||||
```
|
```
|
||||||
|
|
||||||
Do we have the same diff? Great!
|
Do we have the same diff? Great!
|
||||||
|
|
||||||
## 3. Find the Rector Test Case
|
## 4. Find the Rector Test Case
|
||||||
|
|
||||||
Now we need to find the test case. The test case name is rule + `Test` suffix.
|
Now we need to find the test case. The test case name is rule + `Test` suffix.
|
||||||
|
|
||||||
@ -56,7 +97,7 @@ Right here:
|
|||||||
|
|
||||||
![Rule Test Case](/docs/images/docs_rule_test_case.png)
|
![Rule Test Case](/docs/images/docs_rule_test_case.png)
|
||||||
|
|
||||||
## 4. Add Change or No-Change Test Fixture File
|
## 5. Add Change or No-Change Test Fixture File
|
||||||
|
|
||||||
Next to the test case, there is `/Fixture` directory. It contains many test fixture files that verified the Rector rule work correctly in all possible cases.
|
Next to the test case, there is `/Fixture` directory. It contains many test fixture files that verified the Rector rule work correctly in all possible cases.
|
||||||
|
|
||||||
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public const PACKAGE_VERSION = '1478d8eb78069114e5db2b4ac4ac429318690ff1';
|
public const PACKAGE_VERSION = 'bc29f1bb5041f0e6b7eee4bb1b5e4aba2723b83e';
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public const RELEASE_DATE = '2021-07-21 10:50:55';
|
public const RELEASE_DATE = '2021-07-21 11:04:08';
|
||||||
public static function resolvePackageVersion() : string
|
public static function resolvePackageVersion() : string
|
||||||
{
|
{
|
||||||
$process = new \RectorPrefix20210721\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
$process = new \RectorPrefix20210721\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -4,4 +4,4 @@
|
|||||||
|
|
||||||
require_once __DIR__ . '/composer/autoload_real.php';
|
require_once __DIR__ . '/composer/autoload_real.php';
|
||||||
|
|
||||||
return ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55::getLoader();
|
return ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c::getLoader();
|
||||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
// autoload_real.php @generated by Composer
|
// autoload_real.php @generated by Composer
|
||||||
|
|
||||||
class ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55
|
class ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c
|
||||||
{
|
{
|
||||||
private static $loader;
|
private static $loader;
|
||||||
|
|
||||||
@ -22,15 +22,15 @@ class ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55
|
|||||||
return self::$loader;
|
return self::$loader;
|
||||||
}
|
}
|
||||||
|
|
||||||
spl_autoload_register(array('ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55', 'loadClassLoader'), true, true);
|
spl_autoload_register(array('ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c', 'loadClassLoader'), true, true);
|
||||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
|
||||||
spl_autoload_unregister(array('ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55', 'loadClassLoader'));
|
spl_autoload_unregister(array('ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c', 'loadClassLoader'));
|
||||||
|
|
||||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||||
if ($useStaticLoader) {
|
if ($useStaticLoader) {
|
||||||
require __DIR__ . '/autoload_static.php';
|
require __DIR__ . '/autoload_static.php';
|
||||||
|
|
||||||
call_user_func(\Composer\Autoload\ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55::getInitializer($loader));
|
call_user_func(\Composer\Autoload\ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c::getInitializer($loader));
|
||||||
} else {
|
} else {
|
||||||
$classMap = require __DIR__ . '/autoload_classmap.php';
|
$classMap = require __DIR__ . '/autoload_classmap.php';
|
||||||
if ($classMap) {
|
if ($classMap) {
|
||||||
@ -42,19 +42,19 @@ class ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55
|
|||||||
$loader->register(true);
|
$loader->register(true);
|
||||||
|
|
||||||
if ($useStaticLoader) {
|
if ($useStaticLoader) {
|
||||||
$includeFiles = Composer\Autoload\ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55::$files;
|
$includeFiles = Composer\Autoload\ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c::$files;
|
||||||
} else {
|
} else {
|
||||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||||
}
|
}
|
||||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||||
composerRequire74490aa03b8b75ddc474464fa1fbdf55($fileIdentifier, $file);
|
composerRequirea8d0a8b90e75c2db128be55f184dad8c($fileIdentifier, $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $loader;
|
return $loader;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function composerRequire74490aa03b8b75ddc474464fa1fbdf55($fileIdentifier, $file)
|
function composerRequirea8d0a8b90e75c2db128be55f184dad8c($fileIdentifier, $file)
|
||||||
{
|
{
|
||||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||||
require $file;
|
require $file;
|
||||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
namespace Composer\Autoload;
|
namespace Composer\Autoload;
|
||||||
|
|
||||||
class ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55
|
class ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c
|
||||||
{
|
{
|
||||||
public static $files = array (
|
public static $files = array (
|
||||||
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
|
||||||
@ -3841,9 +3841,9 @@ class ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55
|
|||||||
public static function getInitializer(ClassLoader $loader)
|
public static function getInitializer(ClassLoader $loader)
|
||||||
{
|
{
|
||||||
return \Closure::bind(function () use ($loader) {
|
return \Closure::bind(function () use ($loader) {
|
||||||
$loader->prefixLengthsPsr4 = ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55::$prefixLengthsPsr4;
|
$loader->prefixLengthsPsr4 = ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c::$prefixLengthsPsr4;
|
||||||
$loader->prefixDirsPsr4 = ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55::$prefixDirsPsr4;
|
$loader->prefixDirsPsr4 = ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c::$prefixDirsPsr4;
|
||||||
$loader->classMap = ComposerStaticInit74490aa03b8b75ddc474464fa1fbdf55::$classMap;
|
$loader->classMap = ComposerStaticInita8d0a8b90e75c2db128be55f184dad8c::$classMap;
|
||||||
|
|
||||||
}, null, ClassLoader::class);
|
}, null, ClassLoader::class);
|
||||||
}
|
}
|
||||||
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
|||||||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||||
spl_autoload_call('RectorPrefix20210721\AutoloadIncluder');
|
spl_autoload_call('RectorPrefix20210721\AutoloadIncluder');
|
||||||
}
|
}
|
||||||
if (!class_exists('ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55', false) && !interface_exists('ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55', false) && !trait_exists('ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55', false)) {
|
if (!class_exists('ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c', false) && !interface_exists('ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c', false) && !trait_exists('ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c', false)) {
|
||||||
spl_autoload_call('RectorPrefix20210721\ComposerAutoloaderInit74490aa03b8b75ddc474464fa1fbdf55');
|
spl_autoload_call('RectorPrefix20210721\ComposerAutoloaderInita8d0a8b90e75c2db128be55f184dad8c');
|
||||||
}
|
}
|
||||||
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
|
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
|
||||||
spl_autoload_call('RectorPrefix20210721\Doctrine\Inflector\Inflector');
|
spl_autoload_call('RectorPrefix20210721\Doctrine\Inflector\Inflector');
|
||||||
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
|
|||||||
return \RectorPrefix20210721\print_node(...func_get_args());
|
return \RectorPrefix20210721\print_node(...func_get_args());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!function_exists('composerRequire74490aa03b8b75ddc474464fa1fbdf55')) {
|
if (!function_exists('composerRequirea8d0a8b90e75c2db128be55f184dad8c')) {
|
||||||
function composerRequire74490aa03b8b75ddc474464fa1fbdf55() {
|
function composerRequirea8d0a8b90e75c2db128be55f184dad8c() {
|
||||||
return \RectorPrefix20210721\composerRequire74490aa03b8b75ddc474464fa1fbdf55(...func_get_args());
|
return \RectorPrefix20210721\composerRequirea8d0a8b90e75c2db128be55f184dad8c(...func_get_args());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!function_exists('parseArgs')) {
|
if (!function_exists('parseArgs')) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user