diff --git a/TurboDepot-Php/src/main/php/managers/DepotManager.php b/TurboDepot-Php/src/main/php/managers/DepotManager.php index deb549d..681db99 100644 --- a/TurboDepot-Php/src/main/php/managers/DepotManager.php +++ b/TurboDepot-Php/src/main/php/managers/DepotManager.php @@ -248,7 +248,7 @@ public function getLocalizedFilesManager(){ } $this->_localizedFilesManager = new LocalizedFilesManager($localizedFilesSource->path, - $this->_loadedDepotSetup->localizedFiles->locales, $this->_loadedDepotSetup->localizedFiles->locations, + $this->_loadedDepotSetup->localizedFiles->locales, $this->_loadedDepotSetup->localizedFiles->localesPaths, $this->_loadedDepotSetup->localizedFiles->failIfKeyNotFound); } diff --git a/TurboDepot-Php/src/main/php/managers/LocalizedFilesManager.php b/TurboDepot-Php/src/main/php/managers/LocalizedFilesManager.php index d4b6622..58bfc27 100644 --- a/TurboDepot-Php/src/main/php/managers/LocalizedFilesManager.php +++ b/TurboDepot-Php/src/main/php/managers/LocalizedFilesManager.php @@ -14,7 +14,6 @@ use UnexpectedValueException; -use org\turbocommons\src\main\php\managers\LocalizationManager; use org\turbocommons\src\main\php\utils\StringUtils; use org\turbodepot\src\main\php\model\LocalizedFilesObject; @@ -26,11 +25,11 @@ class LocalizedFilesManager extends FilesManager{ /** - * A LocalizationManager class instance that is used by this class + * A LocalesManager class instance that is used by this class * - * @var LocalizationManager + * @var LocalesManager */ - private $_localizationManager = null; + private $_localesManager = null; /** @@ -41,15 +40,15 @@ class LocalizedFilesManager extends FilesManager{ * * We can have a document that is translated into multiple languages or a folder with different contents depending on the language * - * @see LocalizationManager::initialize + * @see LocalesManager::initialize * * @param string $rootPath We need to specify an existing directory as the entry point where all the localized files and directories are stored. The full OS path must be provided. - * @param array $locales List of languages for which we want to load the translations (see TurboCommons LocalizationManager docs for more info) - * @param array $locations A list (sorted by preference) where each item defines a translations location (see TurboCommons LocalizationManager::initialize docs for more info) + * @param array $locales List of languages for which we want to load the translations (see TurboCommons LocalesManager docs for more info) + * @param array $localesPaths An array of filesystem paths where translations are stored * @param boolean $failIfKeyNotFound If set to true, any operation with a folder or file that is named without a translation key that is available for the currently * defined list of translation locations, will throw an exception. If set to false, the key itself will be used as the folder or file name without throwing an error. */ - public function __construct($rootPath, $locales, $locations, $failIfKeyNotFound = false){ + public function __construct($rootPath, $locales, $localesPaths, $failIfKeyNotFound = false){ if (!is_string($rootPath)){ @@ -65,79 +64,73 @@ public function __construct($rootPath, $locales, $locations, $failIfKeyNotFound parent::__construct($rootPath); - $this->_localizationManager = new LocalizationManager(); + $this->_localesManager = new LocalesManager(); - $this->_localizationManager->missingKeyFormat = $failIfKeyNotFound ? '$exception' : '$key'; + $this->_localesManager->setMissingKeyFormat($failIfKeyNotFound ? '$exception' : '$key'); - $this->_localizationManager->initialize(new FilesManager(), $locales, $locations, function($errors) { - - if(count($errors) > 0){ - - throw new UnexpectedValueException($errors[0]['errorMsg']); - } - }); + $this->_localesManager->initialize($locales, $localesPaths); } /** - * Same behaviour as LocalizationManager::setPrimaryLocale + * Same behaviour as LocalesManager::setPrimaryLocale * - * @see LocalizationManager::setPrimaryLocale + * @see LocalesManager::setPrimaryLocale * - * @param string $locale see LocalizationManager::setPrimaryLocale + * @param string $locale see LocalesManager::setPrimaryLocale * * @return void */ public function setPrimaryLocale(string $locale){ - $this->_localizationManager->setPrimaryLocale($locale); + $this->_localesManager->setPrimaryLocale($locale); } /** - * Same behaviour as LocalizationManager::setPrimaryLocales + * Same behaviour as LocalesManager::setPrimaryLocales * - * @see LocalizationManager::setPrimaryLocales() + * @see LocalesManager::setPrimaryLocales() * - * @param array $locales see LocalizationManager::setPrimaryLocales + * @param array $locales see LocalesManager::setPrimaryLocales * * @return void */ public function setPrimaryLocales(array $locales){ - $this->_localizationManager->setPrimaryLocales($locales); + $this->_localesManager->setPrimaryLocales($locales); } /** - * Same behaviour as LocalizationManager::setPrimaryLanguage + * Same behaviour as LocalesManager::setPrimaryLanguage * - * @see LocalizationManager::setPrimaryLanguage() + * @see LocalesManager::setPrimaryLanguage() * - * @param string $language see LocalizationManager::setPrimaryLanguage + * @param string $language see LocalesManager::setPrimaryLanguage * * @return void */ public function setPrimaryLanguage(string $language){ - $this->_localizationManager->setPrimaryLanguage($language); + $this->_localesManager->setPrimaryLanguage($language); } /** - * Same behaviour as LocalizationManager::setPrimaryLanguages + * Same behaviour as LocalesManager::setPrimaryLanguages * - * @see LocalizationManager::setPrimaryLanguages() - * @see LocalizationManager::setPrimaryLocale() - * @see LocalizationManager::setPrimaryLanguage() + * @see LocalesManager::setPrimaryLanguages() + * @see LocalesManager::setPrimaryLocale() + * @see LocalesManager::setPrimaryLanguage() * - * @param array $languages see LocalizationManager::setPrimaryLanguages() + * @param array $languages see LocalesManager::setPrimaryLanguages() * * @return void */ public function setPrimaryLanguages(array $languages){ - $this->_localizationManager->setPrimaryLanguages($languages); + $this->_localesManager->setPrimaryLanguages($languages); } @@ -164,17 +157,16 @@ public function createDirectory($path, bool $recursive = false){ * (The method ignores the . and .. items if exist). * * @see FilesManager::getDirectoryList - * @see LocalizationManager::get + * @see LocalesManager::get * * @param string $path Absolute or relative path to the directory we want to list - * @param string $bundle The name for the resource bundle file. See LocalizationManager::get for more info - * @param string $location A location label to uniquely reference the location and resolve possible conflicts. See LocalizationManager::get for more info - * @param mixed $toReplace A list of values that will replace the wildcards that are found on the translated text. See LocalizationManager::get for more info + * @param string $bundlePath A string with the format 'library_name/bundle_name' that is used to locate the bundle were the key to translate is found + * @param mixed $toReplace A list of values that will replace the wildcards that are found on the translated text. See LocalesManager::get for more info * @param string $sort See FilesManager::getDirectoryList * * @return array The list of LocalizedFilesObject items inside the specified path sorted as requested, or an empty array if no items found inside the folder. */ - public function getDirectoryList($path, string $bundle = '', string $location = '', $toReplace = [], string $sort = ''){ + public function getDirectoryList($path, string $bundlePath = '', $toReplace = [], string $sort = ''){ $fullPath = $this->_composePath($path); @@ -183,8 +175,8 @@ public function getDirectoryList($path, string $bundle = '', string $location = $fileKeys = []; $list = parent::getDirectoryList($path, $sort); - $language = $this->_localizationManager->languages()[0]; - $locale = $this->_localizationManager->locales()[0]; + $language = $this->_localesManager->getLanguages()[0]; + $locale = $this->_localesManager->getLocales()[0]; foreach ($list as $item) { @@ -193,7 +185,7 @@ public function getDirectoryList($path, string $bundle = '', string $location = $isDirectory = true; $extension = ''; $key = preg_replace("/(.*)-[a-z][a-z]_[A-Z][A-Z]$/", '${1}', $item, 1); - $translation = $this->_localizationManager->get($key, $bundle, $location, $toReplace); + $translation = $this->_localesManager->t($key, $bundlePath, $toReplace); // If the folder contains multiple localized contents, the key is duplicate, so we won't add it to the result if(in_array($key, $dirKeys)){ @@ -210,11 +202,11 @@ public function getDirectoryList($path, string $bundle = '', string $location = $extensionWithDot = $extension === '' ? '' : '.'.$extension; $elementNoExt = StringUtils::getPathElementWithoutExt($item); $key = preg_replace("/(.*)-[a-z][a-z]_[A-Z][A-Z]$/", '${1}', $elementNoExt, 1); - $translation = $this->_localizationManager->get($key, $bundle, $location, $toReplace).$extensionWithDot; + $translation = $this->_localesManager->t($key, $bundlePath, $toReplace).$extensionWithDot; if(preg_match("/.*-[a-z][a-z]_[A-Z][A-Z]$/", $elementNoExt)){ - foreach ($this->_localizationManager->locales() as $managerLocale) { + foreach ($this->_localesManager->getLocales() as $managerLocale) { if(in_array($key.'-'.$managerLocale.$extensionWithDot, $list)){ diff --git a/TurboDepot-Php/src/test/libs/turbocommons-php-3.11.0.phar b/TurboDepot-Php/src/test/libs/turbocommons-php-3.12.0.phar similarity index 79% rename from TurboDepot-Php/src/test/libs/turbocommons-php-3.11.0.phar rename to TurboDepot-Php/src/test/libs/turbocommons-php-3.12.0.phar index b2f49ab..882ca76 100644 Binary files a/TurboDepot-Php/src/test/libs/turbocommons-php-3.11.0.phar and b/TurboDepot-Php/src/test/libs/turbocommons-php-3.12.0.phar differ diff --git a/TurboDepot-Php/src/test/php/managers/DepotManagerTest.php b/TurboDepot-Php/src/test/php/managers/DepotManagerTest.php index e3ccf77..7cf6de1 100644 --- a/TurboDepot-Php/src/test/php/managers/DepotManagerTest.php +++ b/TurboDepot-Php/src/test/php/managers/DepotManagerTest.php @@ -244,11 +244,7 @@ public function testGetLocalizedFilesManager(){ $this->setup->depots[0]->localizedFiles->source = "localized_files_source"; $this->setup->depots[0]->localizedFiles->locales = ['en_US', 'es_ES']; - $this->setup->depots[0]->localizedFiles->locations = [[ - 'label' => 'test-json', - 'path' => __DIR__.'/../../resources/managers/localizedFilesManager/locales/test-json/$locale/$bundle.json', - 'bundles' => ['Locales'] - ]]; + $this->setup->depots[0]->localizedFiles->localesPaths = [__DIR__.'/../../resources/managers/localizedFilesManager/locales']; $this->sut = new DepotManager($this->setup); diff --git a/TurboDepot-Php/src/test/php/managers/LocalizedFilesManagerTest.php b/TurboDepot-Php/src/test/php/managers/LocalizedFilesManagerTest.php index 3504ea6..010d5a3 100644 --- a/TurboDepot-Php/src/test/php/managers/LocalizedFilesManagerTest.php +++ b/TurboDepot-Php/src/test/php/managers/LocalizedFilesManagerTest.php @@ -16,6 +16,7 @@ use org\turbocommons\src\main\php\utils\ArrayUtils; use org\turbodepot\src\main\php\managers\FilesManager; use org\turbodepot\src\main\php\managers\LocalizedFilesManager; +use org\turbotesting\src\main\php\utils\AssertUtils; /** @@ -55,13 +56,7 @@ protected function setUp(){ $this->assertTrue($this->filesManager->isDirectoryEmpty($this->tempFolder)); $this->assertFalse($this->filesManager->isFile($this->tempFolder)); - $this->defaultLocations = [[ - 'label' => 'test-json', - 'path' => $this->basePath.'/locales/test-json/$locale/$bundle.json', - 'bundles' => ['Locales'] - ]]; - - $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-1', ['en_US', 'es_ES'], $this->defaultLocations); + $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-1', ['en_US', 'es_ES'], [$this->basePath.'/locales']); } @@ -74,11 +69,6 @@ protected function tearDown(){ // Delete temporary folder $this->filesManager->deleteDirectory($this->tempFolder); - - if($this->exceptionMessage != ''){ - - $this->fail($this->exceptionMessage); - } } @@ -101,80 +91,19 @@ public static function tearDownAfterClass(){ public function testConstruct(){ // Test empty values - try { - $this->sut = new LocalizedFilesManager(null); - $this->exceptionMessage = 'null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Too few arguments to function/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager(null, null); - $this->exceptionMessage = 'null null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Too few arguments to function/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager(null, null, null); - $this->exceptionMessage = 'null null null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/rootPath must be a string/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager('', '', ''); - $this->exceptionMessage = '"" did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Specified rootPath does not exist/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager([], '', ''); - $this->exceptionMessage = '[] did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/rootPath must be a string/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager($this->tempFolder, '', ''); - $this->exceptionMessage = '$this->tempFolder, "" did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Locations must be an array of objects/', $e->getMessage()); - } - - try { - $this->sut = new LocalizedFilesManager($this->tempFolder, '', $this->defaultLocations); - $this->exceptionMessage = '$this->tempFolder, "", $this->defaultLocations did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/no locales defined/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager(null); }, '/Too few arguments to function/'); + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager(null, null); }, '/Too few arguments to function/'); + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager(null, null, null); }, '/rootPath must be a string/'); + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager('', '', ''); }, '/Specified rootPath does not exist/'); + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager([], '', ''); }, '/rootPath must be a string/'); // Test ok values - $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['en_US'], $this->defaultLocations))->dirSep()); - $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['en_US', 'es_ES'], $this->defaultLocations))->dirSep()); + $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['en_US'], [$this->basePath.'/locales']))->dirSep()); + $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['en_US', 'es_ES'], [$this->basePath.'/locales']))->dirSep()); // Test wrong values - try { - $this->sut = new LocalizedFilesManager('nonexistant path', ['en_US'], $this->defaultLocations); - $this->exceptionMessage = 'nonexistant path did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Specified rootPath does not exist: nonexistant path/', $e->getMessage()); - } - - try { - $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['en_US', 'es_ES', 'ca_ES'], $this->defaultLocations))->dirSep()); - $this->exceptionMessage = '"ca_ES" did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/File does not exist.*ca_ES/', $e->getMessage()); - } - - try { - $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['5345345'], $this->defaultLocations))->dirSep()); - $this->exceptionMessage = '"5345345" did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/File does not exist.*5345345/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut = new LocalizedFilesManager('nonexistant path', ['en_US'], [$this->basePath.'/locales']); }, '/Specified rootPath does not exist: nonexistant path/'); + AssertUtils::throwsException(function() { $this->assertSame(DIRECTORY_SEPARATOR, (new LocalizedFilesManager($this->tempFolder, ['5345345'], [$this->basePath.'/locales']))->dirSep()); }, '/locale must be a valid xx_XX value/'); // Test exceptions // Already tested @@ -315,25 +244,20 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, } // Test empty values - try { - $this->sut->getDirectoryList(null); - $this->exceptionMessage = 'null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Path must be a string/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut->getDirectoryList(null); }, '/Path must be a string/'); - $this->assertTrue(ArrayUtils::isArray($this->sut->getDirectoryList(''))); - $this->assertSame(3, count($this->sut->getDirectoryList(''))); + $this->assertTrue(ArrayUtils::isArray($this->sut->getDirectoryList('', 'test-json/users'))); + $this->assertSame(3, count($this->sut->getDirectoryList('', 'test-json/users'))); // Test ok values - $list = $this->sut->getDirectoryList('', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('', 'test-json/users', [], 'nameAsc'); $this->assertSame(3, count($list)); assertLocalizedFilesObject($list[0], true, '', 'en_US', 'LOGIN', 'Login'); assertLocalizedFilesObject($list[1], true, '', 'en_US', 'PASSWORD', 'Password'); assertLocalizedFilesObject($list[2], true, '', 'en_US', 'TAG_NOT_EXISTING_ON_ES_ES', 'Missing tag'); - $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame(4, count($list)); assertLocalizedFilesObject($list[0], false, '', 'en_US', 'non existant key', 'non existant key'); @@ -345,7 +269,7 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, $this->sut->setPrimaryLocale('es_ES'); - $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame(4, count($list)); assertLocalizedFilesObject($list[0], false, '', 'es_ES', 'non existant key', 'non existant key'); @@ -353,14 +277,14 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, // Test non existant tags are translated with the next locale by preference - $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES', 'test-json/users', [], 'nameAsc'); $this->assertSame(2, count($list)); assertLocalizedFilesObject($list[0], true, '', 'es_ES', 'LOGIN', 'Acceso'); assertLocalizedFilesObject($list[1], false, 'txt', 'es_ES', 'TAG_NOT_EXISTING_ON_ES_ES', 'Missing tag.txt'); $this->sut->setPrimaryLocale('en_US'); - $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES', 'test-json/users', [], 'nameAsc'); $this->assertSame(2, count($list)); assertLocalizedFilesObject($list[0], true, '', 'en_US', 'LOGIN', 'Login'); @@ -368,7 +292,7 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, // Test folder containing files with multiple localized contents - $list = $this->sut->getDirectoryList('LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame(3, count($list)); assertLocalizedFilesObject($list[0], false, 'TXT', 'en_US', 'PASSWORD', 'Password.TXT'); @@ -377,7 +301,7 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, $this->sut->setPrimaryLocale('es_ES'); - $list = $this->sut->getDirectoryList('LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame(3, count($list)); assertLocalizedFilesObject($list[0], false, 'TXT', 'es_ES', 'PASSWORD', 'Contraseña.TXT'); @@ -386,50 +310,28 @@ function assertLocalizedFilesObject($object, $isDir, $extension, $locale, $key, // Test folders with multiple localized contents - $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-2', ['en_US', 'es_ES'], $this->defaultLocations); + $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-2', ['en_US', 'es_ES'], [$this->basePath.'/locales']); - $list = $this->sut->getDirectoryList('2019', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('2019', 'test-json/users', [], 'nameAsc'); $this->assertSame(1, count($list)); assertLocalizedFilesObject($list[0], true, '', 'en_US', '10', '10'); - $list = $this->sut->getDirectoryList('2019/10/29', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('2019/10/29', 'test-json/users', [], 'nameAsc'); $this->assertSame(2, count($list)); assertLocalizedFilesObject($list[0], true, '', 'en_US', 'some-folder-title', 'some-folder-title'); assertLocalizedFilesObject($list[1], true, '', 'en_US', 'USER', 'User'); // Test wrong values - $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-1', ['en_US', 'es_ES'], $this->defaultLocations, true); + $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-1', ['en_US', 'es_ES'], [$this->basePath.'/locales'], true); - try { - $list = $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', '', '', [], 'nameAsc'); - $this->exceptionMessage = 'getDirectoryList did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/key not found on Locales/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut->getDirectoryList('TAG_NOT_EXISTING_ON_ES_ES/LOGIN', 'test-json/users', [], 'nameAsc'); }, '/key not found on test-json.users/'); // Test exceptions - try { - $list = $this->sut->getDirectoryList(34545435, '', '', [], 'nameAsc'); - $this->exceptionMessage = '34545435 did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Path must be a string/', $e->getMessage()); - } - - try { - $list = $this->sut->getDirectoryList('LOGIN', 'nonexistantbundle', '', [], 'nameAsc'); - $this->exceptionMessage = 'nonexistantbundle did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Bundle not loaded/', $e->getMessage()); - } - - try { - $list = $this->sut->getDirectoryList('LOGIN', '', 999999, [], 'nameAsc'); - $this->exceptionMessage = '999999 did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/Location <999999> not loaded/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut->getDirectoryList(34545435, 'test-json/users', [], 'nameAsc'); }, '/Path must be a string/'); + AssertUtils::throwsException(function() { $this->sut->getDirectoryList('LOGIN', 'test-json/nonexistant', [], 'nameAsc'); }, '/key not found on test-json.nonexistant/'); + AssertUtils::throwsException(function() { $this->sut->getDirectoryList('LOGIN', '999999/users', [], 'nameAsc'); }, '/key not found on 999999.users/'); } @@ -602,30 +504,13 @@ public function testMergeFiles(){ public function testReadFile(){ // Test empty values - try { - $this->sut->readFile(null); - $this->exceptionMessage = 'null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/pathOrObject is not a valid file/', $e->getMessage()); - } - - try { - $this->sut->readFile(''); - $this->exceptionMessage = 'null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/File does not exist.*test-1/', $e->getMessage()); - } - - try { - $this->sut->readFile(0); - $this->exceptionMessage = 'null did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/pathOrObject is not a valid file/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut->readFile(null); }, '/pathOrObject is not a valid file/'); + AssertUtils::throwsException(function() { $this->sut->readFile(''); }, '/File does not exist.*test-1/'); + AssertUtils::throwsException(function() { $this->sut->readFile(0); }, '/pathOrObject is not a valid file/'); // Test ok values - Passing a LocalizedFilesObject instance - $list = $this->sut->getDirectoryList('LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame('0', $this->sut->readFile($list[0])); $this->assertSame('1', $this->sut->readFile($list[1])); @@ -634,14 +519,14 @@ public function testReadFile(){ $this->sut->setPrimaryLocale('es_ES'); $this->assertSame('2-en_US', $this->sut->readFile($list[2])); - $list = $this->sut->getDirectoryList('LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame('0', $this->sut->readFile($list[0])); $this->assertSame('1', $this->sut->readFile($list[1])); $this->assertSame('2-es_ES', $this->sut->readFile($list[2])); $this->sut->setPrimaryLocale('en_US'); - $list = $this->sut->getDirectoryList('PASSWORD/LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('PASSWORD/LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame('1-en_US', $this->sut->readFile($list[0])); $this->assertSame('0', $this->sut->readFile($list[1])); $this->assertSame('2', $this->sut->readFile($list[2])); @@ -649,14 +534,14 @@ public function testReadFile(){ $this->sut->setPrimaryLocale('es_ES'); $this->assertSame('1-en_US', $this->sut->readFile($list[0])); - $list = $this->sut->getDirectoryList('PASSWORD/LOGIN', '', '', [], 'nameAsc'); + $list = $this->sut->getDirectoryList('PASSWORD/LOGIN', 'test-json/users', [], 'nameAsc'); $this->assertSame('1-es_ES', $this->sut->readFile($list[0])); $this->assertSame('0', $this->sut->readFile($list[1])); $this->assertSame('2', $this->sut->readFile($list[2])); // Test ok values - Passing directly a path as a string - $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-2', ['en_US', 'es_ES'], $this->defaultLocations); + $this->sut = new LocalizedFilesManager($this->basePath.'/paths/test-2', ['en_US', 'es_ES'], [$this->basePath.'/locales']); $this->assertSame('some english text', $this->sut->readFile('2019/10/29/some-folder-title-en_US/text.md')); $this->assertSame('Un texto en español', $this->sut->readFile('2019/10/29/some-folder-title-es_ES/text.md')); @@ -666,19 +551,8 @@ public function testReadFile(){ // Test wrong values // Test exceptions - try { - $this->sut->readFile(9283498234); - $this->exceptionMessage = '9283498234 did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/pathOrObject is not a valid file/', $e->getMessage()); - } - - try { - $this->sut->readFile([12121, 454 , 4545]); - $this->exceptionMessage = '[12121, 454 , 4545] did not cause exception'; - } catch (Throwable $e) { - $this->assertRegExp('/pathOrObject is not a valid file/', $e->getMessage()); - } + AssertUtils::throwsException(function() { $this->sut->readFile(9283498234); }, '/pathOrObject is not a valid file/'); + AssertUtils::throwsException(function() { $this->sut->readFile([12121, 454 , 4545]); }, '/pathOrObject is not a valid file/'); } diff --git a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/en_US/Locales.json b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/en_US/Locales.json deleted file mode 100644 index b2ab7be..0000000 --- a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/en_US/Locales.json +++ /dev/null @@ -1,6 +0,0 @@ -{ -"LOGIN":"Login", -"TAG_NOT_EXISTING_ON_ES_ES": "Missing tag", -"PASSWORD": "Password", -"USER": "User" -} \ No newline at end of file diff --git a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/es_ES/Locales.json b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/es_ES/Locales.json deleted file mode 100644 index 771213d..0000000 --- a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/es_ES/Locales.json +++ /dev/null @@ -1,5 +0,0 @@ -{ -"LOGIN":"Acceso", -"PASSWORD": "Contraseña", -"USER": "Usuario" -} \ No newline at end of file diff --git a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_en_US.properties b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_en_US.properties new file mode 100644 index 0000000..a48f0e0 --- /dev/null +++ b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_en_US.properties @@ -0,0 +1,8 @@ + +LOGIN = Login + +PASSWORD = Password + +TAG_NOT_EXISTING_ON_ES_ES = Missing tag + +USER = User diff --git a/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_es_ES.properties b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_es_ES.properties new file mode 100644 index 0000000..4062c28 --- /dev/null +++ b/TurboDepot-Php/src/test/resources/managers/localizedFilesManager/locales/test-json/users/users_es_ES.properties @@ -0,0 +1,6 @@ + +LOGIN = Acceso + +PASSWORD = Contrase\u00F1a + +USER = Usuario