Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add public getter method for MIME types to be able to use them. #20826

Merged
merged 1 commit into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Illuminate/Http/Testing/MimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,28 @@ public static function from($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);

return self::getMimeTypeFromExtension($extension);
}

/**
* Get the MIME type for a given extension or return all mimes.
*
* @param string $extension
* @return string|array
*/
public static function get($extension = null)
{
return $extension ? self::getMimeTypeFromExtension($extension) : self::$mimes;
}

/**
* Get the MIME type for a given extension.
*
* @param string $extension
* @return string
*/
protected static function getMimeTypeFromExtension($extension)
{
return self::$mimes[$extension] ?? 'application/octet-stream';
}
}
20 changes: 18 additions & 2 deletions tests/Http/HttpMimeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,29 @@

class HttpMimeTypeTest extends TestCase
{
public function testMimeTypeExistsTrue()
public function testMimeTypeFromFileNameExistsTrue()
{
$this->assertSame('image/jpeg', MimeType::from('foo.jpg'));
}

public function testMimeTypeExistsFalse()
public function testMimeTypeFromFileNameExistsFalse()
{
$this->assertSame('application/octet-stream', MimeType::from('foo.bar'));
}

public function testMimeTypeFromExtensionExistsTrue()
{
$this->assertSame('image/jpeg', MimeType::get('jpg'));
}

public function testMimeTypeFromExtensionExistsFalse()
{
$this->assertSame('application/octet-stream', MimeType::get('bar'));
}

public function testGetAllMimeTypes()
{
$this->assertInternalType('array', MimeType::get());
$this->assertArraySubset(['jpg' => 'image/jpeg'], MimeType::get());
}
}