Skip to content

Commit

Permalink
Make the media file class stringable
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Aug 20, 2024
1 parent f64bf84 commit 23a49e2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/framework/src/Support/Filesystem/MediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Support\Filesystem;

use Hyde\Hyde;
use Stringable;
use Hyde\Facades\Config;
use Hyde\Facades\Filesystem;
use Illuminate\Support\Collection;
Expand All @@ -21,7 +22,7 @@
/**
* File abstraction for a project media file.
*/
class MediaFile extends ProjectFile
class MediaFile extends ProjectFile implements Stringable
{
/** @var array<string> The default extensions for media types */
final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
Expand All @@ -42,6 +43,14 @@ public function __construct(string $path)
parent::__construct($this->getNormalizedPath($path));
}

/**
* Cast the instance to a string which is the resolved web link to the media file.
*/
public function __toString(): string
{
return $this->getLink();
}

/**
* Get an array of media asset filenames relative to the `_media/` directory.
*
Expand Down
57 changes: 57 additions & 0 deletions packages/framework/tests/Unit/Support/MediaFileUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,63 @@ public function testGetLinkWithCurrentPageContextAndCustomMediaDirectory()
Hyde::setMediaDirectory('_media'); // Reset to default
}

public function testCanCastToString()
{
$file = new MediaFile('foo.txt');
$this->assertIsString((string) $file);
$this->assertSame('media/foo.txt', (string) $file);
}

public function testStringCastReturnsLink()
{
$file = new MediaFile('foo.txt');
$this->assertSame($file->getLink(), (string) $file);
}

public function testStringCastWithCustomMediaDirectory()
{
Hyde::setMediaDirectory('custom_media');
$file = new MediaFile('foo.txt');
$this->assertSame('custom_media/foo.txt', (string) $file);
Hyde::setMediaDirectory('_media'); // Reset to default
}

public function testStringCastWithPrettyUrls()
{
self::mockConfig(['hyde.enable_cache_busting' => false, 'hyde.pretty_urls' => true]);
$file = new MediaFile('foo.txt');
$this->assertSame('media/foo.txt', (string) $file);
}

public function testStringCastWithNestedFile()
{
$file = new MediaFile('subdirectory/foo.txt');
$this->assertSame('media/subdirectory/foo.txt', (string) $file);
}

public function testStringCastWithBaseUrl()
{
self::mockConfig(['hyde.enable_cache_busting' => false, 'hyde.url' => 'https://example.com']);
$file = new MediaFile('foo.txt');
$this->assertSame('https://example.com/media/foo.txt', (string) $file);
}

public function testStringCastWithCacheBusting()
{
self::mockConfig(['hyde.enable_cache_busting' => true]);
$this->mockFilesystem->shouldReceive('hash')
->andReturn('abc123');
$file = new MediaFile('foo.txt');
$this->assertSame('media/foo.txt?v=abc123', (string) $file);
}

public function testStringCastWithCurrentPageContext()
{
$this->mockCurrentPage('foo/bar');
$file = new MediaFile('baz.txt');
$this->assertSame('../media/baz.txt', (string) $file);
}

// Helper method to mock the current page
protected function mockCurrentPage(string $page): void
{
Expand Down

0 comments on commit 23a49e2

Please sign in to comment.