Skip to content

Commit

Permalink
Add public getter methods for MIME types to be able to use them. (#20826
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mathieutu authored and taylorotwell committed Aug 30, 2017
1 parent 6831938 commit abdc31d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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());
}
}

0 comments on commit abdc31d

Please sign in to comment.