1

I am trying to start using Travis for continuous integration, and all our tests pass except one, although it passes on our local (OSX) machines. The method is for parsing slugs, replacing accented characters with non-accented versions, and other special characters with hyphens, and looks like this:

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

and the test is:

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());

}

The failure happens because when converting the strings, what should be 'blogs' ends up as 'bl-gs'. As stated, this works fine on our local machines. After much testing and attempting to get this to work, I can definitely confirm that the iconv extension definitely exists on the Travis server, so I'm a bit at a loss as to what the problem is.

Thomas Marchant
  • 208
  • 4
  • 14

1 Answers1

0

I'm going to pin it down to a difference in versions of iconv. Looking at Replacing accented characters php gave me a good idea of characters that aren't included in iconv, so I have just manually added those replacements to the method

Community
  • 1
  • 1
Thomas Marchant
  • 208
  • 4
  • 14
  • 1
    Might not be much of a contribution, but I [recreated your problem in C](http://codepad.org/zwAFuNlm) (note that on OS X, `-liconv` is required to compile) - definitely a difference in the underlying library. – Siguza Aug 18 '15 at 14:47