Skip to content
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

Fix weak comparison between '0' and false #99

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ScalarComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f

// always compare as strings to avoid strange behaviour

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behaviour exist since 9 years, changing it may be a BC break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is true, but 0 equals false, so '0' must too, it is also the php behaviour of weak comparison

// otherwise 0 == 'Foobar'
if (\is_string($expected) || \is_string($actual)) {
if ((\is_string($expected) && !\is_bool($actual)) || (\is_string($actual) && !\is_bool($expected))) {
$expectedToCompare = (string) $expectedToCompare;
$actualToCompare = (string) $actualToCompare;

Expand Down
2 changes: 2 additions & 0 deletions tests/ScalarComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function acceptsSucceedsProvider()
['1', true],
[1, true],
[0, false],
['0', false],
[0.1, '0.1']
];
}
Expand Down Expand Up @@ -77,6 +78,7 @@ public function assertEqualsSucceedsProvider()
['1', true],
[1, true],
[0, false],
['0', false],
[0.1, '0.1'],
[false, null],
[false, false],
Expand Down