Search code examples
phpsymfonyphpunitsymfony4iconv

Iconv return incorrect string in phpunit


I have project in SF4 and use phpunit 6.5.8. I tested service that use iconv:

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

When I use this service in application and when $string has value: "ąbć", returned is "abc" but when the same service is runed in phpunit returned is "?b?".

I don't understand why it don't work... Of course the test is negative but in application it works good.


Solution

  • Ok, I solved a problem. Php in CLI don't set locale. So we must set it before tests.

    1. Check location which you have in system:

    locale -a
    

    For example:

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

    2. In the tests add:

    setlocale(LC_CTYPE, 'en_US.utf8');
    

    3. Example:

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

    source: