|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Lunetics\LocaleBundle\Tests\Validator; |
| 4 | + |
| 5 | +use Lunetics\LocaleBundle\Switcher\TargetInformationBuilder; |
| 6 | +use Symfony\Component\HttpFoundation\Request; |
| 7 | +use Symfony\Bundle\FrameworkBundle\Routing\Router; |
| 8 | +use Symfony\Component\Routing\RouterInterface; |
| 9 | + |
| 10 | +class TargetInformationBuilderTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + public function locales() |
| 14 | + { |
| 15 | + return array( |
| 16 | + 'set 1' => array('/hello-world/', 'de', array('de', 'en', 'fr')), |
| 17 | + 'set 2' => array('/', 'de_DE', array('de', 'en', 'fr', 'nl')), |
| 18 | + 'set 3' => array('/test/', 'de', array('de', 'fr_FR', 'es_ES', 'nl')), |
| 19 | + 'set 4' => array('/foo', 'de', array('de', 'en')), |
| 20 | + 'set 5' => array('/foo', 'de', array('de')), |
| 21 | + 'set 6' => array('/', 'de_DE', array('de_DE', 'en', 'fr', 'nl')) |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @dataProvider locales |
| 27 | + */ |
| 28 | + public function testProvideRouteInInformationBuilder($route, $locale, $allowedLocales) |
| 29 | + { |
| 30 | + $request = $this->getRequestWithBrowserPreferences($route); |
| 31 | + $request->setLocale($locale); |
| 32 | + $request->attributes->set('_route', $route); |
| 33 | + $router = $this->getRoute(); |
| 34 | + |
| 35 | + $targetInformationBuilder = new TargetInformationBuilder($route . 'hello'); |
| 36 | + $targetInformation = $targetInformationBuilder->getTargetInformations( |
| 37 | + $request, |
| 38 | + $router, |
| 39 | + $allowedLocales |
| 40 | + ); |
| 41 | + $this->assertEquals($route . 'hello', $targetInformation['current_route']); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dataProvider locales |
| 46 | + */ |
| 47 | + public function testNotProvideRouteInInformationBuilder($route, $locale, $allowedLocales) |
| 48 | + { |
| 49 | + $request = $this->getRequestWithBrowserPreferences($route); |
| 50 | + $request->setLocale($locale); |
| 51 | + $request->attributes->set('_route', $route . 'hello'); |
| 52 | + $router = $this->getRoute(); |
| 53 | + |
| 54 | + $targetInformationBuilder = new TargetInformationBuilder(); |
| 55 | + $targetInformation = $targetInformationBuilder->getTargetInformations( |
| 56 | + $request, |
| 57 | + $router, |
| 58 | + $allowedLocales |
| 59 | + ); |
| 60 | + $this->assertEquals($route . 'hello', $targetInformation['current_route']); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dataProvider locales |
| 65 | + */ |
| 66 | + public function testInformationBuilder($route, $locale, $allowedLocales) |
| 67 | + { |
| 68 | + $request = $this->getRequestWithBrowserPreferences(); |
| 69 | + $request->setLocale($locale); |
| 70 | + $router = $this->getRoute(); |
| 71 | + |
| 72 | + $targetInformationBuilder = new TargetInformationBuilder($route); |
| 73 | + $targetInformation = $targetInformationBuilder->getTargetInformations( |
| 74 | + $request, |
| 75 | + $router, |
| 76 | + $allowedLocales |
| 77 | + ); |
| 78 | + $this->assertEquals($locale, $targetInformation['current_locale']); |
| 79 | + if (count($allowedLocales) > 1) { |
| 80 | + $this->assertCount(count($allowedLocales) - 1, $targetInformation['locales']); |
| 81 | + $this->assertArrayNotHasKey($locale, $targetInformation['locales']); |
| 82 | + } else { |
| 83 | + $this->assertArrayNotHasKey('locales', $targetInformation); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @dataProvider locales |
| 89 | + */ |
| 90 | + public function testShowCurrentLocale($route, $locale, $allowedLocales) |
| 91 | + { |
| 92 | + $request = $this->getRequestWithBrowserPreferences(); |
| 93 | + $request->setLocale($locale); |
| 94 | + $router = $this->getRoute(); |
| 95 | + |
| 96 | + $targetInformationBuilder = new TargetInformationBuilder($route); |
| 97 | + $targetInformation = $targetInformationBuilder->getTargetInformations( |
| 98 | + $request, |
| 99 | + $router, |
| 100 | + $allowedLocales, |
| 101 | + true |
| 102 | + ); |
| 103 | + $this->assertCount(count($allowedLocales), $targetInformation['locales']); |
| 104 | + foreach ($allowedLocales as $allowed) { |
| 105 | + $this->assertArrayHasKey($allowed, $targetInformation['locales']); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private function getRequestWithBrowserPreferences($route = "/") |
| 110 | + { |
| 111 | + $request = Request::create($route); |
| 112 | + $request->headers->set('Accept-language', 'fr-FR,fr;q=0.1,en-US;q=0.6,en;q=0.4'); |
| 113 | + |
| 114 | + return $request; |
| 115 | + } |
| 116 | + |
| 117 | + private function getRoute() |
| 118 | + { |
| 119 | + return $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->disableOriginalConstructor()->getMock(); |
| 120 | + } |
| 121 | +} |
0 commit comments