Fix AUTOLOAD_PATHS and BOOTSTRAP_FILES in tests (#6239)

This commit is contained in:
Tomas Votruba 2021-04-25 11:23:15 +02:00 committed by GitHub
parent 31c06b2fcb
commit 414a656ab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 16 deletions

View File

@ -11,6 +11,8 @@ use PHPUnit\Framework\ExpectationFailedException;
use Psr\Container\ContainerInterface;
use Rector\Core\Application\ApplicationFileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Autoloading\AdditionalAutoloader;
use Rector\Core\Autoloading\BootstrapFilesIncluder;
use Rector\Core\Bootstrap\RectorConfigsResolver;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\Option;
@ -76,6 +78,14 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
$this->removedAndAddedFilesCollector = $this->getService(RemovedAndAddedFilesCollector::class);
$this->removedAndAddedFilesCollector->reset();
/** @var AdditionalAutoloader $additionalAutoloader */
$additionalAutoloader = $this->getService(AdditionalAutoloader::class);
$additionalAutoloader->autoloadPaths();
/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $this->getService(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
/** @var Configuration $configuration */
$configuration = $this->getService(Configuration::class);
$configuration->setIsDryRun(true);

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
@ -147,7 +146,9 @@ CODE_SAMPLE
private function changeToAssign(Switch_ $switch, Match_ $match, Expr $assignExpr): Assign
{
$prevInitializedAssign = $this->betterNodeFinder->findFirstPreviousOfNode($switch, function (Node $node) use ($assignExpr): bool {
$prevInitializedAssign = $this->betterNodeFinder->findFirstPreviousOfNode($switch, function (Node $node) use (
$assignExpr
): bool {
return $node instanceof Assign && $this->nodeComparator->areNodesEqual($node->var, $assignExpr);
});

View File

@ -40,22 +40,12 @@ final class AdditionalAutoloader
$this->dynamicSourceLocatorDecorator = $dynamicSourceLocatorDecorator;
}
public function autoloadWithInputAndSource(InputInterface $input): void
public function autoloadInput(InputInterface $input): void
{
if ($input->hasOption(Option::OPTION_AUTOLOAD_FILE)) {
$this->autoloadInputAutoloadFile($input);
}
$autoloadPaths = $this->parameterProvider->provideArrayParameter(Option::AUTOLOAD_PATHS);
if ($autoloadPaths === []) {
if (! $input->hasOption(Option::OPTION_AUTOLOAD_FILE)) {
return;
}
$this->dynamicSourceLocatorDecorator->addPaths($autoloadPaths);
}
private function autoloadInputAutoloadFile(InputInterface $input): void
{
/** @var string|null $autoloadFile */
$autoloadFile = $input->getOption(Option::OPTION_AUTOLOAD_FILE);
if ($autoloadFile === null) {
@ -63,6 +53,16 @@ final class AdditionalAutoloader
}
$this->fileSystemGuard->ensureFileExists($autoloadFile, 'Extra autoload');
$this->dynamicSourceLocatorDecorator->addPaths([$autoloadFile]);
require_once $autoloadFile;
}
public function autoloadPaths(): void
{
$autoloadPaths = $this->parameterProvider->provideArrayParameter(Option::AUTOLOAD_PATHS);
if ($autoloadPaths === []) {
return;
}
$this->dynamicSourceLocatorDecorator->addPaths($autoloadPaths);
}
}

View File

@ -202,7 +202,9 @@ final class ProcessCommand extends Command
// register autoloaded and included files
$this->bootstrapFilesIncluder->includeBootstrapFiles();
$this->additionalAutoloader->autoloadWithInputAndSource($input);
$this->additionalAutoloader->autoloadInput($input);
$this->additionalAutoloader->autoloadPaths();
// PHPStan has to know about all files!
$this->configurePHPStanNodeScopeResolver($phpFileInfos);