Skip to content

Commit

Permalink
Merge pull request #15 from bakurin/master
Browse files Browse the repository at this point in the history
Fix the case when header value with `*` causes Undefined offset: 0 error
  • Loading branch information
tboronczyk authored Mar 25, 2020
2 parents 4b249fe + ccf3024 commit 5db0c16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
},
"autoload": {
"psr-4": { "Boronczyk\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Boronczyk\\Tests\\": "tests/" }
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/LocalizationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected function parse(string $header): array
$values = [];
foreach (explode(',', $header) as $lang) {
@list($locale, $quality) = explode(';', $lang, 2);
$val = $this->parseLocale($locale);
$val = $this->parseLocale(str_replace('*', $this->defaultLocale, $locale));
$val['quality'] = $this->parseQuality($quality ?? '');
$values[] = $val;
}
Expand Down
32 changes: 21 additions & 11 deletions tests/LocalizationMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
declare(strict_types=1);

namespace Boronczyk\Tests;

error_reporting(E_ALL);
ini_set('display_errors', '1');

Expand All @@ -12,9 +14,6 @@
use Slim\Http\Response;
use Boronczyk\LocalizationMiddleware;

chdir(dirname(__FILE__));
require_once '../vendor/autoload.php';

class LocalizationMiddlewareTest extends TestCase
{
protected static $availableLocales = ['en_US', 'fr_CA', 'es_MX', 'eo'];
Expand Down Expand Up @@ -95,7 +94,7 @@ public function testLocaleFromCookie()
$req = self::createRequest([]);
$resp = self::createResponse();

$ref = new ReflectionClass($req);
$ref = new \ReflectionClass($req);
$prop = $ref->getProperty('cookies');
$prop->setAccessible(true);
$prop->setValue($req, ['locale' => 'fr_CA']);
Expand Down Expand Up @@ -128,7 +127,7 @@ public function testLocaleCookieName()
$req = self::createRequest([]);
$resp = self::createResponse();

$ref = new ReflectionClass($req);
$ref = new \ReflectionClass($req);
$prop = $ref->getProperty('cookies');
$prop->setAccessible(true);
$prop->setValue($req, ['lang' => 'fr_CA']);
Expand Down Expand Up @@ -179,17 +178,28 @@ public function testLocaleCallback()
$this->assertEquals('fr_CA', $resolved);
}

public function testLocaleFromHeader()
/**
* @dataProvider localeFromHeaderDataProvided
*/
public function testLocaleFromHeader(string $header, string $expectedResult)
{
$req = self::createRequest([
'HTTP_ACCEPT_LANGUAGE' => 'fr_CA'
'HTTP_ACCEPT_LANGUAGE' => $header
]);
$resp = self::createResponse();
$lmw = new LocalizationMiddleware(self::$availableLocales, self::$defaultLocale);
$lmw->setSearchOrder([LocalizationMiddleware::FROM_HEADER]);

list($req, $resp) = $lmw->__invoke($req, $resp, self::callable());
$this->assertEquals('fr_CA', $req->getAttribute('locale'));
$this->assertEquals($expectedResult, $req->getAttribute('locale'));
}

public function localeFromHeaderDataProvided(): array
{
return [
['fr_CA', 'fr_CA'],
['en, *;q=0.7', self::$defaultLocale]
];
}

public function testLocaleFromHeaderQuality()
Expand Down Expand Up @@ -266,7 +276,7 @@ public function testLocaleDefault()
]);
$resp = self::createResponse();

$ref = new ReflectionClass($req);
$ref = new \ReflectionClass($req);
$prop = $ref->getProperty('cookies');
$prop->setAccessible(true);
$prop->setValue($req, ['locale' => 'pt_BR']);
Expand Down Expand Up @@ -330,7 +340,7 @@ public function testSearchOrderException()
$lmw = new LocalizationMiddleware(self::$availableLocales, self::$defaultLocale);
$lmw->setSearchOrder([999]);

$this->expectException(DomainException::class);
$this->expectException(\DomainException::class);
$lmw->__invoke($req, $resp, self::callable());
}

Expand All @@ -341,6 +351,6 @@ public function testLocaleFromCallbackException()
$lmw = new LocalizationMiddleware(self::$availableLocales, self::$defaultLocale);
$lmw->setSearchOrder([LocalizationMiddleware::FROM_CALLBACK]);

$this->expectException(LogicException::class);
$this->expectException(\LogicException::class);
$lmw->__invoke($req, $resp, self::callable());
}}

0 comments on commit 5db0c16

Please sign in to comment.