use AbstractHttpKernelTest case and local Kernel over ContainerFactory

This commit is contained in:
Tomas Votruba 2019-02-23 00:08:58 +01:00
parent c5ee28cc2d
commit bfff23fbb2
17 changed files with 103 additions and 239 deletions

View File

@ -1,9 +1,10 @@
<?php declare(strict_types=1);
use Rector\DependencyInjection\ContainerFactory;
use Rector\HttpKernel\RectorKernel;
use Symfony\Component\Console\Input\ArgvInput;
use Symplify\PackageBuilder\Configuration\ConfigFileFinder;
use Symplify\PackageBuilder\Configuration\LevelFileFinder;
use Symplify\PackageBuilder\Console\Input\InputDetector;
$configFiles = [];
@ -18,9 +19,11 @@ $configFiles[] = ConfigFileFinder::provide('rector', ['rector.yml', 'rector.yaml
$configFiles = array_filter($configFiles);
// 3. Build DI container
$containerFactory = new ContainerFactory();
if ($configFiles) {
return $containerFactory->createWithConfigFiles($configFiles);
}
return $containerFactory->create();
$rectorKernel = new RectorKernel('prod', InputDetector::isDebug());
if ($configFiles) {
$rectorKernel->setConfigs($configFiles);
}
$rectorKernel->boot();
return $rectorKernel->getContainer();

View File

@ -5,7 +5,7 @@ services:
Rector\:
resource: '../src'
exclude: '../src/{Node/Attribute.php,Rector/**/*Rector.php,Reporting/FileDiff.php,Testing,RectorDefinition,PhpParser/Node/*Info.php,Exception/*,DependencyInjection/CompilerPass/*,DependencyInjection/Loader/*,Application/Error.php}'
exclude: '../src/{Node/Attribute.php,Rector/**/*Rector.php,Reporting/FileDiff.php,Testing,RectorDefinition,PhpParser/Node/*Info.php,Exception/*,DependencyInjection/CompilerPass/*,DependencyInjection/Loader/*,Application/Error.php,HttpKernel/RectorKernel.php}'
# extra services
Rector\Symfony\Rector\Form\Helper\FormTypeStringToTypeProvider: ~

View File

@ -133,6 +133,5 @@ parameters:
# 3rd party api
- 'src/PhpParser/Node/Value/ValueResolver.php'
SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff:
MissingTraversableParameterTypeHintSpecification:
- 'src/Testing/PHPUnit/AbstractRectorTestCase.php'
# SlevomatCodingStandard\Sniffs\TypeHints\TypeHintDeclarationSniff.MissingTraversableParameterTypeHintSpecification:
# - 'src/Testing/PHPUnit/AbstractRectorTestCase.php'

View File

@ -1,36 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\FileSystemRector\Tests;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\DependencyInjection\ContainerFactory;
use Rector\FileSystemRector\FileSystemFileProcessor;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
abstract class AbstractFileSystemRectorTest extends TestCase
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @var FileSystemFileProcessor
*/
private $fileSystemFileProcessor;
protected function setUp(): void
{
$this->container = (new ContainerFactory())->createWithConfigFiles([$this->provideConfig()]);
$this->fileSystemFileProcessor = $this->container->get(FileSystemFileProcessor::class);
}
protected function doTestFile(string $file): void
{
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($file));
}
abstract protected function provideConfig(): string;
}

View File

@ -18,6 +18,9 @@ final class WrapTransParameterNameRectorTest extends AbstractRectorTestCase
return WrapTransParameterNameRector::class;
}
/**
* @return mixed[]|null
*/
protected function getRectorConfiguration(): ?array
{
return ['$translatorClass' => SomeTranslator::class];

View File

@ -4,15 +4,16 @@ namespace Rector\NodeTypeResolver\Tests\PerNodeTypeResolver;
use PhpParser\Node;
use Rector\Configuration\Option;
use Rector\HttpKernel\RectorKernel;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Parser\Parser;
use Rector\Tests\AbstractContainerAwareTestCase;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
abstract class AbstractNodeTypeResolverTest extends AbstractContainerAwareTestCase
abstract class AbstractNodeTypeResolverTest extends AbstractKernelTestCase
{
/**
* @var NodeTypeResolver
@ -41,11 +42,13 @@ abstract class AbstractNodeTypeResolverTest extends AbstractContainerAwareTestCa
protected function setUp(): void
{
$this->betterNodeFinder = $this->container->get(BetterNodeFinder::class);
$this->parameterProvider = $this->container->get(ParameterProvider::class);
$this->nodeTypeResolver = $this->container->get(NodeTypeResolver::class);
$this->parser = $this->container->get(Parser::class);
$this->nodeScopeAndMetadataDecorator = $this->container->get(NodeScopeAndMetadataDecorator::class);
$this->bootKernel(RectorKernel::class);
$this->betterNodeFinder = self::$container->get(BetterNodeFinder::class);
$this->parameterProvider = self::$container->get(ParameterProvider::class);
$this->nodeTypeResolver = self::$container->get(NodeTypeResolver::class);
$this->parser = self::$container->get(Parser::class);
$this->nodeScopeAndMetadataDecorator = self::$container->get(NodeScopeAndMetadataDecorator::class);
}
/**

View File

@ -4,11 +4,12 @@ namespace Rector\NodeTypeResolver\Tests\PhpDoc\NodeAnalyzer;
use PhpParser\Comment\Doc;
use PhpParser\Node\Scalar\String_;
use Rector\HttpKernel\RectorKernel;
use Rector\NodeTypeResolver\Node\CurrentNodeProvider;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Tests\AbstractContainerAwareTestCase;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class DocBlockAnalyzerTest extends AbstractContainerAwareTestCase
final class DocBlockAnalyzerTest extends AbstractKernelTestCase
{
/**
* @var DocBlockAnalyzer
@ -22,8 +23,10 @@ final class DocBlockAnalyzerTest extends AbstractContainerAwareTestCase
protected function setUp(): void
{
$this->docBlockAnalyzer = $this->container->get(DocBlockAnalyzer::class);
$this->currentNodeProvider = $this->container->get(CurrentNodeProvider::class);
$this->bootKernel(RectorKernel::class);
$this->docBlockAnalyzer = self::$container->get(DocBlockAnalyzer::class);
$this->currentNodeProvider = self::$container->get(CurrentNodeProvider::class);
}
public function testHasAnnotation(): void

View File

@ -1,32 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\DependencyInjection;
use Psr\Container\ContainerInterface;
use Rector\HttpKernel\RectorKernel;
final class ContainerFactory
{
public function create(): ContainerInterface
{
$appKernel = new RectorKernel();
$appKernel->boot();
// this is require to keep CLI verbosity independent on AppKernel dev/prod mode
putenv('SHELL_VERBOSITY=0');
return $appKernel->getContainer();
}
/**
* @param string[] $configFiles
*/
public function createWithConfigFiles(array $configFiles): ContainerInterface
{
$appKernel = new RectorKernel($configFiles);
$appKernel->boot();
// this is require to keep CLI verbosity independent on AppKernel dev/prod mode
putenv('SHELL_VERBOSITY=0');
return $appKernel->getContainer();
}
}

View File

@ -11,36 +11,22 @@ use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\Kernel;
use Symplify\PackageBuilder\Contract\HttpKernel\ExtraConfigAwareKernelInterface;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutoBindParametersCompilerPass;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutoReturnFactoryCompilerPass;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireInterfacesCompilerPass;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireSinglyImplementedCompilerPass;
use Symplify\PackageBuilder\HttpKernel\SimpleKernelTrait;
final class RectorKernel extends Kernel
final class RectorKernel extends Kernel implements ExtraConfigAwareKernelInterface
{
use SimpleKernelTrait;
/**
* @var string[]
*/
private $extraConfigFiles = [];
/**
* @param string[] $configFiles
*/
public function __construct(array $configFiles = [])
{
$this->extraConfigFiles = $configFiles;
$configFilesHash = md5(serialize($configFiles));
// debug: require to invalidate container on service files change
parent::__construct('cli_' . $configFilesHash, true);
}
private $configs = [];
public function getCacheDir(): string
{
@ -52,11 +38,27 @@ final class RectorKernel extends Kernel
{
$loader->load(__DIR__ . '/../../config/config.yaml');
foreach ($this->extraConfigFiles as $extraConfigFile) {
$loader->load($extraConfigFile);
foreach ($this->configs as $config) {
$loader->load($config);
}
}
/**
* @param string[] $configs
*/
public function setConfigs(array $configs): void
{
$this->configs = $configs;
}
/**
* @return BundleInterface[]
*/
public function registerBundles(): iterable
{
return [];
}
protected function build(ContainerBuilder $containerBuilder): void
{
$containerBuilder->addCompilerPass(new RemoveExcludedRectorsCompilerPass());

View File

@ -5,17 +5,16 @@ namespace Rector\Testing\PHPUnit;
use Nette\Utils\FileSystem;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\Application\FileProcessor;
use Rector\Configuration\Option;
use Rector\DependencyInjection\ContainerFactory;
use Rector\Exception\ShouldNotHappenException;
use Rector\HttpKernel\RectorKernel;
use Symfony\Component\Yaml\Yaml;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
abstract class AbstractRectorTestCase extends TestCase
abstract class AbstractRectorTestCase extends AbstractKernelTestCase
{
/**
* @var string
@ -27,11 +26,6 @@ abstract class AbstractRectorTestCase extends TestCase
*/
protected $fileProcessor;
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ParameterProvider
*/
@ -42,16 +36,6 @@ abstract class AbstractRectorTestCase extends TestCase
*/
private $autoloadTestFixture = true;
/**
* @var ContainerInterface[]
*/
private static $containersPerConfig = [];
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
protected function setUp(): void
{
$configFile = $this->provideConfig();
@ -64,10 +48,10 @@ abstract class AbstractRectorTestCase extends TestCase
));
}
$this->createContainer($configFile);
$this->bootKernelWithConfigs(RectorKernel::class, [$configFile]);
$this->fileProcessor = $this->container->get(FileProcessor::class);
$this->parameterProvider = $this->container->get(ParameterProvider::class);
$this->fileProcessor = static::$container->get(FileProcessor::class);
$this->parameterProvider = static::$container->get(ParameterProvider::class);
}
/**
@ -94,9 +78,11 @@ abstract class AbstractRectorTestCase extends TestCase
return $configFileTempPath;
}
$yamlContent = Yaml::dump(['services' => [
$this->getRectorClass() => $this->getRectorConfiguration() ?: null,
]], Yaml::DUMP_OBJECT_AS_MAP);
$yamlContent = Yaml::dump([
'services' => [
$this->getRectorClass() => $this->getRectorConfiguration() ?: null,
],
], Yaml::DUMP_OBJECT_AS_MAP);
FileSystem::write($configFileTempPath, $yamlContent);
@ -135,18 +121,6 @@ abstract class AbstractRectorTestCase extends TestCase
$this->autoloadTestFixture = true;
}
private function createContainer(string $configFile): void
{
$key = md5_file($configFile);
if (isset(self::$containersPerConfig[$key])) {
$this->container = self::$containersPerConfig[$key];
} else {
$this->container = (new ContainerFactory())->createWithConfigFiles([$configFile]);
self::$containersPerConfig[$key] = $this->container;
}
}
/**
* @return string[]
*/

View File

@ -1,29 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Tests;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\DependencyInjection\ContainerFactory;
abstract class AbstractConfigurableContainerAwareTestCase extends TestCase
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* Constructs a test case with the given name.
*
* @param mixed[] $data
*/
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
$this->container = (new ContainerFactory())->createWithConfigFiles([$this->provideConfig()]);
parent::__construct($name, $data, $dataName);
}
abstract protected function provideConfig(): string;
}

View File

@ -1,38 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Tests;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Rector\DependencyInjection\ContainerFactory;
abstract class AbstractContainerAwareTestCase extends TestCase
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ContainerInterface
*/
private static $cachedContainer;
/**
* Constructs a test case with the given name.
*
* @param string|null $data
* @param mixed[] $data
* @param string $dataName
*/
public function __construct($name = null, array $data = [], $dataName = '')
{
if (self::$cachedContainer === null) {
self::$cachedContainer = (new ContainerFactory())->create();
}
$this->container = self::$cachedContainer;
parent::__construct($name, $data, $dataName);
}
}

View File

@ -3,9 +3,10 @@
namespace Rector\Tests\FileSystem\FilesFinder\ExcludePaths;
use Rector\FileSystem\FilesFinder;
use Rector\Tests\AbstractConfigurableContainerAwareTestCase;
use Rector\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class ExcludePathsTest extends AbstractConfigurableContainerAwareTestCase
final class ExcludePathsTest extends AbstractKernelTestCase
{
/**
* @var FilesFinder
@ -14,14 +15,11 @@ final class ExcludePathsTest extends AbstractConfigurableContainerAwareTestCase
public function testShouldFail(): void
{
$this->filesFinder = $this->container->get(FilesFinder::class);
$this->bootKernelWithConfigs(RectorKernel::class, [__DIR__ . '/config/config-with-excluded-paths.yaml']);
$this->filesFinder = self::$container->get(FilesFinder::class);
$splFileInfos = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source'], ['php']);
$this->assertCount(1, $splFileInfos);
}
protected function provideConfig(): string
{
return __DIR__ . '/config/config-with-excluded-paths.yaml';
}
}

View File

@ -4,10 +4,11 @@ namespace Rector\Tests\FileSystem\FilesFinder;
use Iterator;
use Rector\FileSystem\FilesFinder;
use Rector\Tests\AbstractContainerAwareTestCase;
use Rector\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class FilesFinderTest extends AbstractContainerAwareTestCase
final class FilesFinderTest extends AbstractKernelTestCase
{
/**
* @var FilesFinder
@ -16,7 +17,8 @@ final class FilesFinderTest extends AbstractContainerAwareTestCase
protected function setUp(): void
{
$this->filesFinder = $this->container->get(FilesFinder::class);
$this->bootKernel(RectorKernel::class);
$this->filesFinder = self::$container->get(FilesFinder::class);
}
/**

View File

@ -4,11 +4,12 @@ namespace Rector\Tests\PhpParser\Node\Value;
use PhpParser\BuilderFactory;
use PhpParser\Node\Name\FullyQualified;
use Rector\HttpKernel\RectorKernel;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Tests\AbstractContainerAwareTestCase;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class ValueResolverTest extends AbstractContainerAwareTestCase
final class ValueResolverTest extends AbstractKernelTestCase
{
/**
* @var ValueResolver
@ -17,7 +18,8 @@ final class ValueResolverTest extends AbstractContainerAwareTestCase
protected function setUp(): void
{
$this->valueResolver = $this->container->get(ValueResolver::class);
$this->bootKernel(RectorKernel::class);
$this->valueResolver = self::$container->get(ValueResolver::class);
}
public function test(): void

View File

@ -5,10 +5,11 @@ namespace Rector\Tests\PhpParser\Printer;
use Iterator;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Scalar\String_;
use Rector\HttpKernel\RectorKernel;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Tests\AbstractContainerAwareTestCase;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class BetterStandardPrinterTest extends AbstractContainerAwareTestCase
final class BetterStandardPrinterTest extends AbstractKernelTestCase
{
/**
* @var BetterStandardPrinter
@ -17,7 +18,8 @@ final class BetterStandardPrinterTest extends AbstractContainerAwareTestCase
protected function setUp(): void
{
$this->betterStandardPrinter = $this->container->get(BetterStandardPrinter::class);
$this->bootKernel(RectorKernel::class);
$this->betterStandardPrinter = self::$container->get(BetterStandardPrinter::class);
}
/**

View File

@ -3,12 +3,25 @@
namespace Rector\Tests\Rector\Psr4\MultipleClassFileToPsr4ClassesRector;
use Iterator;
use Rector\FileSystemRector\Tests\AbstractFileSystemRectorTest;
use Rector\FileSystemRector\FileSystemFileProcessor;
use Rector\HttpKernel\RectorKernel;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\PackageBuilder\FileSystem\SmartFileInfo;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemRectorTest
final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractKernelTestCase
{
/**
* @var FileSystemFileProcessor
*/
private $fileSystemFileProcessor;
protected function setUp(): void
{
$this->bootKernelWithConfigs(RectorKernel::class, [__DIR__ . '/config.yaml']);
$this->fileSystemFileProcessor = self::$container->get(FileSystemFileProcessor::class);
}
protected function tearDown(): void
{
if (! $this->getProvidedData()) {
@ -26,7 +39,7 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
*/
public function test(string $file, array $expectedExceptions): void
{
$this->doTestFile($file);
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo($file));
foreach ($expectedExceptions as $expectedExceptionLocation => $expectedFormat) {
$this->assertFileExists($expectedExceptionLocation);
@ -68,12 +81,7 @@ final class MultipleClassFileToPsr4ClassesRectorTest extends AbstractFileSystemR
{
$originalFileContent = (new SmartFileInfo(__DIR__ . '/Source/ReadyException.php'))->getContents();
$this->doTestFile(__DIR__ . '/Source/ReadyException.php');
$this->fileSystemFileProcessor->processFileInfo(new SmartFileInfo(__DIR__ . '/Source/ReadyException.php'));
$this->assertStringEqualsFile(__DIR__ . '/Source/ReadyException.php', $originalFileContent);
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yaml';
}
}