|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\HttpKernel\Tests\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\HttpKernel\EventListener\AddRequestFormatsListener; |
| 15 | +use Symfony\Component\HttpFoundation\Request; |
| 16 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 17 | + |
| 18 | +/** |
| 19 | + * Test AddRequestFormatsListener class |
| 20 | + * |
| 21 | + * @author Gildas Quemener <[email protected]> |
| 22 | + */ |
| 23 | +class AddRequestFormatsListenerTest extends \PHPUnit_Framework_TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var AddRequestFormatsListener |
| 27 | + */ |
| 28 | + private $listener; |
| 29 | + |
| 30 | + protected function setUp() |
| 31 | + { |
| 32 | + $this->listener = new AddRequestFormatsListener(array('csv' => array('text/csv', 'text/plain'))); |
| 33 | + } |
| 34 | + |
| 35 | + protected function tearDown() |
| 36 | + { |
| 37 | + $this->listener = null; |
| 38 | + } |
| 39 | + |
| 40 | + public function testIsAnEventSubscriber() |
| 41 | + { |
| 42 | + $this->assertInstanceOf('Symfony\Component\EventDispatcher\EventSubscriberInterface', $this->listener); |
| 43 | + } |
| 44 | + |
| 45 | + public function testRegisteredEvent() |
| 46 | + { |
| 47 | + $this->assertEquals( |
| 48 | + array(KernelEvents::REQUEST => 'onKernelRequest'), |
| 49 | + AddRequestFormatsListener::getSubscribedEvents() |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function testSetAdditionalFormats() |
| 54 | + { |
| 55 | + $request = $this->getRequestMock(); |
| 56 | + $event = $this->getGetResponseEventMock($request); |
| 57 | + |
| 58 | + $request->expects($this->once()) |
| 59 | + ->method('setFormat') |
| 60 | + ->with('csv', array('text/csv', 'text/plain')); |
| 61 | + |
| 62 | + $this->listener->onKernelRequest($event); |
| 63 | + } |
| 64 | + |
| 65 | + protected function getRequestMock() |
| 66 | + { |
| 67 | + return $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 68 | + } |
| 69 | + |
| 70 | + protected function getGetResponseEventMock(Request $request) |
| 71 | + { |
| 72 | + $event = $this |
| 73 | + ->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent') |
| 74 | + ->disableOriginalConstructor() |
| 75 | + ->getMock(); |
| 76 | + |
| 77 | + $event->expects($this->any()) |
| 78 | + ->method('getRequest') |
| 79 | + ->will($this->returnValue($request)); |
| 80 | + |
| 81 | + return $event; |
| 82 | + } |
| 83 | +} |
0 commit comments