From d0099c441814d2aae829168a1cdac764c754e5a0 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 15 Dec 2023 22:51:38 +0100 Subject: [PATCH 1/7] cleanup --- composer.json | 11 ++++------- phpstan.neon | 5 ----- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index b4aa4f601f3..8897ea54370 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "symfony/console": "^6.3", "symfony/dependency-injection": "6.1.*", "symfony/expression-language": "^6.2", + "symfony/finder": "^6.4", "symfony/yaml": "^6.2", "symplify/package-builder": "^11.3", "symplify/php-config-printer": "^11.1", @@ -24,17 +25,13 @@ "phpstan/extension-installer": "^1.2", "phpstan/phpstan": "^1.9", "phpunit/phpunit": "^10.0", - "rector/rector": "^0.15.10", + "rector/rector": "^0.18", "symfony/framework-bundle": "^6.1", - "symplify/easy-ci": "^11.1", - "symplify/easy-coding-standard": "^11.1", + "symplify/easy-coding-standard": "^12.0", "symplify/easy-testing": "^11.1", - "symplify/monorepo-builder": "^11.2", "symplify/phpstan-extensions": "^11.1", "symplify/vendor-patches": "^11.2", - "tomasvotruba/class-leak": "0.1.1.72", - "tomasvotruba/type-coverage": "^0.2", - "tomasvotruba/unused-public": "^0.1" + "tomasvotruba/class-leak": "^0.2" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon index ba4806435e9..02c9e3ebb77 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,11 +10,6 @@ parameters: - '*/tests/**/Source/*' - '*/tests/**/Fixture/*' - unused_public: - methods: true - properties: true - constants: true - ignoreErrors: - message: '#Parameter \#1 \$commandName of method Symfony\\Component\\Console\\Application\:\:setDefaultCommand\(\) expects string, string\|null given#' From 77c9c949347bb294ad7efa65382d3b5336580ab9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 15 Dec 2023 22:53:11 +0100 Subject: [PATCH 2/7] bump --- composer.json | 13 ++--- ...scoveryDirectoryTolerantYamlFileLoader.php | 2 +- src/Finder/ConfigFileFinder.php | 22 ++++----- src/Kernel/ConfigTransformerKernel.php | 25 ++++++---- src/ValueObject/ConvertedContent.php | 3 +- tests/AbstractTestCase.php | 21 ++++++++ .../AbstractConfigFormatConverterTestCase.php | 38 +++++++++++--- .../XmlToPhp/XmlToPhpTest.php | 10 ++-- .../YamlToPhp/YamlToPhpTest.php | 49 +++++++++---------- .../YamlToPhpConverterTest.php | 10 ++-- .../ConfigFileFinder/ConfigFileFinderTest.php | 26 +++++++--- tests/Helper/FixtureFinder.php | 26 ++++++++++ tests/Helper/FixtureSplitter.php | 10 ++++ 13 files changed, 172 insertions(+), 83 deletions(-) create mode 100644 tests/AbstractTestCase.php create mode 100644 tests/Helper/FixtureFinder.php create mode 100644 tests/Helper/FixtureSplitter.php diff --git a/composer.json b/composer.json index 8897ea54370..16807bd5157 100644 --- a/composer.json +++ b/composer.json @@ -9,15 +9,13 @@ "php": ">=8.1", "ext-dom": "*", "nette/utils": "^3.2", - "nikic/php-parser": "^4.17.1", - "symfony/console": "^6.3", + "nikic/php-parser": "^4.18", + "symfony/console": "^6.4", "symfony/dependency-injection": "6.1.*", - "symfony/expression-language": "^6.2", + "symfony/expression-language": "^6.4", "symfony/finder": "^6.4", - "symfony/yaml": "^6.2", - "symplify/package-builder": "^11.3", - "symplify/php-config-printer": "^11.1", - "symplify/symplify-kernel": "^11.1" + "symfony/yaml": "^6.4", + "symplify/php-config-printer": "^11.2" }, "require-dev": { "cweagans/composer-patches": "^1.7", @@ -28,7 +26,6 @@ "rector/rector": "^0.18", "symfony/framework-bundle": "^6.1", "symplify/easy-coding-standard": "^12.0", - "symplify/easy-testing": "^11.1", "symplify/phpstan-extensions": "^11.1", "symplify/vendor-patches": "^11.2", "tomasvotruba/class-leak": "^0.2" diff --git a/src/DependencyInjection/Loader/MissingAutodiscoveryDirectoryTolerantYamlFileLoader.php b/src/DependencyInjection/Loader/MissingAutodiscoveryDirectoryTolerantYamlFileLoader.php index 33a0d864f14..ff9dab0aab2 100644 --- a/src/DependencyInjection/Loader/MissingAutodiscoveryDirectoryTolerantYamlFileLoader.php +++ b/src/DependencyInjection/Loader/MissingAutodiscoveryDirectoryTolerantYamlFileLoader.php @@ -15,6 +15,6 @@ public function registerClasses( string $resource, string|array $exclude = null ): void { - // skip laoding classes, as the resource might not exist and invoke autoloading + // skip loading classes, as the resource might not exist and invoke autoloading } } diff --git a/src/Finder/ConfigFileFinder.php b/src/Finder/ConfigFileFinder.php index b41e7936925..3402330b75e 100644 --- a/src/Finder/ConfigFileFinder.php +++ b/src/Finder/ConfigFileFinder.php @@ -4,9 +4,8 @@ namespace Symplify\ConfigTransformer\Finder; -use Symplify\ConfigTransformer\ValueObject\Configuration; -use Symplify\SmartFileSystem\Finder\SmartFinder; -use Symplify\SmartFileSystem\SmartFileInfo; +use SplFileInfo; +use Symfony\Component\Finder\Finder; final class ConfigFileFinder { @@ -16,16 +15,17 @@ final class ConfigFileFinder */ private const CONFIG_SUFFIXES_REGEX = '#\.(yml|yaml|xml)$#'; - public function __construct( - private readonly SmartFinder $smartFinder - ) { - } - /** - * @return SmartFileInfo[] + * @param string[] $sources + * @return SplFileInfo[] */ - public function findFileInfos(Configuration $configuration): array + public function findFileInfos(array $sources): array { - return $this->smartFinder->find($configuration->getSources(), self::CONFIG_SUFFIXES_REGEX); + $finder = new Finder(); + $finder->files() + ->in($sources) + ->name(self::CONFIG_SUFFIXES_REGEX); + + return iterator_to_array($finder->getIterator()); } } diff --git a/src/Kernel/ConfigTransformerKernel.php b/src/Kernel/ConfigTransformerKernel.php index 0695b463002..a6b09a9fb3f 100644 --- a/src/Kernel/ConfigTransformerKernel.php +++ b/src/Kernel/ConfigTransformerKernel.php @@ -4,20 +4,25 @@ namespace Symplify\ConfigTransformer\Kernel; -use Psr\Container\ContainerInterface; +use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\HttpKernel\Kernel; use Symplify\PhpConfigPrinter\ValueObject\PhpConfigPrinterConfig; -use Symplify\SymplifyKernel\HttpKernel\AbstractSymplifyKernel; -final class ConfigTransformerKernel extends AbstractSymplifyKernel +final class ConfigTransformerKernel extends Kernel { - /** - * @param string[] $configFiles - */ - public function createFromConfigs(array $configFiles): ContainerInterface + public function __construct() { - $configFiles[] = __DIR__ . '/../../config/config.php'; - $configFiles[] = PhpConfigPrinterConfig::FILE_PATH; + parent::__construct('dev', true); + } - return $this->create($configFiles); + public function registerBundles(): iterable + { + return []; + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(__DIR__ . '/../../config/config.php'); + $loader->load(PhpConfigPrinterConfig::FILE_PATH); } } diff --git a/src/ValueObject/ConvertedContent.php b/src/ValueObject/ConvertedContent.php index a59965c16e2..a8adba74647 100644 --- a/src/ValueObject/ConvertedContent.php +++ b/src/ValueObject/ConvertedContent.php @@ -5,13 +5,12 @@ namespace Symplify\ConfigTransformer\ValueObject; use Nette\Utils\Strings; -use Symplify\SmartFileSystem\SmartFileInfo; final class ConvertedContent { public function __construct( private readonly string $convertedContent, - private readonly SmartFileInfo $originalFileInfo + private readonly \SplFileInfo $originalFileInfo ) { } diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php new file mode 100644 index 00000000000..fc3496c7602 --- /dev/null +++ b/tests/AbstractTestCase.php @@ -0,0 +1,21 @@ +boot(); + + $this->container = $configTransformerKernel->getContainer(); + } +} diff --git a/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php b/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php index b8a2783548a..b3bcdd941f6 100644 --- a/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php +++ b/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php @@ -4,37 +4,63 @@ namespace Symplify\ConfigTransformer\Tests\Converter\ConfigFormatConverter; +use Nette\Utils\FileSystem; use Symplify\ConfigTransformer\Converter\ConfigFormatConverter; +<<<<<<< HEAD use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; +<<<<<<< HEAD +======= +use Symplify\ConfigTransformer\Tests\AbstractTestCase; +<<<<<<< HEAD +<<<<<<< HEAD +>>>>>>> af077e6 (fixup! bump) +======= +use Symplify\ConfigTransformer\Tests\AbstractTestCase; +>>>>>>> cd67f40 (bump) use Symplify\EasyTesting\DataProvider\StaticFixtureUpdater; use Symplify\EasyTesting\StaticFixtureSplitter; -use Symplify\PackageBuilder\Testing\AbstractKernelTestCase; use Symplify\SmartFileSystem\SmartFileInfo; use Symplify\SmartFileSystem\SmartFileSystem; +======= +>>>>>>> 19a2662 (fixup! fixup! bump) +======= +use Symplify\ConfigTransformer\Tests\Helper\FixtureSplitter; +>>>>>>> 257b8ab (fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! bump) -abstract class AbstractConfigFormatConverterTestCase extends AbstractKernelTestCase +abstract class AbstractConfigFormatConverterTestCase extends AbstractTestCase { protected ConfigFormatConverter $configFormatConverter; - protected SmartFileSystem $smartFileSystem; + protected FileSystem $smartFileSystem; protected function setUp(): void { +<<<<<<< HEAD +<<<<<<< HEAD $this->bootKernel(ConfigTransformerKernel::class); $this->configFormatConverter = $this->getService(ConfigFormatConverter::class); $this->smartFileSystem = $this->getService(SmartFileSystem::class); +======= + $this->configFormatConverter = $this->container->get(ConfigFormatConverter::class); + $this->smartFileSystem = $this->container->get(FileSystem::class); +>>>>>>> 19a2662 (fixup! fixup! bump) +======= + $this->configFormatConverter = $this->container->get(ConfigFormatConverter::class); + $this->smartFileSystem = $this->container->get(SmartFileSystem::class); +>>>>>>> cd67f40 (bump) } - protected function doTestOutput(SmartFileInfo $fixtureFileInfo, bool $preserveDirStructure = false): void + protected function doTestOutput(\SplFileInfo $fixtureFileInfo, bool $preserveDirStructure = false): void { - $inputAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); + $inputAndExpected = FixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); $this->doTestFileInfo($inputAndExpected->getInputFileInfo(), $inputAndExpected->getExpectedFileContent(), $fixtureFileInfo); } - protected function doTestFileInfo(SmartFileInfo $inputFileInfo, string $expectedContent, SmartFileInfo $fixtureFileInfo): void + protected function doTestFileInfo(\SplFileInfo $inputFileInfo, string $expectedContent, \SplFileInfo $fixtureFileInfo): void { $convertedContent = $this->configFormatConverter->convert($inputFileInfo); StaticFixtureUpdater::updateFixtureContent($inputFileInfo, $convertedContent, $fixtureFileInfo); + $this->assertSame($expectedContent, $convertedContent, $fixtureFileInfo->getRelativeFilePathFromCwd()); } } diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php b/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php index 6eabe2c06dc..d5d73230ce8 100644 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php @@ -7,13 +7,12 @@ use Iterator; use PHPUnit\Framework\Attributes\DataProvider; use Symplify\ConfigTransformer\Tests\Converter\ConfigFormatConverter\AbstractConfigFormatConverterTestCase; -use Symplify\EasyTesting\DataProvider\StaticFixtureFinder; -use Symplify\SmartFileSystem\SmartFileInfo; +use Symplify\ConfigTransformer\Tests\Helper\FixtureFinder; final class XmlToPhpTest extends AbstractConfigFormatConverterTestCase { #[DataProvider('provideData')] - public function test(SmartFileInfo $fixtureFileInfo): void + public function test(\SplFileInfo $fixtureFileInfo): void { $this->smartFileSystem->copy( __DIR__ . '/Source/some.xml', @@ -23,11 +22,8 @@ public function test(SmartFileInfo $fixtureFileInfo): void $this->doTestOutput($fixtureFileInfo); } - /** - * @return Iterator - */ public static function provideData(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.xml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.xml'); } } diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php index 25647ede2e4..baacfecf813 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php @@ -8,29 +8,27 @@ use Nette\Utils\FileSystem; use PHPUnit\Framework\Attributes\DataProvider; use Symplify\ConfigTransformer\Tests\Converter\ConfigFormatConverter\AbstractConfigFormatConverterTestCase; -use Symplify\EasyTesting\DataProvider\StaticFixtureFinder; -use Symplify\EasyTesting\StaticFixtureSplitter; -use Symplify\SmartFileSystem\SmartFileInfo; +use Symplify\ConfigTransformer\Tests\Helper\FixtureFinder; final class YamlToPhpTest extends AbstractConfigFormatConverterTestCase { #[DataProvider('provideDataForRouting')] - public function testRouting(SmartFileInfo $fileInfo): void + public function testRouting(\SplFileInfo $fileInfo): void { $this->doTestOutput($fileInfo, true); } /** - * @return Iterator + * @return Iterator */ public static function provideDataForRouting(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/routing', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/routing', '*.yaml'); } #[DataProvider('provideData')] #[DataProvider('provideDataWithPhpImported')] - public function testNormal(SmartFileInfo $fixtureFileInfo): void + public function testNormal(\SplFileInfo $fixtureFileInfo): void { // for imports $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); @@ -57,13 +55,13 @@ public function testNormal(SmartFileInfo $fixtureFileInfo): void } #[DataProvider('provideDataWithDirectory')] - public function testSpecialCaseWithDirectory(SmartFileInfo $fileInfo): void + public function testSpecialCaseWithDirectory(\SplFileInfo $fileInfo): void { $this->doTestOutputWithExtraDirectory($fileInfo, __DIR__ . '/Fixture/nested'); } #[DataProvider('provideDataExtension')] - public function testEcs(SmartFileInfo $fileInfo): void + public function testEcs(\SplFileInfo $fileInfo): void { $this->doTestOutputWithExtraDirectory($fileInfo, $fileInfo->getPath()); } @@ -72,7 +70,7 @@ public function testEcs(SmartFileInfo $fileInfo): void * @source https://github.com/symfony/maker-bundle/pull/604 */ #[DataProvider('provideDataMakerBundle')] - public function testMakerBundle(SmartFileInfo $fileInfo): void + public function testMakerBundle(\SplFileInfo $fileInfo): void { // needed for all the included $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); @@ -82,56 +80,55 @@ public function testMakerBundle(SmartFileInfo $fileInfo): void ); require_once $temporaryPath . '/../src/SomeClass.php'; - $this->smartFileSystem->mkdir($temporaryPath . '/../src/Controller'); - $this->smartFileSystem->mkdir($temporaryPath . '/../src/Domain'); + FileSystem::createDir($temporaryPath . '/../src/Controller'); + FileSystem::createDir($temporaryPath . '/../src/Domain'); $this->doTestOutput($fileInfo); } /** - * @return Iterator + * @return Iterator */ public static function provideData(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/normal', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/normal', '*.yaml'); } /** - * @return Iterator + * @return Iterator */ public static function provideDataWithPhpImported(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/skip-imported-php', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/skip-imported-php', '*.yaml'); } /** - * @return Iterator + * @return Iterator */ public static function provideDataExtension(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/extension', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/extension', '*.yaml'); } /** - * @return Iterator + * @return Iterator */ public static function provideDataWithDirectory(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/nested', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/nested', '*.yaml'); } /** - * @return Iterator + * @return Iterator */ public static function provideDataMakerBundle(): Iterator { - return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture/maker-bundle', '*.yaml'); + return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/maker-bundle', '*.yaml'); } - private function doTestOutputWithExtraDirectory(SmartFileInfo $fixtureFileInfo, string $extraDirectory): void + private function doTestOutputWithExtraDirectory(\SplFileInfo $fixtureFileInfo, string $extraDirectory): void { $inputAndExpected = StaticFixtureSplitter::splitFileInfoToInputAndExpected($fixtureFileInfo); - $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); // copy /src to temp directory, so Symfony FileLocator knows about it @@ -140,7 +137,7 @@ private function doTestOutputWithExtraDirectory(SmartFileInfo $fixtureFileInfo, ]); $fileTemporaryPath = $temporaryPath . '/' . $fixtureFileInfo->getRelativeFilePathFromDirectory($extraDirectory); - $this->smartFileSystem->dumpFile($fileTemporaryPath, $inputAndExpected->getInput()); + FileSystem::write($fileTemporaryPath, $inputAndExpected->getInput()); // require class to autoload it $expectedFilePath = $temporaryPath . '/src/SomeClass.php'; @@ -148,7 +145,7 @@ private function doTestOutputWithExtraDirectory(SmartFileInfo $fixtureFileInfo, require_once $expectedFilePath; - $inputFileInfo = new SmartFileInfo($fileTemporaryPath); + $inputFileInfo = new \SplFileInfo($fileTemporaryPath); $this->doTestFileInfo($inputFileInfo, $inputAndExpected->getExpected(), $fixtureFileInfo); } diff --git a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php index 175770bd5d2..b55c4283946 100644 --- a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php +++ b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php @@ -5,17 +5,17 @@ namespace Symplify\ConfigTransformer\Tests\Converter\YamlToPhpConverter; use Symplify\ConfigTransformer\Converter\YamlToPhpConverter; -use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; -use Symplify\PackageBuilder\Testing\AbstractKernelTestCase; +use Symplify\ConfigTransformer\Tests\AbstractTestCase; -final class YamlToPhpConverterTest extends AbstractKernelTestCase +final class YamlToPhpConverterTest extends AbstractTestCase { private YamlToPhpConverter $yamlToPhpConverter; protected function setUp(): void { - $this->bootKernel(ConfigTransformerKernel::class); - $this->yamlToPhpConverter = $this->getService(YamlToPhpConverter::class); + parent::setUp(); + + $this->yamlToPhpConverter = $this->container->get(YamlToPhpConverter::class); } public function test(): void diff --git a/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php b/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php index b24ad033b01..ace0785d3cc 100644 --- a/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php +++ b/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php @@ -5,26 +5,38 @@ namespace Symplify\ConfigTransformer\Tests\Finder\ConfigFileFinder; use Symplify\ConfigTransformer\Finder\ConfigFileFinder; +<<<<<<< HEAD +<<<<<<< HEAD use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; +======= +use Symplify\ConfigTransformer\Tests\AbstractTestCase; +>>>>>>> cd67f40 (bump) use Symplify\ConfigTransformer\ValueObject\Configuration; -use Symplify\PackageBuilder\Testing\AbstractKernelTestCase; -final class ConfigFileFinderTest extends AbstractKernelTestCase +final class ConfigFileFinderTest extends AbstractTestCase { private ConfigFileFinder $configFileFinder; protected function setUp(): void { - $this->bootKernel(ConfigTransformerKernel::class); - - $this->configFileFinder = $this->getService(ConfigFileFinder::class); + $this->configFileFinder = $this->container->get(ConfigFileFinder::class); } +======= +use Symplify\ConfigTransformer\Tests\AbstractTestCase; + +/** + * @see \Symplify\ConfigTransformer\Finder\ConfigFileFinder + */ +final class ConfigFileFinderTest extends AbstractTestCase +{ +>>>>>>> 8c52b6b (fixup! fixup! fixup! fixup! fixup! fixup! bump) public function test(): void { - $configuration = new Configuration([__DIR__ . '/Fixture'], true); + $configFileFinder = new ConfigFileFinder(); + ; - $fileInfos = $this->configFileFinder->findFileInfos($configuration); + $fileInfos = $configFileFinder->findFileInfos([__DIR__ . '/Fixture']); $this->assertCount(1, $fileInfos); } } diff --git a/tests/Helper/FixtureFinder.php b/tests/Helper/FixtureFinder.php new file mode 100644 index 00000000000..20ac024016d --- /dev/null +++ b/tests/Helper/FixtureFinder.php @@ -0,0 +1,26 @@ +> + */ + public static function yieldDirectory(string $directory, string $suffix = '*.php.inc'): Iterator + { + $finder = Finder::create()->in($directory)->files()->name($suffix); + + $fileInfos = iterator_to_array($finder->getIterator()); + + foreach ($fileInfos as $fileInfo) { + yield [$fileInfo]; + } + } +} diff --git a/tests/Helper/FixtureSplitter.php b/tests/Helper/FixtureSplitter.php new file mode 100644 index 00000000000..aba2802a892 --- /dev/null +++ b/tests/Helper/FixtureSplitter.php @@ -0,0 +1,10 @@ + Date: Fri, 15 Dec 2023 23:24:44 +0100 Subject: [PATCH 3/7] remove XML support as too complex --- .github/workflows/code_analysis.yaml | 4 - .gitignore | 3 + README.md | 2 +- bin/config-transformer.php | 9 +- composer.json | 6 +- config/config.php | 22 +- src/Collector/XmlImportCollector.php | 31 --- src/Command/SwitchFormatCommand.php | 12 +- src/ConfigLoader.php | 22 +- src/Console/ColorConsoleDiffFormatter.php | 102 ++++++++ src/Console/ConsoleDiffer.php | 22 ++ src/Console/DifferFactory.php | 23 ++ src/Console/Style/SymfonyStyleFactory.php | 20 ++ src/Converter/ConfigFormatConverter.php | 91 +------ .../ContainerBuilderCleaner.php | 106 -------- .../Loader/IdAwareXmlFileLoader.php | 242 ------------------ .../IdAwareXmlFileLoaderFactory.php | 30 --- src/FileSystem/ConfigFileDumper.php | 7 +- src/FileSystem/RelativeFilePathHelper.php | 32 +++ src/Finder/ConfigFileFinder.php | 2 +- src/Kernel/ConfigTransformerKernel.php | 3 + src/Naming/UniqueNaming.php | 26 -- src/Reflection/PrivatesAccessor.php | 28 ++ src/ValueObject/ConvertedContent.php | 14 +- tests/AbstractTestCase.php | 14 +- .../AbstractConfigFormatConverterTestCase.php | 66 ----- .../XmlToPhp/Fixture/doctrine_config.xml | 24 -- .../XmlToPhp/Fixture/multiple_tags.xml | 28 -- .../XmlToPhp/Fixture/no_import_inling.xml | 19 -- .../XmlToPhp/Fixture/single_service_tag.xml | 28 -- .../XmlToPhp/Fixture/some.xml | 43 ---- .../tag_with_additional_attributes.xml | 32 --- .../XmlToPhp/Source/some.xml | 20 -- .../XmlToPhp/XmlToPhpTest.php | 29 --- .../YamlToPhp/YamlToPhpTest.php | 72 ++++-- .../YamlToPhpConverterTest.php | 2 +- .../ConfigFileFinder/ConfigFileFinderTest.php | 24 +- tests/Fixture/some.xml | 10 - tests/Helper/FixtureFinder.php | 2 +- tests/Helper/FixtureSplitter.php | 115 +++++++++ tests/Helper/FixtureUpdater.php | 52 ++++ tests/Helper/ValueObject/InputAndExpected.php | 24 ++ .../InputFileInfoAndExpectedFileInfo.php | 36 +++ 43 files changed, 585 insertions(+), 914 deletions(-) delete mode 100644 src/Collector/XmlImportCollector.php create mode 100644 src/Console/ColorConsoleDiffFormatter.php create mode 100644 src/Console/ConsoleDiffer.php create mode 100644 src/Console/DifferFactory.php create mode 100644 src/Console/Style/SymfonyStyleFactory.php delete mode 100644 src/DependencyInjection/ContainerBuilderCleaner.php delete mode 100644 src/DependencyInjection/Loader/IdAwareXmlFileLoader.php delete mode 100644 src/DependencyInjection/LoaderFactory/IdAwareXmlFileLoaderFactory.php create mode 100644 src/FileSystem/RelativeFilePathHelper.php delete mode 100644 src/Naming/UniqueNaming.php create mode 100644 src/Reflection/PrivatesAccessor.php delete mode 100644 tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/doctrine_config.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/multiple_tags.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/no_import_inling.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/single_service_tag.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/some.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/tag_with_additional_attributes.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/Source/some.xml delete mode 100644 tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php delete mode 100644 tests/Fixture/some.xml create mode 100644 tests/Helper/FixtureUpdater.php create mode 100644 tests/Helper/ValueObject/InputAndExpected.php create mode 100644 tests/Helper/ValueObject/InputFileInfoAndExpectedFileInfo.php diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 7b6523a6cdf..47141dd701c 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -36,10 +36,6 @@ jobs: name: 'PHP Linter' run: vendor/bin/parallel-lint src tests - - - name: 'Check Commented Code' - run: vendor/bin/easy-ci check-commented-code src tests --ansi - - name: 'Check Active Classes' run: vendor/bin/class-leak check src --ansi --skip-type="\Symplify\PhpConfigPrinter\Contract\NodeVisitor\PrePrintNodeVisitorInterface" diff --git a/.gitignore b/.gitignore index d026490bac9..652511177c3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ composer.lock /vendor .phpunit.cache + +# kernel cache +/var diff --git a/README.md b/README.md index f40b71a890e..9066ec47b59 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Convert Symfony XML/YAML configs to PHP +# Convert Symfony YAML configs to PHP [![Downloads total](https://img.shields.io/packagist/dt/symplify/config-transformer.svg?style=flat-square)](https://packagist.org/packages/symplify/config-transformer/stats) diff --git a/bin/config-transformer.php b/bin/config-transformer.php index 1cedf79f0a3..688ccefb673 100755 --- a/bin/config-transformer.php +++ b/bin/config-transformer.php @@ -3,7 +3,6 @@ declare(strict_types=1); use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; -use Symplify\SymplifyKernel\ValueObject\KernelBootAndApplicationRun; $possibleAutoloadPaths = [ // dependency @@ -26,6 +25,10 @@ require_once $scoperAutoloadFilepath; } +$configTransformerKernel = new ConfigTransformerKernel(); +$configTransformerKernel->boot(); -$kernelBootAndApplicationRun = new KernelBootAndApplicationRun(ConfigTransformerKernel::class); -$kernelBootAndApplicationRun->run(); +$container = $configTransformerKernel->getContainer(); + +$configTransformerApplication = $container->get(\Symplify\ConfigTransformer\Console\ConfigTransformerApplication::class); +$configTransformerApplication->run(); diff --git a/composer.json b/composer.json index 16807bd5157..20ec396f350 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "symfony/expression-language": "^6.4", "symfony/finder": "^6.4", "symfony/yaml": "^6.4", - "symplify/php-config-printer": "^11.2" + "symplify/php-config-printer": "^11.3", + "webmozart/assert": "^1.11" }, "require-dev": { "cweagans/composer-patches": "^1.7", @@ -61,7 +62,6 @@ "check-cs": "vendor/bin/ecs check --ansi", "fix-cs": "vendor/bin/ecs check --fix --ansi", "phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify", - "rector": "vendor/bin/rector process --dry-run --ansi", - "release": "vendor/bin/monorepo-builder release patch --ansi" + "rector": "vendor/bin/rector process --dry-run --ansi" } } diff --git a/config/config.php b/config/config.php index e876a7ba277..8b4da9784a5 100644 --- a/config/config.php +++ b/config/config.php @@ -6,14 +6,14 @@ use PhpParser\NodeFinder; use SebastianBergmann\Diff\Differ; use Symfony\Component\Console\Application; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\Yaml\Parser; +use Symplify\ConfigTransformer\Console\ColorConsoleDiffFormatter; use Symplify\ConfigTransformer\Console\ConfigTransformerApplication; -use Symplify\PackageBuilder\Console\Formatter\ColorConsoleDiffFormatter; -use Symplify\PackageBuilder\Console\Output\ConsoleDiffer; -use Symplify\PackageBuilder\Diff\DifferFactory; -use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker; -use Symplify\PackageBuilder\Yaml\ParametersMerger; +use Symplify\ConfigTransformer\Console\ConsoleDiffer; +use Symplify\ConfigTransformer\Console\DifferFactory; +use Symplify\ConfigTransformer\Console\Style\SymfonyStyleFactory; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; return static function (ContainerConfigurator $containerConfigurator): void { @@ -21,7 +21,8 @@ $services->defaults() ->public() - ->autowire(); + ->autowire() + ->autoconfigure(); $services->load('Symplify\ConfigTransformer\\', __DIR__ . '/../src') ->exclude([ @@ -35,18 +36,15 @@ $services->alias(Application::class, ConfigTransformerApplication::class); // color diff - $services->set(DifferFactory::class); $services->set(Differ::class) ->factory([service(DifferFactory::class), 'create']); - $services->set(ConsoleDiffer::class); - $services->set(ColorConsoleDiffFormatter::class); + $services->set(SymfonyStyle::class) + ->factory([service(SymfonyStyleFactory::class), 'create']); + $services->set(BuilderFactory::class); $services->set(NodeFinder::class); $services->set(Parser::class); - - $services->set(ClassLikeExistenceChecker::class); - $services->set(ParametersMerger::class); }; diff --git a/src/Collector/XmlImportCollector.php b/src/Collector/XmlImportCollector.php deleted file mode 100644 index 20272134e32..00000000000 --- a/src/Collector/XmlImportCollector.php +++ /dev/null @@ -1,31 +0,0 @@ - - */ - private array $imports = []; - - public function addImport(mixed $resource, bool|string $ignoreErrors): void - { - $this->imports[] = [ - YamlKey::RESOURCE => $resource, - YamlKey::IGNORE_ERRORS => $ignoreErrors, - ]; - } - - /** - * @return array - */ - public function provide(): array - { - return $this->imports; - } -} diff --git a/src/Command/SwitchFormatCommand.php b/src/Command/SwitchFormatCommand.php index 46ec7513349..5c5ff159e5b 100644 --- a/src/Command/SwitchFormatCommand.php +++ b/src/Command/SwitchFormatCommand.php @@ -12,6 +12,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Symplify\ConfigTransformer\Configuration\ConfigurationFactory; use Symplify\ConfigTransformer\Converter\ConfigFormatConverter; use Symplify\ConfigTransformer\FileSystem\ConfigFileDumper; @@ -19,7 +20,6 @@ use Symplify\ConfigTransformer\ValueObject\Configuration; use Symplify\ConfigTransformer\ValueObject\ConvertedContent; use Symplify\ConfigTransformer\ValueObject\Option; -use Symplify\SmartFileSystem\SmartFileInfo; final class SwitchFormatCommand extends Command { @@ -38,14 +38,14 @@ protected function configure(): void $this->setName('switch-format'); $this->setAliases(['convert', 'transform']); - $this->setDescription('Converts XML/YAML configs to PHP format'); + $this->setDescription('Converts YAML configs to PHP format'); $this->addArgument( Option::SOURCES, InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Path to directory/file with configs', // 99 % of symfony project has this directory - [getcwd() . '/config'] + [getcwd() . '/config', getcwd() . '/app/config'] ); $this->addOption(Option::DRY_RUN, 'n', InputOption::VALUE_NONE, 'Dry run - no removal or config change'); @@ -57,9 +57,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $totalFileCount = $this->getTotalFileCount($configuration); - $fileInfos = $this->configFileFinder->findFileInfos($configuration); + $fileInfos = $this->configFileFinder->findFileInfos($configuration->getSources()); if ($fileInfos === []) { - $successMessage = sprintf('No YAML/XML configs found in %d files, good job!', $totalFileCount); + $successMessage = sprintf('No YAML configs found in %d files, good job!', $totalFileCount); $this->symfonyStyle->success($successMessage); return self::SUCCESS; @@ -89,7 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int return self::SUCCESS; } - private function removeFileInfo(Configuration $configuration, SmartFileInfo $fileInfo): void + private function removeFileInfo(Configuration $configuration, SplFileInfo $fileInfo): void { // only dry run, nothing to remove if ($configuration->isDryRun()) { diff --git a/src/ConfigLoader.php b/src/ConfigLoader.php index 6340518c2fd..ef25f25f43d 100644 --- a/src/ConfigLoader.php +++ b/src/ConfigLoader.php @@ -4,6 +4,7 @@ namespace Symplify\ConfigTransformer; +use Nette\Utils\FileSystem; use Nette\Utils\Strings; use Symfony\Component\Config\Exception\LoaderLoadException; use Symfony\Component\Config\FileLocator; @@ -14,15 +15,13 @@ use Symfony\Component\DependencyInjection\Loader\DirectoryLoader; use Symfony\Component\DependencyInjection\Loader\GlobFileLoader; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; +use Symfony\Component\Finder\SplFileInfo; use Symplify\ConfigTransformer\DependencyInjection\ExtensionFaker; use Symplify\ConfigTransformer\DependencyInjection\Loader\MissingAutodiscoveryDirectoryTolerantYamlFileLoader; use Symplify\ConfigTransformer\DependencyInjection\Loader\SkippingPhpFileLoader; -use Symplify\ConfigTransformer\DependencyInjection\LoaderFactory\IdAwareXmlFileLoaderFactory; use Symplify\ConfigTransformer\Enum\Format; use Symplify\ConfigTransformer\Exception\NotImplementedYetException; use Symplify\ConfigTransformer\ValueObject\ContainerBuilderAndFileContent; -use Symplify\SmartFileSystem\SmartFileInfo; -use Symplify\SmartFileSystem\SmartFileSystem; final class ConfigLoader { @@ -30,7 +29,7 @@ final class ConfigLoader * @see https://regex101.com/r/4Uanps/4 * @var string */ - private const PHP_CONST_REGEX = '#!php/const:?\s*([a-zA-Z0-9_\\\\]+(::[a-zA-Z0-9_]+)?)+(:\s*(.*))?#'; + private const PHP_CONST_REGEX = '#!php/const:?\s*([a-zA-Z0-9_\\\]+(::\w+)?)+(:\s*(.*))?#'; /** * @see https://regex101.com/r/spi4ir/1 @@ -39,18 +38,16 @@ final class ConfigLoader private const UNQUOTED_PARAMETER_REGEX = '#^(\s*\w+:\s+)(\%(.*?)%)(.*?)?$#m'; public function __construct( - private readonly IdAwareXmlFileLoaderFactory $idAwareXmlFileLoaderFactory, - private readonly SmartFileSystem $smartFileSystem, private readonly ExtensionFaker $extensionFaker ) { } public function createAndLoadContainerBuilderFromFileInfo( - SmartFileInfo $smartFileInfo, + SplFileInfo $smartFileInfo, ): ContainerBuilderAndFileContent { $containerBuilder = new ContainerBuilder(); - $delegatingLoader = $this->createLoaderBySuffix($containerBuilder, $smartFileInfo->getSuffix()); + $delegatingLoader = $this->createLoaderBySuffix($containerBuilder, $smartFileInfo->getExtension()); $fileRealPath = $smartFileInfo->getRealPath(); // correct old syntax of tags so we can parse it @@ -63,7 +60,7 @@ public function createAndLoadContainerBuilderFromFileInfo( static fn (array $match): string => $match[1] . '"' . $match[2] . ($match[4] ?? '') . '"' ); - if (in_array($smartFileInfo->getSuffix(), [Format::YML, Format::YAML], true)) { + if (in_array($smartFileInfo->getExtension(), [Format::YML, Format::YAML], true)) { $content = Strings::replace( $content, self::PHP_CONST_REGEX, @@ -75,7 +72,7 @@ public function createAndLoadContainerBuilderFromFileInfo( ); if ($content !== $smartFileInfo->getContents()) { $fileRealPath = sys_get_temp_dir() . '/__symplify_config_tranformer_clean_yaml/' . $smartFileInfo->getFilename(); - $this->smartFileSystem->dumpFile($fileRealPath, $content); + FileSystem::write($fileRealPath, $content); } $this->extensionFaker->fakeInContainerBuilder($containerBuilder, $content); @@ -93,11 +90,6 @@ public function createAndLoadContainerBuilderFromFileInfo( private function createLoaderBySuffix(ContainerBuilder $containerBuilder, string $suffix): DelegatingLoader { - if ($suffix === Format::XML) { - $idAwareXmlFileLoader = $this->idAwareXmlFileLoaderFactory->createFromContainerBuilder($containerBuilder); - return $this->wrapToDelegatingLoader($idAwareXmlFileLoader, $containerBuilder); - } - if (in_array($suffix, [Format::YML, Format::YAML], true)) { $missingAutodiscoveryDirectoryTolerantYamlFileLoader = new MissingAutodiscoveryDirectoryTolerantYamlFileLoader( $containerBuilder, diff --git a/src/Console/ColorConsoleDiffFormatter.php b/src/Console/ColorConsoleDiffFormatter.php new file mode 100644 index 00000000000..280bcd84f7e --- /dev/null +++ b/src/Console/ColorConsoleDiffFormatter.php @@ -0,0 +1,102 @@ + + */ +final class ColorConsoleDiffFormatter +{ + /** + * @var string + * @see https://regex101.com/r/ovLMDF/1 + */ + private const PLUS_START_REGEX = '#^(\+.*)#'; + + /** + * @var string + * @see https://regex101.com/r/xwywpa/1 + */ + private const MINUT_START_REGEX = '#^(\-.*)#'; + + /** + * @var string + * @see https://regex101.com/r/CMlwa8/1 + */ + private const AT_START_REGEX = '#^(@.*)#'; + + /** + * @var string + * @see https://regex101.com/r/qduj2O/1 + */ + private const NEWLINES_REGEX = "#\n\r|\n#"; + + private readonly string $template; + + public function __construct() + { + $this->template = sprintf( + ' ---------- begin diff ----------%s%%s%s ----------- end diff -----------' . PHP_EOL, + PHP_EOL, + PHP_EOL + ); + } + + public function format(string $diff): string + { + return $this->formatWithTemplate($diff, $this->template); + } + + private function formatWithTemplate(string $diff, string $template): string + { + $escapedDiff = OutputFormatter::escape(rtrim($diff)); + + $escapedDiffLines = Strings::split($escapedDiff, self::NEWLINES_REGEX); + + // remove description of added + remove; obvious on diffs + foreach ($escapedDiffLines as $key => $escapedDiffLine) { + if ($escapedDiffLine === '--- Original') { + unset($escapedDiffLines[$key]); + } + + if ($escapedDiffLine === '+++ New') { + unset($escapedDiffLines[$key]); + } + } + + $coloredLines = array_map(function (string $string): string { + $string = $this->makePlusLinesGreen($string); + $string = $this->makeMinusLinesRed($string); + $string = $this->makeAtNoteCyan($string); + + if ($string === ' ') { + return ''; + } + + return $string; + }, $escapedDiffLines); + + return sprintf($template, implode(PHP_EOL, $coloredLines)); + } + + private function makePlusLinesGreen(string $string): string + { + return Strings::replace($string, self::PLUS_START_REGEX, '$1'); + } + + private function makeMinusLinesRed(string $string): string + { + return Strings::replace($string, self::MINUT_START_REGEX, '$1'); + } + + private function makeAtNoteCyan(string $string): string + { + return Strings::replace($string, self::AT_START_REGEX, '$1'); + } +} diff --git a/src/Console/ConsoleDiffer.php b/src/Console/ConsoleDiffer.php new file mode 100644 index 00000000000..4725d5a8d7d --- /dev/null +++ b/src/Console/ConsoleDiffer.php @@ -0,0 +1,22 @@ +differ->diff($old, $new); + return $this->colorConsoleDiffFormatter->format($diff); + } +} diff --git a/src/Console/DifferFactory.php b/src/Console/DifferFactory.php new file mode 100644 index 00000000000..e479f88592c --- /dev/null +++ b/src/Console/DifferFactory.php @@ -0,0 +1,23 @@ +currentFilePathProvider->setFilePath($smartFileInfo->getRealPath()); + $this->currentFilePathProvider->setFilePath($fileInfo->getRealPath()); $containerBuilderAndFileContent = $this->configLoader->createAndLoadContainerBuilderFromFileInfo( - $smartFileInfo + $fileInfo ); - $containerBuilder = $containerBuilderAndFileContent->getContainerBuilder(); - - if (in_array($smartFileInfo->getSuffix(), [Format::YAML, Format::YML], true)) { + if (in_array($fileInfo->getExtension(), [Format::YAML, Format::YML], true)) { $dumpedYaml = $containerBuilderAndFileContent->getFileContent(); - $dumpedYaml = $this->decorateWithCollectedXmlImports($dumpedYaml); - - return $this->yamlToPhpConverter->convert($dumpedYaml, $smartFileInfo->getRealPath()); + return $this->yamlToPhpConverter->convert($dumpedYaml, $fileInfo->getRealPath()); } - if ($smartFileInfo->getSuffix() === Format::XML) { - $dumpedYaml = $this->dumpContainerBuilderToYaml($containerBuilder); - $dumpedYaml = $this->decorateWithCollectedXmlImports($dumpedYaml); - - return $this->yamlToPhpConverter->convert($dumpedYaml, $smartFileInfo->getRealPath()); - } - - $message = sprintf('Suffix "%s" is not support yet', $smartFileInfo->getSuffix()); + $message = sprintf('Suffix "%s" is not support yet', $fileInfo->getExtension()); throw new NotImplementedYetException($message); } - - private function dumpContainerBuilderToYaml(ContainerBuilder $containerBuilder): string - { - $yamlDumper = new YamlDumper($containerBuilder); - $this->containerBuilderCleaner->cleanContainerBuilder($containerBuilder); - - // 1. services and parameters - $content = $yamlDumper->dump(); - if (! is_string($content)) { - throw new ShouldNotHappenException(); - } - - // 2. append extension yaml too - /** @var array $extensionsConfigs */ - $extensionsConfigs = $this->privatesAccessor->getPrivateProperty($containerBuilder, 'extensionConfigs'); - foreach ($extensionsConfigs as $namespace => $configs) { - $mergedConfig = []; - foreach ($configs as $config) { - $mergedConfig = $this->parametersMerger->merge($mergedConfig, $config); - } - - $extensionsConfigs[$namespace] = $mergedConfig; - } - - $extensionsContent = $this->dumpYaml($extensionsConfigs); - - return $content . PHP_EOL . $extensionsContent; - } - - private function decorateWithCollectedXmlImports(string $dumpedYaml): string - { - $collectedXmlImports = $this->xmlImportCollector->provide(); - if ($collectedXmlImports === []) { - return $dumpedYaml; - } - - /** @var array $yamlArray */ - $yamlArray = Yaml::parse($dumpedYaml, Yaml::PARSE_CUSTOM_TAGS); - $yamlArray['imports'] = array_merge($yamlArray['imports'] ?? [], $collectedXmlImports); - - return $this->dumpYaml($yamlArray); - } - - /** - * @param array $yamlArray - */ - private function dumpYaml(array $yamlArray): string - { - if ($yamlArray === []) { - return ''; - } - - return Yaml::dump($yamlArray, 10, 4, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK); - } } diff --git a/src/DependencyInjection/ContainerBuilderCleaner.php b/src/DependencyInjection/ContainerBuilderCleaner.php deleted file mode 100644 index 8d494aa71cb..00000000000 --- a/src/DependencyInjection/ContainerBuilderCleaner.php +++ /dev/null @@ -1,106 +0,0 @@ -removeSymfonyInternalServices($containerBuilder); - $this->removeTemporaryAnonymousIds($containerBuilder); - - foreach ($containerBuilder->getDefinitions() as $definition) { - $this->resolvePolyfillForNameTag($definition); - } - } - - private function removeSymfonyInternalServices(ContainerBuilder $containerBuilder): void - { - $containerBuilder->removeDefinition('service_container'); - $containerBuilder->removeAlias(PsrContainerInterface::class); - $containerBuilder->removeAlias(ContainerInterface::class); - } - - private function removeTemporaryAnonymousIds(ContainerBuilder $containerBuilder): void - { - $definitions = $this->privatesAccessor->getPrivateProperty($containerBuilder, 'definitions'); - - foreach ($definitions as $name => $definition) { - if (! is_string($name)) { - continue; - } - - if (! $this->isGeneratedKeyForAnonymousClass($name)) { - continue; - } - - unset($definitions[$name]); - $definitions[] = $definition; - } - - $this->privatesAccessor->setPrivateProperty($containerBuilder, 'definitions', $definitions); - } - - private function isGeneratedKeyForAnonymousClass(string $name): bool - { - return (bool) Strings::match($name, self::ANONYMOUS_CLASS_REGEX); - } - - private function resolvePolyfillForNameTag(Definition $definition): void - { - if ($definition->getTags() === []) { - return; - } - - $tags = $definition->getTags(); - foreach ($definition->getTags() as $name => $value) { - /** @var mixed[] $tagValues */ - $tagValues = $value[0]; - if ($this->shouldSkipNameTagInlining($tagValues)) { - continue; - } - - unset($tags[$name]); - - $tagValues = []; - foreach ($value as $singleValue) { - $singleTag = array_merge([ - 'name' => $name, - ], $singleValue); - $tagValues[] = $singleTag; - } - - $tags[] = $tagValues; - } - - $definition->setTags($tags); - } - - /** - * @param array $tagValues - */ - private function shouldSkipNameTagInlining(array $tagValues): bool - { - return $tagValues === []; - } -} diff --git a/src/DependencyInjection/Loader/IdAwareXmlFileLoader.php b/src/DependencyInjection/Loader/IdAwareXmlFileLoader.php deleted file mode 100644 index 341c43f42f2..00000000000 --- a/src/DependencyInjection/Loader/IdAwareXmlFileLoader.php +++ /dev/null @@ -1,242 +0,0 @@ -privatesCaller = new PrivatesCaller(); - } - - /** - * @param string|string[]|null $exclude - */ - public function import( - mixed $resource, - string $type = null, - bool|string $ignoreErrors = false, - string $sourceResource = null, - $exclude = null - ): mixed { - $this->xmlImportCollector->addImport($resource, $ignoreErrors); - return null; - } - - public function load(mixed $resource, ?string $type = null): mixed - { - $path = $this->locator->locate($resource); - - if (! is_string($path)) { - throw new XmlParsingException(); - } - - // mostly mimics parseFileToDOM(), just without validation, that often breaks due to missing extension - $domDocument = $this->parseFileToDOMWithoutValidation($path); - - // file not found - if (! $this->container->fileExists($path)) { - return null; - } - - $defaults = $this->privatesCaller->callPrivateMethod($this, 'getServiceDefaults', [$domDocument, $path]); - $this->processAnonymousServices($domDocument, $path); - - // imports - $this->privatesCaller->callPrivateMethod($this, 'parseImports', [$domDocument, $path]); - - // parameters - $this->privatesCaller->callPrivateMethod($this, 'parseParameters', [$domDocument, $path]); - - // faking extensions - $aliasAndNamespaceConfigurableExtension = new AliasAndNamespaceConfigurableExtension( - 'doctrine', - 'http://symfony.com/schema/dic/doctrine' - ); - $this->container->registerExtension($aliasAndNamespaceConfigurableExtension); - - $this->privatesCaller->callPrivateMethod($this, 'loadFromExtensions', [$domDocument]); - - // services - try { - $this->privatesCaller->callPrivateMethod($this, 'parseDefinitions', [$domDocument, $path, $defaults]); - } finally { - $this->instanceof = []; - $this->registerAliasesForSinglyImplementedInterfaces(); - } - - return null; - } - - private function processAnonymousServices(DOMDocument $domDocument, string $file): void - { - $this->count = 0; - - /** @var array $definitions */ - $definitions = []; - - $domxPath = new DOMXPath($domDocument); - $domxPath->registerNamespace('container', self::NS); - - $definitions = $this->processAnonymousServicesInArguments($domxPath, $file, $definitions); - - /** @var DOMNodeList $nodeWithIds */ - $nodeWithIds = $domxPath->query('//container:services/container:service[@id]'); - $hasNamedServices = (bool) $nodeWithIds->length; - - // anonymous services "in the wild" - $anonymousServiceNodes = $domxPath->query('//container:services/container:service[not(@id)]'); - if ($anonymousServiceNodes instanceof DOMNodeList) { - foreach ($anonymousServiceNodes as $anonymouServiceNode) { - /** @var DOMElement $anonymouServiceNode */ - $id = $this->createAnonymousServiceId($hasNamedServices, $anonymouServiceNode, $file); - $anonymouServiceNode->setAttribute(self::ID, $id); - $definitions[$id] = [$anonymouServiceNode, $file, true]; - } - } - - // resolve definitions - uksort($definitions, 'strnatcmp'); - - $inversedDefinitions = array_reverse($definitions); - foreach ($inversedDefinitions as $id => [$domElement, $file]) { - $definition = $this->privatesCaller->callPrivateMethod( - $this, - 'parseDefinition', - [$domElement, $file, new Definition()] - ); - - if ($definition !== null) { - $this->setDefinition($id, $definition); - } - } - } - - /** - * @return mixed[] - * @param mixed[] $definitions - */ - private function processAnonymousServicesInArguments( - DOMXPath $domxPath, - string $file, - array $definitions - ): array { - $nodes = $domxPath->query( - '//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]|//container:bind[not(@id)]|//container:factory[not(@service)]|//container:configurator[not(@service)]' - ); - - if ($nodes !== false) { - /** @var DOMElement $node */ - foreach ($nodes as $node) { - // get current service id - - $parentNode = $node->parentNode; - assert($parentNode instanceof DOMElement); - - // @see https://stackoverflow.com/a/28944/1348344 - $parentServiceId = $parentNode->getAttribute('id'); - - /** @var DOMElement[] $services */ - $services = $this->privatesCaller->callPrivateMethod($this, 'getChildren', [$node, 'service']); - if ($services !== []) { - $id = $this->createUniqueServiceNameFromClass($services[0], $parentServiceId); - - $node->setAttribute(self::ID, $id); - $node->setAttribute('service', $id); - - $definitions[$id] = [$services[0], $file]; - $services[0]->setAttribute(self::ID, $id); - - // anonymous services are always private - // we could not use the constant false here, because of XML parsing - $services[0]->setAttribute('public', 'false'); - } - } - } - - return $definitions; - } - - private function createUniqueServiceNameFromClass(DOMElement $serviceDomElement, string $parentServiceId): string - { - $class = $serviceDomElement->getAttribute('class'); - $serviceName = $parentServiceId . '.' . $this->createServiceNameFromClass($class); - - return $this->uniqueNaming->uniquateName($serviceName); - } - - private function createServiceNameFromClass(string $class): string - { - $serviceName = Strings::replace($class, '#\\\\#', '.'); - $serviceName = strtolower($serviceName); - - return $this->uniqueNaming->uniquateName($serviceName); - } - - private function createAnonymousServiceId(bool $hasNamedServices, DOMElement $domElement, string $file): string - { - $className = $domElement->getAttribute('class'); - if ($hasNamedServices) { - return $this->createServiceNameFromClass($className); - } - - $hashedFileName = hash('sha256', $file); - return sprintf('%d_%s', ++$this->count, $hashedFileName); - } - - private function parseFileToDOMWithoutValidation(string $path): DOMDocument - { - try { - return XmlUtils::loadFile($path); - } catch (InvalidArgumentException $invalidArgumentException) { - $errorMessage = sprintf('Unable to parse file "%s": %s', $path, $invalidArgumentException->getMessage()); - - throw new XmlParsingException( - $errorMessage, - $invalidArgumentException->getCode(), - $invalidArgumentException - ); - } - } -} diff --git a/src/DependencyInjection/LoaderFactory/IdAwareXmlFileLoaderFactory.php b/src/DependencyInjection/LoaderFactory/IdAwareXmlFileLoaderFactory.php deleted file mode 100644 index b60c6be6b1c..00000000000 --- a/src/DependencyInjection/LoaderFactory/IdAwareXmlFileLoaderFactory.php +++ /dev/null @@ -1,30 +0,0 @@ -uniqueNaming, - $this->xmlImportCollector - ); - } -} diff --git a/src/FileSystem/ConfigFileDumper.php b/src/FileSystem/ConfigFileDumper.php index 6778aeb5098..e44b11856ec 100644 --- a/src/FileSystem/ConfigFileDumper.php +++ b/src/FileSystem/ConfigFileDumper.php @@ -4,17 +4,16 @@ namespace Symplify\ConfigTransformer\FileSystem; +use Nette\Utils\FileSystem; use Symfony\Component\Console\Style\SymfonyStyle; +use Symplify\ConfigTransformer\Console\ConsoleDiffer; use Symplify\ConfigTransformer\ValueObject\Configuration; use Symplify\ConfigTransformer\ValueObject\ConvertedContent; -use Symplify\PackageBuilder\Console\Output\ConsoleDiffer; -use Symplify\SmartFileSystem\SmartFileSystem; final class ConfigFileDumper { public function __construct( private readonly SymfonyStyle $symfonyStyle, - private readonly SmartFileSystem $smartFileSystem, private readonly ConsoleDiffer $consoleDiffer ) { } @@ -52,6 +51,6 @@ public function dumpFile(ConvertedContent $convertedContent, Configuration $conf $this->symfonyStyle->title($fileTitle); - $this->smartFileSystem->dumpFile($newFileRealPath, $convertedContent->getConvertedContent()); + FileSystem::write($newFileRealPath, $convertedContent->getConvertedContent()); } } diff --git a/src/FileSystem/RelativeFilePathHelper.php b/src/FileSystem/RelativeFilePathHelper.php new file mode 100644 index 00000000000..f9a4169abd8 --- /dev/null +++ b/src/FileSystem/RelativeFilePathHelper.php @@ -0,0 +1,32 @@ +makePathRelative( + $normalizedFilePath, + (string) realpath($directory) + ); + + return rtrim($relativeFilePath, '/'); + } + + private static function normalizePath(string $path): string + { + return str_replace('\\', '/', $path); + } +} diff --git a/src/Finder/ConfigFileFinder.php b/src/Finder/ConfigFileFinder.php index 3402330b75e..c7b12d263f1 100644 --- a/src/Finder/ConfigFileFinder.php +++ b/src/Finder/ConfigFileFinder.php @@ -4,8 +4,8 @@ namespace Symplify\ConfigTransformer\Finder; -use SplFileInfo; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; final class ConfigFileFinder { diff --git a/src/Kernel/ConfigTransformerKernel.php b/src/Kernel/ConfigTransformerKernel.php index a6b09a9fb3f..86db0ed3d0f 100644 --- a/src/Kernel/ConfigTransformerKernel.php +++ b/src/Kernel/ConfigTransformerKernel.php @@ -8,6 +8,9 @@ use Symfony\Component\HttpKernel\Kernel; use Symplify\PhpConfigPrinter\ValueObject\PhpConfigPrinterConfig; +/** + * @api used in tests and bin + */ final class ConfigTransformerKernel extends Kernel { public function __construct() diff --git a/src/Naming/UniqueNaming.php b/src/Naming/UniqueNaming.php deleted file mode 100644 index 8027793cbd1..00000000000 --- a/src/Naming/UniqueNaming.php +++ /dev/null @@ -1,26 +0,0 @@ - - */ - private array $existingNames = []; - - public function uniquateName(string $name): string - { - if (isset($this->existingNames[$name])) { - $serviceNameCounter = $this->existingNames[$name]; - $this->existingNames[$name] = ++$serviceNameCounter; - return $name . '.' . $serviceNameCounter; - } - - $this->existingNames[$name] = 1; - - return $name; - } -} diff --git a/src/Reflection/PrivatesAccessor.php b/src/Reflection/PrivatesAccessor.php new file mode 100644 index 00000000000..49a9287d904 --- /dev/null +++ b/src/Reflection/PrivatesAccessor.php @@ -0,0 +1,28 @@ +setAccessible(true); + + return $reflectionProperty->getValue($object); + } + + // write private property + public static function writePrivateProperty(object $object, string $propertyName, mixed $value): void + { + $reflectionProperty = new ReflectionProperty($object, $propertyName); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue($object, $value); + } +} diff --git a/src/ValueObject/ConvertedContent.php b/src/ValueObject/ConvertedContent.php index a8adba74647..64562754212 100644 --- a/src/ValueObject/ConvertedContent.php +++ b/src/ValueObject/ConvertedContent.php @@ -5,12 +5,20 @@ namespace Symplify\ConfigTransformer\ValueObject; use Nette\Utils\Strings; +use Symfony\Component\Finder\SplFileInfo; +use Symplify\ConfigTransformer\FileSystem\RelativeFilePathHelper; final class ConvertedContent { + /** + * @var string + * @see https://regex101.com/r/SYP00O/1 + */ + private const LAST_SUFFIX_REGEX = '#\.[^.]+$#'; + public function __construct( private readonly string $convertedContent, - private readonly \SplFileInfo $originalFileInfo + private readonly SplFileInfo $originalFileInfo ) { } @@ -29,12 +37,12 @@ public function getNewRelativeFilePath(): string public function getOriginalFilePathWithoutSuffix(): string { - return $this->originalFileInfo->getRealPathWithoutSuffix(); + return Strings::replace($this->originalFileInfo->getRealPath(), self::LAST_SUFFIX_REGEX, ''); } public function getOriginalRelativeFilePath(): string { - return $this->originalFileInfo->getRelativeFilePathFromCwd(); + return RelativeFilePathHelper::resolveFromDirectory($this->originalFileInfo->getRealPath(), getcwd()); } public function getOriginalContent(): string diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index fc3496c7602..5f1fff7407c 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -5,11 +5,12 @@ namespace Symplify\ConfigTransformer\Tests; use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; abstract class AbstractTestCase extends TestCase { - protected \Symfony\Component\DependencyInjection\ContainerInterface $container; + private ContainerInterface $container; protected function setUp(): void { @@ -18,4 +19,15 @@ protected function setUp(): void $this->container = $configTransformerKernel->getContainer(); } + + /** + * @template T as object + * + * @param class-string $type + * @return T + */ + protected function getService(string $type): object + { + return $this->container->get($type); + } } diff --git a/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php b/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php deleted file mode 100644 index b3bcdd941f6..00000000000 --- a/tests/Converter/ConfigFormatConverter/AbstractConfigFormatConverterTestCase.php +++ /dev/null @@ -1,66 +0,0 @@ ->>>>>> af077e6 (fixup! bump) -======= -use Symplify\ConfigTransformer\Tests\AbstractTestCase; ->>>>>>> cd67f40 (bump) -use Symplify\EasyTesting\DataProvider\StaticFixtureUpdater; -use Symplify\EasyTesting\StaticFixtureSplitter; -use Symplify\SmartFileSystem\SmartFileInfo; -use Symplify\SmartFileSystem\SmartFileSystem; -======= ->>>>>>> 19a2662 (fixup! fixup! bump) -======= -use Symplify\ConfigTransformer\Tests\Helper\FixtureSplitter; ->>>>>>> 257b8ab (fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! bump) - -abstract class AbstractConfigFormatConverterTestCase extends AbstractTestCase -{ - protected ConfigFormatConverter $configFormatConverter; - - protected FileSystem $smartFileSystem; - - protected function setUp(): void - { -<<<<<<< HEAD -<<<<<<< HEAD - $this->bootKernel(ConfigTransformerKernel::class); - $this->configFormatConverter = $this->getService(ConfigFormatConverter::class); - $this->smartFileSystem = $this->getService(SmartFileSystem::class); -======= - $this->configFormatConverter = $this->container->get(ConfigFormatConverter::class); - $this->smartFileSystem = $this->container->get(FileSystem::class); ->>>>>>> 19a2662 (fixup! fixup! bump) -======= - $this->configFormatConverter = $this->container->get(ConfigFormatConverter::class); - $this->smartFileSystem = $this->container->get(SmartFileSystem::class); ->>>>>>> cd67f40 (bump) - } - - protected function doTestOutput(\SplFileInfo $fixtureFileInfo, bool $preserveDirStructure = false): void - { - $inputAndExpected = FixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); - $this->doTestFileInfo($inputAndExpected->getInputFileInfo(), $inputAndExpected->getExpectedFileContent(), $fixtureFileInfo); - } - - protected function doTestFileInfo(\SplFileInfo $inputFileInfo, string $expectedContent, \SplFileInfo $fixtureFileInfo): void - { - $convertedContent = $this->configFormatConverter->convert($inputFileInfo); - StaticFixtureUpdater::updateFixtureContent($inputFileInfo, $convertedContent, $fixtureFileInfo); - - $this->assertSame($expectedContent, $convertedContent, $fixtureFileInfo->getRelativeFilePathFromCwd()); - } -} diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/doctrine_config.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/doctrine_config.xml deleted file mode 100644 index 9dee458fa04..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/doctrine_config.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - ------ -extension('doctrine', [ - 'dbal' => [ - 'url' => '%env(resolve:DATABASE_URL)%', - ], - ]); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/multiple_tags.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/multiple_tags.xml deleted file mode 100644 index bb6e8a474e0..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/multiple_tags.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - ------ -services(); - - $services->set('mime_types') - ->tag('tag1') - ->tag('tag2'); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/no_import_inling.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/no_import_inling.xml deleted file mode 100644 index 8343da9033e..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/no_import_inling.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - ------ -import(__DIR__ . '/some.php'); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/single_service_tag.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/single_service_tag.xml deleted file mode 100644 index f7286775ae1..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/single_service_tag.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - ------ -services(); - - $services->set('mime_types') - ->tag('controller.service_subscriber') - ->tag('controller.service_arguments'); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/some.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/some.xml deleted file mode 100644 index 9f50aca3ce9..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/some.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - 10000 - - - - ------ -services(); - - $services->set('mime_types', MimeTypes::class) - ->args([ - '', - ]) - ->call('setDefault', [ - service('mime_types'), - ]) - ->call('setExtra', [ - '10000', - ]); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/tag_with_additional_attributes.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/tag_with_additional_attributes.xml deleted file mode 100644 index 092f3ab14df..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Fixture/tag_with_additional_attributes.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - ------ -services(); - - $services->set('mime_types') - ->tag('tag1', [ - 'priority' => 1, - ]) - ->tag('tag2', [ - 'priority' => 2, - ]); -}; diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/Source/some.xml b/tests/Converter/ConfigFormatConverter/XmlToPhp/Source/some.xml deleted file mode 100644 index c14e4eaf2c4..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/Source/some.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - 10000 - - - - diff --git a/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php b/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php deleted file mode 100644 index d5d73230ce8..00000000000 --- a/tests/Converter/ConfigFormatConverter/XmlToPhp/XmlToPhpTest.php +++ /dev/null @@ -1,29 +0,0 @@ -smartFileSystem->copy( - __DIR__ . '/Source/some.xml', - sys_get_temp_dir() . '/_temp_fixture_easy_testing/some.xml' - ); - - $this->doTestOutput($fixtureFileInfo); - } - - public static function provideData(): Iterator - { - return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.xml'); - } -} diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php index baacfecf813..ddb3ba6577d 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php @@ -7,13 +7,27 @@ use Iterator; use Nette\Utils\FileSystem; use PHPUnit\Framework\Attributes\DataProvider; -use Symplify\ConfigTransformer\Tests\Converter\ConfigFormatConverter\AbstractConfigFormatConverterTestCase; +use Symfony\Component\Finder\SplFileInfo; +use Symplify\ConfigTransformer\Converter\ConfigFormatConverter; +use Symplify\ConfigTransformer\FileSystem\RelativeFilePathHelper; +use Symplify\ConfigTransformer\Tests\AbstractTestCase; use Symplify\ConfigTransformer\Tests\Helper\FixtureFinder; +use Symplify\ConfigTransformer\Tests\Helper\FixtureSplitter; +use Symplify\ConfigTransformer\Tests\Helper\FixtureUpdater; -final class YamlToPhpTest extends AbstractConfigFormatConverterTestCase +final class YamlToPhpTest extends AbstractTestCase { + private ConfigFormatConverter $configFormatConverter; + + protected function setUp(): void + { + parent::setUp(); + + $this->configFormatConverter = $this->getService(ConfigFormatConverter::class); + } + #[DataProvider('provideDataForRouting')] - public function testRouting(\SplFileInfo $fileInfo): void + public function testRouting(SplFileInfo $fileInfo): void { $this->doTestOutput($fileInfo, true); } @@ -28,11 +42,13 @@ public static function provideDataForRouting(): Iterator #[DataProvider('provideData')] #[DataProvider('provideDataWithPhpImported')] - public function testNormal(\SplFileInfo $fixtureFileInfo): void + public function testNormal(SplFileInfo $fixtureFileInfo): void { // for imports - $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); - $this->smartFileSystem->mirror(__DIR__ . '/Fixture/normal', $temporaryPath); + $temporaryPath = FixtureSplitter::getTemporaryPath(); + + $filesystem = new \Symfony\Component\Filesystem\Filesystem(); + $filesystem->mirror(__DIR__ . '/Fixture/normal', $temporaryPath); // for the "resource: items/" FileSystem::createDir($temporaryPath . '/items'); @@ -55,13 +71,13 @@ public function testNormal(\SplFileInfo $fixtureFileInfo): void } #[DataProvider('provideDataWithDirectory')] - public function testSpecialCaseWithDirectory(\SplFileInfo $fileInfo): void + public function testSpecialCaseWithDirectory(SplFileInfo $fileInfo): void { $this->doTestOutputWithExtraDirectory($fileInfo, __DIR__ . '/Fixture/nested'); } #[DataProvider('provideDataExtension')] - public function testEcs(\SplFileInfo $fileInfo): void + public function testEcs(SplFileInfo $fileInfo): void { $this->doTestOutputWithExtraDirectory($fileInfo, $fileInfo->getPath()); } @@ -70,11 +86,11 @@ public function testEcs(\SplFileInfo $fileInfo): void * @source https://github.com/symfony/maker-bundle/pull/604 */ #[DataProvider('provideDataMakerBundle')] - public function testMakerBundle(\SplFileInfo $fileInfo): void + public function testMakerBundle(SplFileInfo $fileInfo): void { // needed for all the included - $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); - $this->smartFileSystem->dumpFile( + $temporaryPath = FixtureSplitter::getTemporaryPath(); + FileSystem::write( $temporaryPath . '/../src/SomeClass.php', ' + * @return Iterator */ public static function provideDataMakerBundle(): Iterator { return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/maker-bundle', '*.yaml'); } - private function doTestOutputWithExtraDirectory(\SplFileInfo $fixtureFileInfo, string $extraDirectory): void + private function doTestOutput(SplFileInfo $fixtureFileInfo, bool $preserveDirStructure = false): void + { + $inputAndExpected = FixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); + + $this->doTestFileInfo($inputAndExpected->getInputFileInfo(), $inputAndExpected->getExpectedFileContent(), $fixtureFileInfo); + } + + private function doTestFileInfo(SplFileInfo $inputFileInfo, string $expectedContent, SplFileInfo $fixtureFileInfo): void { - $inputAndExpected = StaticFixtureSplitter::splitFileInfoToInputAndExpected($fixtureFileInfo); - $temporaryPath = StaticFixtureSplitter::getTemporaryPath(); + $convertedContent = $this->configFormatConverter->convert($inputFileInfo); + FixtureUpdater::updateFixtureContent($inputFileInfo, $convertedContent, $fixtureFileInfo); + + $filePath = RelativeFilePathHelper::resolveFromDirectory($fixtureFileInfo->getRealPath(), getcwd()); + + $this->assertSame($expectedContent, $convertedContent, $filePath); + } + + private function doTestOutputWithExtraDirectory(SplFileInfo $fixtureFileInfo, string $extraDirectory): void + { + $inputAndExpected = FixtureSplitter::splitFileInfoToInputAndExpected($fixtureFileInfo); + $temporaryPath = FixtureSplitter::getTemporaryPath(); // copy /src to temp directory, so Symfony FileLocator knows about it - $this->smartFileSystem->mirror($extraDirectory, $temporaryPath, null, [ + $fileSystem = new \Symfony\Component\Filesystem\Filesystem(); + $fileSystem->mirror($extraDirectory, $temporaryPath, null, [ 'override' => true, ]); - $fileTemporaryPath = $temporaryPath . '/' . $fixtureFileInfo->getRelativeFilePathFromDirectory($extraDirectory); + $fileTemporaryPath = $temporaryPath . '/' . RelativeFilePathHelper::resolveFromDirectory($fixtureFileInfo->getRealPath(), $extraDirectory); + FileSystem::write($fileTemporaryPath, $inputAndExpected->getInput()); // require class to autoload it @@ -145,8 +180,7 @@ private function doTestOutputWithExtraDirectory(\SplFileInfo $fixtureFileInfo, s require_once $expectedFilePath; - $inputFileInfo = new \SplFileInfo($fileTemporaryPath); - + $inputFileInfo = new SplFileInfo($fileTemporaryPath, '', ''); $this->doTestFileInfo($inputFileInfo, $inputAndExpected->getExpected(), $fixtureFileInfo); } } diff --git a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php index b55c4283946..bf6857e553c 100644 --- a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php +++ b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php @@ -15,7 +15,7 @@ protected function setUp(): void { parent::setUp(); - $this->yamlToPhpConverter = $this->container->get(YamlToPhpConverter::class); + $this->yamlToPhpConverter = $this->getService(YamlToPhpConverter::class); } public function test(): void diff --git a/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php b/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php index ace0785d3cc..18874bf2e62 100644 --- a/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php +++ b/tests/Finder/ConfigFileFinder/ConfigFileFinderTest.php @@ -4,37 +4,17 @@ namespace Symplify\ConfigTransformer\Tests\Finder\ConfigFileFinder; +use PHPUnit\Framework\TestCase; use Symplify\ConfigTransformer\Finder\ConfigFileFinder; -<<<<<<< HEAD -<<<<<<< HEAD -use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; -======= -use Symplify\ConfigTransformer\Tests\AbstractTestCase; ->>>>>>> cd67f40 (bump) -use Symplify\ConfigTransformer\ValueObject\Configuration; - -final class ConfigFileFinderTest extends AbstractTestCase -{ - private ConfigFileFinder $configFileFinder; - - protected function setUp(): void - { - $this->configFileFinder = $this->container->get(ConfigFileFinder::class); - } - -======= -use Symplify\ConfigTransformer\Tests\AbstractTestCase; /** * @see \Symplify\ConfigTransformer\Finder\ConfigFileFinder */ -final class ConfigFileFinderTest extends AbstractTestCase +final class ConfigFileFinderTest extends TestCase { ->>>>>>> 8c52b6b (fixup! fixup! fixup! fixup! fixup! fixup! bump) public function test(): void { $configFileFinder = new ConfigFileFinder(); - ; $fileInfos = $configFileFinder->findFileInfos([__DIR__ . '/Fixture']); $this->assertCount(1, $fileInfos); diff --git a/tests/Fixture/some.xml b/tests/Fixture/some.xml deleted file mode 100644 index f202ddcc505..00000000000 --- a/tests/Fixture/some.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - diff --git a/tests/Helper/FixtureFinder.php b/tests/Helper/FixtureFinder.php index 20ac024016d..172080a3f96 100644 --- a/tests/Helper/FixtureFinder.php +++ b/tests/Helper/FixtureFinder.php @@ -5,8 +5,8 @@ namespace Symplify\ConfigTransformer\Tests\Helper; use Iterator; -use SplFileInfo; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; final class FixtureFinder { diff --git a/tests/Helper/FixtureSplitter.php b/tests/Helper/FixtureSplitter.php index aba2802a892..c94a1d611ef 100644 --- a/tests/Helper/FixtureSplitter.php +++ b/tests/Helper/FixtureSplitter.php @@ -4,7 +4,122 @@ namespace Symplify\ConfigTransformer\Tests\Helper; +use Nette\Utils\FileSystem; +use Nette\Utils\Strings; +use Symfony\Component\Finder\SplFileInfo; +use Symplify\ConfigTransformer\Tests\Helper\ValueObject\InputAndExpected; +use Symplify\ConfigTransformer\Tests\Helper\ValueObject\InputFileInfoAndExpectedFileInfo; + final class FixtureSplitter { + /** + * @see https://regex101.com/r/8fuULy/1 + * @var string + */ + private const SPLIT_LINE_REGEX = "#\-\-\-\-\-\r?\n#"; + + public static ?string $customTemporaryPath = null; + + public static function splitFileInfoToInputAndExpected(SplFileInfo $smartFileInfo): InputAndExpected + { + $splitLineCount = count(Strings::matchAll($smartFileInfo->getContents(), self::SPLIT_LINE_REGEX)); + + // if more or less, it could be a test cases for monorepo line in it + if ($splitLineCount === 1) { + // input → expected + [$input, $expected] = Strings::split($smartFileInfo->getContents(), self::SPLIT_LINE_REGEX); + + $expected = self::retypeExpected($expected); + + return new InputAndExpected($input, $expected); + } + + // no changes + return new InputAndExpected($smartFileInfo->getContents(), $smartFileInfo->getContents()); + } + + public static function splitFileInfoToLocalInputAndExpectedFileInfos( + SplFileInfo $smartFileInfo, + bool $autoloadTestFixture = false, + bool $preserveDirStructure = false + ): InputFileInfoAndExpectedFileInfo { + $inputAndExpected = self::splitFileInfoToInputAndExpected($smartFileInfo); + + $prefix = ''; + if ($preserveDirStructure) { + $dir = explode('Fixture', $smartFileInfo->getRealPath(), 2); + $prefix = isset($dir[1]) ? dirname($dir[1]) . '/' : ''; + $prefix = ltrim($prefix, '/\\'); + } + + $inputFileInfo = self::createTemporaryFileInfo( + $smartFileInfo, + $prefix . 'input', + $inputAndExpected->getInput() + ); + + // some files needs to be autoload to enable reflection + if ($autoloadTestFixture) { + require_once $inputFileInfo->getRealPath(); + } + + $expectedFileInfo = self::createTemporaryFileInfo( + $smartFileInfo, + $prefix . 'expected', + $inputAndExpected->getExpected() + ); + + return new InputFileInfoAndExpectedFileInfo($inputFileInfo, $expectedFileInfo); + } + + public static function getTemporaryPath(): string + { + if (self::$customTemporaryPath !== null) { + return self::$customTemporaryPath; + } + + return sys_get_temp_dir() . '/_temp_fixture_easy_testing'; + } + + public static function createTemporaryFileInfo( + SplFileInfo $fixtureSmartFileInfo, + string $prefix, + string $fileContent + ): SplFileInfo { + $temporaryFilePath = self::createTemporaryPathWithPrefix($fixtureSmartFileInfo, $prefix); + $dir = dirname($temporaryFilePath); + if (! is_dir($dir)) { + mkdir($dir, 0777, true); + } + + FileSystem::write($temporaryFilePath, $fileContent); + return new SplFileInfo($temporaryFilePath, '', ''); + } + + private static function createTemporaryPathWithPrefix(SplFileInfo $smartFileInfo, string $prefix): string + { + $hash = Strings::substring(md5($smartFileInfo->getRealPath()), -20); + + $fileBasename = $smartFileInfo->getBasename('.inc'); + + return self::getTemporaryPath() . sprintf('/%s_%s_%s', $prefix, $hash, $fileBasename); + } + + private static function retypeExpected(mixed $expected): mixed + { + if (! is_numeric(trim((string) $expected))) { + return $expected; + } + + // value re-type + if (strlen((string) (int) $expected) === strlen(trim((string) $expected))) { + return (int) $expected; + } + + if (strlen((string) (float) $expected) === strlen(trim((string) $expected))) { + return (float) $expected; + } + return $expected; + } } diff --git a/tests/Helper/FixtureUpdater.php b/tests/Helper/FixtureUpdater.php new file mode 100644 index 00000000000..c47a2c7580d --- /dev/null +++ b/tests/Helper/FixtureUpdater.php @@ -0,0 +1,52 @@ +getRealPath(), $newOriginalContent); + } + + public static function updateExpectedFixtureContent( + string $newOriginalContent, + SplFileInfo $expectedFixtureFileInfo + ): void { + if (! getenv('UPDATE_TESTS') && ! getenv('UT')) { + return; + } + + FileSystem::write($expectedFixtureFileInfo->getRealPath(), $newOriginalContent); + } + + private static function resolveNewFixtureContent( + SplFileInfo|string $originalFileInfo, + string $changedContent + ): string { + if ($originalFileInfo instanceof SplFileInfo) { + $originalContent = $originalFileInfo->getContents(); + } else { + $originalContent = $originalFileInfo; + } + + if ($originalContent === $changedContent) { + return $originalContent; + } + + return $originalContent . '-----' . PHP_EOL . $changedContent; + } +} diff --git a/tests/Helper/ValueObject/InputAndExpected.php b/tests/Helper/ValueObject/InputAndExpected.php new file mode 100644 index 00000000000..c6a95b73061 --- /dev/null +++ b/tests/Helper/ValueObject/InputAndExpected.php @@ -0,0 +1,24 @@ +input; + } + + public function getExpected(): string + { + return $this->expected; + } +} diff --git a/tests/Helper/ValueObject/InputFileInfoAndExpectedFileInfo.php b/tests/Helper/ValueObject/InputFileInfoAndExpectedFileInfo.php new file mode 100644 index 00000000000..43d2e8ae007 --- /dev/null +++ b/tests/Helper/ValueObject/InputFileInfoAndExpectedFileInfo.php @@ -0,0 +1,36 @@ +inputFileInfo; + } + + public function getExpectedFileInfo(): SplFileInfo + { + return $this->expectedFileInfo; + } + + public function getExpectedFileContent(): string + { + return $this->expectedFileInfo->getContents(); + } + + public function getExpectedFileInfoRealPath(): string + { + return $this->expectedFileInfo->getRealPath(); + } +} From 85a62a96b4c366fa045cfd86f181847b411aac5a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Dec 2023 11:20:45 +0100 Subject: [PATCH 4/7] simplify test set --- bin/config-transformer.php | 7 +- composer.json | 1 - config/config.php | 17 ---- src/Command/SwitchFormatCommand.php | 2 +- src/Console/ConfigTransformerApplication.php | 8 +- src/Console/ConsoleDiffer.php | 15 +++- src/Console/DifferFactory.php | 23 ------ .../Fixture/nested/some_config/defaults.yaml | 27 ------- .../Fixture/nested/some_config/exclude.yaml | 30 ------- .../Fixture/nested/some_config/import_me.yml | 2 - .../Fixture/nested/some_config/imports.yaml | 12 --- .../Fixture/nested/src/SomeClass.php | 9 --- .../default_with_bind.yaml | 0 .../extension_nested_merge_double_key.yaml} | 0 .../normal/import_already_converted.php | 11 --- .../{maker-bundle => normal}/parameters.yaml | 0 .../{maker-bundle => normal}/reference.yaml | 0 .../resource_arguments_and_bind.yaml | 0 .../services_alias.yaml | 0 .../services_arguments.yaml | 0 .../services_calls.yaml | 0 .../services_configurator.yaml | 0 .../services_decoration.yaml | 0 .../services_deprecation.yaml | 0 .../services_factory.yaml | 0 .../services_imports.yaml | 0 .../services_lazy.yaml | 0 .../services_load_resources_simple.yaml | 0 .../services_null_value.yaml | 0 .../services_parent.yaml | 0 .../services_shared.yaml | 0 .../services_tags.yaml | 0 .../Fixture/normal/services_test.yaml | 23 ------ .../YamlToPhp/YamlToPhpTest.php | 81 ------------------- 34 files changed, 26 insertions(+), 242 deletions(-) delete mode 100644 src/Console/DifferFactory.php delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/defaults.yaml delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/exclude.yaml delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/import_me.yml delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/imports.yaml delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/src/SomeClass.php rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/default_with_bind.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{extension/nested_merge_double_key.yaml => normal/extension_nested_merge_double_key.yaml} (100%) delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/import_already_converted.php rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/parameters.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/reference.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/resource_arguments_and_bind.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_alias.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_arguments.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_calls.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_configurator.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_decoration.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_deprecation.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_factory.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_imports.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_lazy.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_load_resources_simple.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_null_value.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_parent.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_shared.yaml (100%) rename tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/{maker-bundle => normal}/services_tags.yaml (100%) delete mode 100644 tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_test.yaml diff --git a/bin/config-transformer.php b/bin/config-transformer.php index 688ccefb673..62d283c7bc1 100755 --- a/bin/config-transformer.php +++ b/bin/config-transformer.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Output\ConsoleOutput; use Symplify\ConfigTransformer\Kernel\ConfigTransformerKernel; $possibleAutoloadPaths = [ @@ -31,4 +33,7 @@ $container = $configTransformerKernel->getContainer(); $configTransformerApplication = $container->get(\Symplify\ConfigTransformer\Console\ConfigTransformerApplication::class); -$configTransformerApplication->run(); + +$input = new ArgvInput(); +$output = new ConsoleOutput(); +$configTransformerApplication->run($input, $output); diff --git a/composer.json b/composer.json index 20ec396f350..1b178d43d0a 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,6 @@ "php": ">=8.1", "ext-dom": "*", "nette/utils": "^3.2", - "nikic/php-parser": "^4.18", "symfony/console": "^6.4", "symfony/dependency-injection": "6.1.*", "symfony/expression-language": "^6.4", diff --git a/config/config.php b/config/config.php index 8b4da9784a5..5e3589ccea7 100644 --- a/config/config.php +++ b/config/config.php @@ -2,17 +2,10 @@ declare(strict_types=1); -use PhpParser\BuilderFactory; -use PhpParser\NodeFinder; -use SebastianBergmann\Diff\Differ; use Symfony\Component\Console\Application; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symfony\Component\Yaml\Parser; -use Symplify\ConfigTransformer\Console\ColorConsoleDiffFormatter; use Symplify\ConfigTransformer\Console\ConfigTransformerApplication; -use Symplify\ConfigTransformer\Console\ConsoleDiffer; -use Symplify\ConfigTransformer\Console\DifferFactory; use Symplify\ConfigTransformer\Console\Style\SymfonyStyleFactory; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; @@ -35,16 +28,6 @@ // console $services->alias(Application::class, ConfigTransformerApplication::class); - // color diff - $services->set(Differ::class) - ->factory([service(DifferFactory::class), 'create']); - $services->set(ConsoleDiffer::class); - $services->set(ColorConsoleDiffFormatter::class); - $services->set(SymfonyStyle::class) ->factory([service(SymfonyStyleFactory::class), 'create']); - - $services->set(BuilderFactory::class); - $services->set(NodeFinder::class); - $services->set(Parser::class); }; diff --git a/src/Command/SwitchFormatCommand.php b/src/Command/SwitchFormatCommand.php index 5c5ff159e5b..bdb1af4ddd5 100644 --- a/src/Command/SwitchFormatCommand.php +++ b/src/Command/SwitchFormatCommand.php @@ -48,7 +48,7 @@ protected function configure(): void [getcwd() . '/config', getcwd() . '/app/config'] ); - $this->addOption(Option::DRY_RUN, 'n', InputOption::VALUE_NONE, 'Dry run - no removal or config change'); + $this->addOption(Option::DRY_RUN, null, InputOption::VALUE_NONE, 'Dry run - no removal or config change'); } protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Console/ConfigTransformerApplication.php b/src/Console/ConfigTransformerApplication.php index 1a501ddcfb5..177f8c06e82 100644 --- a/src/Console/ConfigTransformerApplication.php +++ b/src/Console/ConfigTransformerApplication.php @@ -17,7 +17,11 @@ public function __construct(SwitchFormatCommand $switchFormatCommand) $this->add($switchFormatCommand); - // make single command application - $this->setDefaultCommand($switchFormatCommand->getName(), true); + // hid unnecesary command + $this->get('help')->setHidden(); + $this->get('completion')->setHidden(); + + // make single command application for fast run + $this->setDefaultCommand($switchFormatCommand->getName()); } } diff --git a/src/Console/ConsoleDiffer.php b/src/Console/ConsoleDiffer.php index 4725d5a8d7d..c8d42d8a896 100644 --- a/src/Console/ConsoleDiffer.php +++ b/src/Console/ConsoleDiffer.php @@ -5,18 +5,29 @@ namespace Symplify\ConfigTransformer\Console; use SebastianBergmann\Diff\Differ; +use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; +use Symplify\ConfigTransformer\Reflection\PrivatesAccessor; final class ConsoleDiffer { public function __construct( - private readonly Differ $differ, private readonly ColorConsoleDiffFormatter $colorConsoleDiffFormatter ) { } public function diff(string $old, string $new): string { - $diff = $this->differ->diff($old, $new); + $differ = $this->createDiffer(); + + $diff = $differ->diff($old, $new); return $this->colorConsoleDiffFormatter->format($diff); } + + private function createDiffer(): Differ + { + $unifiedDiffOutputBuilder = new UnifiedDiffOutputBuilder(''); + PrivatesAccessor::writePrivateProperty($unifiedDiffOutputBuilder, 'contextLines', 10000); + + return new Differ($unifiedDiffOutputBuilder); + } } diff --git a/src/Console/DifferFactory.php b/src/Console/DifferFactory.php deleted file mode 100644 index e479f88592c..00000000000 --- a/src/Console/DifferFactory.php +++ /dev/null @@ -1,23 +0,0 @@ -services(); - - $services->defaults() - ->autowire() - ->public(); - - $services->set(Filesystem::class); - - $services->set(FinderSanitizer::class); -}; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/exclude.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/exclude.yaml deleted file mode 100644 index 7284ee0a44e..00000000000 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/exclude.yaml +++ /dev/null @@ -1,30 +0,0 @@ -services: - _defaults: - autowire: true - public: true - - Symplify\Autodiscovery\: - resource: '../src' - exclude: - - '../src/HttpKernel/*' - - '../src/That/*' ------ -services(); - - $services->defaults() - ->autowire() - ->public(); - - $services->load('Symplify\Autodiscovery\\', __DIR__ . '/../src') - ->exclude([ - __DIR__ . '/../src/HttpKernel/*', - __DIR__ . '/../src/That/*', - ]); -}; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/import_me.yml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/import_me.yml deleted file mode 100644 index 42946fd6307..00000000000 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/import_me.yml +++ /dev/null @@ -1,2 +0,0 @@ -parameters: - key: value diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/imports.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/imports.yaml deleted file mode 100644 index 02dc5b1363d..00000000000 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/some_config/imports.yaml +++ /dev/null @@ -1,12 +0,0 @@ -imports: - - { resource: 'import_me.yml' } ------ -import(__DIR__ . '/import_me.php'); -}; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/src/SomeClass.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/src/SomeClass.php deleted file mode 100644 index 5a2960cd0db..00000000000 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/nested/src/SomeClass.php +++ /dev/null @@ -1,9 +0,0 @@ -extension('hautelook_alice', [ - 'fixtures_path' => 'fixtures', - ]); -}; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/parameters.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/parameters.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/parameters.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/parameters.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/reference.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/reference.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/reference.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/reference.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/resource_arguments_and_bind.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/resource_arguments_and_bind.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/resource_arguments_and_bind.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/resource_arguments_and_bind.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_alias.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_alias.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_alias.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_alias.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_arguments.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_arguments.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_arguments.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_arguments.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_calls.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_calls.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_calls.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_calls.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_configurator.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_configurator.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_configurator.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_configurator.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_decoration.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_decoration.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_decoration.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_decoration.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_deprecation.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_deprecation.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_deprecation.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_deprecation.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_factory.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_factory.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_factory.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_factory.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_imports.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_imports.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_imports.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_imports.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_lazy.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_lazy.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_lazy.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_lazy.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_load_resources_simple.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_load_resources_simple.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_load_resources_simple.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_load_resources_simple.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_null_value.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_null_value.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_null_value.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_null_value.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_parent.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_parent.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_parent.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_parent.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_shared.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_shared.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_shared.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_shared.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_tags.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_tags.yaml similarity index 100% rename from tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/maker-bundle/services_tags.yaml rename to tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_tags.yaml diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_test.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_test.yaml deleted file mode 100644 index c44d85186fc..00000000000 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/normal/services_test.yaml +++ /dev/null @@ -1,23 +0,0 @@ -services: - _defaults: - autowire: true - autoconfigure: true - - App\Tests\Behat\: - resource: 'tests/Behat/*' ------ -services(); - - $services->defaults() - ->autowire() - ->autoconfigure(); - - $services->load('App\Tests\Behat\\', __DIR__ . '/tests/Behat/*'); -}; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php index ddb3ba6577d..260a51a57e5 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php @@ -70,38 +70,6 @@ public function testNormal(SplFileInfo $fixtureFileInfo): void $this->doTestOutput($fixtureFileInfo); } - #[DataProvider('provideDataWithDirectory')] - public function testSpecialCaseWithDirectory(SplFileInfo $fileInfo): void - { - $this->doTestOutputWithExtraDirectory($fileInfo, __DIR__ . '/Fixture/nested'); - } - - #[DataProvider('provideDataExtension')] - public function testEcs(SplFileInfo $fileInfo): void - { - $this->doTestOutputWithExtraDirectory($fileInfo, $fileInfo->getPath()); - } - - /** - * @source https://github.com/symfony/maker-bundle/pull/604 - */ - #[DataProvider('provideDataMakerBundle')] - public function testMakerBundle(SplFileInfo $fileInfo): void - { - // needed for all the included - $temporaryPath = FixtureSplitter::getTemporaryPath(); - FileSystem::write( - $temporaryPath . '/../src/SomeClass.php', - 'doTestOutput($fileInfo); - } - /** * @return Iterator */ @@ -118,30 +86,6 @@ public static function provideDataWithPhpImported(): Iterator return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/skip-imported-php', '*.yaml'); } - /** - * @return Iterator - */ - public static function provideDataExtension(): Iterator - { - return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/extension', '*.yaml'); - } - - /** - * @return Iterator - */ - public static function provideDataWithDirectory(): Iterator - { - return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/nested', '*.yaml'); - } - - /** - * @return Iterator - */ - public static function provideDataMakerBundle(): Iterator - { - return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/maker-bundle', '*.yaml'); - } - private function doTestOutput(SplFileInfo $fixtureFileInfo, bool $preserveDirStructure = false): void { $inputAndExpected = FixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); @@ -158,29 +102,4 @@ private function doTestFileInfo(SplFileInfo $inputFileInfo, string $expectedCont $this->assertSame($expectedContent, $convertedContent, $filePath); } - - private function doTestOutputWithExtraDirectory(SplFileInfo $fixtureFileInfo, string $extraDirectory): void - { - $inputAndExpected = FixtureSplitter::splitFileInfoToInputAndExpected($fixtureFileInfo); - $temporaryPath = FixtureSplitter::getTemporaryPath(); - - // copy /src to temp directory, so Symfony FileLocator knows about it - $fileSystem = new \Symfony\Component\Filesystem\Filesystem(); - $fileSystem->mirror($extraDirectory, $temporaryPath, null, [ - 'override' => true, - ]); - - $fileTemporaryPath = $temporaryPath . '/' . RelativeFilePathHelper::resolveFromDirectory($fixtureFileInfo->getRealPath(), $extraDirectory); - - FileSystem::write($fileTemporaryPath, $inputAndExpected->getInput()); - - // require class to autoload it - $expectedFilePath = $temporaryPath . '/src/SomeClass.php'; - $this->assertFileExists($expectedFilePath); - - require_once $expectedFilePath; - - $inputFileInfo = new SplFileInfo($fileTemporaryPath, '', ''); - $this->doTestFileInfo($inputFileInfo, $inputAndExpected->getExpected(), $fixtureFileInfo); - } } From 907f94f0dd336f3e7ea1651dd4810544dd88a55f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Dec 2023 11:29:57 +0100 Subject: [PATCH 5/7] remove dom as unused --- composer.json | 21 ++++++++++--------- config/config.php | 5 ----- .../Command/SwitchFormatCommand.php | 2 +- src/Console/ConfigTransformerApplication.php | 2 +- src/Enum/Format.php | 5 ----- .../YamlToPhp/Fixture/routing/routes.yaml | 3 +-- .../routes_with_defaults_controller.yaml | 3 +-- .../YamlToPhp/Fixture/routing/with_host.yaml | 3 +-- .../YamlToPhp/YamlToPhpTest.php | 21 ++++--------------- .../Fixture/expected_parameters.php | 2 ++ .../YamlToPhpConverterTest.php | 14 ++++--------- 11 files changed, 26 insertions(+), 55 deletions(-) rename src/{ => Console}/Command/SwitchFormatCommand.php (98%) diff --git a/composer.json b/composer.json index 1b178d43d0a..fb9bd1e27ad 100644 --- a/composer.json +++ b/composer.json @@ -7,28 +7,29 @@ ], "require": { "php": ">=8.1", - "ext-dom": "*", "nette/utils": "^3.2", + "symfony/config": "^6.4", "symfony/console": "^6.4", "symfony/dependency-injection": "6.1.*", "symfony/expression-language": "^6.4", + "symfony/filesystem": "^6.4", "symfony/finder": "^6.4", + "symfony/http-kernel": "^6.1", "symfony/yaml": "^6.4", - "symplify/php-config-printer": "^11.3", + "symplify/php-config-printer": "^11.3.6", "webmozart/assert": "^1.11" }, "require-dev": { "cweagans/composer-patches": "^1.7", "php-parallel-lint/php-parallel-lint": "^1.3", - "phpstan/extension-installer": "^1.2", - "phpstan/phpstan": "^1.9", - "phpunit/phpunit": "^10.0", - "rector/rector": "^0.18", - "symfony/framework-bundle": "^6.1", + "phpstan/extension-installer": "^1.3", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^10.5", + "rector/rector": "^0.18.12", "symplify/easy-coding-standard": "^12.0", - "symplify/phpstan-extensions": "^11.1", - "symplify/vendor-patches": "^11.2", - "tomasvotruba/class-leak": "^0.2" + "symplify/phpstan-extensions": "^11.4", + "symplify/vendor-patches": "^11.3", + "tomasvotruba/class-leak": "^0.2.6" }, "autoload": { "psr-4": { diff --git a/config/config.php b/config/config.php index 5e3589ccea7..b086e61487b 100644 --- a/config/config.php +++ b/config/config.php @@ -2,10 +2,8 @@ declare(strict_types=1); -use Symfony\Component\Console\Application; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; -use Symplify\ConfigTransformer\Console\ConfigTransformerApplication; use Symplify\ConfigTransformer\Console\Style\SymfonyStyleFactory; use function Symfony\Component\DependencyInjection\Loader\Configurator\service; @@ -25,9 +23,6 @@ __DIR__ . '/../src/ValueObject', ]); - // console - $services->alias(Application::class, ConfigTransformerApplication::class); - $services->set(SymfonyStyle::class) ->factory([service(SymfonyStyleFactory::class), 'create']); }; diff --git a/src/Command/SwitchFormatCommand.php b/src/Console/Command/SwitchFormatCommand.php similarity index 98% rename from src/Command/SwitchFormatCommand.php rename to src/Console/Command/SwitchFormatCommand.php index bdb1af4ddd5..fe1c0e2e8b6 100644 --- a/src/Command/SwitchFormatCommand.php +++ b/src/Console/Command/SwitchFormatCommand.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Symplify\ConfigTransformer\Command; +namespace Symplify\ConfigTransformer\Console\Command; use Nette\Utils\FileSystem; use Symfony\Component\Console\Command\Command; diff --git a/src/Console/ConfigTransformerApplication.php b/src/Console/ConfigTransformerApplication.php index 177f8c06e82..6dd803ed9ed 100644 --- a/src/Console/ConfigTransformerApplication.php +++ b/src/Console/ConfigTransformerApplication.php @@ -5,7 +5,7 @@ namespace Symplify\ConfigTransformer\Console; use Symfony\Component\Console\Application; -use Symplify\ConfigTransformer\Command\SwitchFormatCommand; +use Symplify\ConfigTransformer\Console\Command\SwitchFormatCommand; final class ConfigTransformerApplication extends Application { diff --git a/src/Enum/Format.php b/src/Enum/Format.php index f9878e01302..bf31d5003d4 100644 --- a/src/Enum/Format.php +++ b/src/Enum/Format.php @@ -16,11 +16,6 @@ final class Format */ public const YML = 'yml'; - /** - * @var string - */ - public const XML = 'xml'; - /** * @var string */ diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes.yaml index c941ceb0f56..b3ba716d8d5 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes.yaml +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes.yaml @@ -12,12 +12,11 @@ research_thank_you: declare(strict_types=1); -use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routingConfigurator): void { $routingConfigurator->add('research_thank_you', 'research/thank-you') - ->controller(TemplateController::class) + ->controller('Symfony\Bundle\FrameworkBundle\Controller\TemplateController') ->defaults([ 'template' => 'research/thank-you.twig', ]) diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes_with_defaults_controller.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes_with_defaults_controller.yaml index b162dc59569..bdce8085c23 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes_with_defaults_controller.yaml +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/routes_with_defaults_controller.yaml @@ -9,12 +9,11 @@ research_thank_you: declare(strict_types=1); -use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routingConfigurator): void { $routingConfigurator->add('research_thank_you', 'research/thank-you') - ->controller(TemplateController::class) + ->controller('Symfony\Bundle\FrameworkBundle\Controller\TemplateController') ->defaults([ 'template' => 'research/thank-you.twig', ]); diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/with_host.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/with_host.yaml index 377781c6e3d..932ec7bef0c 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/with_host.yaml +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/with_host.yaml @@ -7,11 +7,10 @@ with_host: declare(strict_types=1); -use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routingConfigurator): void { $routingConfigurator->add('with_host', 'research/thank-you') ->host('getrector.com') - ->controller(TemplateController::class); + ->controller('Symfony\Bundle\FrameworkBundle\Controller\TemplateController'); }; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php index 260a51a57e5..733e3143ab2 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php @@ -22,7 +22,6 @@ final class YamlToPhpTest extends AbstractTestCase protected function setUp(): void { parent::setUp(); - $this->configFormatConverter = $this->getService(ConfigFormatConverter::class); } @@ -32,9 +31,6 @@ public function testRouting(SplFileInfo $fileInfo): void $this->doTestOutput($fileInfo, true); } - /** - * @return Iterator - */ public static function provideDataForRouting(): Iterator { return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/routing', '*.yaml'); @@ -70,17 +66,11 @@ public function testNormal(SplFileInfo $fixtureFileInfo): void $this->doTestOutput($fixtureFileInfo); } - /** - * @return Iterator - */ public static function provideData(): Iterator { return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/normal', '*.yaml'); } - /** - * @return Iterator - */ public static function provideDataWithPhpImported(): Iterator { return FixtureFinder::yieldDirectory(__DIR__ . '/Fixture/skip-imported-php', '*.yaml'); @@ -90,16 +80,13 @@ private function doTestOutput(SplFileInfo $fixtureFileInfo, bool $preserveDirStr { $inputAndExpected = FixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos($fixtureFileInfo, false, $preserveDirStructure); - $this->doTestFileInfo($inputAndExpected->getInputFileInfo(), $inputAndExpected->getExpectedFileContent(), $fixtureFileInfo); - } - - private function doTestFileInfo(SplFileInfo $inputFileInfo, string $expectedContent, SplFileInfo $fixtureFileInfo): void - { + $inputFileInfo = $inputAndExpected->getInputFileInfo(); $convertedContent = $this->configFormatConverter->convert($inputFileInfo); + + // run `UT=1 vendor/bin/phpunit` to update test fixtures content FixtureUpdater::updateFixtureContent($inputFileInfo, $convertedContent, $fixtureFileInfo); $filePath = RelativeFilePathHelper::resolveFromDirectory($fixtureFileInfo->getRealPath(), getcwd()); - - $this->assertSame($expectedContent, $convertedContent, $filePath); + $this->assertSame($inputAndExpected->getExpectedFileContent(), $convertedContent, $filePath); } } diff --git a/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php b/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php index 4cc3ec4a8db..92036ffe584 100644 --- a/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php +++ b/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php @@ -8,4 +8,6 @@ $parameters = $containerConfigurator->parameters(); $parameters->set('key', 'value'); + + $parameters->set('format', '\d+'); }; diff --git a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php index bf6857e553c..1c01d662b48 100644 --- a/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php +++ b/tests/Converter/YamlToPhpConverter/YamlToPhpConverterTest.php @@ -9,20 +9,14 @@ final class YamlToPhpConverterTest extends AbstractTestCase { - private YamlToPhpConverter $yamlToPhpConverter; - - protected function setUp(): void - { - parent::setUp(); - - $this->yamlToPhpConverter = $this->getService(YamlToPhpConverter::class); - } - public function test(): void { - $printedPhpConfigContent = $this->yamlToPhpConverter->convertYamlArray([ + $yamlToPhpConverter = $this->getService(YamlToPhpConverter::class); + + $printedPhpConfigContent = $yamlToPhpConverter->convertYamlArray([ 'parameters' => [ 'key' => 'value', + 'format' => '\d+', ], ], 'file_path.yaml'); From cb64ae744233db6961a8b509581ac201474fa1e9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Dec 2023 12:04:24 +0100 Subject: [PATCH 6/7] gone --- .github/workflows/code_analysis.yaml | 2 +- src/Converter/YamlToPhpConverter.php | 2 -- ...AliasAndNamespaceConfigurableExtension.php | 34 ------------------- 3 files changed, 1 insertion(+), 37 deletions(-) delete mode 100644 src/ValueObject/DependencyInjection/Extension/AliasAndNamespaceConfigurableExtension.php diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 47141dd701c..9e0d1107441 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -38,7 +38,7 @@ jobs: - name: 'Check Active Classes' - run: vendor/bin/class-leak check src --ansi --skip-type="\Symplify\PhpConfigPrinter\Contract\NodeVisitor\PrePrintNodeVisitorInterface" + run: vendor/bin/class-leak check bin src --ansi --skip-type="\Symplify\PhpConfigPrinter\Contract\NodeVisitor\PrePrintNodeVisitorInterface" name: ${{ matrix.actions.name }} diff --git a/src/Converter/YamlToPhpConverter.php b/src/Converter/YamlToPhpConverter.php index a1ee1a080b5..09245612269 100644 --- a/src/Converter/YamlToPhpConverter.php +++ b/src/Converter/YamlToPhpConverter.php @@ -14,8 +14,6 @@ /** * @api - * @source https://raw.githubusercontent.com/archeoprog/maker-bundle/make-convert-services/src/Util/PhpServicesCreator.php - * * @see \Symplify\ConfigTransformer\Tests\Converter\YamlToPhpConverter\YamlToPhpConverterTest */ final class YamlToPhpConverter diff --git a/src/ValueObject/DependencyInjection/Extension/AliasAndNamespaceConfigurableExtension.php b/src/ValueObject/DependencyInjection/Extension/AliasAndNamespaceConfigurableExtension.php deleted file mode 100644 index c24e09cf844..00000000000 --- a/src/ValueObject/DependencyInjection/Extension/AliasAndNamespaceConfigurableExtension.php +++ /dev/null @@ -1,34 +0,0 @@ -alias; - } - - public function getNamespace(): string - { - return $this->namespace; - } - - /** - * @param string[] $configs - */ - public function load(array $configs, ContainerBuilder $containerBuilder): void - { - } -} From b4072f95d8f69df73be8cdaa8d6c739199e329dd Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Dec 2023 12:07:43 +0100 Subject: [PATCH 7/7] handle param escape later --- .../YamlToPhp/Fixture/routing/requirement_d_param.yaml | 2 +- .../Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php | 2 +- .../YamlToPhpConverter/Fixture/expected_parameters.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/requirement_d_param.yaml b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/requirement_d_param.yaml index 22f293d9e2e..6d20abd2df3 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/requirement_d_param.yaml +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/Fixture/routing/requirement_d_param.yaml @@ -15,6 +15,6 @@ return static function (RoutingConfigurator $routingConfigurator): void { $routingConfigurator->add('site_root', '/{siteId}') ->controller('AppBundle\Controller\Site\IndexController') ->requirements([ - 'siteId' => '\d+', + 'siteId' => 'd+', ]); }; diff --git a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php index 733e3143ab2..42210f299b5 100644 --- a/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php +++ b/tests/Converter/ConfigFormatConverter/YamlToPhp/YamlToPhpTest.php @@ -63,7 +63,7 @@ public function testNormal(SplFileInfo $fixtureFileInfo): void $temporaryPath . '/directory-with-unquoted-strings' ); - $this->doTestOutput($fixtureFileInfo); + $this->doTestOutput($fixtureFileInfo, false); } public static function provideData(): Iterator diff --git a/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php b/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php index 92036ffe584..d395f50d367 100644 --- a/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php +++ b/tests/Converter/YamlToPhpConverter/Fixture/expected_parameters.php @@ -9,5 +9,5 @@ $parameters->set('key', 'value'); - $parameters->set('format', '\d+'); + $parameters->set('format', 'd+'); };