Fix LocaleMiddleware tests

This commit is contained in:
Pierre Rudloff 2017-05-30 23:41:26 +02:00
parent 0f80cbd333
commit 433a580d64
2 changed files with 26 additions and 4 deletions

View file

@ -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'));
}
}

View file

@ -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));
}
}