Enrique Moreno Tent
Enrique Moreno Tent

Reputation: 25277

Testing special characters with PHP Unit

I am testing my controller from Symfony2 with PHPUnit and the class WebTestCase

return self::$client->request(
    'POST', '/withdraw',
    array("amount" => 130),
    array(),array());

$this->assertEquals(
    "You can withdraw up to £100.00.",
     $crawler->filter("#error-notification")->text());

But I get this error:

Expected: "You can withdraw up to £100.00."
Actual:   "You can withdraw up to £100.00."

The thing is that in the webpage and the source code it looks fine, so I am thinking that maybe PHPUnit is having some trouble to fetch the text as UTF8?

What am I missing?

Upvotes: 4

Views: 2436

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

solution:

Make sure the mbstring extension is enabled.

There was a bug about failing tests with iconv reported in the kohana bugtracker.


tips:

As proposed in this question/answer - you can test for correct UTF-8 output:

$this->assertEquals(
    mb_detect_encoding(
        crawler->filter("#error-notification")->text(),
        'UTF-8'
    ),
    'UTF-8'
);

You can include accept-charset headers with requests sent by the client:

$client->request(
    'POST', '/withdraw',
    array("amount" => 130),
    array(),
    array(),
    array('HTTP_ACCEPT_CHARSET' => 'utf-8')
);

Upvotes: 5

Related Questions