|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\FractorRuleGenerator\Tests\Functional\Console\Command; |
| 6 | + |
| 7 | +use a9f\Fractor\FileSystem\FileInfoFactory; |
| 8 | +use a9f\FractorRuleGenerator\Console\Command\GenerateRuleCommand; |
| 9 | +use a9f\FractorRuleGenerator\Factory\TemplateFactory; |
| 10 | +use a9f\FractorRuleGenerator\FileSystem\ConfigFilesystem; |
| 11 | +use a9f\FractorRuleGenerator\FileSystem\TemplateFileSystem; |
| 12 | +use a9f\FractorRuleGenerator\Finder\TemplateFinder; |
| 13 | +use a9f\FractorRuleGenerator\Generator\FileGenerator; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Console\Application; |
| 16 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 17 | +use Symfony\Component\Console\Tester\CommandTester; |
| 18 | +use Symfony\Component\Filesystem\Filesystem; |
| 19 | + |
| 20 | +final class GenerateRuleCommandTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var array<int, string> |
| 24 | + */ |
| 25 | + private array $testFilesToDelete = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var array<int, string> |
| 29 | + */ |
| 30 | + private array $testDirsToDelete = []; |
| 31 | + |
| 32 | + private CommandTester $commandTester; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $fileSystem = new Filesystem(); |
| 39 | + $templateFactory = new TemplateFactory(); |
| 40 | + $templateFileSystem = new TemplateFileSystem($fileSystem); |
| 41 | + |
| 42 | + $fileInfoFactory = new FileInfoFactory($fileSystem); |
| 43 | + $templateFinder = new TemplateFinder($fileInfoFactory); |
| 44 | + $fileGenerator = new FileGenerator($fileSystem, $templateFactory, $templateFileSystem); |
| 45 | + |
| 46 | + $outputStyle = new ConsoleOutput(); |
| 47 | + |
| 48 | + $configFilesystem = new ConfigFilesystem($fileSystem, $templateFactory); |
| 49 | + |
| 50 | + $createdCommand = new GenerateRuleCommand( |
| 51 | + $templateFinder, |
| 52 | + $fileGenerator, |
| 53 | + $outputStyle, |
| 54 | + $configFilesystem, |
| 55 | + $fileInfoFactory |
| 56 | + ); |
| 57 | + |
| 58 | + $application = new Application(); |
| 59 | + $application->add($createdCommand); |
| 60 | + |
| 61 | + $foundCommand = $application->find('generate-rule'); |
| 62 | + |
| 63 | + $this->commandTester = new CommandTester($foundCommand); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tear down for remove of the test files |
| 68 | + */ |
| 69 | + protected function tearDown(): void |
| 70 | + { |
| 71 | + foreach ($this->testFilesToDelete as $absoluteFileName) { |
| 72 | + if (@is_file($absoluteFileName)) { |
| 73 | + unlink($absoluteFileName); |
| 74 | + } |
| 75 | + } |
| 76 | + foreach ($this->testDirsToDelete as $absoluteDirName) { |
| 77 | + if (@is_dir($absoluteDirName)) { |
| 78 | + self::rmdir($absoluteDirName, true); |
| 79 | + } |
| 80 | + } |
| 81 | + parent::tearDown(); |
| 82 | + } |
| 83 | + |
| 84 | + public function testCreateRuleForFlexForm(): void |
| 85 | + { |
| 86 | + $this->commandTester->setInputs(['7', 'x', 'MigrateFlexForm', 'Migrate FlexForm field', '0']); |
| 87 | + |
| 88 | + $this->commandTester->execute([ |
| 89 | + 'command' => 'generate-rule', |
| 90 | + ]); |
| 91 | + |
| 92 | + self::assertSame(0, $this->commandTester->getStatusCode()); |
| 93 | + |
| 94 | + $basePathConfig = __DIR__ . '/../../../../../typo3-fractor/config'; |
| 95 | + $basePathRules = __DIR__ . '/../../../../../typo3-fractor/rules'; |
| 96 | + $basePathRuleTests = __DIR__ . '/../../../../../typo3-fractor/rules-tests'; |
| 97 | + |
| 98 | + $this->testFilesToDelete[] = $basePathConfig . '/typo3-7.php'; |
| 99 | + self::assertFileExists($basePathConfig . '/typo3-7.php'); |
| 100 | + self::assertFileExists($basePathRules . '/TYPO3v7/FlexForm/MigrateFlexFormFractor.php'); |
| 101 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/FlexForm/MigrateFlexFormFractor/config/fractor.php'); |
| 102 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/FlexForm/MigrateFlexFormFractor/Fixture/fixture.xml'); |
| 103 | + self::assertFileExists( |
| 104 | + $basePathRuleTests . '/TYPO3v7/FlexForm/MigrateFlexFormFractor/MigrateFlexFormFractorTest.php.inc' |
| 105 | + ); |
| 106 | + |
| 107 | + $this->testDirsToDelete[] = $basePathRules . '/TYPO3v7'; |
| 108 | + $this->testDirsToDelete[] = $basePathRuleTests . '/TYPO3v7'; |
| 109 | + } |
| 110 | + |
| 111 | + public function testCreateRuleForFluid(): void |
| 112 | + { |
| 113 | + $this->commandTester->setInputs(['7', 'x', 'MigrateFluid', 'Migrate Fluid field', '1']); |
| 114 | + |
| 115 | + $this->commandTester->execute([ |
| 116 | + 'command' => 'generate-rule', |
| 117 | + ]); |
| 118 | + |
| 119 | + self::assertSame(0, $this->commandTester->getStatusCode()); |
| 120 | + |
| 121 | + $basePathConfig = __DIR__ . '/../../../../../typo3-fractor/config'; |
| 122 | + $basePathRules = __DIR__ . '/../../../../../typo3-fractor/rules'; |
| 123 | + $basePathRuleTests = __DIR__ . '/../../../../../typo3-fractor/rules-tests'; |
| 124 | + |
| 125 | + $this->testFilesToDelete[] = $basePathConfig . '/typo3-7.php'; |
| 126 | + self::assertFileExists($basePathConfig . '/typo3-7.php'); |
| 127 | + self::assertFileExists($basePathRules . '/TYPO3v7/Fluid/MigrateFluidFractor.php'); |
| 128 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Fluid/MigrateFluidFractor/config/fractor.php'); |
| 129 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Fluid/MigrateFluidFractor/Fixture/fixture.html'); |
| 130 | + self::assertFileExists( |
| 131 | + $basePathRuleTests . '/TYPO3v7/Fluid/MigrateFluidFractor/MigrateFluidFractorTest.php.inc' |
| 132 | + ); |
| 133 | + |
| 134 | + $this->testDirsToDelete[] = $basePathRules . '/TYPO3v7'; |
| 135 | + $this->testDirsToDelete[] = $basePathRuleTests . '/TYPO3v7'; |
| 136 | + } |
| 137 | + |
| 138 | + public function testCreateRuleForTypoScript(): void |
| 139 | + { |
| 140 | + $this->commandTester->setInputs(['7', 'x', 'MigrateTypoScript', 'Migrate TypoScript setting', '2']); |
| 141 | + |
| 142 | + $this->commandTester->execute([ |
| 143 | + 'command' => 'generate-rule', |
| 144 | + ]); |
| 145 | + |
| 146 | + self::assertSame(0, $this->commandTester->getStatusCode()); |
| 147 | + |
| 148 | + $basePathConfig = __DIR__ . '/../../../../../typo3-fractor/config'; |
| 149 | + $basePathRules = __DIR__ . '/../../../../../typo3-fractor/rules'; |
| 150 | + $basePathRuleTests = __DIR__ . '/../../../../../typo3-fractor/rules-tests'; |
| 151 | + |
| 152 | + $this->testFilesToDelete[] = $basePathConfig . '/typo3-7.php'; |
| 153 | + self::assertFileExists($basePathConfig . '/typo3-7.php'); |
| 154 | + self::assertFileExists($basePathRules . '/TYPO3v7/TypoScript/MigrateTypoScriptFractor.php'); |
| 155 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/TypoScript/MigrateTypoScriptFractor/config/fractor.php'); |
| 156 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/TypoScript/MigrateTypoScriptFractor/Fixture/fixture.typoscript'); |
| 157 | + self::assertFileExists( |
| 158 | + $basePathRuleTests . '/TYPO3v7/TypoScript/MigrateTypoScriptFractor/MigrateTypoScriptFractorTest.php.inc' |
| 159 | + ); |
| 160 | + |
| 161 | + $this->testDirsToDelete[] = $basePathRules . '/TYPO3v7'; |
| 162 | + $this->testDirsToDelete[] = $basePathRuleTests . '/TYPO3v7'; |
| 163 | + } |
| 164 | + |
| 165 | + public function testCreateRuleForYaml(): void |
| 166 | + { |
| 167 | + $this->commandTester->setInputs(['7', 'x', 'MigrateYaml', 'Migrate Yaml setting', '3']); |
| 168 | + |
| 169 | + $this->commandTester->execute([ |
| 170 | + 'command' => 'generate-rule', |
| 171 | + ]); |
| 172 | + |
| 173 | + self::assertSame(0, $this->commandTester->getStatusCode()); |
| 174 | + |
| 175 | + $basePathConfig = __DIR__ . '/../../../../../typo3-fractor/config'; |
| 176 | + $basePathRules = __DIR__ . '/../../../../../typo3-fractor/rules'; |
| 177 | + $basePathRuleTests = __DIR__ . '/../../../../../typo3-fractor/rules-tests'; |
| 178 | + |
| 179 | + $this->testFilesToDelete[] = $basePathConfig . '/typo3-7.php'; |
| 180 | + self::assertFileExists($basePathConfig . '/typo3-7.php'); |
| 181 | + self::assertFileExists($basePathRules . '/TYPO3v7/Yaml/MigrateYamlFractor.php'); |
| 182 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Yaml/MigrateYamlFractor/config/fractor.php'); |
| 183 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Yaml/MigrateYamlFractor/Fixture/fixture.yaml'); |
| 184 | + self::assertFileExists( |
| 185 | + $basePathRuleTests . '/TYPO3v7/Yaml/MigrateYamlFractor/MigrateYamlFractorTest.php.inc' |
| 186 | + ); |
| 187 | + |
| 188 | + $this->testDirsToDelete[] = $basePathRules . '/TYPO3v7'; |
| 189 | + $this->testDirsToDelete[] = $basePathRuleTests . '/TYPO3v7'; |
| 190 | + } |
| 191 | + |
| 192 | + public function testCreateRuleForComposer(): void |
| 193 | + { |
| 194 | + $this->commandTester->setInputs(['7', 'x', 'MigrateComposer', 'Migrate Composer setting', '4']); |
| 195 | + |
| 196 | + $this->commandTester->execute([ |
| 197 | + 'command' => 'generate-rule', |
| 198 | + ]); |
| 199 | + |
| 200 | + self::assertSame(0, $this->commandTester->getStatusCode()); |
| 201 | + |
| 202 | + $basePathConfig = __DIR__ . '/../../../../../typo3-fractor/config'; |
| 203 | + $basePathRules = __DIR__ . '/../../../../../typo3-fractor/rules'; |
| 204 | + $basePathRuleTests = __DIR__ . '/../../../../../typo3-fractor/rules-tests'; |
| 205 | + |
| 206 | + $this->testFilesToDelete[] = $basePathConfig . '/typo3-7.php'; |
| 207 | + self::assertFileExists($basePathConfig . '/typo3-7.php'); |
| 208 | + self::assertFileExists($basePathRules . '/TYPO3v7/Composer/MigrateComposerFractor.php'); |
| 209 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Composer/MigrateComposerFractor/config/fractor.php'); |
| 210 | + self::assertFileExists($basePathRuleTests . '/TYPO3v7/Composer/MigrateComposerFractor/Fixture/fixture.json'); |
| 211 | + self::assertFileExists( |
| 212 | + $basePathRuleTests . '/TYPO3v7/Composer/MigrateComposerFractor/MigrateComposerFractorTest.php.inc' |
| 213 | + ); |
| 214 | + |
| 215 | + $this->testDirsToDelete[] = $basePathRules . '/TYPO3v7'; |
| 216 | + $this->testDirsToDelete[] = $basePathRuleTests . '/TYPO3v7'; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Wrapper function for rmdir, allowing recursive deletion of folders and files |
| 221 | + * |
| 222 | + * @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally. |
| 223 | + * @param bool $removeNonEmpty Allow deletion of non-empty directories |
| 224 | + * @return bool TRUE if operation was successful |
| 225 | + * @source TYPO3: \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir |
| 226 | + */ |
| 227 | + private static function rmdir(string $path, bool $removeNonEmpty = false): bool |
| 228 | + { |
| 229 | + $OK = false; |
| 230 | + // Remove trailing slash |
| 231 | + $path = preg_replace('|/$|', '', $path) ?? ''; |
| 232 | + $isWindows = DIRECTORY_SEPARATOR === '\\'; |
| 233 | + if (file_exists($path)) { |
| 234 | + $OK = true; |
| 235 | + if (! is_link($path) && is_dir($path)) { |
| 236 | + if ($removeNonEmpty === true && ($handle = @opendir($path))) { |
| 237 | + $entries = []; |
| 238 | + |
| 239 | + while (false !== ($file = readdir($handle))) { |
| 240 | + if ($file === '.') { |
| 241 | + continue; |
| 242 | + } |
| 243 | + if ($file === '..') { |
| 244 | + continue; |
| 245 | + } |
| 246 | + $entries[] = $path . '/' . $file; |
| 247 | + } |
| 248 | + |
| 249 | + closedir($handle); |
| 250 | + |
| 251 | + foreach ($entries as $entry) { |
| 252 | + if (! static::rmdir($entry, true)) { |
| 253 | + $OK = false; |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + if ($OK) { |
| 258 | + $OK = @rmdir($path); |
| 259 | + } |
| 260 | + } elseif (is_link($path) && is_dir($path) && $isWindows) { |
| 261 | + $OK = @rmdir($path); |
| 262 | + } else { |
| 263 | + // If $path is a file, simply remove it |
| 264 | + $OK = @unlink($path); |
| 265 | + } |
| 266 | + clearstatcache(); |
| 267 | + } elseif (is_link($path)) { |
| 268 | + $OK = @unlink($path); |
| 269 | + if (! $OK && $isWindows) { |
| 270 | + // Try to delete dead folder links on Windows systems |
| 271 | + $OK = @rmdir($path); |
| 272 | + } |
| 273 | + clearstatcache(); |
| 274 | + } |
| 275 | + return $OK; |
| 276 | + } |
| 277 | +} |
0 commit comments