Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Oct 29, 2023
1 parent 859a42a commit 7bc623f
Show file tree
Hide file tree
Showing 34 changed files with 634 additions and 626 deletions.
13 changes: 5 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"name": "Laravel-Lang Team",
"homepage": "https://github.com/Laravel-Lang"
"homepage": "https://laravel-lang.com"
}
],
"support": {
Expand All @@ -40,19 +40,16 @@
"prefer-stable": true,
"autoload": {
"psr-4": {
"LaravelLang\\Locales\\": "src"
"LaravelLang\\Locales\\": "src/"
},
"files": [
"helper.php"
"helpers/lang.php"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
},
"files": [
"tests/Fixtures/collision_functions.php"
]
"Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Concerns/Aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace LaravelLang\Locales\Concerns;

use LaravelLang\Locales\Enums\Locales as LocaleCode;
use LaravelLang\Locales\Enums\Locale as LocaleCode;
use LaravelLang\Locales\Helpers\Config;

trait Aliases
Expand Down
4 changes: 2 additions & 2 deletions src/Enums/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

enum Config: string
{
case PublicKey = 'translations';
case PrivateKey = 'translations-private';
case PublicKey = 'localization';
case PrivateKey = 'localization-private';
}
2 changes: 1 addition & 1 deletion src/Enums/Locales.php → src/Enums/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* ISO-639-1 standard
* @see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
*/
enum Locales: string
enum Locale: string
{
use Values;

Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
namespace LaravelLang\Locales\Exceptions;

use DragonCode\Support\Facades\Helpers\Arr;
use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;
use RuntimeException;

class BaseException extends RuntimeException
{
protected function stringify(array|Locales|string $locales): string
protected function stringify(array|Locale|string $locales): string
{
$locales = is_array($locales) ? $locales : [$locales];

return Arr::of($locales)
->map(static fn (Locales|string $locale) => $locale?->value ?? $locale)
->map(static fn (Locale|string $locale) => $locale?->value ?? $locale)
->implode(', ')
->toString();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/ProtectedLocaleException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

namespace LaravelLang\Locales\Exceptions;

use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;

class ProtectedLocaleException extends BaseException
{
public function __construct(array|Locales|string $locales)
public function __construct(array|Locale|string $locales)
{
$locales = $this->stringify($locales);

Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/UnknownLocaleCodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

namespace LaravelLang\Locales\Exceptions;

use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;

class UnknownLocaleCodeException extends BaseException
{
public function __construct(array|Locales|string $locale)
public function __construct(array|Locale|string $locale)
{
$locale = $this->stringify($locale);

Expand Down
23 changes: 11 additions & 12 deletions src/Facades/Helpers/Locales.php → src/Facades/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@

declare(strict_types=1);

namespace LaravelLang\Locales\Facades\Helpers;
namespace LaravelLang\Locales\Facades;

use Illuminate\Support\Facades\Facade;
use LaravelLang\Locales\Enums\Locales as LocaleCode;
use LaravelLang\Locales\Helpers\Locales as Helper;
use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Helpers\Locales as LocalesService;

/**
* @method static array available()
* @method static array installed()
* @method static array installedWithoutProtects()
* @method static array notInstalled()
* @method static array protects()
* @method static bool isAvailable(string|LocaleCode|null $locale)
* @method static bool isInstalled(string|LocaleCode|null $locale)
* @method static bool isProtected(string|LocaleCode|null $locale)
* @method static array<string> available()
* @method static array<string> installed()
* @method static array<string> notInstalled()
* @method static array<string> protects()
* @method static bool isAvailable(string|Locale|null $locale)
* @method static bool isInstalled(string|Locale|null $locale)
* @method static bool isProtected(string|Locale|null $locale)
* @method static string getDefault()
* @method static string getFallback()
*/
class Locales extends Facade
{
protected static function getFacadeAccessor(): string
{
return Helper::class;
return LocalesService::class;
}
}
6 changes: 3 additions & 3 deletions src/Helpers/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use DragonCode\Contracts\Support\Stringable;
use LaravelLang\Locales\Concerns\Aliases;
use LaravelLang\Locales\Constants\Types;
use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;

class Config
{
Expand Down Expand Up @@ -74,11 +74,11 @@ public function setPackage(string $base_path, string $plugin_class, string $pack
$this->setPrivate('packages', $items);
}

public function langPath(Locales|string|null ...$paths): string
public function langPath(Locale|string|null ...$paths): string
{
$path = $this->arr->of($paths)
->filter()
->map(fn (Locales|string $value) => $this->toAlias($value, $this))
->map(fn (Locale|string $value) => $this->toAlias($value, $this))
->implode('/');

return $this->path(lang_path(), $path);
Expand Down
7 changes: 1 addition & 6 deletions src/Helpers/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use DragonCode\Support\Facades\Filesystem\File;
use DragonCode\Support\Facades\Filesystem\Path;
use LaravelLang\Locales\Concerns\Aliases;
use LaravelLang\Locales\Enums\Locales as LocaleCodes;
use LaravelLang\Locales\Enums\Locale as LocaleCodes;

class Locales
{
Expand Down Expand Up @@ -57,11 +57,6 @@ public function installed(): array
->toArray();
}

public function installedWithoutProtects(): array
{
return array_values(array_diff($this->installed(), $this->protects()));
}

public function notInstalled(): array
{
return array_values(array_diff($this->available(), $this->installed()));
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use DragonCode\Support\Facades\Filesystem\Path;
use DragonCode\Support\Facades\Helpers\Arr;
use DragonCode\Support\Facades\Helpers\Str;
use LaravelLang\Locales\Enums\Locales as LocaleCode;
use LaravelLang\Locales\Enums\Locale as LocaleCode;

class Remove extends Processor
{
Expand Down
14 changes: 9 additions & 5 deletions src/TextDecorator.php → tests/Datasets/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@

declare(strict_types=1);

namespace LaravelLang\Locales;
use LaravelLang\Locales\Enums\Locale;

interface TextDecorator
{
public function convert(string $locale, string $value): string;
}
dataset('locales', Locale::values());

dataset('incorrect-locales', ['FOO', 'BAR', 'AA', 'BB', 'cc', 'dd', null]);

dataset('aliased-locales', [
Locale::German->value => [Locale::German, 'de-DE'],
Locale::French->value => [Locale::French, 'fr-custom'],
]);
10 changes: 5 additions & 5 deletions tests/Helpers/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
declare(strict_types=1);

use LaravelLang\Locales\Enums\Config;
use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;

function setAlias(Locales $locale, string $alias): void
function setAlias(Locale $locale, string $alias): void
{
config()->set(Config::PublicKey->value . '.aliases.' . $locale->value, $alias);
}

function setLocales(Locales|string|null $main = null, Locales|string|null $fallback = null): void
function setLocales(Locale|string|null $main = null, Locale|string|null $fallback = null): void
{
if ($main) {
setMainLocale($main);
Expand All @@ -34,12 +34,12 @@ function setLocales(Locales|string|null $main = null, Locales|string|null $fallb
}
}

function setMainLocale(Locales|string $locale): void
function setMainLocale(Locale|string $locale): void
{
config()->set('app.locale', $locale->value ?? $locale);
}

function setFallbackLocale(Locales|string $locale): void
function setFallbackLocale(Locale|string $locale): void
{
config()->set('app.fallback_locale', $locale->value ?? $locale);
}
6 changes: 3 additions & 3 deletions tests/Helpers/paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
declare(strict_types=1);

use DragonCode\Support\Facades\Filesystem\Directory;
use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;

function createLocale(Locales|string $locale): void
function createLocale(Locale|string $locale): void
{
Directory::ensureDirectory(
lang_path($locale->value ?? $locale)
);
}

function createLocales(Locales|string ...$locales): void
function createLocales(Locale|string ...$locales): void
{
foreach ($locales as $locale) {
createLocale($locale);
Expand Down
11 changes: 6 additions & 5 deletions tests/Unit/AliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

declare(strict_types=1);

use LaravelLang\Locales\Enums\Locales;
use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Facades\Locales;

it('checks the use of aliases', function () {
createLocales(Locales::German, Locales::GermanSwitzerland, Locales::SerbianCyrillic);
setLocales(fallback: Locales::French);
createLocales(Locale::German, Locale::GermanSwitzerland, Locale::SerbianCyrillic);
setLocales(fallback: Locale::French);

setAlias(Locales::German, 'de-DE');
setAlias(Locales::GermanSwitzerland, 'de-CH');
setAlias(Locale::German, 'de-DE');
setAlias(Locale::GermanSwitzerland, 'de-CH');

expect(Locales::installed())->toBe([
'de-CH',
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Facades/Locales/AvailableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the "laravel-Lang/locales" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <[email protected]>
* @copyright 2023 Laravel Lang Team
* @license MIT
*
* @see https://laravel-lang.com
*/

declare(strict_types=1);

use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Facades\Locales;

it('checks the list of available locales')
->expect(Locales::available())
->toBeArray()
->toBeIn([Locale::English->value])
->not->toBeIn(['foo', 'bar']);

it('checks the list of available locales taking into account aliases', function (Locale $locale, string $alias) {
setAlias($locale, $alias);

expect(Locales::available())
->toBeArray()
->toBeIn([$alias])
->not->toBeIn([$locale->value]);
})->with('aliased-locales');

it('checks incorrect locales against the list of available ones', function (?string $locale) {
expect(Locales::available())->toBeArray()->not->toBeIn([$locale]);
})->with('incorrect-locales');
54 changes: 54 additions & 0 deletions tests/Unit/Facades/Locales/GetDefaultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of the "laravel-Lang/locales" project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Andrey Helldar <[email protected]>
* @copyright 2023 Laravel Lang Team
* @license MIT
*
* @see https://laravel-lang.com
*/

declare(strict_types=1);

use LaravelLang\Locales\Enums\Locale;
use LaravelLang\Locales\Facades\Locales;

it('returns English locale')
->expect(Locales::getDefault())
->toBe(Locale::English->value);

it('returns German locale', function () {
setLocales(main: Locale::German);

expect(Locales::getDefault())
->toBe(Locale::German->value);
});

it('returns English locale if the main one is specified incorrectly', function () {
setLocales(main: 'foo');

expect(Locales::getDefault())
->toBe(Locale::English->value);
});

it('returns English locale if primary and fallback are incorrect', function () {
setLocales('foo', 'foo');

expect(Locales::getDefault())
->toBe(Locale::English->value);
});

it('will return the locale by alias', function (Locale $locale, string $alias) {
setLocales($locale);

setAlias($locale, $alias);

expect(Locales::getDefault())
->toBe($alias)
->not->toBe($locale->value);
})->with('aliased-locales');
Loading

0 comments on commit 7bc623f

Please sign in to comment.