Skip to content

Commit

Permalink
Fix an issue where the same mediaCollections could be registered mult…
Browse files Browse the repository at this point in the history
…iple times. (#3598)
  • Loading branch information
timvandijck committed May 3, 2024
1 parent 8d6bac7 commit 3cac2fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public function addMediaCollection(string $name): MediaCollection
{
$mediaCollection = MediaCollection::create($name);

$this->mediaCollections[] = $mediaCollection;
$this->mediaCollections[$name] = $mediaCollection;

return $mediaCollection;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/MediaCollections/GetRegisteredMediaCollections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithConversion;

it('calling getRegisteredMediaCollections multiple times should return the same result', function () {
$testModel = new class() extends TestModelWithConversion
{
public function registerMediaCollections(): void
{
$this->addMediaCollection('images')
->useDisk('secondMediaDisk');
}
};

$result = $testModel->getRegisteredMediaCollections();
ray($result);

expect($result)->toHaveCount(1);

$result = $testModel->getRegisteredMediaCollections();
ray($result);

expect($result)->toHaveCount(1);
});

0 comments on commit 3cac2fd

Please sign in to comment.