Skip to content

Commit

Permalink
[Json] Change unit test.
Browse files Browse the repository at this point in the history
The casting from string to float using (float) is not locale aware.

Probably this function could be refactored implementing something like this
http://www.php.net/manual/en/function.floatval.php#92563
  • Loading branch information
Maks3w committed May 10, 2012
1 parent 68a1d6c commit 465e6d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions library/Zend/Json/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function _encodeArray(&$array)
* If value type is not a string, number, boolean, or null, the string
* 'null' is returned.
*
* @param $value mixed
* @param mixed $value
* @return string
*/
protected function _encodeDatum(&$value)
Expand All @@ -244,7 +244,7 @@ protected function _encodeDatum(&$value)

if (is_int($value) || is_float($value)) {
$result = (string) $value;
$result = str_replace(",", ".", $result);
$result = str_replace(',', '.', $result);
} elseif (is_string($value)) {
$result = $this->_encodeString($value);
} elseif (is_bool($value)) {
Expand Down
16 changes: 8 additions & 8 deletions tests/Zend/Json/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

namespace ZendTest\Json;

use Zend\Json;

/**
Expand Down Expand Up @@ -693,17 +694,16 @@ public function testUtf8JSONExprFinder()
/**
* @group ZF-4437
*/
public function testKommaDecimalIsConvertedToCorrectJSONWithDot()
public function testCommaDecimalIsConvertedToCorrectJSONWithDot()
{
$localeInfo = localeconv();
if($localeInfo['decimal_point'] != ',') {
$this->markTestSkipped("This test only works for platforms where , is the decimal point separator.");
setlocale(LC_ALL, 'Spanish_Spain', 'es_ES', 'es_ES.utf-8');
if (strcmp('1,2', (string)floatval(1.20)) != 0) {
$this->markTestSkipped('This test only works for platforms where "," is the decimal point separator.');
}

Json\Json::$useBuiltinEncoderDecoder = true;
$this->assertEquals("[1.20, 1.68]", Json\Encoder::encode(array(
(float)"1,20", (float)"1,68"
)));

$actual = Json\Encoder::encode(array(floatval(1.20), floatval(1.68)));
$this->assertEquals('[1.2,1.68]', $actual);
}

public function testEncodeObjectImplementingIterator()
Expand Down
2 changes: 1 addition & 1 deletion tests/Zend/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function setUp()

LocaleTestHelper::resetObject();
LocaleTestHelper::setCache($this->_cache);
putenv("HTTP_ACCEPT_LANGUAGE=,de,en-UK-US;q=0.5,fr_FR;q=0.2");
putenv('HTTP_ACCEPT_LANGUAGE=,de,en-UK-US;q=0.5,fr_FR;q=0.2');
}

public function tearDown()
Expand Down

0 comments on commit 465e6d8

Please sign in to comment.