Skip to content

Commit

Permalink
docs: Extended doc for media data
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhanShares committed Oct 6, 2023
1 parent 43f4932 commit 3bc9c5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,10 @@ You can update a media name with an instance of Media.
```php
$media = Media::first();
$media->name = 'New name';
$media->data = ['additional_data' => 'new additional data']
$media->data = ['additional_data' => 'additional data']
$media->save()
```

Certainly! Here's an update to your documentation that includes the new features:

---

### Update Media

With an instance of `Media`, you can perform various update operations:
Expand All @@ -239,6 +235,13 @@ $media->data = ['additional_data' => 'new additional data'];
$media->save();
```

#### You can also remove all data:

```php
$media->data = [];
$media->save();
```

#### Update Media File Name:

Updating the media file name will also rename the actual file in storage.
Expand Down
32 changes: 29 additions & 3 deletions tests/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,21 @@ public function it_can_create_a_media_record_with_media_uploader()
])
->upload();

$fetch = Media::find($mediaOne->id);
$fetch = Media::with('collections')->find($mediaOne->id);

$this->assertEquals($fetch->id, $mediaOne->id);

$this->assertEquals('image', $mediaOne->name);
$this->assertEquals('image.jpg', $mediaOne->file_name);
$this->assertEquals('default', $mediaOne->disk);

// test disks
$this->assertEquals('one', $mediaOne->collections->first()->name);
// todo: support multiple disks

$this->assertEquals('extra data value', $fetch->data['extraData']);
$this->assertEquals('additional data value', $fetch->data['additional_data']);
$this->assertEquals('anything else?', $fetch->data['something-else']);
}

/** @test */
Expand All @@ -51,11 +63,25 @@ public function it_can_update_a_media_record()
$this->assertEquals('image', $mediaOne->name);

$mediaOne->name = 'new-name';
$mediaOne->data = ['newData' => 'new value'];
$mediaOne->data = ['metadata' => 'file metadata'];
$mediaOne->save();

$this->assertEquals('new-name', $mediaOne->name);
$this->assertEquals(['newData' => 'new value'], $mediaOne->data);
$this->assertEquals(['metadata' => 'file metadata'], $mediaOne->data);

// update data
$mediaOne->data = [
'metadata' => 'updated existing key data',
'extra_data' => 'new extra data',
];
$mediaOne->save();

$this->assertEquals('new extra data', $mediaOne->data['extra_data']);
$this->assertEquals('updated existing key data', $mediaOne->data['metadata']);

$mediaOne->data = [];
$mediaOne->save();
$this->assertEquals(0, count($mediaOne->data));

// todo: make a fluent api like the following?
// $mediaOne->rename('new-file')
Expand Down

0 comments on commit 3bc9c5f

Please sign in to comment.