Skip to content

Commit

Permalink
feature #9862 [FrameworkBundle] Added configuration for additionnal r…
Browse files Browse the repository at this point in the history
…equest formats (gquemener)

This PR was squashed before being merged into the 2.5-dev branch (closes #9862).

Discussion
----------

[FrameworkBundle] Added configuration for additionnal request formats

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony#8934
| License       | MIT
| Doc PR        |  symfony/symfony-docs#3402

Reopening of symfony/symfony#8944

# TODO
  - [x] Fix wrong xml configuration definition (Thanks @wouterj)
  - [x] Change configuration key `additional_formats` to a more meaningful one
  - [x] Write documentation (new entry or replace http://symfony.com/doc/current/cookbook/request/mime_type.html ?)

Commits
-------

f90ba11 [FrameworkBundle] Added configuration for additionnal request formats
  • Loading branch information
fabpot committed Feb 20, 2014
2 parents c8da30a + 586d347 commit 936b9e5
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
57 changes: 57 additions & 0 deletions EventListener/AddRequestFormatsListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
* Adds configured formats to each request
*
* @author Gildas Quemener <gildas.quemener@gmail.com>
*/
class AddRequestFormatsListener implements EventSubscriberInterface
{
/**
* @var array
*/
protected $formats;

/**
* @param array $formats
*/
public function __construct(array $formats)
{
$this->formats = $formats;
}

/**
* Adds request formats
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
foreach ($this->formats as $format => $mimeTypes) {
$event->getRequest()->setFormat($format, $mimeTypes);
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(KernelEvents::REQUEST => 'onKernelRequest');
}
}
83 changes: 83 additions & 0 deletions Tests/EventListener/AddRequestFormatsListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Test AddRequestFormatsListener class
*
* @author Gildas Quemener <gildas.quemener@gmail.com>
*/
class AddRequestFormatsListenerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AddRequestFormatsListener
*/
private $listener;

protected function setUp()
{
$this->listener = new AddRequestFormatsListener(array('csv' => array('text/csv', 'text/plain')));
}

protected function tearDown()
{
$this->listener = null;
}

public function testIsAnEventSubscriber()
{
$this->assertInstanceOf('Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener);
}

public function testRegisteredEvent()
{
$this->assertEquals(
array(KernelEvents::REQUEST => 'onKernelRequest'),
AddRequestFormatsListener::getSubscribedEvents()
);
}

public function testSetAdditionalFormats()
{
$request = $this->getRequestMock();
$event = $this->getGetResponseEventMock($request);

$request->expects($this->once())
->method('setFormat')
->with('csv', array('text/csv', 'text/plain'));

$this->listener->onKernelRequest($event);
}

protected function getRequestMock()
{
return $this->getMock('Symfony\Component\HttpFoundation\Request');
}

protected function getGetResponseEventMock(Request $request)
{
$event = $this
->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
->disableOriginalConstructor()
->getMock();

$event->expects($this->any())
->method('getRequest')
->will($this->returnValue($request));

return $event;
}
}

0 comments on commit 936b9e5

Please sign in to comment.