|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the LuneticsLocaleBundle package. |
| 4 | + * |
| 5 | + * <https://github.com/lunetics/LocaleBundle/> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that is distributed with this source code. |
| 9 | + */ |
| 10 | +namespace Lunetics\LocaleBundle\Tests\LocaleGuesser; |
| 11 | + |
| 12 | +use Lunetics\LocaleBundle\LocaleGuesser\DomainLocaleGuesser; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Jachim Coudenys <[email protected]> |
| 16 | + */ |
| 17 | +class DomainLocaleGuesserTest extends \PHPUnit_Framework_TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @dataProvider dataDomains |
| 21 | + * |
| 22 | + * @param bool $expected |
| 23 | + * @param string $host |
| 24 | + * @param bool $allowed |
| 25 | + * @param string $mappedLocale |
| 26 | + */ |
| 27 | + public function testGuessLocale($expected, $expectedLocale, $host, $allowed, $mappedLocale) |
| 28 | + { |
| 29 | + //$this->markTestSkipped(); |
| 30 | + $metaValidator = $this->getMockMetaValidator(); |
| 31 | + $localeMap = $this->getMockDomainLocaleMap(); |
| 32 | + $localeMap->expects($this->any()) |
| 33 | + ->method('getLocale') |
| 34 | + ->will($this->returnValue($mappedLocale)); |
| 35 | + |
| 36 | + if ($allowed) { |
| 37 | + $metaValidator->expects($this->once()) |
| 38 | + ->method('isAllowed') |
| 39 | + ->will($this->returnValue($allowed)); |
| 40 | + } |
| 41 | + |
| 42 | + $request = $this->getMockRequest(); |
| 43 | + $request->expects($this->once()) |
| 44 | + ->method('getHost') |
| 45 | + ->will($this->returnValue($host)); |
| 46 | + |
| 47 | + $guesser = new DomainLocaleGuesser($metaValidator, $localeMap); |
| 48 | + |
| 49 | + $this->assertEquals($expected, $guesser->guessLocale($request)); |
| 50 | + $this->assertEquals($expectedLocale, $guesser->getIdentifiedLocale()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + public function dataDomains() |
| 57 | + { |
| 58 | + return array( |
| 59 | + array(false, false, 'localhost', false, false), |
| 60 | + array(true, 'nl_BE', 'dutchversion.be', true, 'nl_BE'), |
| 61 | + array(true, 'en_BE', 'sub.dutchversion.be', true, 'en_BE'), |
| 62 | + array(true, 'fr_BE', 'frenchversion.be', true, 'fr_BE'), |
| 63 | + array(true, 'fr_BE', 'test.frenchversion.be', true, 'fr_BE'), |
| 64 | + //array(true, 'de_CH', 'domain.ch', true, 'de_CH'), |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + private function getMockDomainLocaleMap() |
| 69 | + { |
| 70 | + return $this |
| 71 | + ->getMockBuilder('\Lunetics\LocaleBundle\LocaleInformation\DomainLocaleMap') |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->getMock(); |
| 74 | + } |
| 75 | + |
| 76 | + private function getMockMetaValidator() |
| 77 | + { |
| 78 | + return $this |
| 79 | + ->getMockBuilder('\Lunetics\LocaleBundle\Validator\MetaValidator') |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->getMock(); |
| 82 | + } |
| 83 | + |
| 84 | + private function getMockRequest() |
| 85 | + { |
| 86 | + return $this->getMock('Symfony\Component\HttpFoundation\Request'); |
| 87 | + } |
| 88 | +} |
0 commit comments