File tree Expand file tree Collapse file tree
Unit/AttachmentCollections Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2424 */
2525
2626 'attachment_model ' => \Javaabu \Mediapicker \Models \Attachment::class,
27+
28+ /*
29+ |--------------------------------------------------------------------------
30+ | Attachment Observer
31+ |--------------------------------------------------------------------------
32+ |
33+ | Class used to observe changes to attachments
34+ |
35+ */
36+
37+ 'attachment_observer ' => \Javaabu \Mediapicker \Models \Observers \AttachmentObserver::class,
2738];
Original file line number Diff line number Diff line change 77use Javaabu \Mediapicker \Commands \AttachmentsCleanCommand ;
88use Javaabu \Mediapicker \Commands \AttachmentsClearCommand ;
99use Javaabu \Mediapicker \Commands \AttachmentsRegenerateCommand ;
10+ use Javaabu \Mediapicker \Models \Observers \AttachmentObserver ;
1011
1112class MediapickerServiceProvider extends ServiceProvider
1213{
@@ -27,7 +28,9 @@ public function boot()
2728 }
2829
2930 $ attachmentClass = $ this ->getAttachmentClass ();
30- //$attachmentClass::observe(new AttachmentObserver());
31+ $ attachmentObserverClass = config ('mediapicker.attachment_observer ' , AttachmentObserver::class);
32+
33+ $ attachmentClass ::observe (new $ attachmentObserverClass );
3134 // TODO
3235 }
3336
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Mediapicker \Models \Observers ;
4+
5+ use Javaabu \Mediapicker \Contracts \Attachment ;
6+
7+ class AttachmentObserver
8+ {
9+ public function creating (Attachment $ attachment ): void
10+ {
11+ if ($ attachment ->shouldSortWhenCreating ()) {
12+ if (is_null ($ attachment ->order_column )) {
13+ $ attachment ->setHighestOrderNumber ();
14+ }
15+ }
16+ }
17+
18+ // TODO
19+ }
Original file line number Diff line number Diff line change 99use Illuminate \Support \Facades \View ;
1010use Javaabu \Helpers \HelpersServiceProvider ;
1111use Javaabu \Mediapicker \Models \Attachment ;
12+ use Javaabu \Mediapicker \Tests \TestSupport \Models \ModelWithSingleFile ;
13+ use Javaabu \Mediapicker \Tests \TestSupport \Models \ModelWithUnacceptedFile ;
14+ use Javaabu \Mediapicker \Tests \TestSupport \Models \ModelWithUnacceptedMimeType ;
1215use Javaabu \Mediapicker \Tests \TestSupport \Models \Post ;
1316use Javaabu \Mediapicker \Tests \TestSupport \Models \User ;
1417use Orchestra \Testbench \TestCase as BaseTestCase ;
@@ -103,6 +106,21 @@ protected function getModel(): Post
103106 return Post::factory ()->create ();
104107 }
105108
109+ protected function getModelWithUnacceptedFile (): ModelWithUnacceptedFile
110+ {
111+ return ModelWithUnacceptedFile::factory ()->create ();
112+ }
113+
114+ protected function getModelWithUnacceptedMimeType (): ModelWithUnacceptedMimeType
115+ {
116+ return ModelWithUnacceptedMimeType::factory ()->create ();
117+ }
118+
119+ protected function getModelWithSingleFile (): ModelWithSingleFile
120+ {
121+ return ModelWithSingleFile::factory ()->create ();
122+ }
123+
106124 protected function getAttachment (?Model $ model = null ): Attachment
107125 {
108126 $ media = $ this ->getMedia ();
Original file line number Diff line number Diff line change 33namespace Javaabu \Mediapicker \Tests \TestSupport \Factories ;
44
55use Illuminate \Database \Eloquent \Factories \Factory ;
6+ use Illuminate \Database \Eloquent \Model ;
67use Javaabu \Mediapicker \Tests \TestSupport \Models \Post ;
78
89class PostFactory extends Factory
910{
1011 protected $ model = Post::class;
1112
13+ protected function newInstance (array $ arguments = [])
14+ {
15+ $ new_instance = parent ::newInstance ($ arguments );
16+
17+ $ new_instance ->setModel ($ this ->model );
18+
19+ return $ new_instance ;
20+ }
21+
22+ /**
23+ * @param class-string<Model> $model_class
24+ */
25+ public function setModel (string $ model_class ): self
26+ {
27+ $ this ->model = $ model_class ;
28+
29+ return $ this ;
30+ }
31+
1232 public function definition (): array
1333 {
1434 return [
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Mediapicker \Tests \TestSupport \Models ;
4+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Javaabu \Mediapicker \Concerns \InteractsWithAttachments ;
7+ use Javaabu \Mediapicker \Contracts \HasAttachments ;
8+ use Javaabu \Mediapicker \Tests \TestSupport \Factories \PostFactory ;
9+ use Spatie \MediaLibrary \MediaCollections \File ;
10+
11+ class ModelWithSingleFile extends Post
12+ {
13+ public function registerAttachmentCollections ()
14+ {
15+ $ this ->addAttachmentCollection ('test ' )
16+ ->singleFile ();
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Mediapicker \Tests \TestSupport \Models ;
4+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Javaabu \Mediapicker \Concerns \InteractsWithAttachments ;
7+ use Javaabu \Mediapicker \Contracts \HasAttachments ;
8+ use Javaabu \Mediapicker \Tests \TestSupport \Factories \PostFactory ;
9+ use Spatie \MediaLibrary \MediaCollections \File ;
10+
11+ class ModelWithUnacceptedFile extends Post
12+ {
13+ public function registerAttachmentCollections ()
14+ {
15+ $ this ->addAttachmentCollection ('test ' )
16+ ->acceptsFile (function (File $ file ) {
17+ return $ file ->name != 'test.jpg ' ;
18+ });
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Javaabu \Mediapicker \Tests \TestSupport \Models ;
4+ use Illuminate \Database \Eloquent \Factories \HasFactory ;
5+ use Illuminate \Database \Eloquent \Model ;
6+ use Javaabu \Mediapicker \Concerns \InteractsWithAttachments ;
7+ use Javaabu \Mediapicker \Contracts \HasAttachments ;
8+ use Javaabu \Mediapicker \Tests \TestSupport \Factories \PostFactory ;
9+ use Spatie \MediaLibrary \MediaCollections \File ;
10+
11+ class ModelWithUnacceptedMimeType extends Post
12+ {
13+ public function registerAttachmentCollections ()
14+ {
15+ $ this ->addAttachmentCollection ('test ' )
16+ ->acceptsMimeTypes (['text/plain ' ]);
17+ }
18+ }
Original file line number Diff line number Diff line change @@ -12,8 +12,10 @@ class Post extends Model implements HasAttachments
1212 use InteractsWithAttachments;
1313 use HasFactory;
1414
15+ protected $ table = 'posts ' ;
16+
1517 protected static function newFactory ()
1618 {
17- return new PostFactory ();
19+ return ( new PostFactory ())-> setModel ( static ::class );
1820 }
1921}
You can’t perform that action at this time.
0 commit comments