From 93dbae87b0377cc31ea85ab759c66af92c901482 Mon Sep 17 00:00:00 2001 From: WovenCoast Date: Wed, 12 Mar 2025 11:16:29 +0500 Subject: [PATCH 1/2] fix: phpunit deprecations --- tests/Feature/AdminModelTest.php | 21 ++++++------- tests/Feature/AllowedMimeTypesTest.php | 21 ++++++------- tests/Feature/HelpersServiceProviderTest.php | 3 +- tests/Feature/ValidatorsTest.php | 5 ++-- tests/Unit/EnumsTest.php | 7 +++-- tests/Unit/HasCachedSoftDeleteCountTest.php | 7 +++-- tests/Unit/HelpersTest.php | 31 ++++++++++---------- 7 files changed, 51 insertions(+), 44 deletions(-) diff --git a/tests/Feature/AdminModelTest.php b/tests/Feature/AdminModelTest.php index 803c1ce..a49e4ac 100644 --- a/tests/Feature/AdminModelTest.php +++ b/tests/Feature/AdminModelTest.php @@ -7,6 +7,7 @@ use Javaabu\Helpers\AdminModel\AdminModel; use Javaabu\Helpers\AdminModel\IsAdminModel; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class CategoryWithDateCast extends Model implements AdminModel { @@ -121,7 +122,7 @@ class AdminModelTest extends TestCase { use RefreshDatabase; - /** @test */ + #[Test] public function it_can_get_list_of_date_fields(): void { $this->assertEquals([ @@ -131,7 +132,7 @@ public function it_can_get_list_of_date_fields(): void ], CategoryWithDateCast::getDateFieldsList()); } - /** @test */ + #[Test] public function it_can_determine_if_an_attribute_is_an_allowed_date_field(): void { $this->assertTrue(CategoryWithDateCast::isAllowedDateField('published_at')); @@ -140,7 +141,7 @@ public function it_can_determine_if_an_attribute_is_an_allowed_date_field(): voi $this->assertFalse(CategoryWithDateCast::isAllowedDateField('name')); } - /** @test */ + #[Test] public function it_can_determine_date_fields_from_date_casts(): void { $category = new CategoryWithDateCast(); @@ -148,7 +149,7 @@ public function it_can_determine_date_fields_from_date_casts(): void $this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes()); } - /** @test */ + #[Test] public function it_can_determine_date_fields_from_datetime_casts(): void { $category = new CategoryWithDateTimeCast(); @@ -156,7 +157,7 @@ public function it_can_determine_date_fields_from_datetime_casts(): void $this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes()); } - /** @test */ + #[Test] public function it_can_filter_models_by_date_range(): void { $category = new CategoryWithDateCast([ @@ -171,7 +172,7 @@ public function it_can_filter_models_by_date_range(): void $this->assertEquals($category->id, $found->id); } - /** @test */ + #[Test] public function it_can_search_models_using_a_partial_match(): void { $category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']); @@ -182,7 +183,7 @@ public function it_can_search_models_using_a_partial_match(): void $this->assertEquals($category->id, $found->id); } - /** @test */ + #[Test] public function it_can_search_models_using_a_single_searchable(): void { $category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']); @@ -194,7 +195,7 @@ public function it_can_search_models_using_a_single_searchable(): void $this->assertNull(CategoryWithSearchable::search('some-slug')->first()); } - /** @test */ + #[Test] public function it_can_search_models_using_a_string_searchable(): void { $category = new CategoryWithStringSearchable(['name' => 'Apple', 'slug' => 'some-slug']); @@ -206,7 +207,7 @@ public function it_can_search_models_using_a_string_searchable(): void $this->assertNull(CategoryWithStringSearchable::search('some-slug')->first()); } - /** @test */ + #[Test] public function it_can_search_models_using_multiple_searchables(): void { $category = new CategoryWithMultipleSearchable(['name' => 'Apple', 'slug' => 'some-slug']); @@ -221,7 +222,7 @@ public function it_can_search_models_using_multiple_searchables(): void $this->assertEquals($category->id, $found->id); } - /** @test */ + #[Test] public function it_can_search_models_without_searchable(): void { $category = new CategoryWithoutSearchable(['name' => 'Apple', 'slug' => 'some-slug']); diff --git a/tests/Feature/AllowedMimeTypesTest.php b/tests/Feature/AllowedMimeTypesTest.php index 532a7c7..3a7b5e4 100644 --- a/tests/Feature/AllowedMimeTypesTest.php +++ b/tests/Feature/AllowedMimeTypesTest.php @@ -4,6 +4,7 @@ use Javaabu\Helpers\Media\AllowedMimeTypes; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class AllowedMimeTypesTest extends TestCase { @@ -15,20 +16,20 @@ protected function setUp(): void $this->app['config']->set('defaults.max_image_file_size', 1024 * 2); } - /** @test */ + #[Test] public function it_can_get_the_max_file_size_based_on_multiple_file_types(): void { $this->assertEquals(1024 * 10, AllowedMimeTypes::getMaxFileSize(['document', 'image'])); } - /** @test */ + #[Test] public function it_can_get_the_max_file_size_based_on_file_type(): void { $this->assertEquals(1024 * 2, AllowedMimeTypes::getMaxFileSize('image')); $this->assertEquals(1024 * 10, AllowedMimeTypes::getMaxFileSize('document')); } - /** @test */ + #[Test] public function it_can_register_mimetypes(): void { // not registered @@ -40,7 +41,7 @@ public function it_can_register_mimetypes(): void $this->assertEquals(['text/plain'], AllowedMimeTypes::getAllowedMimeTypes('paper')); } - /** @test */ + #[Test] public function it_can_register_file_size_settings(): void { // not registered @@ -52,7 +53,7 @@ public function it_can_register_file_size_settings(): void $this->assertEquals('max_document_file_size', AllowedMimeTypes::getFileSizeSetting('paper')); } - /** @test */ + #[Test] public function it_can_register_mime_type_extensions(): void { // not registered @@ -64,7 +65,7 @@ public function it_can_register_mime_type_extensions(): void $this->assertEquals('pp', AllowedMimeTypes::getExtension('text/paper')); } - /** @test */ + #[Test] public function it_can_get_file_size_setting_from_type(): void { $this->assertEquals('max_image_file_size', AllowedMimeTypes::getFileSizeSetting('icon')); @@ -72,26 +73,26 @@ public function it_can_get_file_size_setting_from_type(): void $this->assertEquals('max_upload_file_size', AllowedMimeTypes::getFileSizeSetting('document')); } - /** @test */ + #[Test] public function it_can_get_file_extension_from_mime_type(): void { $this->assertEquals('jpeg', AllowedMimeTypes::getExtension('image/jpeg')); } - /** @test */ + #[Test] public function it_can_get_file_extensions_from_mime_types(): void { $this->assertEquals(['jpeg', 'ico'], AllowedMimeTypes::getExtensions(['image/jpeg', 'image/x-icon', 'image/x-ico'])); } - /** @test */ + #[Test] public function it_can_check_if_a_given_mime_type_as_a_string_is_an_allowed_mime_type(): void { $result = AllowedMimeTypes::isAllowedMimeType('image/jpeg', 'image'); $this->assertTrue($result); } - /** @test */ + #[Test] public function it_can_check_if_a_given_mime_type_as_an_array_is_an_allowed_mime_type() { $result = AllowedMimeTypes::isAllowedMimeType('image/jpeg', ['image', 'video']); diff --git a/tests/Feature/HelpersServiceProviderTest.php b/tests/Feature/HelpersServiceProviderTest.php index 5d94d5d..8ef3571 100644 --- a/tests/Feature/HelpersServiceProviderTest.php +++ b/tests/Feature/HelpersServiceProviderTest.php @@ -3,6 +3,7 @@ namespace Javaabu\Helpers\Tests\Feature; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class HelpersServiceProviderTest extends TestCase { @@ -20,7 +21,7 @@ protected function tearDown(): void parent::tearDown(); } - /** @test */ + #[Test] public function it_loads_local_helpers() { $this->withoutExceptionHandling(); diff --git a/tests/Feature/ValidatorsTest.php b/tests/Feature/ValidatorsTest.php index 7fdb82c..1aaa135 100644 --- a/tests/Feature/ValidatorsTest.php +++ b/tests/Feature/ValidatorsTest.php @@ -3,6 +3,7 @@ namespace Javaabu\Helpers\Tests\Feature; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class ValidatorsTest extends TestCase { @@ -38,7 +39,7 @@ protected function tearDown(): void parent::tearDown(); } - /** @test */ + #[Test] public function it_can_validate_a_slug() { $validator = validator( @@ -53,7 +54,7 @@ public function it_can_validate_a_slug() ); } - /** @test */ + #[Test] public function it_can_load_localized_validation() { $this->app->setLocale('dv'); diff --git a/tests/Unit/EnumsTest.php b/tests/Unit/EnumsTest.php index add09d3..bcbb54d 100644 --- a/tests/Unit/EnumsTest.php +++ b/tests/Unit/EnumsTest.php @@ -6,10 +6,11 @@ use Javaabu\Helpers\Enums\IsStatusEnum; use Javaabu\Helpers\Enums\PublishStatuses; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class EnumsTest extends TestCase { - /** @test */ + #[Test] public function it_can_generate_enum_label() { $this->assertEquals('Pending', PublishStatuses::PENDING->getLabel()); @@ -21,7 +22,7 @@ public function it_can_generate_enum_label() ], PublishStatuses::getLabels()); } - /** @test */ + #[Test] public function it_can_see_that_an_enum_is_a_status_enum() { $this->assertTrue(in_array(IsStatusEnum::class, class_implements(PublishStatuses::class))); @@ -31,7 +32,7 @@ public function it_can_see_that_an_enum_is_a_status_enum() $this->assertTrue(method_exists(PublishStatuses::class, 'getColor')); } - /** @test */ + #[Test] public function it_can_get_enum_color() { // Check if enum implements HasColor diff --git a/tests/Unit/HasCachedSoftDeleteCountTest.php b/tests/Unit/HasCachedSoftDeleteCountTest.php index 5c3d7ea..19b6495 100644 --- a/tests/Unit/HasCachedSoftDeleteCountTest.php +++ b/tests/Unit/HasCachedSoftDeleteCountTest.php @@ -5,12 +5,13 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Javaabu\Helpers\Tests\TestCase; use Javaabu\Helpers\Tests\TestSupport\Models\Post; +use PHPUnit\Framework\Attributes\Test; class HasCachedSoftDeleteCountTest extends TestCase { use RefreshDatabase; - /** @test */ + #[Test] public function it_can_check_whether_a_model_has_soft_deletes() { $post = new Post([ @@ -30,7 +31,7 @@ public function it_can_check_whether_a_model_has_soft_deletes() $this->assertFalse(Post::hasRecordsInTrash()); } - /** @test */ + #[Test] public function it_clears_the_cached_soft_deletes_when_a_model_is_deleted() { $post = new Post([ @@ -47,7 +48,7 @@ public function it_clears_the_cached_soft_deletes_when_a_model_is_deleted() $this->assertFalse(cache()->has(Post::getSoftDeleteCacheKey())); } - /** @test */ + #[Test] public function it_clears_the_cached_soft_deletes_when_a_model_is_restored() { $post = new Post([ diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 168c989..49ecb7a 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -3,6 +3,7 @@ namespace Javaabu\Helpers\Tests\Unit; use Javaabu\Helpers\Tests\TestCase; +use PHPUnit\Framework\Attributes\Test; class HelpersTest extends TestCase { @@ -12,14 +13,14 @@ protected function defineEnvironment($app) config()->set('filesystems.disks.local.serve', false); } - /** @test */ + #[Test] public function it_can_convert_seconds_to_human_readable_time_with_zeros_omitted() { $this->assertEquals('1 year 2 months 14 days 30 minutes 15 seconds', seconds_to_human_readable(37931415, false, false)); $this->assertEquals('1 month 14 days 5 hours 15 seconds', seconds_to_human_readable(3819615, false, false)); } - /** @test */ + #[Test] public function it_can_convert_seconds_to_human_readable_time() { $this->assertEquals('2 years 2 months 14 days 5 hours 30 minutes 15 seconds', seconds_to_human_readable(69485415)); @@ -33,7 +34,7 @@ public function it_can_convert_seconds_to_human_readable_time() $this->assertEquals('1 second', seconds_to_human_readable(1)); } - /** @test */ + #[Test] public function it_can_convert_seconds_to_human_readable_time_in_abbreviated_format() { $this->assertEquals('2 years 2 months 14 days 5 hrs 30 mins 15 secs', seconds_to_human_readable(69485415, true)); @@ -45,14 +46,14 @@ public function it_can_convert_seconds_to_human_readable_time_in_abbreviated_for $this->assertEquals('1 sec', seconds_to_human_readable(1, true)); } - /** @test */ + #[Test] public function it_correctly_runs_to_array() { $this->assertEquals(['mango', 'apple'], to_array('mango,apple')); $this->assertEquals(['mango', 'apple'], to_array(['mango', 'apple'])); } - /** @test */ + #[Test] public function it_correctly_runs_safe_in_array() { $this->assertFalse(safe_in_array('mango', 'mango,apple')); @@ -62,20 +63,20 @@ public function it_correctly_runs_safe_in_array() $this->assertTrue(safe_in_array('mango', ['mango', 'apple'])); } - /** @test */ + #[Test] public function it_correctly_runs_strip_protocol() { $this->assertEquals('google.com', strip_protocol('https://google.com')); $this->assertEquals('google.com', strip_protocol('http://google.com')); } - /** @test */ + #[Test] public function it_correctly_runs_url_path() { $this->assertEquals('about', url_path('https://google.com/about?foo=bar&buz=true')); } - /** @test */ + #[Test] public function it_correctly_runs_top_level_slugs() { $this->assertEquals([], top_level_slugs()); @@ -85,7 +86,7 @@ public function it_correctly_runs_top_level_slugs() $this->assertEquals(['welcome'], top_level_slugs()); } - /** @test */ + #[Test] public function it_correctly_runs_is_color() { $this->assertFalse(is_color('red')); @@ -94,7 +95,7 @@ public function it_correctly_runs_is_color() $this->assertFalse(is_color('#0000001')); } - /** @test */ + #[Test] public function it_correctly_runs_number_format_exact() { $this->assertEquals('1,120.5', number_format_exact(1120.5)); @@ -105,21 +106,21 @@ public function it_correctly_runs_number_format_exact() $this->assertEquals('1,120', number_format_exact('1120.000', max_decimals: 0)); } - /** @test */ + #[Test] public function it_correctly_runs_remove_prefix() { $this->assertEquals('google.com', remove_prefix('http://', 'http://google.com')); $this->assertEquals('google.com', remove_prefix('http://', 'google.com')); } - /** @test */ + #[Test] public function it_correctly_runs_remove_suffix() { $this->assertEquals('google.com', remove_suffix('/about', 'google.com/about')); $this->assertEquals('google.com', remove_suffix('/about', 'google.com')); } - /** @test */ + #[Test] public function it_correctly_runs_slug_to_title() { $this->assertEquals('Apple', slug_to_title('apple')); @@ -127,7 +128,7 @@ public function it_correctly_runs_slug_to_title() $this->assertEquals('Apple Magic', slug_to_title('apple Magic', ' ')); } - /** @test */ + #[Test] public function it_can_convert_a_full_url_to_a_relative_url() { $this->assertEquals('google.com', relative_url('https://google.com'), 'Cannot remove protocol from external url'); @@ -140,7 +141,7 @@ public function it_can_convert_a_full_url_to_a_relative_url() $this->assertEquals('', relative_url(''), 'Cannot handle empty string'); } - /** @test */ + #[Test] public function it_can_add_query_args_to_a_url() { $this->withoutExceptionHandling(); From 4ee5651009d61ff264c7f7db7e55695f1784851c Mon Sep 17 00:00:00 2001 From: WovenCoast Date: Wed, 12 Mar 2025 11:22:01 +0500 Subject: [PATCH 2/2] feat: add getAllowedTypes for use in request validation --- src/Media/AllowedMimeTypes.php | 9 +++++++++ tests/Feature/AllowedMimeTypesTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Media/AllowedMimeTypes.php b/src/Media/AllowedMimeTypes.php index 93098f0..28d7186 100644 --- a/src/Media/AllowedMimeTypes.php +++ b/src/Media/AllowedMimeTypes.php @@ -266,6 +266,15 @@ abstract class AllowedMimeTypes 'text/x-scriptzsh' => 'zsh', ]; + /** + * Get all allowed types + * + * @return array + */ + public static function getAllowedTypes() + { + return array_keys(self::$allowed_mime_types); + } public static function registerMimeTypes(string $key, array $mime_types): void { diff --git a/tests/Feature/AllowedMimeTypesTest.php b/tests/Feature/AllowedMimeTypesTest.php index 3a7b5e4..643a382 100644 --- a/tests/Feature/AllowedMimeTypesTest.php +++ b/tests/Feature/AllowedMimeTypesTest.php @@ -102,4 +102,18 @@ public function it_can_check_if_a_given_mime_type_as_an_array_is_an_allowed_mime $this->assertTrue($result); } + #[Test] + public function it_can_get_all_allowed_types() + { + $result = AllowedMimeTypes::getAllowedTypes(); + $this->assertEquals([ + 'image', + 'icon', + 'document', + 'video', + 'audio', + 'excel', + 'paper', + ], $result); + } }