From 572f67801409ec1e7450f519590244995e57ec14 Mon Sep 17 00:00:00 2001 From: Shift Date: Sun, 17 Mar 2024 20:52:33 +0000 Subject: [PATCH 1/4] Bump PHPUnit dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 62306f7..a15319e 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 2/4] Ignore PHPUnit cache folder --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 467d3d7..c9d4490 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 3/4] 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 f74e082..83e16b9 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 3fa842c..615a85b 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 900ad26..c617322 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 a5b2162..10bade5 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 889fef4..92f59e7 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 4/4] 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 10bade5..e9285d9 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']);