swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
446 stars 51 forks source link

Failed asserting that two strings are identical in Swaggest\JsonSchema\Tests\PHPUnit\Example\ExampleTest::testNameMapper #37

Closed massadm closed 6 years ago

massadm commented 6 years ago

Not a bug! Depends on the PHP configuration.

php -i | grep "PHP Version\|precision"

PHP Version => 7.1.14
precision => 14 => 14
serialize_precision => 17 => 17

phpunit --filter testNameMapper ./tests/src/PHPUnit/Example/ExampleTest.php

Swaggest\JsonSchema\Tests\PHPUnit\Example\ExampleTest::testNameMapper
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
 {
     "id": 1,
     "date_time": "2015-10-28T07:28:00Z",
-    "price": 2.2
+    "price": 2.2000000000000002
 }

/misc/local/workspace/php/php-json-schema/tests/src/PHPUnit/Example/ExampleTest.php:141

php -i | grep "PHP Version\|precision"

PHP Version => 7.1.14
precision => 14 => 14
serialize_precision => -1 => -1

phpunit --filter testNameMapper ./tests/src/PHPUnit/Example/ExampleTest.php

OK (1 test, 6 assertions)

Just FYI. Have a nice day!

vearutop commented 6 years ago

Thanks, though I'm not sure how to benefit from this information :)

massadm commented 6 years ago

Related information

I suggest adding the normalization of precision with ini_set() before the tests.

Like this

if (version_compare(phpversion(), '7.1', '>=')) {
    ini_set( 'precision', 14 );
    ini_set( 'serialize_precision', -1 );
}
elseif (version_compare(phpversion(), '7.0', '>=')) {
    ???
}
elseif (version_compare(phpversion(), '5.3.5', '>=')) {
    ini_set( 'precision', 14 );
    ini_set( 'serialize_precision', 17 );
}

Backup settings then override then run tests then restore settings back.