From c7f316aff10f90a8d6e9edc251044eae646319f0 Mon Sep 17 00:00:00 2001 From: Christopher Fuchs Date: Thu, 23 Apr 2020 10:39:03 +0200 Subject: [PATCH 1/3] Added support for laravel 7 --- .travis.yml | 1 - composer.json | 12 ++++++------ resources/lang/en/validation.php | 2 ++ tests/TranslationDatabaseTest.php | 22 +++++++++++----------- tests/TranslationTest.php | 16 ++++++++-------- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3e40dec..13ed9fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - '7.2' - '7.4' cache: diff --git a/composer.json b/composer.json index 284066e..22a8e7c 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,15 @@ ] }, "require": { - "php": ">=7.2.0", + "php": ">=7.4.0", "ext-intl": "*", - "illuminate/support": "^6", - "illuminate/translation": "^6", - "illuminate/validation": "^6" + "illuminate/support": "^7", + "illuminate/translation": "^7", + "illuminate/validation": "^7" }, "require-dev": { - "orchestra/testbench": "^3.3", - "phpunit/phpunit": "^8.0", + "orchestra/testbench": "5.1.0", + "phpunit/phpunit": "^9.0", "friendsofphp/php-cs-fixer": "^2.16" }, "scripts": { diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index a90a321..8e6018f 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -81,8 +81,10 @@ 'string' => 'The {attribute} must be a string.', 'starts_with' => 'The {attribute} must start with one of the following: {values}', 'timezone' => 'The {attribute} must be a valid zone.', + 'uploaded' => 'The {attribute} failed to upload.', 'unique' => 'The {attribute} has already been taken.', 'url' => 'The {attribute} format is invalid.', + 'uuid' => 'The :attribute must be a valid UUID.', /* |-------------------------------------------------------------------------- diff --git a/tests/TranslationDatabaseTest.php b/tests/TranslationDatabaseTest.php index 291f0d5..97989ec 100644 --- a/tests/TranslationDatabaseTest.php +++ b/tests/TranslationDatabaseTest.php @@ -17,7 +17,7 @@ public function setUp(): void $this->loadMigrationsFrom([ '--database' => 'testing', - '--realpath' => realpath(__DIR__ . '/database/migrations'), + '--path' => realpath(__DIR__ . '/database/migrations'), ]); } @@ -33,16 +33,6 @@ public function getEnvironmentSetUp($app) ]); } - /** - * Create test user. - * - * @return User - */ - public function makeUser() - { - return User::create(['name' => 'test']); - } - public function testDatabaseValidators() { $this->makeUser(); @@ -66,4 +56,14 @@ public function testDatabaseValidators() $this->assertEquals('The selected exists is invalid.', $errors->first('exists')); $this->assertEquals('The unique has already been taken.', $errors->first('unique')); } + + /** + * Create test user. + * + * @return User + */ + private function makeUser() + { + return User::create(['name' => 'test']); + } } diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 641fd9a..4c7d8d9 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -80,7 +80,7 @@ public function testValidatorMessages() 'before_date' => Carbon::now()->addDay()->toDateString(), 'before_or_equal_date' => Carbon::now()->addDay()->toDateString(), 'between_numeric' => 10, - 'between_file' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, null, null, true), + 'between_file' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, null, true), 'between_string' => Str::random(10), 'between_array' => range(1, 10), 'boolean' => 'string', @@ -98,22 +98,22 @@ public function testValidatorMessages() ], 'email' => 'notanemail', 'filled' => '', - 'image' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, null, null, true), + 'image' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, null, true), 'in' => 'c', 'in_array' => 4, 'integer' => 4.20, 'ip' => 'a.a.a.a', 'json' => '{a:c}', 'max_numeric' => 1234567, - 'max_file_plural' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, 0, null, true), + 'max_file_plural' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, 0, true), 'max_string_plural' => 'abcdefghijklmn', 'max_array_plural' => [1, 2, 3, 4, 5, 6], - 'max_file_singular' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, 5, null, true), + 'max_file_singular' => new UploadedFile($fixturesPath . '/texts/test_between.txt', Str::random(), null, 0, true), 'max_string_singular' => 'string|max:1', 'max_array_singular' => [1, 2], - 'mimes' => new UploadedFile($this->fixturesPath . '/images/image.jpg', Str::random(), null, null, null, true), + 'mimes' => new UploadedFile($this->fixturesPath . '/images/image.jpg', Str::random(), null, null, true), 'min_numeric' => 1, - 'min_file' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, null, true), + 'min_file' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, true), 'min_string' => 'a', 'min_array' => [1], 'numeric' => 'qwe', @@ -126,10 +126,10 @@ public function testValidatorMessages() 'required_without_all' => '', 'same' => 'notthesame', 'size_numeric' => 12, - 'size_file_plural' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, null, true), + 'size_file_plural' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, true), 'size_string_plural' => 'ab', 'size_array_plural' => ['a', 'b'], - 'size_file_singular' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, null, true), + 'size_file_singular' => new UploadedFile($fixturesPath . '/texts/test_empty.txt', Str::random(), null, null, true), 'size_string_singular' => 'ab', 'size_array_singular' => ['a', 'b'], 'starts_with' => 'abcdef', From 9af2be427af884007d52c611295da897b54606e0 Mon Sep 17 00:00:00 2001 From: Christopher Fuchs Date: Thu, 23 Apr 2020 10:53:03 +0200 Subject: [PATCH 2/3] Use minimum PHP 7.4 * added missing parameter and property types --- src/TranslationServiceProvider.php | 14 ++--- src/Translator.php | 93 ++++++------------------------ src/ValidationServiceProvider.php | 2 +- tests/TestCase.php | 5 +- tests/TranslationDatabaseTest.php | 2 +- tests/TranslationTest.php | 12 ++-- 6 files changed, 33 insertions(+), 95 deletions(-) diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index 9f55716..4060491 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -37,7 +37,7 @@ public function register() /** * Bootstrap the application events. */ - public function boot() + public function boot(): void { $this->publishConfig(); $this->publishLangFiles(); @@ -46,7 +46,7 @@ public function boot() /** * * Publish config files using php artisan vendor:publish. */ - protected function publishConfig() + protected function publishConfig(): void { $configFile = 'translator.php'; @@ -60,7 +60,7 @@ protected function publishConfig() /** * Publish lang files using php artisan vendor:publish. */ - protected function publishLangFiles() + protected function publishLangFiles(): void { $langPath = $this->getLangPath(); @@ -86,20 +86,16 @@ protected function publishLangFiles() /** * Get config files directory. - * - * @return string */ - protected function getConfigPath() + protected function getConfigPath(): string { return __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'config' . \DIRECTORY_SEPARATOR; } /** * Get language files directory. - * - * @return string */ - protected function getLangPath() + protected function getLangPath(): string { return __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'resources' . \DIRECTORY_SEPARATOR . 'lang' . \DIRECTORY_SEPARATOR; } diff --git a/src/Translator.php b/src/Translator.php index 51d2443..8a6a9b0 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -17,45 +17,33 @@ class Translator extends NamespacedItemResolver implements TranslatorContract /** * The loader implementation. - * - * @varLoader */ - protected $loader; + protected Loader $loader; /** * The default locale being used by the translator. - * - * @var string */ - protected $locale; + protected ?string $locale = null; /** * The fallback locale used by the translator. - * - * @var string */ - protected $fallback; + protected ?string $fallback = null; /** * Locale region used by translator. - * - * @var string */ - protected $region; + protected ?string $region = null; /** * The array of loaded translation groups. - * - * @var array */ - protected $loaded = []; + protected array $loaded = []; /** * Create a new translator instance. - * - * @param string $locale */ - public function __construct(Loader $loader, $locale) + public function __construct(Loader $loader, string $locale) { $this->loader = $loader; $this->setLocale($locale); @@ -63,14 +51,8 @@ public function __construct(Loader $loader, $locale) /** * Determine if a translation exists. - * - * @param string $key - * @param null|string $locale - * @param bool $fallback - * - * @return bool */ - public function has($key, $locale = null) + public function has(string $key, ?string $locale = null): bool { return $this->getMessage($key, $locale) !== $key; } @@ -180,50 +162,39 @@ public function locale() * * @param string $fallback */ - public function setFallback($fallback) + public function setFallback(?string $fallback) { $this->fallback = $fallback; } /** * Get the fallback locale being used. - * - * @return string */ - public function getFallback() + public function getFallback(): ?string { return $this->fallback; } /** * Set locale region. - * - * @param string $region */ - public function setRegion($region) + public function setRegion(?string $region) { $this->region = $region; } /** * Get locale region. - * - * @return null|string */ - public function getRegion() + public function getRegion(): ?string { return $this->region; } /** - * Get locale with region separated by hypen. - * - * @param null|string $locale - * @param null|string $region - * - * @return string + * Get locale with region separated by hyphen. */ - public function getLocaleRegion($locale = null, $region = null) + public function getLocaleRegion(?string $locale = null, ?string $region = null): string { $locale = $locale ?: ($this->getLocale() ?: $this->getFallback()); $region = $region ?: $this->getRegion(); @@ -255,12 +226,8 @@ public function parseKey($key) /** * Load the specified language group. - * - * @param string $namespace - * @param string $group - * @param string $locale */ - public function load($namespace, $group, $locale) + public function load(string $namespace, string $group, string $locale) { if ($this->isLoaded($namespace, $group, $locale)) { return; @@ -276,11 +243,8 @@ public function load($namespace, $group, $locale) /** * Add a new namespace to the loader. - * - * @param string $namespace - * @param string $hint */ - public function addNamespace($namespace, $hint) + public function addNamespace(string $namespace, string $hint) { $this->loader->addNamespace($namespace, $hint); } @@ -288,13 +252,9 @@ public function addNamespace($namespace, $hint) /** * Get the translation for the given key. * - * @param string $key - * @param null|string $locale - * @param bool $fallback - * * @return null|array|string */ - public function getMessage($key, $locale = null) + public function getMessage(string $key, ?string $locale = null) { [$namespace, $group, $item] = $this->parseKey($key); @@ -320,14 +280,9 @@ public function getMessage($key, $locale = null) /** * Retrieve a language line out the loaded array. * - * @param string $namespace - * @param string $group - * @param string $locale - * @param string $item - * * @return null|array|string */ - protected function getLine($namespace, $group, $locale, $item) + protected function getLine(string $namespace, string $group, string $locale, string $item) { $line = Arr::get($this->loaded[$namespace][$group][$locale], $item); @@ -340,26 +295,16 @@ protected function getLine($namespace, $group, $locale, $item) /** * Get the array of locales to be checked. - * - * @param null|string $locale - * - * @return array */ - protected function parseLocale($locale) + protected function parseLocale(?string $locale): array { return array_filter([$locale ?: $this->locale, $this->fallback]); } /** * Determine if the given group has been loaded. - * - * @param string $namespace - * @param string $group - * @param string $locale - * - * @return bool */ - protected function isLoaded($namespace, $group, $locale) + protected function isLoaded(string $namespace, string $group, string $locale): bool { return isset($this->loaded[$namespace][$group][$locale]); } diff --git a/src/ValidationServiceProvider.php b/src/ValidationServiceProvider.php index 71f597c..964e32c 100644 --- a/src/ValidationServiceProvider.php +++ b/src/ValidationServiceProvider.php @@ -8,7 +8,7 @@ class ValidationServiceProvider extends LaravelProvider { - public function boot() + public function boot(): void { app('validator')->resolver(function ($translator, $data, $rules, $messages, $customAttributes) { return new Validator($translator, $data, $rules, $messages, $customAttributes); diff --git a/tests/TestCase.php b/tests/TestCase.php index 3f7f8b1..acfe98a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,10 +11,7 @@ */ class TestCase extends Orchestra\Testbench\TestCase { - /** - * @var null|string - */ - protected $fixturesPath; + protected ?string $fixturesPath; public function setUp(): void { diff --git a/tests/TranslationDatabaseTest.php b/tests/TranslationDatabaseTest.php index 97989ec..4e18f2b 100644 --- a/tests/TranslationDatabaseTest.php +++ b/tests/TranslationDatabaseTest.php @@ -33,7 +33,7 @@ public function getEnvironmentSetUp($app) ]); } - public function testDatabaseValidators() + public function testDatabaseValidators(): void { $this->makeUser(); diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 4c7d8d9..1c4ba9f 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -14,27 +14,27 @@ */ class TranslationTest extends TestCase { - public function testAttributesPlaceholder() + public function testAttributesPlaceholder(): void { $this->assertEquals('Hello, Jane!', trans('test::test.hello', ['name' => 'Jane'])); $this->assertEquals('Hello, Jon!', trans('test::test.hello', ['name' => 'Jon'])); } - public function testPlurals() + public function testPlurals(): void { $this->assertEquals('no apples', trans_choice('test::test.apples', 0)); $this->assertEquals('1 apple', trans_choice('test::test.apples', 1)); $this->assertEquals('2 apples', trans_choice('test::test.apples', 2)); } - public function testPluralsChoiceArray() + public function testPluralsChoiceArray(): void { $this->assertEquals('no apples', trans_choice('test::test.apples', [])); $this->assertEquals('1 apple', trans_choice('test::test.apples', [1])); $this->assertEquals('2 apples', trans_choice('test::test.apples', [1, 2])); } - public function testPluralsChoiceCountable() + public function testPluralsChoiceCountable(): void { $this->assertEquals('no apples', trans_choice('test::test.apples', collect())); $this->assertEquals('1 apple', trans_choice('test::test.apples', collect([1]))); @@ -49,14 +49,14 @@ public function testPluralsOffset() $this->assertEquals('You and 2 others liked this', trans_choice('test::test.offset', 3)); } - public function testSelect() + public function testSelect(): void { $this->assertEquals('He has two legs and is male!', trans('test::test.select', ['gender' => 'male'])); $this->assertEquals('She has two legs and is female!', trans('test::test.select', ['gender' => 'female'])); $this->assertEquals('It has two legs and is penguin!', trans('test::test.select', ['gender' => 'penguin'])); } - public function testValidatorMessages() + public function testValidatorMessages(): void { $imgDir = $this->fixturesPath . '/images'; From c0b8f920dfa699c61341d1da9d643fe3dedab919 Mon Sep 17 00:00:00 2001 From: Christopher Fuchs Date: Thu, 23 Apr 2020 10:54:16 +0200 Subject: [PATCH 3/3] Use phpunit binary from vendor folder --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 13ed9fe..ebde125 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,4 @@ before_script: script: - composer lint - - phpunit + - vendor/bin/phpunit