Skip to content

Commit 0aa7cc0

Browse files
committed
tests for the TranslatorListener
1 parent e6fcfdb commit 0aa7cc0

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

EventListener/TranslatorListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1415
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1516
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1617
use Symfony\Component\HttpKernel\KernelEvents;
1718
use Symfony\Component\HttpFoundation\RequestStack;
18-
use Symfony\Component\Translator\TranslatorInterface;
19+
use Symfony\Component\Translation\TranslatorInterface;
1920

2021
/**
2122
* Synchronizes the locale between the request and the translator.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
16+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
use Symfony\Component\HttpKernel\EventListener\TranslatorListener;
18+
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
20+
class TranslatorListenerTest extends \PHPUnit_Framework_TestCase
21+
{
22+
private $listener;
23+
private $translator;
24+
private $requestStack;
25+
26+
protected function setUp()
27+
{
28+
$this->translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
29+
$this->requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack');
30+
$this->listener = new TranslatorListener($this->translator, $this->requestStack);
31+
}
32+
33+
public function testLocaleIsSetInOnKernelRequest()
34+
{
35+
$this->translator
36+
->expects($this->once())
37+
->method('setLocale')
38+
->with($this->equalTo('fr'));
39+
40+
$event = new GetResponseEvent($this->createHttpKernel(), $this->createRequest('fr'), HttpKernelInterface::MASTER_REQUEST);
41+
$this->listener->onKernelRequest($event);
42+
}
43+
44+
public function testLocaleIsSetInOnKernelFinishRequestWhenParentRequestExists()
45+
{
46+
$this->translator
47+
->expects($this->once())
48+
->method('setLocale')
49+
->with($this->equalTo('fr'));
50+
51+
$this->setMasterRequest($this->createRequest('fr'));
52+
$event = new FinishRequestEvent($this->createHttpKernel(), $this->createRequest('de'), HttpKernelInterface::SUB_REQUEST);
53+
$this->listener->onKernelFinishRequest($event);
54+
}
55+
56+
public function testLocaleIsNotSetInOnKernelFinishRequestWhenParentRequestDoesNotExist()
57+
{
58+
$this->translator
59+
->expects($this->never())
60+
->method('setLocale');
61+
62+
$event = new FinishRequestEvent($this->createHttpKernel(), $this->createRequest('de'), HttpKernelInterface::SUB_REQUEST);
63+
$this->listener->onKernelFinishRequest($event);
64+
}
65+
66+
private function createHttpKernel()
67+
{
68+
return $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
69+
}
70+
71+
private function createRequest($locale)
72+
{
73+
$request = new Request();
74+
$request->setLocale($locale);
75+
76+
return $request;
77+
}
78+
79+
private function setMasterRequest($request)
80+
{
81+
$this->requestStack
82+
->expects($this->any())
83+
->method('getParentRequest')
84+
->will($this->returnValue($request));
85+
}
86+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"symfony/config": "~2.0",
2929
"symfony/console": "~2.2",
3030
"symfony/dependency-injection": "~2.0",
31+
"symfony/event-dispatcher": "~2.3",
3132
"symfony/expression-language": "~2.4",
3233
"symfony/finder": "~2.0",
3334
"symfony/process": "~2.0",

0 commit comments

Comments
 (0)