diff --git a/classes/LocaleMiddleware.php b/classes/LocaleMiddleware.php index 42f3515..8084ea8 100644 --- a/classes/LocaleMiddleware.php +++ b/classes/LocaleMiddleware.php @@ -40,7 +40,7 @@ class LocaleMiddleware && $parsedLocale[1]['language'] == $proposedLocale['language'] && $parsedLocale[1]['region'] == $proposedLocale['region'] ) { - return $proposedLocale['language'].'_'.$proposedLocale['region']; + return new Locale($proposedLocale['language'].'_'.$proposedLocale['region']); } } } @@ -61,10 +61,10 @@ class LocaleMiddleware if (!isset($curLocale)) { if (isset($headers[0])) { $this->locale->setLocale( - AcceptLanguage::detect([$this, 'testLocale'], 'en_US', $headers[0]) + AcceptLanguage::detect([$this, 'testLocale'], new Locale('en_US'), $headers[0]) ); } else { - $this->locale->setLocale('en_US'); + $this->locale->setLocale(new Locale('en_US')); } } diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index de54aee..275ff39 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -5,7 +5,9 @@ namespace Alltube\Test; +use Alltube\LocaleManager; use Alltube\LocaleMiddleware; +use Slim\Container; use Slim\Http\Environment; use Slim\Http\Request; use Slim\Http\Response; @@ -28,7 +30,9 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->locale = getenv('LANG'); - $this->middleware = new LocaleMiddleware(); + $container = new Container(); + $container['locale'] = new LocaleManager(); + $this->middleware = new LocaleMiddleware($container); } /** @@ -88,4 +92,22 @@ class LocaleMiddlewareTest extends \PHPUnit_Framework_TestCase $this->assertEquals('fr_FR', getenv('LANG')); $this->assertEquals('fr_FR', setlocale(LC_ALL, null)); } + + /** + * Test the __invoke() function withot the Accept-Language header. + * + * @return void + */ + public function testInvokeWithoutHeader() + { + $request = Request::createFromEnvironment(Environment::mock()); + $this->middleware->__invoke( + $request->withoutHeader('Accept-Language'), + new Response(), + function () { + } + ); + $this->assertEquals('en_US', getenv('LANG')); + $this->assertEquals('en_US', setlocale(LC_ALL, null)); + } }