-
-
Notifications
You must be signed in to change notification settings - Fork 894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve ApiTestAssertionsTrait::assertJsonEquals with assertEquals
insteads of assertEqualsCanonicalizing
#5751
Comments
JSON objects aren't ordered but PHP arrays are. To prevent false positives we will have to order the keys of associative array keys. A better fix would be to correct the underlying PHPUnit bug: sebastianbergmann/phpunit#5019 |
Sure ! But Exemple : <?php
namespace App\Tests\Api;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
/**
* @internal
*
* @coversNothing
*/
class CanolicalizingTest extends ApiTestCase
{
public function testAssertEquals(): void
{
static::assertEquals(
[
'a' => 5,
'b' => 10,
],
[
'b' => 10,
'a' => 5,
],
'It work',
);
static::assertEquals(
[
5,
10,
],
[
10,
5,
],
'assertEquals :-(',
);
}
} Result :
This exemple will also fail for deep arrays : // [..]
static::assertEquals(
[
[
'id' => 1,
],
[
'id' => 2,
],
],
[
[
'id' => 2,
],
[
'id' => 1,
],
],
'assertEquals :-(',
);
// [..] |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Description
Dear ApiPlatform,
this issue is a development tools improvement to make functionnal tests easier and more effective.
I suggest to replace this code :
core/src/Symfony/Bundle/Test/ApiTestAssertionsTrait.php
Line 77 in 63697e7
With :
Why ?
assertEqualsCanonicalizing
can produce unexpected resultsAccording to PhpUnit's doc, by canonicalizing comparison, keys are ignored and will produce non expected results.
Example
This exemple with produce :
Object
are supposed be an unordered collection of zero or more name/value pairsAccording https://www.rfc-editor.org/rfc/rfc7159.html :
So, i think order of properties won't be used to assert a result. That mean this changed won't produce regression for existing projects (or will improve tests with previously false positive passing tests 😝).
Reference
This issues refer to another issue from api-platform/api-platform api-platform/api-platform#1372
Thanks for your great work.
The text was updated successfully, but these errors were encountered: