diff --git a/src/Bundles/Storage/INI/IniStorage.php b/src/Bundles/Storage/INI/IniStorage.php index 9fc06c1d..4f008cfa 100644 --- a/src/Bundles/Storage/INI/IniStorage.php +++ b/src/Bundles/Storage/INI/IniStorage.php @@ -172,4 +172,9 @@ public function buildFileContentBuffer(Locale $locale, array &$contentBuffer, st return $translationCount; } + + public function getContentFileTemplate(): string + { + return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'StorageFileTemplate.ini') ?: ''; + } } diff --git a/src/Bundles/Storage/INI/Template/StorageFileTemplate.ini b/src/Bundles/Storage/INI/Template/StorageFileTemplate.ini new file mode 100644 index 00000000..e69de29b diff --git a/src/Bundles/Storage/JSON/JsonStorage.php b/src/Bundles/Storage/JSON/JsonStorage.php index 97cbdb1d..d38286e4 100644 --- a/src/Bundles/Storage/JSON/JsonStorage.php +++ b/src/Bundles/Storage/JSON/JsonStorage.php @@ -105,4 +105,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora return new StorageSaveResult(1, 1); } + + public function getContentFileTemplate(): string + { + return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'StorageFileTemplate.json') ?: ''; + } } diff --git a/src/Bundles/Storage/JSON/Template/StorageFileTemplate.json b/src/Bundles/Storage/JSON/Template/StorageFileTemplate.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/src/Bundles/Storage/JSON/Template/StorageFileTemplate.json @@ -0,0 +1 @@ +{} diff --git a/src/Bundles/Storage/PHP/PhpStorage.php b/src/Bundles/Storage/PHP/PhpStorage.php index f31ca752..d7984c12 100644 --- a/src/Bundles/Storage/PHP/PhpStorage.php +++ b/src/Bundles/Storage/PHP/PhpStorage.php @@ -122,4 +122,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora return new StorageSaveResult(1, 1); } + + public function getContentFileTemplate(): string + { + return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'StorageFileTemplate.php') ?: ''; + } } diff --git a/src/Bundles/Storage/PHP/Template/StorageFileTemplate.php b/src/Bundles/Storage/PHP/Template/StorageFileTemplate.php new file mode 100644 index 00000000..0dae23de --- /dev/null +++ b/src/Bundles/Storage/PHP/Template/StorageFileTemplate.php @@ -0,0 +1,5 @@ + + + + diff --git a/src/Bundles/Storage/Shopware6/Shopware6Storage.php b/src/Bundles/Storage/Shopware6/Shopware6Storage.php index 38024301..9f938327 100644 --- a/src/Bundles/Storage/Shopware6/Shopware6Storage.php +++ b/src/Bundles/Storage/Shopware6/Shopware6Storage.php @@ -227,4 +227,9 @@ private function getXmlAdapter(array $locales): ShopwareXmlInterface throw new Exception('No Shopware XML Adapter found for configuration with tag: ' . $xml->getName()); } + + public function getContentFileTemplate(): string + { + return ''; + } } diff --git a/src/Bundles/Storage/StorageFactory.php b/src/Bundles/Storage/StorageFactory.php index de55627d..42fc739c 100644 --- a/src/Bundles/Storage/StorageFactory.php +++ b/src/Bundles/Storage/StorageFactory.php @@ -115,4 +115,15 @@ public function getStorageByFormat(string $name, TranslationSet $set): StorageIn throw new ConfigurationException('No storage found for name: ' . $name); } + + public function getStorageFileTemplate(string $filename): string + { + foreach ($this->storages as $storage) { + if (strtolower($storage->getStorageName()) === strtolower(pathinfo($filename, PATHINFO_EXTENSION))) { + return $storage->getContentFileTemplate(); + } + } + + throw new ConfigurationException('No file template storage found for file: ' . $filename); + } } diff --git a/src/Bundles/Storage/StorageInterface.php b/src/Bundles/Storage/StorageInterface.php index 7c1241d1..feb87a1d 100644 --- a/src/Bundles/Storage/StorageInterface.php +++ b/src/Bundles/Storage/StorageInterface.php @@ -67,4 +67,11 @@ public function saveTranslationLocale(Locale $locale, string $filename): Storage * waiting for certain processes to complete. */ public function saveTranslation(Translation $translation, Locale $locale): StorageSaveResult; + + /** + * Get the content file template to create automate files. + * + * @return string The content file template. + */ + public function getContentFileTemplate(): string; } diff --git a/src/Bundles/Storage/Strings/StringsStorage.php b/src/Bundles/Storage/Strings/StringsStorage.php index f8dfd06e..98df54fe 100644 --- a/src/Bundles/Storage/Strings/StringsStorage.php +++ b/src/Bundles/Storage/Strings/StringsStorage.php @@ -155,4 +155,9 @@ private function getTranslationFromLine(string $line): ?Translation return null; } + + public function getContentFileTemplate(): string + { + return file_get_contents(__DIR__ . '/Template/StorageFileTemplate.strings') ?: ''; + } } diff --git a/src/Bundles/Storage/Strings/Template/StorageFileTemplate.strings b/src/Bundles/Storage/Strings/Template/StorageFileTemplate.strings new file mode 100644 index 00000000..b804f747 --- /dev/null +++ b/src/Bundles/Storage/Strings/Template/StorageFileTemplate.strings @@ -0,0 +1,4 @@ +/* + * Example: + * "views.components.textelementinput.placeholder" = "Geben Sie Ihren Text ein"; +*/ diff --git a/src/Bundles/Storage/YAML/Template/StorageFileTemplate.yaml b/src/Bundles/Storage/YAML/Template/StorageFileTemplate.yaml new file mode 100644 index 00000000..e69de29b diff --git a/src/Bundles/Storage/YAML/YamlStorage.php b/src/Bundles/Storage/YAML/YamlStorage.php index 30adbef5..0e98fff1 100644 --- a/src/Bundles/Storage/YAML/YamlStorage.php +++ b/src/Bundles/Storage/YAML/YamlStorage.php @@ -96,4 +96,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora return new StorageSaveResult(1, 1); } + + public function getContentFileTemplate(): string + { + return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'StorageFileTemplate.yaml') ?: ''; + } } diff --git a/src/Commands/AvailableServicesCommand.php b/src/Commands/AvailableServicesCommand.php index e3099187..416beb89 100644 --- a/src/Commands/AvailableServicesCommand.php +++ b/src/Commands/AvailableServicesCommand.php @@ -50,7 +50,7 @@ public function execute(InputInterface $input, OutputInterface $output): int $configFile = $this->getConfigFile($input); - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $configLoader->load($configFile); # ----------------------------------------------------------------- diff --git a/src/Commands/ExportCommand.php b/src/Commands/ExportCommand.php index 138f3983..8a90fe48 100644 --- a/src/Commands/ExportCommand.php +++ b/src/Commands/ExportCommand.php @@ -71,7 +71,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/FixKeysCommand.php b/src/Commands/FixKeysCommand.php index 5b001522..e33fd826 100644 --- a/src/Commands/FixKeysCommand.php +++ b/src/Commands/FixKeysCommand.php @@ -51,7 +51,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/FixMessCommand.php b/src/Commands/FixMessCommand.php index 156d469a..e0ba43e8 100644 --- a/src/Commands/FixMessCommand.php +++ b/src/Commands/FixMessCommand.php @@ -50,7 +50,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/FixSpellingCommand.php b/src/Commands/FixSpellingCommand.php index a8c6e430..04bf7a0f 100644 --- a/src/Commands/FixSpellingCommand.php +++ b/src/Commands/FixSpellingCommand.php @@ -70,7 +70,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $spellChecker = SpellCheckerFactory::getInstance()->fromService($service, $input->getOptions()); diff --git a/src/Commands/FixStructureCommand.php b/src/Commands/FixStructureCommand.php index baa0b60e..4044d714 100644 --- a/src/Commands/FixStructureCommand.php +++ b/src/Commands/FixStructureCommand.php @@ -52,7 +52,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/ImportCommand.php b/src/Commands/ImportCommand.php index 9c3da82a..48b0ce24 100644 --- a/src/Commands/ImportCommand.php +++ b/src/Commands/ImportCommand.php @@ -79,7 +79,7 @@ public function execute(InputInterface $input, OutputInterface $output): int } - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/ListTranslationKeysCommand.php b/src/Commands/ListTranslationKeysCommand.php index c7faef76..6a17943c 100644 --- a/src/Commands/ListTranslationKeysCommand.php +++ b/src/Commands/ListTranslationKeysCommand.php @@ -47,7 +47,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/ListTranslationsCommand.php b/src/Commands/ListTranslationsCommand.php index 615fca7e..4a17c007 100644 --- a/src/Commands/ListTranslationsCommand.php +++ b/src/Commands/ListTranslationsCommand.php @@ -49,7 +49,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/MigrateCommand.php b/src/Commands/MigrateCommand.php index b6cf84b7..77bd9105 100644 --- a/src/Commands/MigrateCommand.php +++ b/src/Commands/MigrateCommand.php @@ -52,7 +52,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); foreach ($config->getTranslationSets() as $set) { diff --git a/src/Commands/ScanUsageCommand.php b/src/Commands/ScanUsageCommand.php index 19cc5566..0eb0fe8e 100644 --- a/src/Commands/ScanUsageCommand.php +++ b/src/Commands/ScanUsageCommand.php @@ -70,7 +70,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $scanner = ScannerFactory::getInstance()->getScanner($scannerName); diff --git a/src/Commands/StatusCommand.php b/src/Commands/StatusCommand.php index 90685da5..dc8227d0 100644 --- a/src/Commands/StatusCommand.php +++ b/src/Commands/StatusCommand.php @@ -50,7 +50,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/TranslateCommand.php b/src/Commands/TranslateCommand.php index c27aa617..443f8649 100644 --- a/src/Commands/TranslateCommand.php +++ b/src/Commands/TranslateCommand.php @@ -88,7 +88,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/ValidateAllCommand.php b/src/Commands/ValidateAllCommand.php index 50c0cab8..d2a74ead 100644 --- a/src/Commands/ValidateAllCommand.php +++ b/src/Commands/ValidateAllCommand.php @@ -74,7 +74,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); diff --git a/src/Commands/ValidateCoverageCommand.php b/src/Commands/ValidateCoverageCommand.php index c8a9e7c6..edb987e0 100644 --- a/src/Commands/ValidateCoverageCommand.php +++ b/src/Commands/ValidateCoverageCommand.php @@ -51,7 +51,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $covSuccess = $coverageCLI->execute($config); diff --git a/src/Commands/ValidateMessCommand.php b/src/Commands/ValidateMessCommand.php index e30daa21..415aa342 100644 --- a/src/Commands/ValidateMessCommand.php +++ b/src/Commands/ValidateMessCommand.php @@ -59,7 +59,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $validatorResult = $validatorCLI->execute($config->getTranslationSets()); diff --git a/src/Commands/ValidateSimilarityCommand.php b/src/Commands/ValidateSimilarityCommand.php index b2bea847..237b3088 100644 --- a/src/Commands/ValidateSimilarityCommand.php +++ b/src/Commands/ValidateSimilarityCommand.php @@ -71,7 +71,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $similarityService = new Similarity(); diff --git a/src/Commands/ValidateSpellingCommand.php b/src/Commands/ValidateSpellingCommand.php index 8fad1f30..7d053e16 100644 --- a/src/Commands/ValidateSpellingCommand.php +++ b/src/Commands/ValidateSpellingCommand.php @@ -78,7 +78,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $validatorResult = $validatorCLI->execute($config->getTranslationSets()); diff --git a/src/Commands/ValidateStructureCommand.php b/src/Commands/ValidateStructureCommand.php index 229aa06f..5139cbb2 100644 --- a/src/Commands/ValidateStructureCommand.php +++ b/src/Commands/ValidateStructureCommand.php @@ -67,7 +67,7 @@ public function execute(InputInterface $input, OutputInterface $output): int # ----------------------------------------------------------------- # ----------------------------------------------------------------- - $configLoader = new ConfigurationLoader(new XmlLoader()); + $configLoader = new ConfigurationLoader(new XmlLoader(), $io); $config = $configLoader->load($configFile); $translationSetCLI->showConfig($config->getTranslationSets()); diff --git a/src/Components/Configuration/ConfigurationLoader.php b/src/Components/Configuration/ConfigurationLoader.php index 0c2857f9..7d84d868 100644 --- a/src/Components/Configuration/ConfigurationLoader.php +++ b/src/Components/Configuration/ConfigurationLoader.php @@ -7,6 +7,7 @@ use Exception; use PHPUnuhi\Bundles\Storage\StorageFactory; use PHPUnuhi\Components\Filter\FilterHandler; +use PHPUnuhi\Configuration\Services\CommandPrompt; use PHPUnuhi\Configuration\Services\ConfigurationValidator; use PHPUnuhi\Configuration\Services\CoverageLoader; use PHPUnuhi\Configuration\Services\FilterLoader; @@ -15,6 +16,7 @@ use PHPUnuhi\Configuration\Services\ProtectionLoader; use PHPUnuhi\Configuration\Services\RulesLoader; use PHPUnuhi\Configuration\Services\StyleLoader; +use PHPUnuhi\Configuration\Services\TranslationFile; use PHPUnuhi\Exceptions\ConfigurationException; use PHPUnuhi\Models\Configuration\Attribute; use PHPUnuhi\Models\Configuration\CaseStyleSetting; @@ -27,12 +29,12 @@ use PHPUnuhi\Services\Loaders\Xml\XmlLoaderInterface; use PHPUnuhi\Traits\XmlTrait; use SimpleXMLElement; +use Symfony\Component\Console\Style\SymfonyStyle; class ConfigurationLoader { use XmlTrait; - private XmlLoaderInterface $xmlLoader; private FilterHandler $filterHandler; @@ -58,8 +60,9 @@ class ConfigurationLoader private LocalesPlaceholderProcessor $localesPlaceholderProcessor; + private TranslationFile $translationFile; - public function __construct(XmlLoaderInterface $xmlLoader) + public function __construct(XmlLoaderInterface $xmlLoader, SymfonyStyle $style) { $this->xmlLoader = $xmlLoader; @@ -72,6 +75,7 @@ public function __construct(XmlLoaderInterface $xmlLoader) $this->protectionLoader = new ProtectionLoader(); $this->coverageLoader = new CoverageLoader(); $this->localesPlaceholderProcessor = new LocalesPlaceholderProcessor(); + $this->translationFile = new TranslationFile(new CommandPrompt($style)); } @@ -145,6 +149,8 @@ public function load(string $rootConfigFilename): Configuration throw new ConfigurationException('Invalid configuration! No translation-sets have been found!'); } + $this->translationFile->autoCreate($config); + $this->configValidator->validateConfig($config); return $config; diff --git a/src/Components/Configuration/Services/CommandPrompt.php b/src/Components/Configuration/Services/CommandPrompt.php new file mode 100644 index 00000000..3b17136c --- /dev/null +++ b/src/Components/Configuration/Services/CommandPrompt.php @@ -0,0 +1,26 @@ +styler = $styler; + } + + public function askYesNoQuestion(string $question): bool + { + $answer = $this->styler->askQuestion( + new Question($question, 'yes') + ); + return preg_match('/^(:?yes|j|y|ja)$/i', $answer) === 1; + } +} diff --git a/src/Components/Configuration/Services/LocalesLoader.php b/src/Components/Configuration/Services/LocalesLoader.php index 725f22e9..1c98c57c 100644 --- a/src/Components/Configuration/Services/LocalesLoader.php +++ b/src/Components/Configuration/Services/LocalesLoader.php @@ -17,14 +17,11 @@ class LocalesLoader private LocalesPlaceholderProcessor $placholderProcessor; - - public function __construct() { $this->placholderProcessor = new LocalesPlaceholderProcessor(); } - /** * @throws ConfigurationException * @return array diff --git a/src/Components/Configuration/Services/TranslationFile.php b/src/Components/Configuration/Services/TranslationFile.php new file mode 100644 index 00000000..3b37d8d0 --- /dev/null +++ b/src/Components/Configuration/Services/TranslationFile.php @@ -0,0 +1,58 @@ +commandPrompt = $commandPrompt; + } + + public function autoCreate(Configuration $configuration): void + { + foreach ($configuration->getTranslationSets() as $translationSet) { + foreach ($translationSet->getLocales() as $locale) { + $this->ensureExists($locale); + } + } + } + + private function ensureExists(Locale $locale): void + { + if (file_exists($locale->getFilename())) { + return; + } + + $question = + "Not found: {$locale->getFilename()}" . PHP_EOL . + ' Should be generated?'; + + if ($this->commandPrompt->askYesNoQuestion($question) === false) { + return; + } + + $this->createFile($locale->getFilename()); + } + + private function createFile(string $filename): void + { + $basedir = dirname($filename); + + if (is_dir($basedir) === false) { + mkdir($basedir, 0755, true); + }; + + $content = StorageFactory::getInstance()->getStorageFileTemplate($filename); + + file_put_contents($filename, $content); + } +} diff --git a/tests/phpunit/Bundles/Storage/JSON/JsonStorageTest.php b/tests/phpunit/Bundles/Storage/JSON/JsonStorageTest.php index 06807dbd..dbb3b7c4 100644 --- a/tests/phpunit/Bundles/Storage/JSON/JsonStorageTest.php +++ b/tests/phpunit/Bundles/Storage/JSON/JsonStorageTest.php @@ -43,4 +43,10 @@ public function testHierarchy(): void $this->assertTrue($hierarchy->isNestedStorage()); $this->assertEquals('.', $hierarchy->getDelimiter()); } + + public function testContentFileTemplate(): void + { + $content = $this->storage->getContentFileTemplate(); + $this->assertStringEqualsFile(__DIR__ . '/../../../../../src/Bundles/Storage/JSON/Template/StorageFileTemplate.json', $content); + } } diff --git a/tests/phpunit/Bundles/Storage/PHP/PhpStorageTest.php b/tests/phpunit/Bundles/Storage/PHP/PhpStorageTest.php index 44b7d9d9..03eb8b17 100644 --- a/tests/phpunit/Bundles/Storage/PHP/PhpStorageTest.php +++ b/tests/phpunit/Bundles/Storage/PHP/PhpStorageTest.php @@ -43,4 +43,10 @@ public function testHierarchy(): void $this->assertTrue($hierarchy->isNestedStorage()); $this->assertEquals('.', $hierarchy->getDelimiter()); } + + public function testContentFileTemplate(): void + { + $content = $this->storage->getContentFileTemplate(); + $this->assertStringEqualsFile(__DIR__ . '/../../../../../src/Bundles/Storage/PHP/Template/StorageFileTemplate.php', $content); + } } diff --git a/tests/phpunit/Bundles/Storage/PO/PoStorageTest.php b/tests/phpunit/Bundles/Storage/PO/PoStorageTest.php index a22b64d4..3baf4cef 100644 --- a/tests/phpunit/Bundles/Storage/PO/PoStorageTest.php +++ b/tests/phpunit/Bundles/Storage/PO/PoStorageTest.php @@ -43,4 +43,10 @@ public function testHierarchy(): void $this->assertFalse($hierarchy->isNestedStorage()); $this->assertEquals('', $hierarchy->getDelimiter()); } + + public function testContentFileTemplate(): void + { + $content = $this->storage->getContentFileTemplate(); + $this->assertStringEqualsFile(__DIR__ . '/../../../../../src/Bundles/Storage/PO/Template/StorageFileTemplate.po', $content); + } } diff --git a/tests/phpunit/Bundles/Storage/YAML/YamlStorageTest.php b/tests/phpunit/Bundles/Storage/YAML/YamlStorageTest.php index f18e415a..25e30d32 100644 --- a/tests/phpunit/Bundles/Storage/YAML/YamlStorageTest.php +++ b/tests/phpunit/Bundles/Storage/YAML/YamlStorageTest.php @@ -43,4 +43,10 @@ public function testHierarchy(): void $this->assertTrue($hierarchy->isNestedStorage()); $this->assertEquals('.', $hierarchy->getDelimiter()); } + + public function testContentFileTemplate(): void + { + $content = $this->storage->getContentFileTemplate(); + $this->assertStringEqualsFile(__DIR__ . '/../../../../../src/Bundles/Storage/YAML/Template/StorageFileTemplate.yaml', $content); + } } diff --git a/tests/phpunit/Components/Configuration/ConfigurationLoaderTest.php b/tests/phpunit/Components/Configuration/ConfigurationLoaderTest.php index f414173c..cd6430cd 100644 --- a/tests/phpunit/Components/Configuration/ConfigurationLoaderTest.php +++ b/tests/phpunit/Components/Configuration/ConfigurationLoaderTest.php @@ -15,6 +15,7 @@ use PHPUnuhi\Tests\Utils\Fakes\FakeStorage; use PHPUnuhi\Tests\Utils\Fakes\FakeStorageNoFilter; use PHPUnuhi\Tests\Utils\Fakes\FakeXmlLoader; +use Symfony\Component\Console\Style\SymfonyStyle; class ConfigurationLoaderTest extends TestCase { @@ -24,8 +25,9 @@ class ConfigurationLoaderTest extends TestCase public function testNonExistingFileThrowsException(): void { $this->expectException(Exception::class); + $io = $this->createMock(SymfonyStyle::class); - $loader = new ConfigurationLoader(new XmlLoader()); + $loader = new ConfigurationLoader(new XmlLoader(), $io); $loader->load('not-existing.xml'); } @@ -205,7 +207,12 @@ private function loadXml(string $xml, StorageInterface $storage): Configuration StorageFactory::getInstance()->resetStorages(); StorageFactory::getInstance()->registerStorage($storage); - $loader = new ConfigurationLoader(new FakeXmlLoader($xml)); + $io = $this->createMock(SymfonyStyle::class); + $io->expects($this->any()) + ->method('askQuestion') + ->willReturn('no'); + + $loader = new ConfigurationLoader(new FakeXmlLoader($xml), $io); return $loader->load('not-existing.xml'); } diff --git a/tests/phpunit/Components/Configuration/Services/CommandPromptTest.php b/tests/phpunit/Components/Configuration/Services/CommandPromptTest.php new file mode 100644 index 00000000..b4ecfe92 --- /dev/null +++ b/tests/phpunit/Components/Configuration/Services/CommandPromptTest.php @@ -0,0 +1,45 @@ +createMock(SymfonyStyle::class); + $styler->expects($this->once()) + ->method('askQuestion') + ->willReturn($data->answer); // @phpstan-ignore property.notFound + + $commandPrompt = new CommandPrompt($styler); + $actual = $commandPrompt->askYesNoQuestion('Are you sure?'); + + $this->assertEquals($data->expect, $actual); // @phpstan-ignore property.notFound + } + + /** + * @return array + */ + public function userAnswer(): array + { + return [ + 'yes' => [(object) ['answer' => 'yes', 'expect' => true]], + 'j' => [(object) ['answer' => 'j', 'expect' => true]], + 'y' => [(object) ['answer' => 'y', 'expect' => true]], + 'jA' => [(object) ['answer' => 'jA', 'expect' => true]], + 'nein' => [(object) ['answer' => 'nein', 'expect' => false]], + 'no' => [(object) ['answer' => 'no', 'expect' => false]], + 'n' => [(object) ['answer' => 'n', 'expect' => false]], + 'noyes' => [(object) ['answer' => 'noyes', 'expect' => false]], + ]; + } +} diff --git a/tests/phpunit/Components/Configuration/Services/TranslationFileTest.php b/tests/phpunit/Components/Configuration/Services/TranslationFileTest.php new file mode 100644 index 00000000..c78024c2 --- /dev/null +++ b/tests/phpunit/Components/Configuration/Services/TranslationFileTest.php @@ -0,0 +1,114 @@ +commandPrompt = $this->createMock(CommandPrompt::class); + + $this->locale = $this->createMock(Locale::class); + + $this->translationSet = $this->createMock(TranslationSet::class); + $this->translationSet + ->expects($this->once()) + ->method('getLocales') + ->willReturn([$this->locale]); + + $this->configuration = $this->createMock(Configuration::class); + $this->configuration + ->expects($this->once()) + ->method('getTranslationSets') + ->willReturn([ + $this->translationSet + ]); + + $this->translationFileSubject = new TranslationFile($this->commandPrompt); + } + + public function testNoCreateWithExistingFile(): void + { + //Arrange + /** @phpstan-ignore method.notFound */ + $this->locale + ->expects($this->once()) + ->method('getFilename') + ->willReturn(__FILE__); + + //Assert + /** @phpstan-ignore method.notFound */ + $this->commandPrompt + ->expects($this->never()) + ->method('askYesNoQuestion'); + + //Act + $this->translationFileSubject->autoCreate($this->configuration); + } + + public function testAutoCreateWithNonExistingFileAndUserDeclines(): void + { + //Arrange + /** @phpstan-ignore method.notFound */ + $this->locale + ->expects($this->any()) + ->method('getFilename') + ->willReturn(__DIR__ . '/tranlastionset/de.ini'); + + //Assert + /** @phpstan-ignore method.notFound */ + $this->commandPrompt + ->expects($this->once()) + ->method('askYesNoQuestion') + ->willReturn(false); + + //Act + $this->translationFileSubject->autoCreate($this->configuration); + + //Assert + $this->assertFileDoesNotExist(__DIR__ . '/tranlastionset/de.ini'); + } + + public function testAutoCreateWithNonExistingFileAndUserAccepts(): void + { + //Arrange + /** @phpstan-ignore method.notFound */ + $this->locale + ->expects($this->any()) + ->method('getFilename') + ->willReturn(__DIR__ . '/tranlastionset/de.ini'); + + //Assert + /** @phpstan-ignore method.notFound */ + $this->commandPrompt + ->expects($this->once()) + ->method('askYesNoQuestion') + ->willReturn(true); + + //Act + $this->translationFileSubject->autoCreate($this->configuration); + + //Assert + $this->assertFileExists(__DIR__ . '/tranlastionset/de.ini'); + } + + protected function tearDown(): void + { + @exec('rm -Rf ' . __DIR__ . '/tranlastionset/'); + } +} diff --git a/tests/phpunit/Utils/Fakes/FakeEmptyDelimiterStorage.php b/tests/phpunit/Utils/Fakes/FakeEmptyDelimiterStorage.php index 3a23caf5..62118885 100644 --- a/tests/phpunit/Utils/Fakes/FakeEmptyDelimiterStorage.php +++ b/tests/phpunit/Utils/Fakes/FakeEmptyDelimiterStorage.php @@ -59,4 +59,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora { return new StorageSaveResult(0, 0); } + + public function getContentFileTemplate(): string + { + return ''; + } } diff --git a/tests/phpunit/Utils/Fakes/FakeStorage.php b/tests/phpunit/Utils/Fakes/FakeStorage.php index 2dc49f7d..b287d26f 100644 --- a/tests/phpunit/Utils/Fakes/FakeStorage.php +++ b/tests/phpunit/Utils/Fakes/FakeStorage.php @@ -55,4 +55,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora { return new StorageSaveResult(0, 0); } + + public function getContentFileTemplate(): string + { + return 'fake/content.fake'; + } } diff --git a/tests/phpunit/Utils/Fakes/FakeStorageNoFilter.php b/tests/phpunit/Utils/Fakes/FakeStorageNoFilter.php index 405ff151..8d34b178 100644 --- a/tests/phpunit/Utils/Fakes/FakeStorageNoFilter.php +++ b/tests/phpunit/Utils/Fakes/FakeStorageNoFilter.php @@ -55,4 +55,9 @@ public function saveTranslation(Translation $translation, Locale $locale): Stora { return new StorageSaveResult(0, 0); } + + public function getContentFileTemplate(): string + { + return 'fake'; + } } diff --git a/tests/playground/custom-xml/src/XmlStorage.php b/tests/playground/custom-xml/src/XmlStorage.php index 323f1ddc..77dde4bd 100644 --- a/tests/playground/custom-xml/src/XmlStorage.php +++ b/tests/playground/custom-xml/src/XmlStorage.php @@ -48,5 +48,8 @@ public function saveTranslationLocale(\PHPUnuhi\Models\Translation\Locale $local { } - -} \ No newline at end of file + public function getContentFileTemplate(): string + { + return ""; + } +}