You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, phpunit has two ways of making sure variables are the same. assertEquals, and assertSame. Doing == and === checks respectively.
The problem comes when we are comparing objects. We can't always use assertSame, as we may not be able to provide the desired object beforehand. So we use assertEquals there. This however leads to problems in code where the properties of a class may not be a single type. Which can be due to union types, or just because of legacy code.
// https://3v4l.org/PpqMIclassProblem
{
publicfunction__construct(
privatebool|string$issue
) {}
}
var_dump(newProblem(true) == newProblem('There is a problem')); // true
The solution
I'd like to propose a new assertion: assertStrictEqals. Which would check every property to be exactly the same. on the two objects. This is already featured in other test frameworks like jest.
Is this something that could be implemented in PHPUnit? If so i'd be happy to provide a PR for it
The text was updated successfully, but these errors were encountered:
I do not think that a generic comparison of objects based on properties is useful and PHPUnit already has two ways for comparing objects: assertEquals() with custom comparators and assertObjectEquals().
@BackEndTea IMO assertEquals() is very helpful for these kinds of comparisons, as its output is much more verbose when comparisons fail compared to assertObjectEquals(). If you want to continue using assertEquals() but with type safety, I recommend using my StrictPHPUnitExtension.
The problem
Currently, phpunit has two ways of making sure variables are the same.
assertEquals
, andassertSame
. Doing==
and===
checks respectively.The problem comes when we are comparing objects. We can't always use assertSame, as we may not be able to provide the desired object beforehand. So we use
assertEquals
there. This however leads to problems in code where the properties of a class may not be a single type. Which can be due to union types, or just because of legacy code.The solution
I'd like to propose a new assertion:
assertStrictEqals
. Which would check every property to be exactly the same. on the two objects. This is already featured in other test frameworks like jest.Is this something that could be implemented in PHPUnit? If so i'd be happy to provide a PR for it
The text was updated successfully, but these errors were encountered: