Updated Rector to commit 86ccf3f6854c78c6b7b06b8471d922724cb6542b

86ccf3f685 [Testing] Use own FixtureFileFinder to keep dependency low (#2858)
This commit is contained in:
Tomas Votruba 2022-08-30 16:19:25 +00:00
parent eb540c7f2a
commit d197e99c8b
11 changed files with 144 additions and 33 deletions

View File

@ -8,7 +8,11 @@ use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* Only for testing, @todo move to testing
*/
final class FileInfoParser
{
/**
@ -34,9 +38,13 @@ final class FileInfoParser
}
/**
* @return Stmt[]
* @param \Symplify\SmartFileSystem\SmartFileInfo|string $smartFileInfo
*/
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $smartFileInfo) : array
public function parseFileInfoToNodesAndDecorate($smartFileInfo) : array
{
if (\is_string($smartFileInfo)) {
$smartFileInfo = FixtureTempFileDumper::dump($smartFileInfo);
}
$stmts = $this->rectorParser->parseFile($smartFileInfo);
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$file = new File($smartFileInfo, $smartFileInfo->getContents());

View File

@ -0,0 +1,33 @@
<?php
declare (strict_types=1);
namespace Rector\Testing\Fixture;
use Iterator;
use RectorPrefix202208\Symfony\Component\Finder\Finder;
use RectorPrefix202208\Symfony\Component\Finder\SplFileInfo;
use Symplify\SmartFileSystem\SmartFileInfo;
final class FixtureFileFinder
{
/**
* @return Iterator<array<int, SmartFileInfo>>
*/
public static function yieldDirectory(string $directory, string $suffix = '*.php.inc') : Iterator
{
$fileInfos = self::findFilesInDirectory($directory, $suffix);
foreach ($fileInfos as $fileInfo) {
// @todo is this one needed? maybe symfony is good enough :)
$smartFileInfo = new SmartFileInfo($fileInfo->getRealPath());
(yield [$smartFileInfo]);
}
}
/**
* @return SplFileInfo[]
*/
private static function findFilesInDirectory(string $directory, string $suffix) : array
{
$finder = (new Finder())->in($directory)->files()->name($suffix);
$fileInfos = \iterator_to_array($finder);
return \array_values($fileInfos);
}
}

View File

@ -0,0 +1,25 @@
<?php
declare (strict_types=1);
namespace Rector\Testing\Fixture;
use RectorPrefix202208\Nette\Utils\FileSystem;
use RectorPrefix202208\Nette\Utils\Strings;
use RectorPrefix202208\Webmozart\Assert\Assert;
final class FixtureSplitter
{
/**
* @var string
* @see https://regex101.com/r/zZDoyy/1
*/
public const SPLIT_LINE_REGEX = '#\\-\\-\\-\\-\\-\\r?\\n#';
/**
* @return array<string, string>
*/
public static function loadFileAndSplitInputAndExpected(string $filePath) : array
{
Assert::fileExists($filePath);
$fixtureFileContents = FileSystem::read($filePath);
return Strings::split($fixtureFileContents, self::SPLIT_LINE_REGEX);
}
}

View File

@ -0,0 +1,21 @@
<?php
declare (strict_types=1);
namespace Rector\Testing\Fixture;
use RectorPrefix202208\Nette\Utils\FileSystem;
use Symplify\SmartFileSystem\SmartFileInfo;
final class FixtureTempFileDumper
{
/**
* @var string
*/
public const TEMP_FIXTURE_DIRECTORY = '/rector/tests_fixture_';
public static function dump(string $fileContents, string $suffix = 'php') : SmartFileInfo
{
// the "php" suffix is important, because that will hook into \Rector\Core\Application\FileProcessor\PhpFileProcessor
$temporaryFileName = \sys_get_temp_dir() . self::TEMP_FIXTURE_DIRECTORY . '/' . \md5($fileContents) . '.' . $suffix;
FileSystem::write($temporaryFileName, $fileContents);
return new SmartFileInfo($temporaryFileName);
}
}

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Testing\PHPUnit;
use Iterator;
use RectorPrefix202208\Nette\Utils\Strings;
use PHPStan\Analyser\NodeScopeResolver;
use PHPUnit\Framework\ExpectationFailedException;
use RectorPrefix202208\Psr\Container\ContainerInterface;
@ -16,10 +17,12 @@ use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Testing\Contract\RectorTestInterface;
use Rector\Testing\Fixture\FixtureFileFinder;
use Rector\Testing\Fixture\FixtureSplitter;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use Rector\Testing\PHPUnit\Behavior\MovingFilesTrait;
use RectorPrefix202208\Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use SplFileInfo;
use RectorPrefix202208\Symplify\EasyTesting\DataProvider\StaticFixtureUpdater;
use RectorPrefix202208\Symplify\EasyTesting\StaticFixtureSplitter;
use RectorPrefix202208\Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTestCase implements RectorTestInterface
@ -59,6 +62,7 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
$this->applicationFileProcessor = $this->getService(ApplicationFileProcessor::class);
$this->parameterProvider = $this->getService(ParameterProvider::class);
$this->dynamicSourceLocatorProvider = $this->getService(DynamicSourceLocatorProvider::class);
// restore added and removed files to 0
$this->removedAndAddedFilesCollector = $this->getService(RemovedAndAddedFilesCollector::class);
$this->removedAndAddedFilesCollector->reset();
/** @var AdditionalAutoloader $additionalAutoloader */
@ -79,30 +83,43 @@ abstract class AbstractRectorTestCase extends \Rector\Testing\PHPUnit\AbstractTe
*/
protected function yieldFilesFromDirectory(string $directory, string $suffix = '*.php.inc') : Iterator
{
return StaticFixtureFinder::yieldDirectoryExclusively($directory, $suffix);
}
/**
* @return Iterator<string, array<int, SmartFileInfo>>
*/
protected function yieldFilesWithPathnameFromDirectory(string $directory, string $suffix = '*.php.inc') : Iterator
{
return StaticFixtureFinder::yieldDirectoryExclusivelyWithRelativePathname($directory, $suffix);
return FixtureFileFinder::yieldDirectory($directory, $suffix);
}
protected function isWindows() : bool
{
return \strncasecmp(\PHP_OS, 'WIN', 3) === 0;
}
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo, bool $allowMatches = \true) : void
protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo)
{
$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo);
$inputFileInfo = $inputFileInfoAndExpectedFileInfo->getInputFileInfo();
if (Strings::match($fixtureFileInfo->getContents(), FixtureSplitter::SPLIT_LINE_REGEX)) {
// changed content
[$inputFileContents, $expectedFileContents] = FixtureSplitter::loadFileAndSplitInputAndExpected($fixtureFileInfo->getRealPath());
} else {
// no change
$inputFileContents = $fixtureFileInfo->getContents();
$expectedFileContents = $fixtureFileInfo->getContents();
}
$fileSuffix = $this->resolveOriginalFixtureFileSuffix($fixtureFileInfo);
$inputFileInfo = FixtureTempFileDumper::dump($inputFileContents, $fileSuffix);
$expectedFileInfo = FixtureTempFileDumper::dump($expectedFileContents, $fileSuffix);
$this->originalTempFileInfo = $inputFileInfo;
$expectedFileInfo = $inputFileInfoAndExpectedFileInfo->getExpectedFileInfo();
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo, $allowMatches);
$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo);
//, $allowMatches);
}
protected function getFixtureTempDirectory() : string
{
return \sys_get_temp_dir() . '/_temp_fixture_easy_testing';
return \sys_get_temp_dir() . FixtureTempFileDumper::TEMP_FIXTURE_DIRECTORY;
}
private function resolveOriginalFixtureFileSuffix(SplFileInfo $splFileInfo) : string
{
$fixtureRealPath = $splFileInfo->getRealPath();
if (\substr_compare($fixtureRealPath, '.inc', -\strlen('.inc')) === 0) {
$fixtureRealPath = \rtrim($fixtureRealPath, '.inc');
}
if (\substr_compare($fixtureRealPath, '.blade.php', -\strlen('.blade.php')) === 0) {
return 'blade.php';
}
return \pathinfo($fixtureRealPath, \PATHINFO_EXTENSION);
}
private function includePreloadFilesAndScoperAutoload() : void
{

View File

@ -6,6 +6,7 @@ namespace Rector\Testing\PHPUnit\Behavior;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\PhpParser\Printer\NodesWithFileDestinationPrinter;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Testing\Fixture\FixtureTempFileDumper;
use RectorPrefix202208\Webmozart\Assert\Assert;
/**
* @property-read RemovedAndAddedFilesCollector $removedAndAddedFilesCollector
@ -34,8 +35,8 @@ trait MovingFilesTrait
* to make work in all OSs, for example:
* In MacOS, the realpath() of sys_get_temp_dir() pointed to /private/var/* which symlinked of /var/*
*/
[, $expectedFilePathWithContentFilePath] = \explode('_temp_fixture_easy_testing', $expectedFilePathWithContent->getFilePath());
[, $addedFilePathWithContentFilePath] = \explode('_temp_fixture_easy_testing', $addedFilePathWithContent->getFilePath());
[, $expectedFilePathWithContentFilePath] = \explode(FixtureTempFileDumper::TEMP_FIXTURE_DIRECTORY, $expectedFilePathWithContent->getFilePath());
[, $addedFilePathWithContentFilePath] = \explode(FixtureTempFileDumper::TEMP_FIXTURE_DIRECTORY, $addedFilePathWithContent->getFilePath());
$this->assertSame($expectedFilePathWithContentFilePath, $addedFilePathWithContentFilePath);
$this->assertSame($expectedFilePathWithContent->getFileContent(), $addedFilePathWithContent->getFileContent());
}

View File

@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'b01ea044294135d9ae9546939d66b8181af50940';
public const PACKAGE_VERSION = '86ccf3f6854c78c6b7b06b8471d922724cb6542b';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-08-30 14:59:35';
public const RELEASE_DATE = '2022-08-30 16:15:10';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6::getLoader();
return ComposerAutoloaderInitd0502117be6e6d45dc8ce25866585357::getLoader();

View File

@ -2703,6 +2703,9 @@ return array(
'Rector\\Symfony\\ValueObject\\ValidatorAssert\\ClassMethodAndAnnotation' => $vendorDir . '/rector/rector-symfony/src/ValueObject/ValidatorAssert/ClassMethodAndAnnotation.php',
'Rector\\Symfony\\ValueObject\\ValidatorAssert\\PropertyAndAnnotation' => $vendorDir . '/rector/rector-symfony/src/ValueObject/ValidatorAssert/PropertyAndAnnotation.php',
'Rector\\Testing\\Contract\\RectorTestInterface' => $baseDir . '/packages/Testing/Contract/RectorTestInterface.php',
'Rector\\Testing\\Fixture\\FixtureFileFinder' => $baseDir . '/packages/Testing/Fixture/FixtureFileFinder.php',
'Rector\\Testing\\Fixture\\FixtureSplitter' => $baseDir . '/packages/Testing/Fixture/FixtureSplitter.php',
'Rector\\Testing\\Fixture\\FixtureTempFileDumper' => $baseDir . '/packages/Testing/Fixture/FixtureTempFileDumper.php',
'Rector\\Testing\\PHPUnit\\AbstractRectorTestCase' => $baseDir . '/packages/Testing/PHPUnit/AbstractRectorTestCase.php',
'Rector\\Testing\\PHPUnit\\AbstractTestCase' => $baseDir . '/packages/Testing/PHPUnit/AbstractTestCase.php',
'Rector\\Testing\\PHPUnit\\Behavior\\MovingFilesTrait' => $baseDir . '/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6
class ComposerAutoloaderInitd0502117be6e6d45dc8ce25866585357
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitd0502117be6e6d45dc8ce25866585357', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitd0502117be6e6d45dc8ce25866585357', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitedf27f069b71e894f79831b06b417de6::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitd0502117be6e6d45dc8ce25866585357::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInitedf27f069b71e894f79831b06b417de6::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInitd0502117be6e6d45dc8ce25866585357::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequireedf27f069b71e894f79831b06b417de6($fileIdentifier, $file);
composerRequired0502117be6e6d45dc8ce25866585357($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInitedf27f069b71e894f79831b06b417de6
* @param string $file
* @return void
*/
function composerRequireedf27f069b71e894f79831b06b417de6($fileIdentifier, $file)
function composerRequired0502117be6e6d45dc8ce25866585357($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInitedf27f069b71e894f79831b06b417de6
class ComposerStaticInitd0502117be6e6d45dc8ce25866585357
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -2988,6 +2988,9 @@ class ComposerStaticInitedf27f069b71e894f79831b06b417de6
'Rector\\Symfony\\ValueObject\\ValidatorAssert\\ClassMethodAndAnnotation' => __DIR__ . '/..' . '/rector/rector-symfony/src/ValueObject/ValidatorAssert/ClassMethodAndAnnotation.php',
'Rector\\Symfony\\ValueObject\\ValidatorAssert\\PropertyAndAnnotation' => __DIR__ . '/..' . '/rector/rector-symfony/src/ValueObject/ValidatorAssert/PropertyAndAnnotation.php',
'Rector\\Testing\\Contract\\RectorTestInterface' => __DIR__ . '/../..' . '/packages/Testing/Contract/RectorTestInterface.php',
'Rector\\Testing\\Fixture\\FixtureFileFinder' => __DIR__ . '/../..' . '/packages/Testing/Fixture/FixtureFileFinder.php',
'Rector\\Testing\\Fixture\\FixtureSplitter' => __DIR__ . '/../..' . '/packages/Testing/Fixture/FixtureSplitter.php',
'Rector\\Testing\\Fixture\\FixtureTempFileDumper' => __DIR__ . '/../..' . '/packages/Testing/Fixture/FixtureTempFileDumper.php',
'Rector\\Testing\\PHPUnit\\AbstractRectorTestCase' => __DIR__ . '/../..' . '/packages/Testing/PHPUnit/AbstractRectorTestCase.php',
'Rector\\Testing\\PHPUnit\\AbstractTestCase' => __DIR__ . '/../..' . '/packages/Testing/PHPUnit/AbstractTestCase.php',
'Rector\\Testing\\PHPUnit\\Behavior\\MovingFilesTrait' => __DIR__ . '/../..' . '/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php',
@ -3192,9 +3195,9 @@ class ComposerStaticInitedf27f069b71e894f79831b06b417de6
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitedf27f069b71e894f79831b06b417de6::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitd0502117be6e6d45dc8ce25866585357::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd0502117be6e6d45dc8ce25866585357::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd0502117be6e6d45dc8ce25866585357::$classMap;
}, null, ClassLoader::class);
}