8000 [HttpFoundation] Allow to get all the mime types associated to a format in the Request by GuilhemN · Pull Request #17318 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,22 @@ public function getMimeType($format)
return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
}

/**
* Gets the mime types associated with the format.
*
* @param string $format The format
*
* @return array The associated mime types
*/
public static function getMimeTypes($format)
{
if (null === static::$formats) {
static::initializeFormats();
}

return isset(static::$formats[$format]) ? static::$formats[$format] : array();
}

/**
* Gets the format associated with the mime type.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ public function testGetMimeTypeFromFormat($format, $mimeTypes)
}
}

/**
* @dataProvider getFormatToMimeTypeMapProvider
*/
public function testGetMimeTypesFromFormat($format, $mimeTypes)
{
if (null !== $format) {
$this->assertEquals($mimeTypes, Request::getMimeTypes($format));
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think one more test could be useful, which asserts what getMimeTypes() returns, if the format is not found in Request::$formats.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, added :-)

public function testGetMimeTypesFromInexistentFormat()
{
$request = new Request();
$this->assertNull($request->getMimeType('foo'));
$this->assertEquals(array(), Request::getMimeTypes('foo'));
}

public function getFormatToMimeTypeMapProvider()
{
return array(
Expand Down
0