Skip to content

Commit 96f2897

Browse files
authored
Merge pull request #1 from WovenCoast/main
feat: add getAllowedTypes for use in request validation
2 parents 231118c + 4ee5651 commit 96f2897

File tree

8 files changed

+74
-44
lines changed

8 files changed

+74
-44
lines changed

src/Media/AllowedMimeTypes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ abstract class AllowedMimeTypes
266266
'text/x-scriptzsh' => 'zsh',
267267
];
268268

269+
/**
270+
* Get all allowed types
271+
*
272+
* @return array
273+
*/
274+
public static function getAllowedTypes()
275+
{
276+
return array_keys(self::$allowed_mime_types);
277+
}
269278

270279
public static function registerMimeTypes(string $key, array $mime_types): void
271280
{

tests/Feature/AdminModelTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Javaabu\Helpers\AdminModel\AdminModel;
88
use Javaabu\Helpers\AdminModel\IsAdminModel;
99
use Javaabu\Helpers\Tests\TestCase;
10+
use PHPUnit\Framework\Attributes\Test;
1011

1112
class CategoryWithDateCast extends Model implements AdminModel
1213
{
@@ -121,7 +122,7 @@ class AdminModelTest extends TestCase
121122
{
122123
use RefreshDatabase;
123124

124-
/** @test */
125+
#[Test]
125126
public function it_can_get_list_of_date_fields(): void
126127
{
127128
$this->assertEquals([
@@ -131,7 +132,7 @@ public function it_can_get_list_of_date_fields(): void
131132
], CategoryWithDateCast::getDateFieldsList());
132133
}
133134

134-
/** @test */
135+
#[Test]
135136
public function it_can_determine_if_an_attribute_is_an_allowed_date_field(): void
136137
{
137138
$this->assertTrue(CategoryWithDateCast::isAllowedDateField('published_at'));
@@ -140,23 +141,23 @@ public function it_can_determine_if_an_attribute_is_an_allowed_date_field(): voi
140141
$this->assertFalse(CategoryWithDateCast::isAllowedDateField('name'));
141142
}
142143

143-
/** @test */
144+
#[Test]
144145
public function it_can_determine_date_fields_from_date_casts(): void
145146
{
146147
$category = new CategoryWithDateCast();
147148

148149
$this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes());
149150
}
150151

151-
/** @test */
152+
#[Test]
152153
public function it_can_determine_date_fields_from_datetime_casts(): void
153154
{
154155
$category = new CategoryWithDateTimeCast();
155156

156157
$this->assertEquals(['published_at', 'created_at', 'updated_at'], $category->getDateAttributes());
157158
}
158159

159-
/** @test */
160+
#[Test]
160161
public function it_can_filter_models_by_date_range(): void
161162
{
162163
$category = new CategoryWithDateCast([
@@ -171,7 +172,7 @@ public function it_can_filter_models_by_date_range(): void
171172
$this->assertEquals($category->id, $found->id);
172173
}
173174

174-
/** @test */
175+
#[Test]
175176
public function it_can_search_models_using_a_partial_match(): void
176177
{
177178
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
@@ -182,7 +183,7 @@ public function it_can_search_models_using_a_partial_match(): void
182183
$this->assertEquals($category->id, $found->id);
183184
}
184185

185-
/** @test */
186+
#[Test]
186187
public function it_can_search_models_using_a_single_searchable(): void
187188
{
188189
$category = new CategoryWithSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
@@ -194,7 +195,7 @@ public function it_can_search_models_using_a_single_searchable(): void
194195
$this->assertNull(CategoryWithSearchable::search('some-slug')->first());
195196
}
196197

197-
/** @test */
198+
#[Test]
198199
public function it_can_search_models_using_a_string_searchable(): void
199200
{
200201
$category = new CategoryWithStringSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
@@ -206,7 +207,7 @@ public function it_can_search_models_using_a_string_searchable(): void
206207
$this->assertNull(CategoryWithStringSearchable::search('some-slug')->first());
207208
}
208209

209-
/** @test */
210+
#[Test]
210211
public function it_can_search_models_using_multiple_searchables(): void
211212
{
212213
$category = new CategoryWithMultipleSearchable(['name' => 'Apple', 'slug' => 'some-slug']);
@@ -221,7 +222,7 @@ public function it_can_search_models_using_multiple_searchables(): void
221222
$this->assertEquals($category->id, $found->id);
222223
}
223224

224-
/** @test */
225+
#[Test]
225226
public function it_can_search_models_without_searchable(): void
226227
{
227228
$category = new CategoryWithoutSearchable(['name' => 'Apple', 'slug' => 'some-slug']);

tests/Feature/AllowedMimeTypesTest.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Javaabu\Helpers\Media\AllowedMimeTypes;
66
use Javaabu\Helpers\Tests\TestCase;
7+
use PHPUnit\Framework\Attributes\Test;
78

89
class AllowedMimeTypesTest extends TestCase
910
{
@@ -15,20 +16,20 @@ protected function setUp(): void
1516
$this->app['config']->set('defaults.max_image_file_size', 1024 * 2);
1617
}
1718

18-
/** @test */
19+
#[Test]
1920
public function it_can_get_the_max_file_size_based_on_multiple_file_types(): void
2021
{
2122
$this->assertEquals(1024 * 10, AllowedMimeTypes::getMaxFileSize(['document', 'image']));
2223
}
2324

24-
/** @test */
25+
#[Test]
2526
public function it_can_get_the_max_file_size_based_on_file_type(): void
2627
{
2728
$this->assertEquals(1024 * 2, AllowedMimeTypes::getMaxFileSize('image'));
2829
$this->assertEquals(1024 * 10, AllowedMimeTypes::getMaxFileSize('document'));
2930
}
3031

31-
/** @test */
32+
#[Test]
3233
public function it_can_register_mimetypes(): void
3334
{
3435
// not registered
@@ -40,7 +41,7 @@ public function it_can_register_mimetypes(): void
4041
$this->assertEquals(['text/plain'], AllowedMimeTypes::getAllowedMimeTypes('paper'));
4142
}
4243

43-
/** @test */
44+
#[Test]
4445
public function it_can_register_file_size_settings(): void
4546
{
4647
// not registered
@@ -52,7 +53,7 @@ public function it_can_register_file_size_settings(): void
5253
$this->assertEquals('max_document_file_size', AllowedMimeTypes::getFileSizeSetting('paper'));
5354
}
5455

55-
/** @test */
56+
#[Test]
5657
public function it_can_register_mime_type_extensions(): void
5758
{
5859
// not registered
@@ -64,34 +65,34 @@ public function it_can_register_mime_type_extensions(): void
6465
$this->assertEquals('pp', AllowedMimeTypes::getExtension('text/paper'));
6566
}
6667

67-
/** @test */
68+
#[Test]
6869
public function it_can_get_file_size_setting_from_type(): void
6970
{
7071
$this->assertEquals('max_image_file_size', AllowedMimeTypes::getFileSizeSetting('icon'));
7172
$this->assertEquals('max_image_file_size', AllowedMimeTypes::getFileSizeSetting('image'));
7273
$this->assertEquals('max_upload_file_size', AllowedMimeTypes::getFileSizeSetting('document'));
7374
}
7475

75-
/** @test */
76+
#[Test]
7677
public function it_can_get_file_extension_from_mime_type(): void
7778
{
7879
$this->assertEquals('jpeg', AllowedMimeTypes::getExtension('image/jpeg'));
7980
}
8081

81-
/** @test */
82+
#[Test]
8283
public function it_can_get_file_extensions_from_mime_types(): void
8384
{
8485
$this->assertEquals(['jpeg', 'ico'], AllowedMimeTypes::getExtensions(['image/jpeg', 'image/x-icon', 'image/x-ico']));
8586
}
8687

87-
/** @test */
88+
#[Test]
8889
public function it_can_check_if_a_given_mime_type_as_a_string_is_an_allowed_mime_type(): void
8990
{
9091
$result = AllowedMimeTypes::isAllowedMimeType('image/jpeg', 'image');
9192
$this->assertTrue($result);
9293
}
9394

94-
/** @test */
95+
#[Test]
9596
public function it_can_check_if_a_given_mime_type_as_an_array_is_an_allowed_mime_type()
9697
{
9798
$result = AllowedMimeTypes::isAllowedMimeType('image/jpeg', ['image', 'video']);
@@ -101,4 +102,18 @@ public function it_can_check_if_a_given_mime_type_as_an_array_is_an_allowed_mime
101102
$this->assertTrue($result);
102103
}
103104

105+
#[Test]
106+
public function it_can_get_all_allowed_types()
107+
{
108+
$result = AllowedMimeTypes::getAllowedTypes();
109+
$this->assertEquals([
110+
'image',
111+
'icon',
112+
'document',
113+
'video',
114+
'audio',
115+
'excel',
116+
'paper',
117+
], $result);
118+
}
104119
}

tests/Feature/HelpersServiceProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Javaabu\Helpers\Tests\Feature;
44

55
use Javaabu\Helpers\Tests\TestCase;
6+
use PHPUnit\Framework\Attributes\Test;
67

78
class HelpersServiceProviderTest extends TestCase
89
{
@@ -20,7 +21,7 @@ protected function tearDown(): void
2021
parent::tearDown();
2122
}
2223

23-
/** @test */
24+
#[Test]
2425
public function it_loads_local_helpers()
2526
{
2627
$this->withoutExceptionHandling();

tests/Feature/ValidatorsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Javaabu\Helpers\Tests\Feature;
44

55
use Javaabu\Helpers\Tests\TestCase;
6+
use PHPUnit\Framework\Attributes\Test;
67

78
class ValidatorsTest extends TestCase
89
{
@@ -38,7 +39,7 @@ protected function tearDown(): void
3839
parent::tearDown();
3940
}
4041

41-
/** @test */
42+
#[Test]
4243
public function it_can_validate_a_slug()
4344
{
4445
$validator = validator(
@@ -53,7 +54,7 @@ public function it_can_validate_a_slug()
5354
);
5455
}
5556

56-
/** @test */
57+
#[Test]
5758
public function it_can_load_localized_validation()
5859
{
5960
$this->app->setLocale('dv');

tests/Unit/EnumsTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use Javaabu\Helpers\Enums\IsStatusEnum;
77
use Javaabu\Helpers\Enums\PublishStatuses;
88
use Javaabu\Helpers\Tests\TestCase;
9+
use PHPUnit\Framework\Attributes\Test;
910

1011
class EnumsTest extends TestCase
1112
{
12-
/** @test */
13+
#[Test]
1314
public function it_can_generate_enum_label()
1415
{
1516
$this->assertEquals('Pending', PublishStatuses::PENDING->getLabel());
@@ -21,7 +22,7 @@ public function it_can_generate_enum_label()
2122
], PublishStatuses::getLabels());
2223
}
2324

24-
/** @test */
25+
#[Test]
2526
public function it_can_see_that_an_enum_is_a_status_enum()
2627
{
2728
$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()
3132
$this->assertTrue(method_exists(PublishStatuses::class, 'getColor'));
3233
}
3334

34-
/** @test */
35+
#[Test]
3536
public function it_can_get_enum_color()
3637
{
3738
// Check if enum implements HasColor

tests/Unit/HasCachedSoftDeleteCountTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use Illuminate\Foundation\Testing\RefreshDatabase;
66
use Javaabu\Helpers\Tests\TestCase;
77
use Javaabu\Helpers\Tests\TestSupport\Models\Post;
8+
use PHPUnit\Framework\Attributes\Test;
89

910
class HasCachedSoftDeleteCountTest extends TestCase
1011
{
1112
use RefreshDatabase;
1213

13-
/** @test */
14+
#[Test]
1415
public function it_can_check_whether_a_model_has_soft_deletes()
1516
{
1617
$post = new Post([
@@ -30,7 +31,7 @@ public function it_can_check_whether_a_model_has_soft_deletes()
3031
$this->assertFalse(Post::hasRecordsInTrash());
3132
}
3233

33-
/** @test */
34+
#[Test]
3435
public function it_clears_the_cached_soft_deletes_when_a_model_is_deleted()
3536
{
3637
$post = new Post([
@@ -47,7 +48,7 @@ public function it_clears_the_cached_soft_deletes_when_a_model_is_deleted()
4748
$this->assertFalse(cache()->has(Post::getSoftDeleteCacheKey()));
4849
}
4950

50-
/** @test */
51+
#[Test]
5152
public function it_clears_the_cached_soft_deletes_when_a_model_is_restored()
5253
{
5354
$post = new Post([

0 commit comments

Comments
 (0)