From 5476056ab063481fa16efef5a448d4e71767ca94 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Mon, 10 Apr 2023 23:57:11 +0300 Subject: [PATCH 01/15] add idea folders to git ignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index f792fcc0..467d3d75 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ /.phpunit.result.cache /build/ /coverage.clover +/.idea +/.vscode +/.fleet From 6bd3b778fbb6e354e363512b6736e7bdb32ca4d5 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 22:37:56 +0200 Subject: [PATCH 02/15] l11 --- .github/workflows/run-tests.yml | 53 ++++++++++++++++++++++++++++----- composer.json | 11 +++---- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7d666d47..2cd8f08e 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,13 +14,23 @@ jobs: strategy: fail-fast: true matrix: - php: [8.0, 8.1] - laravel: [8.*, 9.*, 10.*] + php: [8.0, 8.1, 8.2, 8.3] + laravel: [8.*, 9.*, 10.*, 11.*] dependency-version: [prefer-stable] os: [ubuntu-latest] exclude: - - laravel: 10.* - php: 8.0 + - php: '8.0' + laravel: '10' + - php: '8.0' + laravel: '11' + - php: '8.2' + laravel: '8' + - php: '8.3' + laravel: '8' + - php: '8.3' + laravel: '9' + - php: '8.1' + laravel: '11' name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} @@ -46,10 +56,37 @@ jobs: coverage: xdebug tools: composer:v2 - - name: Install dependencies - run: | - composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update - composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + - name: Select Laravel 8 + uses: nick-invision/retry@v2 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer require "laravel/framework:^8.83.27" "phpunit/phpunit:^9.6.15" --no-update --no-interaction + if: "matrix.laravel == '8'" + + - name: Select Laravel 9 + uses: nick-invision/retry@v2 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer require "laravel/framework:^9.52.16" "phpunit/phpunit:^9.6.15" --no-update --no-interaction + if: "matrix.laravel == '9'" + + - name: Select Laravel 10 + uses: nick-invision/retry@v2 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer require "laravel/framework:^10.34.2" "phpunit/phpunit:^10.4.2" --no-update --no-interaction + if: "matrix.laravel == '10'" + + - name: Select Laravel 11 + uses: nick-invision/retry@v2 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer require "laravel/framework:^11.0" "phpunit/phpunit:^10.4.2" --no-update --no-interaction + if: "matrix.laravel == '11'" - name: Execute tests run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover diff --git a/composer.json b/composer.json index 6988335b..737eb9df 100644 --- a/composer.json +++ b/composer.json @@ -31,15 +31,16 @@ }, "require": { "php": "^8.0", - "illuminate/contracts": "^8.0 || ^9.0 || ^10.0", - "illuminate/database": "^8.0 || ^9.0 || ^10.0", - "illuminate/support": "^8.0 || ^9.0 || ^10.0" + "illuminate/contracts": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/database": "^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0" }, "require-dev": { "laravel/legacy-factories": "^1.0.4", + "laravel/pint": "^1.14", "mockery/mockery": "^1.3.3", - "orchestra/testbench": "^6.0 || ^7.0 || ^8.0", - "phpunit/phpunit": "^9.0" + "orchestra/testbench": "^6.0 || ^7.0 || ^8.0 || ^9.0", + "phpunit/phpunit": "^9.0 || ^10.0" }, "minimum-stability": "dev", "prefer-stable": true, From 836278b9244a61b4cf3e14bf80fa093eb6dfe50f Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 22:46:39 +0200 Subject: [PATCH 03/15] Switch php_cs to laravel/pint --- .gitattributes | 2 +- .php_cs | 35 ----------------------------------- composer.json | 2 +- pint.json | 29 +++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 37 deletions(-) delete mode 100644 .php_cs create mode 100644 pint.json diff --git a/.gitattributes b/.gitattributes index 65b9655b..78cc4c77 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,6 +5,6 @@ /.gitattributes export-ignore /.gitignore export-ignore /.gitbook.yaml export-ignore -/.php_cs export-ignore /.styleci.yml export-ignore /.phpunit.xml.dist export-ignore +/pint.json export-ignore diff --git a/.php_cs b/.php_cs deleted file mode 100644 index f310dd9b..00000000 --- a/.php_cs +++ /dev/null @@ -1,35 +0,0 @@ -in('src/Translatable'); - -return PhpCsFixer\Config::create() - ->setRules([ - 'ordered_class_elements' => [ - 'order' => [ - 'use_trait', - 'constant_public', - 'constant_protected', - 'constant_private', - 'property_public', - 'property_protected', - 'property_private', - 'construct', - 'method_public_static', - 'method_public', - 'method_protected_static', - 'method_protected', - 'method_private_static', - 'method_private', - 'destruct', - 'magic', - ], - 'sortAlgorithm' => 'alpha', - ], - 'yoda_style' => [ - 'equal' => false, - 'identical' => false, - 'less_and_greater' => null, - ], - ]) - ->setFinder($finder); diff --git a/composer.json b/composer.json index 737eb9df..62306f78 100644 --- a/composer.json +++ b/composer.json @@ -65,7 +65,7 @@ } }, "scripts": { - "csfix": "php-cs-fixer fix --using-cache=no", + "lint": "pint src/Translatable", "test": "vendor/bin/phpunit", "test-coverage": "vendor/bin/phpunit --coverage-html=build" } diff --git a/pint.json b/pint.json new file mode 100644 index 00000000..4373f2dd --- /dev/null +++ b/pint.json @@ -0,0 +1,29 @@ +{ + "rules": { + "ordered_class_elements": { + "order": [ + "use_trait", + "constant_public", + "constant_protected", + "constant_private", + "property_public", + "property_protected", + "property_private", + "construct", + "method_public_static", + "method_public", + "method_protected_static", + "method_protected", + "method_private_static", + "method_private", + "destruct", + "magic" + ] + }, + "yoda_style": { + "equal": false, + "identical": false, + "less_and_greater": null + } + } +} From 384038ecb6fe1d06fbb61441c9dc4beee4ad72b1 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 22:46:50 +0200 Subject: [PATCH 04/15] Apply style fixes --- src/Translatable/Contracts/Translatable.php | 4 ++-- src/Translatable/Traits/Relationship.php | 10 +++++----- src/Translatable/Translatable.php | 6 +++--- src/Translatable/Validation/RuleFactory.php | 3 ++- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Translatable/Contracts/Translatable.php b/src/Translatable/Contracts/Translatable.php index e8242260..748546d0 100644 --- a/src/Translatable/Contracts/Translatable.php +++ b/src/Translatable/Contracts/Translatable.php @@ -24,7 +24,7 @@ public function getDefaultLocale(): ?string; public function getNewTranslation(string $locale): Model; - public function getTranslation(?string $locale = null, bool $withFallback = null): ?Model; + public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?Model; public function getTranslationOrNew(?string $locale = null): Model; @@ -34,7 +34,7 @@ public function hasTranslation(?string $locale = null): bool; public function isTranslationAttribute(string $key): bool; - public function replicateWithTranslations(array $except = null): Model; + public function replicateWithTranslations(?array $except = null): Model; public function setDefaultLocale(?string $locale); diff --git a/src/Translatable/Traits/Relationship.php b/src/Translatable/Traits/Relationship.php index 606368a6..22832dc5 100644 --- a/src/Translatable/Traits/Relationship.php +++ b/src/Translatable/Traits/Relationship.php @@ -73,15 +73,15 @@ public function translation(): HasOne }); } + public function translations(): HasMany + { + return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey()); + } + protected function localeOrFallback() { return $this->useFallback() && ! $this->translations()->where($this->getLocaleKey(), $this->locale())->exists() ? $this->getFallbackLocale() : $this->locale(); } - - public function translations(): HasMany - { - return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey()); - } } diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 17800bd4..41f5f8d4 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -21,7 +21,7 @@ */ trait Translatable { - use Scopes, Relationship; + use Relationship, Scopes; protected static $autoloadTranslations = null; @@ -186,7 +186,7 @@ public function getNewTranslation(string $locale): Model return $translation; } - public function getTranslation(?string $locale = null, bool $withFallback = null): ?Model + public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?Model { $configFallbackLocale = $this->getFallbackLocale(); $locale = $locale ?: $this->locale(); @@ -279,7 +279,7 @@ public function isTranslationAttribute(string $key): bool return in_array($key, $this->translatedAttributes); } - public function replicateWithTranslations(array $except = null): Model + public function replicateWithTranslations(?array $except = null): Model { $newInstance = $this->replicate($except); diff --git a/src/Translatable/Validation/RuleFactory.php b/src/Translatable/Validation/RuleFactory.php index bf0d30b3..fee16dbb 100644 --- a/src/Translatable/Validation/RuleFactory.php +++ b/src/Translatable/Validation/RuleFactory.php @@ -9,6 +9,7 @@ class RuleFactory { const FORMAT_ARRAY = 1; + const FORMAT_KEY = 2; /** @@ -77,6 +78,7 @@ public function parse(array $input): array foreach ($input as $key => $value) { if (! $this->isTranslatable($key)) { $rules[$key] = $value; + continue; } @@ -94,7 +96,6 @@ protected function formatKey(string $locale, string $key): string } /** - * @param string $locale * @param string|string[]|mixed $rule * @return string|string[]|mixed */ From 1a13780511b31bd9dfc33d9140f664183a293238 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 22:50:51 +0200 Subject: [PATCH 05/15] Update run-tests.yml --- .github/workflows/run-tests.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 2cd8f08e..4e16bf5d 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -23,14 +23,14 @@ jobs: laravel: '10' - php: '8.0' laravel: '11' + - php: '8.1' + laravel: '11' - php: '8.2' laravel: '8' - php: '8.3' laravel: '8' - php: '8.3' laravel: '9' - - php: '8.1' - laravel: '11' name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} @@ -88,6 +88,13 @@ jobs: command: composer require "laravel/framework:^11.0" "phpunit/phpunit:^10.4.2" --no-update --no-interaction if: "matrix.laravel == '11'" + - name: Install PHP Dependencies + uses: nick-invision/retry@v2 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer update --no-interaction --no-progress + - name: Execute tests run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover From 572f67801409ec1e7450f519590244995e57ec14 Mon Sep 17 00:00:00 2001 From: Shift Date: Sun, 17 Mar 2024 20:52:33 +0000 Subject: [PATCH 06/15] Bump PHPUnit dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 62306f78..a15319e0 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "laravel/pint": "^1.14", "mockery/mockery": "^1.3.3", "orchestra/testbench": "^6.0 || ^7.0 || ^8.0 || ^9.0", - "phpunit/phpunit": "^9.0 || ^10.0" + "phpunit/phpunit": "^10.0" }, "minimum-stability": "dev", "prefer-stable": true, From 521b44ae8b7a6b8a6dd6e6f69f9af94317057d1c Mon Sep 17 00:00:00 2001 From: Shift Date: Sun, 17 Mar 2024 20:52:33 +0000 Subject: [PATCH 07/15] Ignore PHPUnit cache folder --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 467d3d75..c9d44901 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ /vendor/ /composer.lock /coverage/ -/.phpunit.result.cache +/.phpunit.cache /build/ /coverage.clover /.idea From 260779a17899572a6325f73470f3499758a5aee0 Mon Sep 17 00:00:00 2001 From: Shift Date: Sun, 17 Mar 2024 20:52:34 +0000 Subject: [PATCH 08/15] Adopt PHP attributes in test classes --- tests/EloquentOverrideTest.php | 13 ++-- tests/LocalesTest.php | 45 +++++------ tests/ScopesTest.php | 43 ++++++----- tests/TranslatableTest.php | 135 +++++++++++++++++---------------- tests/ValidationTest.php | 51 +++++++------ 5 files changed, 146 insertions(+), 141 deletions(-) diff --git a/tests/EloquentOverrideTest.php b/tests/EloquentOverrideTest.php index f74e0821..83e16b91 100644 --- a/tests/EloquentOverrideTest.php +++ b/tests/EloquentOverrideTest.php @@ -2,11 +2,12 @@ namespace Astrotomic\Translatable\Tests; +use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Tests\Eloquent\Vegetable; final class EloquentOverrideTest extends TestCase { - /** @test */ + #[Test] public function to_array_returns_translated_attributes(): void { $vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']); @@ -15,7 +16,7 @@ public function to_array_returns_translated_attributes(): void static::assertEquals('Peas', $vegetable->toArray()['name']); } - /** @test */ + #[Test] public function to_array_wont_break_if_no_translations_exist(): void { $vegetable = factory(Vegetable::class)->make(); @@ -23,7 +24,7 @@ public function to_array_wont_break_if_no_translations_exist(): void static::assertIsArray($vegetable->toArray()); } - /** @test */ + #[Test] public function translated_attributes_can_be_accessed_as_properties(): void { $vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']); @@ -32,7 +33,7 @@ public function translated_attributes_can_be_accessed_as_properties(): void static::assertEquals('Peas', $vegetable->name); } - /** @test */ + #[Test] public function it_can_hide_translated_attributes(): void { $vegetable = factory(Vegetable::class)->make(['name:en' => 'Peas']); @@ -44,7 +45,7 @@ public function it_can_hide_translated_attributes(): void static::assertFalse(isset($vegetable->toArray()['name'])); } - /** @test */ + #[Test] public function it_finds_custom_primary_keys(): void { $vegetable = new Vegetable(); @@ -52,7 +53,7 @@ public function it_finds_custom_primary_keys(): void static::assertEquals('vegetable_identity', $vegetable->getTranslationRelationKey()); } - /** @test */ + #[Test] public function setAttribute_returns_parent_setAttribute(): void { $vegetable = new Vegetable(); diff --git a/tests/LocalesTest.php b/tests/LocalesTest.php index 3fa842c7..615a85bf 100644 --- a/tests/LocalesTest.php +++ b/tests/LocalesTest.php @@ -2,12 +2,13 @@ namespace Astrotomic\Translatable\Tests; +use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Exception\LocalesNotDefinedException; use Astrotomic\Translatable\Locales; final class LocalesTest extends TestCase { - /** @test */ + #[Test] public function locales_is_declared_as_a_singleton_instance(): void { $singletonHash = spl_object_hash(app(Locales::class)); @@ -16,7 +17,7 @@ public function locales_is_declared_as_a_singleton_instance(): void static::assertEquals($singletonHash, spl_object_hash($this->app->make(Locales::class))); } - /** @test */ + #[Test] public function it_loads_the_locales_from_the_configuration(): void { $this->app['config']->set('translatable.locales', [ @@ -34,7 +35,7 @@ public function it_loads_the_locales_from_the_configuration(): void static::assertEquals(['de', 'en'], $this->app->make('translatable.locales')->all()); } - /** @test */ + #[Test] public function it_throws_an_exception_if_there_are_no_locales(): void { $this->expectException(LocalesNotDefinedException::class); @@ -43,7 +44,7 @@ public function it_throws_an_exception_if_there_are_no_locales(): void $this->app->make('translatable.locales')->load(); } - /** @test */ + #[Test] public function all_language_locales_are_loaded_from_the_configuration(): void { $this->app['config']->set('translatable.locales', [ @@ -58,7 +59,7 @@ public function all_language_locales_are_loaded_from_the_configuration(): void static::assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->all()); } - /** @test */ + #[Test] public function it_loads_locales_and_countries(): void { $this->app['config']->set('translatable.locales', [ @@ -76,7 +77,7 @@ public function it_loads_locales_and_countries(): void static::assertEquals(['en', 'en-GB', 'en-US', 'de', 'de-DE', 'de-CH'], $this->app->make('translatable.locales')->all()); } - /** @test */ + #[Test] public function can_return_locales_as_array(): void { $this->app['config']->set('translatable.locales', [ @@ -91,7 +92,7 @@ public function can_return_locales_as_array(): void static::assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->toArray()); } - /** @test */ + #[Test] public function can_retrieve_current_configuration(): void { $this->app['config']->set('translatable.locale', 'de'); @@ -99,7 +100,7 @@ public function can_retrieve_current_configuration(): void static::assertEquals('de', $this->app->make('translatable.locales')->current()); } - /** @test */ + #[Test] public function current_can_return_the_translator_locale_if_configuration_is_empty(): void { $this->app['config']->set('translatable.locale', null); @@ -108,7 +109,7 @@ public function current_can_return_the_translator_locale_if_configuration_is_emp static::assertEquals('en', $this->app->make('translatable.locales')->current()); } - /** @test */ + #[Test] public function it_checks_if_it_has_a_locale(): void { $this->app['config']->set('translatable.locales', [ @@ -124,7 +125,7 @@ public function it_checks_if_it_has_a_locale(): void static::assertFalse($this->app->make('translatable.locales')->has('jp')); } - /** @test */ + #[Test] public function can_access_as_an_array(): void { $this->app['config']->set('translatable.locales', [ @@ -140,7 +141,7 @@ public function can_access_as_an_array(): void static::assertFalse(isset($this->app->make('translatable.locales')['jp'])); } - /** @test */ + #[Test] public function can_retrieve_a_specific_locale_by_get(): void { $this->app['config']->set('translatable.locales', [ @@ -156,7 +157,7 @@ public function can_retrieve_a_specific_locale_by_get(): void static::assertNull($this->app->make('translatable.locales')->get('jp')); } - /** @test */ + #[Test] public function missing_locale_returns_null_by_get(): void { $this->app['config']->set('translatable.locales', [ @@ -172,7 +173,7 @@ public function missing_locale_returns_null_by_get(): void static::assertNull($this->app->make('translatable.locales')['jp']); } - /** @test */ + #[Test] public function it_can_add_a_locale(): void { $this->app['config']->set('translatable.locales', [ @@ -186,7 +187,7 @@ public function it_can_add_a_locale(): void static::assertTrue($this->app->make('translatable.locales')->has('en')); } - /** @test */ + #[Test] public function locale_can_be_added_by_accessing_as_an_array(): void { $this->app['config']->set('translatable.locales', [ @@ -200,7 +201,7 @@ public function locale_can_be_added_by_accessing_as_an_array(): void static::assertTrue($this->app->make('translatable.locales')->has('en')); } - /** @test */ + #[Test] public function locale_country_can_be_added_by_accessing_as_an_array(): void { $this->app['config']->set('translatable.locales', [ @@ -214,7 +215,7 @@ public function locale_country_can_be_added_by_accessing_as_an_array(): void static::assertTrue($this->app->make('translatable.locales')->has('de-AT')); } - /** @test */ + #[Test] public function can_forget_a_locale(): void { $this->app['config']->set('translatable.locales', [ @@ -229,7 +230,7 @@ public function can_forget_a_locale(): void static::assertFalse($this->app->make('translatable.locales')->has('en')); } - /** @test */ + #[Test] public function can_forget_a_locale_using_unset_as_an_array(): void { $this->app['config']->set('translatable.locales', [ @@ -244,7 +245,7 @@ public function can_forget_a_locale_using_unset_as_an_array(): void static::assertFalse($this->app->make('translatable.locales')->has('en')); } - /** @test */ + #[Test] public function can_retrieve_the_locale_country_separator(): void { $this->app['config']->set('translatable.locale_separator', '_'); @@ -252,7 +253,7 @@ public function can_retrieve_the_locale_country_separator(): void static::assertEquals('_', $this->app->make('translatable.locales')->getLocaleSeparator()); } - /** @test */ + #[Test] public function can_set_a_default_locale_country_separator_if_configuration_is_missing(): void { $this->app['config']->set('translatable.locale_separator', null); @@ -260,20 +261,20 @@ public function can_set_a_default_locale_country_separator_if_configuration_is_m static::assertEquals('-', $this->app->make('translatable.locales')->getLocaleSeparator()); } - /** @test */ + #[Test] public function can_get_a_country_locale_formatted_with_separator(): void { static::assertEquals('de-AT', $this->app->make('translatable.locales')->getCountryLocale('de', 'AT')); } - /** @test */ + #[Test] public function can_determine_if_a_locale_is_country_based(): void { static::assertTrue($this->app->make('translatable.locales')->isLocaleCountryBased('de-AT')); static::assertFalse($this->app->make('translatable.locales')->isLocaleCountryBased('de')); } - /** @test */ + #[Test] public function can_get_a_locale_from_the_country_locale(): void { static::assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de-AT')); diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index 900ad267..c6173228 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -2,12 +2,13 @@ namespace Astrotomic\Translatable\Tests; +use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Tests\Eloquent\Country; use Astrotomic\Translatable\Tests\Eloquent\Vegetable; final class ScopesTest extends TestCase { - /** @test */ + #[Test] public function translated_in_scope_returns_only_translated_records_for_this_locale(): void { factory(Country::class)->create(['code' => 'ca', 'name:ca' => 'Català']); @@ -16,7 +17,7 @@ public function translated_in_scope_returns_only_translated_records_for_this_loc static::assertEquals(1, Country::translatedIn('fr')->count()); } - /** @test */ + #[Test] public function translated_in_scope_works_with_default_locale(): void { app()->setLocale('de'); @@ -27,7 +28,7 @@ public function translated_in_scope_works_with_default_locale(): void static::assertEquals('Griechenland', Country::translatedIn()->first()->name); } - /** @test */ + #[Test] public function not_translated_in_scope_returns_only_not_translated_records_for_this_locale(): void { factory(Country::class)->create(['code' => 'ca', 'name:ca' => 'Català']); @@ -41,7 +42,7 @@ public function not_translated_in_scope_returns_only_not_translated_records_for_ static::assertFalse($notTranslated->last()->hasTranslation('en')); } - /** @test */ + #[Test] public function not_translated_in_scope_works_with_default_locale(): void { app()->setLocale('en'); @@ -54,7 +55,7 @@ public function not_translated_in_scope_works_with_default_locale(): void static::assertFalse($notTranslated->first()->hasTranslation('en')); } - /** @test */ + #[Test] public function translated_scope_returns_records_with_at_least_one_translation(): void { factory(Country::class)->create(['code' => 'ca']); @@ -64,7 +65,7 @@ public function translated_scope_returns_records_with_at_least_one_translation() static::assertEquals('English', Country::with('translations')->translated()->first()->{'name:en'}); } - /** @test */ + #[Test] public function lists_of_translated_fields(): void { app()->setLocale('de'); @@ -80,7 +81,7 @@ public function lists_of_translated_fields(): void static::assertEquals('Griechenland', $countries->first()->name); } - /** @test */ + #[Test] public function lists_of_translated_fields_with_fallback(): void { app('config')->set('translatable.fallback_locale', 'en'); @@ -102,7 +103,7 @@ public function lists_of_translated_fields_with_fallback(): void static::assertEquals('France', $countries->last()->name); } - /** @test */ + #[Test] public function lists_of_translated_fields_disable_autoload_translations(): void { app()->setLocale('de'); @@ -116,7 +117,7 @@ public function lists_of_translated_fields_disable_autoload_translations(): void Country::defaultAutoloadTranslations(); } - /** @test */ + #[Test] public function lists_of_translated_fields_enable_autoload_translations(): void { app()->setLocale('de'); @@ -139,7 +140,7 @@ public function lists_of_translated_fields_enable_autoload_translations(): void Country::defaultAutoloadTranslations(); } - /** @test */ + #[Test] public function scope_withTranslation_without_fallback(): void { factory(Country::class)->create(['code' => 'el', 'name:en' => 'Greece']); @@ -150,7 +151,7 @@ public function scope_withTranslation_without_fallback(): void static::assertSame('Greece', $result->translations->first()->name); } - /** @test */ + #[Test] public function scope_withTranslation_with_fallback(): void { app('config')->set('translatable.fallback_locale', 'de'); @@ -164,7 +165,7 @@ public function scope_withTranslation_with_fallback(): void static::assertEquals('Griechenland', $result->translations->where('locale', 'de')->first()->name); } - /** @test */ + #[Test] public function scope_withTranslation_with_country_based_fallback(): void { app('config')->set('translatable.fallback_locale', 'en'); @@ -196,7 +197,7 @@ public function scope_withTranslation_with_country_based_fallback(): void static::assertEquals('Zucchini', $translations[2]->name); } - /** @test */ + #[Test] public function whereTranslation_filters_by_translation(): void { factory(Country::class)->create(['code' => 'gr', 'name:en' => 'Greece']); @@ -204,7 +205,7 @@ public function whereTranslation_filters_by_translation(): void static::assertSame('gr', Country::whereTranslation('name', 'Greece')->first()->code); } - /** @test */ + #[Test] public function orWhereTranslation_filters_by_translation(): void { factory(Country::class)->create(['code' => 'gr', 'name:en' => 'Greece']); @@ -217,7 +218,7 @@ public function orWhereTranslation_filters_by_translation(): void static::assertSame('France', $result->last()->name); } - /** @test */ + #[Test] public function whereTranslation_filters_by_translation_and_locale(): void { factory(Country::class)->create(['code' => 'gr', 'name:de' => 'Griechenland']); @@ -230,7 +231,7 @@ public function whereTranslation_filters_by_translation_and_locale(): void static::assertSame('gr', $result->first()->code); } - /** @test */ + #[Test] public function whereTranslationLike_filters_by_translation(): void { factory(Country::class)->create(['code' => 'gr', 'name:en' => 'Greece']); @@ -238,7 +239,7 @@ public function whereTranslationLike_filters_by_translation(): void static::assertSame('gr', Country::whereTranslationLike('name', '%Greec%')->first()->code); } - /** @test */ + #[Test] public function orWhereTranslationLike_filters_by_translation(): void { factory(Country::class)->create(['code' => 'gr', 'name:en' => 'Greece']); @@ -251,7 +252,7 @@ public function orWhereTranslationLike_filters_by_translation(): void static::assertSame('France', $result->last()->name); } - /** @test */ + #[Test] public function whereTranslationLike_filters_by_translation_and_locale(): void { factory(Country::class)->create(['code' => 'gr', 'name:de' => 'Griechenland']); @@ -264,7 +265,7 @@ public function whereTranslationLike_filters_by_translation_and_locale(): void static::assertEquals('gr', $result->first()->code); } - /** @test */ + #[Test] public function orderByTranslation_sorts_by_key_asc(): void { factory(Country::class)->create(['code' => 'el', 'name' => 'Greece']); @@ -273,7 +274,7 @@ public function orderByTranslation_sorts_by_key_asc(): void static::assertEquals('fr', Country::orderByTranslation('name')->get()->first()->code); } - /** @test */ + #[Test] public function orderByTranslation_sorts_by_key_desc(): void { factory(Country::class)->create(['code' => 'el', 'name' => 'Greece']); @@ -282,7 +283,7 @@ public function orderByTranslation_sorts_by_key_desc(): void static::assertEquals('el', Country::orderByTranslation('name', 'desc')->get()->first()->code); } - /** @test */ + #[Test] public function test_orderByTranslation_sorts_by_key_asc_even_if_locale_is_missing(): void { factory(Vegetable::class)->create(['en' => ['name' => 'Potatoes'], 'fr' => ['name' => 'Pommes de Terre']]); diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index a5b2162e..10bade55 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -2,6 +2,7 @@ namespace Astrotomic\Translatable\Tests; +use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Locales; use Astrotomic\Translatable\Tests\Eloquent\Country; use Astrotomic\Translatable\Tests\Eloquent\CountryStrict; @@ -16,7 +17,7 @@ final class TranslatableTest extends TestCase { - /** @test */ + #[Test] public function it_finds_the_default_translation_class(): void { static::assertEquals( @@ -25,7 +26,7 @@ public function it_finds_the_default_translation_class(): void ); } - /** @test */ + #[Test] public function it_finds_the_translation_class_with_namespace_set(): void { $this->app->make('config')->set('translatable.translation_model_namespace', 'App\Models\Translations'); @@ -36,7 +37,7 @@ public function it_finds_the_translation_class_with_namespace_set(): void ); } - /** @test */ + #[Test] public function it_finds_the_translation_class_with_suffix_set(): void { $this->app->make('config')->set('translatable.translation_suffix', 'Trans'); @@ -47,7 +48,7 @@ public function it_finds_the_translation_class_with_suffix_set(): void ); } - /** @test */ + #[Test] public function it_returns_custom_TranslationModelName(): void { $vegetable = new Vegetable(); @@ -64,7 +65,7 @@ public function it_returns_custom_TranslationModelName(): void ); } - /** @test */ + #[Test] public function it_returns_relation_key(): void { $vegetable = new Vegetable(); @@ -74,7 +75,7 @@ public function it_returns_relation_key(): void static::assertEquals('my_awesome_key', $vegetable->getRelationKey()); } - /** @test */ + #[Test] public function it_returns_the_translation(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς', 'name:en' => 'Peas']); @@ -90,7 +91,7 @@ public function it_returns_the_translation(): void static::assertEquals('Peas', $vegetable->translate()->name); } - /** @test */ + #[Test] public function it_returns_the_translation_with_accessor(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς', 'name:en' => 'Peas']); @@ -99,7 +100,7 @@ public function it_returns_the_translation_with_accessor(): void static::assertEquals('Peas', $vegetable->{'name:en'}); } - /** @test */ + #[Test] public function it_returns_null_when_the_locale_doesnt_exist(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς']); @@ -107,7 +108,7 @@ public function it_returns_null_when_the_locale_doesnt_exist(): void static::assertSame(null, $vegetable->{'name:unknown-locale'}); } - /** @test */ + #[Test] public function it_saves_translations(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς', 'name:en' => 'Peas']); @@ -121,7 +122,7 @@ public function it_saves_translations(): void static::assertEquals('Pea', $vegetable->name); } - /** @test */ + #[Test] public function it_saves_translations_with_mutator(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς', 'name:en' => 'Peas']); @@ -138,7 +139,7 @@ public function it_saves_translations_with_mutator(): void static::assertEquals('Μπιζέλι', $vegetable->translate()->name); } - /** @test */ + #[Test] public function it_does_not_lazy_load_translations_when_updating_non_translated_attributes(): void { DB::enableQueryLog(); @@ -161,7 +162,7 @@ public function it_does_not_lazy_load_translations_when_updating_non_translated_ DB::disableQueryLog(); } - /** @test */ + #[Test] public function it_uses_default_locale_to_return_translations(): void { $vegetable = factory(Vegetable::class)->create(['name:el' => 'Αρακάς']); @@ -176,7 +177,7 @@ public function it_uses_default_locale_to_return_translations(): void static::assertEquals('Μπιζέλι', $vegetable->translate('el')->name); } - /** @test */ + #[Test] public function it_creates_translations_using_the_shortcut(): void { $vegetable = factory(Vegetable::class)->create(); @@ -193,7 +194,7 @@ public function it_creates_translations_using_the_shortcut(): void ]); } - /** @test */ + #[Test] public function it_creates_translations_using_mass_assignment(): void { $vegetable = Vegetable::create([ @@ -205,7 +206,7 @@ public function it_creates_translations_using_mass_assignment(): void static::assertEquals('Peas', $vegetable->name); } - /** @test */ + #[Test] public function it_creates_translations_using_mass_assignment_and_locales(): void { $vegetable = Vegetable::create([ @@ -223,7 +224,7 @@ public function it_creates_translations_using_mass_assignment_and_locales(): voi static::assertEquals('Pois', $vegetable->translate('fr')->name); } - /** @test */ + #[Test] public function it_skips_mass_assignment_if_attributes_non_fillable(): void { $this->expectException(MassAssignmentException::class); @@ -238,7 +239,7 @@ public function it_skips_mass_assignment_if_attributes_non_fillable(): void static::assertNull($country->translate('fr')); } - /** @test */ + #[Test] public function it_returns_if_object_has_translation(): void { $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); @@ -247,7 +248,7 @@ public function it_returns_if_object_has_translation(): void static::assertFalse($vegetable->hasTranslation('some-code')); } - /** @test */ + #[Test] public function it_returns_default_translation(): void { $this->app->make('config')->set('translatable.fallback_locale', 'de'); @@ -262,7 +263,7 @@ public function it_returns_default_translation(): void static::assertSame('Erbsen', $vegetable->translateOrDefault()->name); } - /** @test */ + #[Test] public function fallback_option_in_config_overrides_models_fallback_option(): void { $this->app->make('config')->set('translatable.fallback_locale', 'de'); @@ -280,7 +281,7 @@ public function fallback_option_in_config_overrides_models_fallback_option(): vo static::assertNull($vegetable->getTranslation('ch')); } - /** @test */ + #[Test] public function configuration_defines_if_fallback_is_used(): void { $this->app->make('config')->set('translatable.fallback_locale', 'de'); @@ -291,7 +292,7 @@ public function configuration_defines_if_fallback_is_used(): void static::assertEquals('de', $vegetable->getTranslation('ch')->locale); } - /** @test */ + #[Test] public function useTranslationFallback_overrides_configuration(): void { $this->app->make('config')->set('translatable.fallback_locale', 'de'); @@ -303,7 +304,7 @@ public function useTranslationFallback_overrides_configuration(): void static::assertNull($vegetable->getTranslation('ch')); } - /** @test */ + #[Test] public function it_returns_null_if_fallback_is_not_defined(): void { $this->app->make('config')->set('translatable.fallback_locale', 'ch'); @@ -313,7 +314,7 @@ public function it_returns_null_if_fallback_is_not_defined(): void static::assertNull($vegetable->getTranslation('pl', true)); } - /** @test */ + #[Test] public function it_fills_a_non_default_language_with_fallback_set(): void { $this->app->make('config')->set('translatable.fallback_locale', 'en'); @@ -328,7 +329,7 @@ public function it_fills_a_non_default_language_with_fallback_set(): void static::assertEquals('Peas', $vegetable->translate('en')->name); } - /** @test */ + #[Test] public function it_creates_a_new_translation(): void { $this->app->make('config')->set('translatable.fallback_locale', 'en'); @@ -340,7 +341,7 @@ public function it_creates_a_new_translation(): void static::assertEquals('Peas', $vegetable->translate('en')->name); } - /** @test */ + #[Test] public function the_locale_key_is_locale_by_default(): void { $vegetable = new Vegetable(); @@ -348,7 +349,7 @@ public function the_locale_key_is_locale_by_default(): void static::assertEquals('locale', $vegetable->getLocaleKey()); } - /** @test */ + #[Test] public function the_locale_key_can_be_overridden_in_configuration(): void { $this->app->make('config')->set('translatable.locale_key', 'language_id'); @@ -357,7 +358,7 @@ public function the_locale_key_can_be_overridden_in_configuration(): void static::assertEquals('language_id', $vegetable->getLocaleKey()); } - /** @test */ + #[Test] public function the_locale_key_can_be_customized_per_model(): void { $vegetable = new Vegetable(); @@ -379,13 +380,13 @@ public function test_the_translation_model_can_be_customized(): void CountryStrict::reguard(); } - /** @test */ + #[Test] public function it_reads_the_configuration(): void { static::assertEquals('Translation', $this->app->make('config')->get('translatable.translation_suffix')); } - /** @test */ + #[Test] public function getting_translation_does_not_create_translation(): void { $vegetable = factory(Vegetable::class)->create(); @@ -393,7 +394,7 @@ public function getting_translation_does_not_create_translation(): void static::assertNull($vegetable->getTranslation('en', false)); } - /** @test */ + #[Test] public function getting_translated_field_does_not_create_translation(): void { $this->app->setLocale('en'); @@ -402,7 +403,7 @@ public function getting_translated_field_does_not_create_translation(): void static::assertNull($vegetable->getTranslation('en')); } - /** @test */ + #[Test] public function it_has_methods_that_return_always_a_translation(): void { $vegetable = factory(Vegetable::class)->create(); @@ -412,7 +413,7 @@ public function it_has_methods_that_return_always_a_translation(): void static::assertEquals('xyz', $vegetable->translateOrNew()->locale); } - /** @test */ + #[Test] public function it_throws_an_exception_if_translation_does_not_exist(): void { $this->expectException(ModelNotFoundException::class); @@ -426,7 +427,7 @@ public function it_throws_an_exception_if_translation_does_not_exist(): void $vegetable->translateOrFail('xyz'); } - /** @test */ + #[Test] public function it_returns_if_attribute_is_translated(): void { $vegetable = new Vegetable(); @@ -435,7 +436,7 @@ public function it_returns_if_attribute_is_translated(): void static::assertFalse($vegetable->isTranslationAttribute('some-field')); } - /** @test */ + #[Test] public function config_overrides_apps_locale(): void { $veegtable = factory(Vegetable::class)->create(['name:de' => 'Erbsen']); @@ -444,7 +445,7 @@ public function config_overrides_apps_locale(): void static::assertEquals('Erbsen', $veegtable->name); } - /** @test */ + #[Test] public function locales_as_array_keys_are_properly_detected(): void { $this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]); @@ -460,7 +461,7 @@ public function locales_as_array_keys_are_properly_detected(): void static::assertEquals('US Peas', $vegetable->getTranslation('en-US')->name); } - /** @test */ + #[Test] public function locale_separator_can_be_configured(): void { $this->app->make('config')->set('translatable.locales', ['en' => ['GB']]); @@ -473,7 +474,7 @@ public function locale_separator_can_be_configured(): void static::assertEquals('Peas', $vegetable->getTranslation('en_GB')->name); } - /** @test */ + #[Test] public function fallback_for_country_based_locales(): void { $this->app->make('config')->set('translatable.use_fallback', true); @@ -491,7 +492,7 @@ public function fallback_for_country_based_locales(): void static::assertEquals('French fries', $vegetable->getTranslation('en-US')->name); } - /** @test */ + #[Test] public function fallback_for_country_based_locales_with_no_base_locale(): void { $this->app->make('config')->set('translatable.use_fallback', true); @@ -508,7 +509,7 @@ public function fallback_for_country_based_locales_with_no_base_locale(): void static::assertEquals('Chips', $vegetable->getTranslation('pt-BR')->name); } - /** @test */ + #[Test] public function to_array_and_fallback_with_country_based_locales_enabled(): void { $this->app->make('config')->set('translatable.locale', 'en-GB'); @@ -523,7 +524,7 @@ public function to_array_and_fallback_with_country_based_locales_enabled(): void static::assertEquals('Frites', $vegetable['name']); } - /** @test */ + #[Test] public function it_skips_translations_in_to_array_when_config_is_set(): void { $this->app->make('config')->set('translatable.to_array_always_loads_translations', false); @@ -534,7 +535,7 @@ public function it_skips_translations_in_to_array_when_config_is_set(): void static::assertFalse(isset($vegetable['name'])); } - /** @test */ + #[Test] public function it_returns_translations_in_to_array_when_config_is_set_but_translations_are_loaded(): void { $this->app->make('config')->set('translatable.to_array_always_loads_translations', false); @@ -545,7 +546,7 @@ public function it_returns_translations_in_to_array_when_config_is_set_but_trans static::assertTrue(isset($vegetable['name'])); } - /** @test */ + #[Test] public function it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model(): void { $person = new Person(['name' => 'john doe']); @@ -554,7 +555,7 @@ public function it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on static::assertEquals('John Doe', $person->name); } - /** @test */ + #[Test] public function it_deletes_all_translations(): void { $vegetable = factory(Vegetable::class)->create(['name:es' => 'Guisantes', 'name:en' => 'Peas']); @@ -566,7 +567,7 @@ public function it_deletes_all_translations(): void static::assertEquals(0, count($vegetable->translations)); } - /** @test */ + #[Test] public function it_deletes_translations_for_given_locales(): void { $vegetable = factory(Vegetable::class)->create(['name:es' => 'Guisantes', 'name:en' => 'Peas']); @@ -578,7 +579,7 @@ public function it_deletes_translations_for_given_locales(): void static::assertEquals(1, count($vegetable->translations)); } - /** @test */ + #[Test] public function passing_an_empty_array_should_not_delete_translations(): void { $vegetable = factory(Vegetable::class)->create(['name:es' => 'Guisantes', 'name:en' => 'Peas']); @@ -590,7 +591,7 @@ public function passing_an_empty_array_should_not_delete_translations(): void static::assertEquals(2, count($vegetable->translations)); } - /** @test */ + #[Test] public function fill_with_translation_key(): void { $vegetable = new Vegetable(); @@ -607,7 +608,7 @@ public function fill_with_translation_key(): void static::assertEquals('Erbsen', $vegetable->translate('de')->name); } - /** @test */ + #[Test] public function it_uses_the_default_locale_from_the_model(): void { $vegetable = new Vegetable(); @@ -629,7 +630,7 @@ public function it_uses_the_default_locale_from_the_model(): void static::assertEquals('Pois', $vegetable->name); } - /** @test */ + #[Test] public function replicate_entity(): void { $vegetable = new Vegetable(); @@ -658,7 +659,7 @@ public function replicate_entity(): void static::assertEquals($replicated->translate('de')->vegetable_identity, $replicated->identity); } - /** @test */ + #[Test] public function can_get_translations_as_array(): void { $vegetable = factory(Vegetable::class)->create([ @@ -674,7 +675,7 @@ public function can_get_translations_as_array(): void ], $vegetable->getTranslationsArray()); } - /** @test */ + #[Test] public function fill_will_ignore_unkown_locales(): void { config(['translatable.locales' => ['en']]); @@ -694,7 +695,7 @@ public function fill_will_ignore_unkown_locales(): void static::assertDatabaseMissing('vegetable_translations', ['locale' => 'ua']); } - /** @test */ + #[Test] public function fill_will_ignore_unkown_locales_with_translations(): void { config(['translatable.locales' => ['en']]); @@ -715,7 +716,7 @@ public function fill_will_ignore_unkown_locales_with_translations(): void static::assertDatabaseMissing('vegetable_translations', ['locale' => 'ua']); } - /** @test */ + #[Test] public function it_uses_fallback_locale_if_default_is_empty(): void { $this->app->make('config')->set('translatable.use_fallback', true); @@ -733,7 +734,7 @@ public function it_uses_fallback_locale_if_default_is_empty(): void static::assertEquals('Peas', $vegetable->name); } - /** @test */ + #[Test] public function it_uses_value_when_fallback_is_not_available(): void { $this->app->make('config')->set('translatable.fallback_locale', 'it'); @@ -753,7 +754,7 @@ public function it_uses_value_when_fallback_is_not_available(): void static::assertEquals('Erbsen', $vegetable->getAttribute('name')); } - /** @test */ + #[Test] public function empty_translated_attribute(): void { $this->app->setLocale('invalid'); @@ -762,7 +763,7 @@ public function empty_translated_attribute(): void static::assertNull($vegetable->name); } - /** @test */ + #[Test] public function numeric_translated_attribute(): void { $this->app->make('config')->set('translatable.fallback_locale', 'de'); @@ -797,7 +798,7 @@ protected function isEmptyTranslatableAttribute(string $key, $value): bool static::assertSame('1', $vegetable->name); } - /** @test */ + #[Test] public function translation_relation(): void { $this->app->make('config')->set('translatable.fallback_locale', 'fr'); @@ -813,7 +814,7 @@ public function translation_relation(): void static::assertEquals('en', $peas->translation->locale); } - /** @test */ + #[Test] public function translation_relation_can_use_fallback_locale(): void { $this->app->make('config')->set('translatable.fallback_locale', 'fr'); @@ -826,7 +827,7 @@ public function translation_relation_can_use_fallback_locale(): void static::assertEquals('fr', $peas->translation->locale); } - /** @test */ + #[Test] public function translation_relation_returns_null_if_no_available_locale_was_found(): void { $this->app->make('config')->set('translatable.fallback_locale', 'xyz'); @@ -838,7 +839,7 @@ public function translation_relation_returns_null_if_no_available_locale_was_fou static::assertNull($peas->translation); } - /** @test */ + #[Test] public function can_fill_conflicting_attribute_locale(): void { $this->app->make('config')->set('translatable.locales', ['en', 'id']); @@ -865,7 +866,7 @@ public function can_fill_conflicting_attribute_locale(): void static::assertEquals('en:my country', $country->getTranslation('en', false)->name); } - /** @test */ + #[Test] public function it_returns_first_existing_translation_as_fallback(): void { /** @var Locales $helper */ @@ -942,7 +943,7 @@ public function it_returns_first_existing_translation_as_fallback(): void static::assertEquals($helper->getCountryLocale('de', 'DE'), $translation->locale); } - /** @test */ + #[Test] public function it_uses_translation_relation_if_locale_matches(): void { $this->app->make('config')->set('translatable.use_fallback', false); @@ -962,7 +963,7 @@ public function it_uses_translation_relation_if_locale_matches(): void static::assertFalse($country->relationLoaded('translations')); } - /** @test */ + #[Test] public function it_uses_translations_relation_if_locale_does_not_match(): void { $this->app->make('config')->set('translatable.use_fallback', false); @@ -983,7 +984,7 @@ public function it_uses_translations_relation_if_locale_does_not_match(): void static::assertTrue($country->relationLoaded('translations')); } - /** @test */ + #[Test] public function it_does_not_load_translation_relation_if_not_already_loaded(): void { $this->app->make('config')->set('translatable.use_fallback', false); @@ -1002,7 +1003,7 @@ public function it_does_not_load_translation_relation_if_not_already_loaded(): v static::assertTrue($country->relationLoaded('translations')); } - /** @test */ + #[Test] public function it_does_not_delete_translations_on_cascade_by_default() { $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); @@ -1016,7 +1017,7 @@ public function it_does_not_delete_translations_on_cascade_by_default() $this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]); } - /** @test */ + #[Test] public function it_deletes_translations_on_cascade() { Vegetable::enableDeleteTranslationsCascade(); @@ -1031,7 +1032,7 @@ public function it_deletes_translations_on_cascade() $this->assertDatabaseMissing('vegetable_translations', ['vegetable_identity' => $vegetable->identity]); } - /** @test */ + #[Test] public function it_does_not_delete_on_cascade_after_retrieving_a_model() { Vegetable::enableDeleteTranslationsCascade(); @@ -1047,7 +1048,7 @@ public function it_does_not_delete_on_cascade_after_retrieving_a_model() $this->assertDatabaseHas('vegetable_translations', ['vegetable_identity' => $vegetable->identity]); } - /** @test */ + #[Test] public function it_can_restore_translations_in_a_transaction() { Vegetable::enableDeleteTranslationsCascade(); diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 889fef41..92f59e73 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -2,6 +2,7 @@ namespace Astrotomic\Translatable\Tests; +use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Locales; use Astrotomic\Translatable\Validation\RuleFactory; use Illuminate\Validation\Rule; @@ -10,7 +11,7 @@ final class ValidationTest extends TestCase { - /** @test */ + #[Test] public function it_does_not_touch_untranslated_keys(): void { $rules = [ @@ -24,7 +25,7 @@ public function it_does_not_touch_untranslated_keys(): void static::assertEquals($rules, RuleFactory::make($rules)); } - /** @test */ + #[Test] public function format_array_it_replaces_single_key(): void { $rules = [ @@ -41,7 +42,7 @@ public function format_array_it_replaces_single_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_replaces_sub_key(): void { $rules = [ @@ -58,7 +59,7 @@ public function format_array_it_replaces_sub_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_replaces_middle_key(): void { $rules = [ @@ -75,7 +76,7 @@ public function format_array_it_replaces_middle_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_replaces_middle_key_with_custom_prefix(): void { $rules = [ @@ -92,7 +93,7 @@ public function format_array_it_replaces_middle_key_with_custom_prefix(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY, '{')); } - /** @test */ + #[Test] public function format_array_it_replaces_middle_key_with_custom_suffix(): void { $rules = [ @@ -109,7 +110,7 @@ public function format_array_it_replaces_middle_key_with_custom_suffix(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY, '%', '}')); } - /** @test */ + #[Test] public function format_array_it_replaces_middle_key_with_custom_delimiters(): void { $rules = [ @@ -126,7 +127,7 @@ public function format_array_it_replaces_middle_key_with_custom_delimiters(): vo ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY, '{', '}')); } - /** @test */ + #[Test] public function format_array_it_replaces_middle_key_with_custom_regex_delimiters(): void { $rules = [ @@ -143,7 +144,7 @@ public function format_array_it_replaces_middle_key_with_custom_regex_delimiters ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY, '$', '$')); } - /** @test */ + #[Test] public function format_array_it_uses_config_as_default(): void { app('config')->set('translatable.rule_factory', [ @@ -168,7 +169,7 @@ public function format_array_it_uses_config_as_default(): void ], RuleFactory::make($rules)); } - /** @test */ + #[Test] public function format_key_it_replaces_single_key(): void { $rules = [ @@ -185,7 +186,7 @@ public function format_key_it_replaces_single_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_replaces_sub_key(): void { $rules = [ @@ -202,7 +203,7 @@ public function format_key_it_replaces_sub_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_replaces_middle_key(): void { $rules = [ @@ -219,7 +220,7 @@ public function format_key_it_replaces_middle_key(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_uses_config_as_default(): void { app('config')->set('translatable.rule_factory', [ @@ -244,7 +245,7 @@ public function format_key_it_uses_config_as_default(): void ], RuleFactory::make($rules)); } - /** @test */ + #[Test] public function it_replaces_key_with_custom_locales(): void { $rules = [ @@ -262,7 +263,7 @@ public function it_replaces_key_with_custom_locales(): void ])); } - /** @test */ + #[Test] public function it_throws_exception_with_undefined_locales(): void { $this->expectException(InvalidArgumentException::class); @@ -279,7 +280,7 @@ public function it_throws_exception_with_undefined_locales(): void ]); } - /** @test */ + #[Test] public function format_array_it_replaces_single_rule(): void { $rules = [ @@ -300,7 +301,7 @@ public function format_array_it_replaces_single_rule(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_replaces_imploded_rules(): void { $rules = [ @@ -321,7 +322,7 @@ public function format_array_it_replaces_imploded_rules(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_replaces_array_of_rules(): void { $rules = [ @@ -342,7 +343,7 @@ public function format_array_it_replaces_array_of_rules(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_ARRAY)); } - /** @test */ + #[Test] public function format_array_it_does_not_touch_non_string_rule(): void { $rules = [ @@ -361,7 +362,7 @@ public function format_array_it_does_not_touch_non_string_rule(): void static::assertInstanceOf(RequiredIf::class, $formattedRules['de-AT.content']); } - /** @test */ + #[Test] public function format_array_it_does_not_touch_non_string_rule_in_array(): void { $rules = [ @@ -387,7 +388,7 @@ public function format_array_it_does_not_touch_non_string_rule_in_array(): void static::assertInstanceOf(RequiredIf::class, $formattedRules['de-AT.content'][1]); } - /** @test */ + #[Test] public function format_key_it_replaces_single_rule(): void { $rules = [ @@ -408,7 +409,7 @@ public function format_key_it_replaces_single_rule(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_replaces_imploded_rules(): void { $rules = [ @@ -429,7 +430,7 @@ public function format_key_it_replaces_imploded_rules(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_replaces_array_of_rules(): void { $rules = [ @@ -450,7 +451,7 @@ public function format_key_it_replaces_array_of_rules(): void ], RuleFactory::make($rules, RuleFactory::FORMAT_KEY)); } - /** @test */ + #[Test] public function format_key_it_does_not_touch_non_string_rule(): void { $rules = [ @@ -469,7 +470,7 @@ public function format_key_it_does_not_touch_non_string_rule(): void static::assertInstanceOf(RequiredIf::class, $formattedRules['content:de-AT']); } - /** @test */ + #[Test] public function format_key_it_does_not_touch_non_string_rule_in_array(): void { $rules = [ From 1b47948719189444638be19d5f7857c32cc1b7a0 Mon Sep 17 00:00:00 2001 From: Shift Date: Sun, 17 Mar 2024 20:52:34 +0000 Subject: [PATCH 09/15] Add return types to test methods --- tests/TranslatableTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 10bade55..e9285d94 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -1004,7 +1004,7 @@ public function it_does_not_load_translation_relation_if_not_already_loaded(): v } #[Test] - public function it_does_not_delete_translations_on_cascade_by_default() + public function it_does_not_delete_translations_on_cascade_by_default(): void { $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); @@ -1018,7 +1018,7 @@ public function it_does_not_delete_translations_on_cascade_by_default() } #[Test] - public function it_deletes_translations_on_cascade() + public function it_deletes_translations_on_cascade(): void { Vegetable::enableDeleteTranslationsCascade(); $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); @@ -1033,7 +1033,7 @@ public function it_deletes_translations_on_cascade() } #[Test] - public function it_does_not_delete_on_cascade_after_retrieving_a_model() + public function it_does_not_delete_on_cascade_after_retrieving_a_model(): void { Vegetable::enableDeleteTranslationsCascade(); $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); @@ -1049,7 +1049,7 @@ public function it_does_not_delete_on_cascade_after_retrieving_a_model() } #[Test] - public function it_can_restore_translations_in_a_transaction() + public function it_can_restore_translations_in_a_transaction(): void { Vegetable::enableDeleteTranslationsCascade(); $vegetable = factory(Vegetable::class)->create(['name:en' => 'Peas']); From 8c86d10ba975dd389667ad4773d1e6b5a3619052 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 22:55:53 +0200 Subject: [PATCH 10/15] fix run-tests.yml --- .github/workflows/run-tests.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 4e16bf5d..d67da085 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -15,22 +15,22 @@ jobs: fail-fast: true matrix: php: [8.0, 8.1, 8.2, 8.3] - laravel: [8.*, 9.*, 10.*, 11.*] + laravel: ['8.*', '9.*', '10.*', '11.*'] dependency-version: [prefer-stable] os: [ubuntu-latest] exclude: - php: '8.0' - laravel: '10' + laravel: '10.*' - php: '8.0' - laravel: '11' + laravel: '11.*' - php: '8.1' - laravel: '11' + laravel: '11.*' - php: '8.2' - laravel: '8' + laravel: '8.*' - php: '8.3' - laravel: '8' + laravel: '8.*' - php: '8.3' - laravel: '9' + laravel: '9.*' name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} @@ -62,7 +62,7 @@ jobs: timeout_minutes: 5 max_attempts: 5 command: composer require "laravel/framework:^8.83.27" "phpunit/phpunit:^9.6.15" --no-update --no-interaction - if: "matrix.laravel == '8'" + if: "matrix.laravel == '8.*'" - name: Select Laravel 9 uses: nick-invision/retry@v2 @@ -70,7 +70,7 @@ jobs: timeout_minutes: 5 max_attempts: 5 command: composer require "laravel/framework:^9.52.16" "phpunit/phpunit:^9.6.15" --no-update --no-interaction - if: "matrix.laravel == '9'" + if: "matrix.laravel == '9.*'" - name: Select Laravel 10 uses: nick-invision/retry@v2 @@ -78,7 +78,7 @@ jobs: timeout_minutes: 5 max_attempts: 5 command: composer require "laravel/framework:^10.34.2" "phpunit/phpunit:^10.4.2" --no-update --no-interaction - if: "matrix.laravel == '10'" + if: "matrix.laravel == '10.*'" - name: Select Laravel 11 uses: nick-invision/retry@v2 @@ -86,7 +86,7 @@ jobs: timeout_minutes: 5 max_attempts: 5 command: composer require "laravel/framework:^11.0" "phpunit/phpunit:^10.4.2" --no-update --no-interaction - if: "matrix.laravel == '11'" + if: "matrix.laravel == '11.*'" - name: Install PHP Dependencies uses: nick-invision/retry@v2 From 22129b329a44746d3b22405698d7aed029be2257 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 23:07:50 +0200 Subject: [PATCH 11/15] Fix phpunit 10 warnings --- phpunit.xml.dist | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5b011d3a..9a0e6b9a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,30 +1,30 @@ - - ./tests/ + ./tests/ ./tests/factories/ ./tests/migrations/ ./tests/models/ - - - ./src/Translatable/ - - + + + ./src/Translatable/ + + From e7ba80519ba5f73442aaadd4130f46918bf53173 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 23:11:42 +0200 Subject: [PATCH 12/15] Fix test - use less strict version for pint --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a15319e0..0c33f7c0 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ }, "require-dev": { "laravel/legacy-factories": "^1.0.4", - "laravel/pint": "^1.14", + "laravel/pint": "^1.0", "mockery/mockery": "^1.3.3", "orchestra/testbench": "^6.0 || ^7.0 || ^8.0 || ^9.0", "phpunit/phpunit": "^10.0" From 72d7b1a8c4d439bfac5218dee98e529533c65265 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 23:13:29 +0200 Subject: [PATCH 13/15] remove useless option in run-tests.yml --- .github/workflows/run-tests.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index d67da085..8453aadd 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -32,8 +32,6 @@ jobs: - php: '8.3' laravel: '9.*' - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} - steps: - name: Checkout code uses: actions/checkout@v4.1.2 From 3ae411a9865e1b6883850081323b294af3b98aac Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 23:15:49 +0200 Subject: [PATCH 14/15] Update README.md files --- README.md | 3 ++- docs/README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ff36dd2a..53c84c37 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,8 @@ If you discover any security related issues, please check [SECURITY](https://git ## Versions | Package | Laravel | PHP | -|:--------------------|:------------------------------| :-------- | +|:--------------------|:------------------------------|:----------| +| **v11.13 - v11.13** | `8.* / 9.* / 10.*` / `11.*` | `^8.0` | | **v11.12 - v11.12** | `8.* / 9.* / 10.*` | `^8.0` | | **v11.10 - v11.11** | `8.* / 9.*` | `^8.0` | | **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` | diff --git a/docs/README.md b/docs/README.md index ae0ab600..c0b5b344 100644 --- a/docs/README.md +++ b/docs/README.md @@ -74,7 +74,8 @@ echo $post->translate('fr')->title; // Mon premier post ## Versions | Package | Laravel | PHP | -|:--------------------|:------------------------------| :-------- | +|:--------------------|:------------------------------|:----------| +| **v11.13 - v11.13** | `8.* / 9.* / 10.*` / `11.*` | `^8.0` | | **v11.12 - v11.12** | `8.* / 9.* / 10.*` | `^8.0` | | **v11.10 - v11.11** | `8.* / 9.*` | `^8.0` | | **v11.6 - v11.9** | `5.8.* / 6.* / 7.* / 8.*` | `>=7.2` | From 51a70b8f5154ba10ce866194367dddabd99b71c3 Mon Sep 17 00:00:00 2001 From: Oleksandr Moik Date: Sun, 17 Mar 2024 23:43:12 +0200 Subject: [PATCH 15/15] Apply patch from styleci --- tests/EloquentOverrideTest.php | 2 +- tests/LocalesTest.php | 2 +- tests/ScopesTest.php | 2 +- tests/TranslatableTest.php | 26 +++++++++++++------------- tests/ValidationTest.php | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/EloquentOverrideTest.php b/tests/EloquentOverrideTest.php index 83e16b91..db00032f 100644 --- a/tests/EloquentOverrideTest.php +++ b/tests/EloquentOverrideTest.php @@ -2,8 +2,8 @@ namespace Astrotomic\Translatable\Tests; -use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Tests\Eloquent\Vegetable; +use PHPUnit\Framework\Attributes\Test; final class EloquentOverrideTest extends TestCase { diff --git a/tests/LocalesTest.php b/tests/LocalesTest.php index 615a85bf..41d5c5f1 100644 --- a/tests/LocalesTest.php +++ b/tests/LocalesTest.php @@ -2,9 +2,9 @@ namespace Astrotomic\Translatable\Tests; -use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Exception\LocalesNotDefinedException; use Astrotomic\Translatable\Locales; +use PHPUnit\Framework\Attributes\Test; final class LocalesTest extends TestCase { diff --git a/tests/ScopesTest.php b/tests/ScopesTest.php index c6173228..dd7da8da 100644 --- a/tests/ScopesTest.php +++ b/tests/ScopesTest.php @@ -2,9 +2,9 @@ namespace Astrotomic\Translatable\Tests; -use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Tests\Eloquent\Country; use Astrotomic\Translatable\Tests\Eloquent\Vegetable; +use PHPUnit\Framework\Attributes\Test; final class ScopesTest extends TestCase { diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index e9285d94..f22c835b 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -2,7 +2,6 @@ namespace Astrotomic\Translatable\Tests; -use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Locales; use Astrotomic\Translatable\Tests\Eloquent\Country; use Astrotomic\Translatable\Tests\Eloquent\CountryStrict; @@ -14,6 +13,7 @@ use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\DB; +use PHPUnit\Framework\Attributes\Test; final class TranslatableTest extends TestCase { @@ -211,8 +211,8 @@ public function it_creates_translations_using_mass_assignment_and_locales(): voi { $vegetable = Vegetable::create([ 'quantity' => 5, - 'en' => ['name' => 'Peas'], - 'fr' => ['name' => 'Pois'], + 'en' => ['name' => 'Peas'], + 'fr' => ['name' => 'Pois'], ]); static::assertEquals(5, $vegetable->quantity); @@ -230,8 +230,8 @@ public function it_skips_mass_assignment_if_attributes_non_fillable(): void $this->expectException(MassAssignmentException::class); $country = CountryStrict::create([ 'code' => 'be', - 'en' => ['name' => 'Belgium'], - 'fr' => ['name' => 'Belgique'], + 'en' => ['name' => 'Belgium'], + 'fr' => ['name' => 'Belgique'], ]); static::assertEquals('be', $country->code); @@ -322,8 +322,8 @@ public function it_fills_a_non_default_language_with_fallback_set(): void $vegetable = new Vegetable(); $vegetable->fill([ 'quantity' => 5, - 'en' => ['name' => 'Peas'], - 'de' => ['name' => 'Erbsen'], + 'en' => ['name' => 'Peas'], + 'de' => ['name' => 'Erbsen'], ]); static::assertEquals('Peas', $vegetable->translate('en')->name); @@ -451,7 +451,7 @@ public function locales_as_array_keys_are_properly_detected(): void $this->app->config->set('translatable.locales', ['en' => ['US', 'GB']]); $vegetable = Vegetable::create([ - 'en' => ['name' => 'Peas'], + 'en' => ['name' => 'Peas'], 'en-US' => ['name' => 'US Peas'], 'en-GB' => ['name' => 'GB Peas'], ]); @@ -484,9 +484,9 @@ public function fallback_for_country_based_locales(): void $this->app->make('translatable.locales')->load(); $vegetable = factory(Vegetable::class)->create([ - 'fr' => ['name' => 'Frites'], + 'fr' => ['name' => 'Frites'], 'en-GB' => ['name' => 'Chips'], - 'en' => ['name' => 'French fries'], + 'en' => ['name' => 'French fries'], ]); static::assertEquals('French fries', $vegetable->getTranslation('en-US')->name); @@ -502,7 +502,7 @@ public function fallback_for_country_based_locales_with_no_base_locale(): void $this->app->make('translatable.locales')->load(); $vegetable = factory(Vegetable::class)->create([ - 'en' => ['name' => 'Chips'], + 'en' => ['name' => 'Chips'], 'pt-PT' => ['name' => 'Batatas fritas'], ]); @@ -682,8 +682,8 @@ public function fill_will_ignore_unkown_locales(): void $vegetable = new Vegetable(); $vegetable->fill([ - 'en' => ['name' => 'Peas'], - 'ua' => ['name' => 'Unkown'], + 'en' => ['name' => 'Peas'], + 'ua' => ['name' => 'Unkown'], ]); $vegetable->save(); diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 92f59e73..53263ae6 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -2,12 +2,12 @@ namespace Astrotomic\Translatable\Tests; -use PHPUnit\Framework\Attributes\Test; use Astrotomic\Translatable\Locales; use Astrotomic\Translatable\Validation\RuleFactory; use Illuminate\Validation\Rule; use Illuminate\Validation\Rules\RequiredIf; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\Test; final class ValidationTest extends TestCase {