iconv 在 phpunit 中返回不正确的字符串



我在SF4中有项目并使用phpunit 6.5.8。我测试了使用 iconv 的服务:

iconv('UTF-8', 'ASCII//TRANSLIT', $string)

当我在应用程序中使用此服务并且$string具有值时:"ąbć",返回的是"abc",但是当在phpunit中运行相同的服务时,返回的是"?b?

我不明白为什么它不起作用...当然,测试是阴性的,但在应用中效果很好。

好的,我解决了一个问题。CLI 中的 php 不设置语言环境。所以我们必须在测试之前设置它。

1.检查您在系统中的位置:

locale -a

例如:

$ locale -a
C
C.UTF-8
en_US.utf8

2.在测试中添加:

setlocale(LC_CTYPE, 'en_US.utf8');

3. 示例:

public static function setUpBeforeClass()
{
setlocale(LC_CTYPE, 'en_US.utf8');
}

源:

  • http://php.net/manual/pl/function.iconv.php#86077
  • http://php.net/manual/pl/function.iconv.php#77315

我做了一个类来处理字符串,其中一个方法使用iconv。

在Windows中使用phpunit时,iconv无法音译区域设置问题。 即使在代码上使用 setlocale((,使用下面的代码,结果也总是测试失败

/**
* @param $str Convert string to lowercase and replace special chars to equivalents ou remove its
* @return string
*/
public static function slugify($str)
{
$str = self::toUtf8($str); // Convert from any encoding to UTF-8
$str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str); // transliterate
$str = strtolower($str); // lowercase
return $str;
}

单元测试

public function testSlugfy()
{
// Basic String
$str = StringUtils::slugify($this->basicstring);
$this->assertEquals(strtolower($this->basicstring), $str, 'Basic String cannot be slugfied');
// Latin String
$str = StringUtils::slugify($this->latinstring);
$this->assertEquals(strtolower($this->basicstring), $str, 'Latin1 String cannot be slugfied');
// UTF-8 String
$str = StringUtils::slugify($str);
$this->assertEquals(strtolower($this->basicstring), $str, 'UTF8 String cannot be slugfied');
}

在我的应用程序中没有任何麻烦,但 PHPUnit 测试失败,上面的代码。

因此,为了通过测试,我将函数更改为

/**
* @param $str Convert string to lowercase and replace special chars to equivalents ou remove its
* @return string
*/
public static function slugify($str)
{
$string = self::toUtf8($str);
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
if ($string != htmlentities($str, ENT_QUOTES, 'UTF-8')) { // iconv fails
$string = htmlentities($str, ENT_QUOTES, 'UTF-8');
$string = preg_replace('#&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);#i', '$1', $string);
// If need to leave only 0-9, a-z and A-Z
//            $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
//            $string = preg_replace(array('#[^0-9a-z]#i', '#[ -]+#'), ' ', $string);
$string = trim($string, ' -');
}
// lowercase
$str = strtolower($string);
return $str;
}

在单元测试类构造函数上,我使用另一种方法创建字符串,以避免文件字符集和区域设置问题的麻烦。

private $basicstring;
private $latinstring;
private $utf8string;
public function __construct()
{
// ASCII string
$this->basicstring = 'aeioucAEIOUC';
// To avoid troubles using command line in different locales
// the string used to create different charset is a plain HTML entities
// Using html_entity_decode to convert the
// string ãéìôüçÃÉìÔÜÇ
// into ãéìôüçÃÉìÔÜÇ in different charsets
$html_chars = 'ãéìôüçÃÉìÔÜÇ';
$this->utf8string = html_entity_decode($html_chars, ENT_HTML5, 'UTF-8');
$this->latinstring = html_entity_decode($html_chars, ENT_HTML5, 'ISO-8859-1');
parent::__construct();
}

我也在使用Symfony 4

最新更新