在Travis上运行测试时,PHP iconv()不一致


PHP iconv() inconsistent when tests run on Travis

我正在尝试开始使用Travis进行持续集成,除了一个测试之外,我们所有的测试都通过了,尽管它在我们的本地(OSX)机器上通过了。该方法用于解析段代码,用非重音版本替换重音字符,并用连字符替换其他特殊字符,如下所示:

public function sanitize(array $substitutions = array('&' => 'and'))
{
    foreach ($this->_segments as $i => $segment) {
        // Perform substitutions
        foreach ($substitutions as $find => $replace) {
            $segment = str_replace($find, $replace, $segment);
        }
        // Transliterate
        if (function_exists('iconv')) {
            $segment = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $segment);
            // Remove any weird characters added by the transliteration
            $segment = str_replace(array('"', '''', '`', '^'), '', $segment);
        }
        // Lowercase
        $segment = strtolower($segment);
        // Replace any non-alphanumeric characters with hyphens
        $segment = preg_replace('/[^a-z0-9]/i', '-', $segment);
        // Set any double hyphens to just a single hyphen
        $segment = preg_replace('/-+/i', '-', $segment);
        // Remove any hyphens at the start or end
        $segment = trim($segment, '-');
        $this->_segments[$i] = $segment;
    }
    return $this;
}

,测试是:

public function testSanitize()
{
    $segments = array(
        '(MY Website',
        'bløgs',
        'march!/2013',
        'me & you',
        '50 % 5 = 10.00',
        'YåYéî!',
    );
    $slug = new Slug($segments);
    $this->assertSame($slug, $slug->sanitize());
    $this->assertSame(array(
        'my-website',
        'blogs',
        'march-2013',
        'me-and-you',
        '50-5-10-00',
        'yayei',
    ), $slug->getSegments());
}

发生故障是因为在转换字符串时,应该是'blogs'的内容最终被转换为'bl-gs'。如前所述,这在我们的本地机器上运行良好。在许多测试并试图使其工作后,我可以肯定地确认iconv扩展绝对存在于Travis服务器上,所以我有点茫然,不知道问题是什么。

我要把它归结为iconv版本的不同。php让我很好地了解了iconv中不包含的字符,所以我只是手动将这些替换添加到方法